📄 listeneradaptor.java
字号:
/* * * $Id: ListenerAdaptor.java,v 1.3 2004/05/24 18:34:53 jice Exp $ * * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" * must not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. */package net.jxta.endpoint;import net.jxta.util.SimpleSelectable;import net.jxta.util.SimpleSelector;import org.apache.log4j.Level;import org.apache.log4j.Logger;import java.io.IOException;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;import java.util.Iterator;/* * The legacy getMessenger asynchronous API never returns any object to the invoker until a messenger could actually be made, * allowing the application to supply a listener to be invoked when the operation completes. The legacy Messenger API also * provides a method to send messages that calls a listener to report the outcome of the operation. <p/> * * The model has changed, so that an asynchronous messenger is made unresolved and returned immediately to the invoker, which can * then request opening or even just send a message to force the opening. Subsequently, the messenger can be used as a control * block to monitor progress with {@link Messenger#register} and {@link Messenger#waitState}.<p/> * * Likewise, the outcome of sending a message is a property of that message. Messages can be selected to monitor property changes * with {@link Message#register} and {@link Message#getMessageproperty} (the outcome property key is * <code>Messenger.class</code>).<p/> * * This class here provides the legacy listener model on top of the new model for applications that prefere listeners. This class * is used internally to emulate the legacy listener behaviour, so that applications do not need to be adapted.<p/> * * Note: one instance of this class gets instantiated by each EndpointService interface. However, it does not start using any * resources until it first gets used.<p/> */public class ListenerAdaptor implements Runnable { // FIXME - jice@jxta.org 20040413: Eventhough it is not as critical as it used to be we should get rid of old, never resolved entries.. // Attempts are supposed to always fail or succeed rather soon. Here, we trust transports in that matter. Is it safe ? /** * Log4J Category **/ private static final Logger LOG = Logger.getLogger(ListenerAdaptor.class.getName()); HashMap inprogress = null; /** * The thread that does the work. **/ private Thread bgThread = null; /** * The selector that we use to watch messengers progress. **/ private SimpleSelector selector = null; /** * Are we asked to stop ? **/ private boolean stopped = false; /** * The threadgroup that we use. **/ private ThreadGroup threadGroup = null; public ListenerAdaptor(ThreadGroup threadGroup) { this.threadGroup = threadGroup; } private void stop() { stopped = true; // Stop the thread if it was ever created. if (bgThread != null) { bgThread.interrupt(); bgThread = null; } } // Cannot be re-started. Do not call once stopped. private void init() { if (bgThread != null) { return; } inprogress = new HashMap(32); selector = new SimpleSelector(); bgThread = new Thread(threadGroup, this, "Listener Adaptor"); bgThread.setDaemon(true); bgThread.start(); } public void shutdown() { synchronized(this) { stop(); } } /** * Stop watching a given selectable. **/ private void forgetSelectable(SimpleSelectable ts) { // Either way, we're done with this one. ts.unregister(selector); synchronized(this) { inprogress.remove(ts.getIdentityReference()); } } /** * Select the given message and invoke the given listener when the message sending is complete. * * @param listener The listener to invoke. If null the resolution will take place, but obviously no listener will be invoked. * @param m The message being sent. * @return true if the message was registered succesfully or the listener is null. If true it is guaranteed that the listener * will be invoked unless null. If false, it is guaranteed that the listener will not be invoked. **/ public boolean watchMessage( OutgoingMessageEventListener listener, Message m ) { synchronized(this) { if (stopped) { return false; } if (listener == null) { // We're done, then. The invoker does not really care. return true; } // Init if needed. init(); // First we must ensure that if the state changes we'll get to handle it. ListenerContainer allListeners = (ListenerContainer) inprogress.get(m.getIdentityReference()); if (allListeners == null) { // Use ArrayList. The code is optimized for that. allListeners = new MessageListenerContainer(); inprogress.put(m.getIdentityReference(), allListeners); } allListeners.add(listener); } // When we do that, the selector get notified. Therefore we will always check the initial state automatically. If the // selectable is already done with, the listener will be called by the selector's handler. m.register(selector); return true; } /** * Select the given messenger and invoke the given listener when the messenger is resolved.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -