📄 subscriptionservice.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . 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. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS 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 THE COPYRIGHT OWNER OR 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. * * $Id$ *///// SubscriptionService.java//// john.mettraux@openwfe.org//// generated with // jtmpl 1.1.01 2004/05/19 (john.mettraux@openwfe.org)//package openwfe.org.sub;import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;import openwfe.org.AbstractService;import openwfe.org.ApplicationContext;/** * This service will wait for registrations : other service subscribing for * topics by indicating their own service name and giving the Method they * want to be notified if anything is posted for the given topic. * This allows for an 'event system' or some multiplexing... * * <p><font size=2>CVS Info : * <br>$Author$ * <br>$Id$ </font> * * @author john.mettraux@openwfe.org */public class SubscriptionService extends AbstractService{ /* * this class was imagined in Schiphol gate F3 and written on * KL 835 to Singapore, near Bob, an english bricklayer heading to * Bali. I had seat 21H on a Boeing 747-400. */ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(SubscriptionService.class.getName()); // // CONSTANTS & co /** * This service should be stored in the application context * as "subscriptionService" (that's the value that this constant * is carrying). */ public final static String S_SUBSCRIPTION_SERVICE = "subscriptionService"; // // FIELDS private java.util.Map topicMap = new java.util.HashMap(); // // CONSTRUCTORS // // METHODS /** * Any service may used to method to 'subscribe' to a given topic. * The service has to give its serviceName in the applicationContext * (this is how it will be reached) and the Method instance that as * to be used for the notification. */ public void subscribe (final String topicName, final String subscriberServiceName, final Method method) { java.util.List subscribers = (java.util.List)this.topicMap .get(topicName); if (subscribers == null) { subscribers = new java.util.ArrayList(10); this.topicMap.put(topicName, subscribers); } subscribers.add(new TopicMapEntry(subscriberServiceName, method)); log.debug ("subscribe() topic:'"+topicName+"' method:'"+method.getName()+"'"); } /** * Emitting services use this method to 'multiplex' the event to * the topic. The subscribed services will get notified by this * SubscriptionService. */ public void post (final String topicName, final Object[] arguments) throws Throwable { log.debug("post() topic:'"+topicName+"'"); java.util.List subscribers = (java.util.List)this.topicMap .get(topicName); if (subscribers == null) return; final java.util.Iterator it = subscribers.iterator(); while (it.hasNext()) { final TopicMapEntry entry = (TopicMapEntry)it.next(); final Object service = getContext() .lookup(entry.subscriberServiceName); try { entry.method.invoke(service, arguments); } catch (final IllegalArgumentException iae) { log.warn ("post() failure for topic:'"+topicName+ "' subscriber:'"+entry.subscriberServiceName+"'", iae); } catch (final IllegalAccessException iae) { log.warn ("post() failure for topic:'"+topicName+ "' subscriber:'"+entry.subscriberServiceName+"'", iae); } catch (final InvocationTargetException ite) { log.debug ("post() invocation exception for topic:'"+topicName+ "' subscriber:'"+entry.subscriberServiceName+"'", ite.getCause()); throw ite.getCause(); } } } // // STATIC METHODS /** * Given an applicationContext, this static method will return * the subscription service (if any) stored in it under the * name given by S_SUBSCRIPTION_SERVICE. */ public static SubscriptionService getSubcriptionService (final ApplicationContext context) { return (SubscriptionService)context.lookup(S_SUBSCRIPTION_SERVICE); } // // INNER CLASSES /** * Just a struct[ure] class. */ protected static class TopicMapEntry { public String subscriberServiceName = null; public Method method = null; public TopicMapEntry (final String subscriberServiceName, final Method method) { this.subscriberServiceName = subscriberServiceName; this.method = method; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -