FileDocCategorySizeDatePackage
ResponseListenerImpl.javaAPI DocphoneME MR2 API (J2ME)3563Wed May 02 18:00:44 BST 2007com.sun.midp.content

ResponseListenerImpl

public class ResponseListenerImpl extends Object implements Runnable
Thread to monitor pending invocations and notify a listener when a matching one is present.

Fields Summary
private final RegistryImpl
registry
ContenHandlerServer for which this is listening.
private javax.microedition.content.ResponseListener
listener
The listener to notify.
private Thread
thread
The active thread processing the run method.
Constructors Summary
ResponseListenerImpl(RegistryImpl registry, javax.microedition.content.ResponseListener listener)
Create a new listener for pending invocations.

param
registry the RegistryImpl to listen for
param
listener the listener to notify when present

	this.registry = registry;
	setListener(listener);
    
Methods Summary
public voidrun()
The run method checks for pending invocations for a desired Registry. When an invocation is available the listener is notified.

	Thread mythread = Thread.currentThread();
	while (mythread == thread) {
	    // Remember the listener to notify to avoid a race condition
	    ResponseListener l = listener;
	    if (l != null) {
		// Wait for a matching invocation
		boolean pending =
		    InvocationStore.listen(registry.application.getStorageId(),
					   registry.application.getClassname(),
					   false, true);
		if (pending) {
		    l.invocationResponseNotify(registry.getRegistry());
		}
	    }
	}
    
voidsetListener(javax.microedition.content.ResponseListener listener)
Set the listener to be notified and start/stop the monitoring thread as necesary. If the listener is non-null make sure there is a thread active to monitor it. If there is no listener, then stop the monitor thread. Unblock any blocked threads so they can get the updated listener.

param
listener the listener to update

	this.listener = listener;

	if (listener != null) {
	    // Ensure a thread is running to watch for it
	    if (thread == null || !thread.isAlive()) {
		thread = new Thread(this);
		thread.start();
	    }
	} else {
	    // Forget the thread doing the listening; it will exit
	    thread = null;
	}

	/*
	 * Reset notified flags on pending responses.
	 * Unblock any threads waiting to notify current listeners
	 */
	InvocationStore.setListenNotify(registry.application.getStorageId(),
					registry.application.getClassname(),
					false);
	InvocationStore.cancel();