001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.content;
028:
029: import javax.microedition.content.RequestListener;
030:
031: /**
032: * Thread to monitor pending invocations and notify a listener
033: * when a matching one is present.
034: */
035: class RequestListenerImpl implements Runnable {
036:
037: /** ContenHandlerImpl for which this is listening. */
038: private final ContentHandlerImpl handler;
039:
040: /** The active thread processing the run method. */
041: private Thread thread;
042:
043: /**
044: * Create a new listener for pending invocations.
045: *
046: * @param handler the ContentHandlerImpl to listen for
047: * @param listener the listener to notify when present
048: */
049: RequestListenerImpl(ContentHandlerImpl handler,
050: RequestListener listener) {
051: this .handler = handler;
052: setListener(listener);
053: }
054:
055: /**
056: * Set the listener to be notified and start/stop the monitoring
057: * thread as necesary.
058: * If the listener is non-null make sure there is a thread active
059: * to monitor it.
060: * If there is no listener, then stop the monitor thread.
061: * Unblock any blocked threads so they can get the updated listener.
062: * @param listener the listener to update
063: */
064: void setListener(RequestListener listener) {
065:
066: if (listener != null) {
067: // Ensure a thread is running to watch for it
068: if (thread == null || !thread.isAlive()) {
069: thread = new Thread(this );
070: thread.start();
071: }
072: } else {
073: // Forget the thread doing the listening; it will exit
074: thread = null;
075: }
076:
077: /*
078: * Reset notified flags on pending requests.
079: * Unblock any threads waiting to notify current listeners
080: */
081: InvocationStore.setListenNotify(handler.storageId,
082: handler.classname, true);
083: InvocationStore.cancel();
084: }
085:
086: /**
087: * The run method checks for pending invocations for a
088: * desired ContentHandler or application.
089: * When an invocation is available the listener is
090: * notified.
091: */
092: public void run() {
093: Thread mythread = Thread.currentThread();
094: while (mythread == thread) {
095: // Wait for a matching invocation
096: boolean pending = InvocationStore.listen(handler.storageId,
097: handler.classname, true, true);
098: if (pending) {
099: handler.requestNotify();
100: }
101: }
102: }
103: }
|