KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > messaging > lrmp > LRMPMessageHandlerFactory


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.messaging.lrmp;
56
57 import java.util.Map JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.ResourceBundle JavaDoc;
60
61 import org.apache.log4j.Logger;
62
63 import org.lateralnz.common.util.ResourceUtils;
64 import org.lateralnz.common.util.StringUtils;
65 import org.lateralnz.messaging.Message;
66 import org.lateralnz.messaging.MessageHandler;
67 import org.lateralnz.messaging.MessageListener;
68
69 /**
70  * factory class for notification handlers, so we don't have to instantiate multiple copies.
71  * (in other words, there should be only one notification handler in a VM per IP address & port)
72  *
73  * @author J R Briggs
74  */

75 public class LRMPMessageHandlerFactory {
76   private static final Logger log = Logger.getLogger(LRMPMessageHandlerFactory.class.getName());
77   private static LRMPMessageHandlerFactory obj = new LRMPMessageHandlerFactory();
78   private static ResourceBundle JavaDoc resources = ResourceUtils.getStaticBundle(LRMPMessageHandlerFactory.class);
79   
80   private String JavaDoc defaultAddress = null;
81   private int defaultPort = 5555;
82   
83   private Map JavaDoc handlers = new HashMap JavaDoc();
84   
85   private LRMPMessageHandlerFactory() {
86   }
87   
88   public static final LRMPMessageHandlerFactory getInstance() {
89     return obj;
90   }
91
92  /**
93   * get the handler with the specified ip address and port
94   */

95   public MessageHandler getHandler(String JavaDoc ipaddress, int port) throws Exception JavaDoc {
96     String JavaDoc key = ipaddress + port;
97     if (!handlers.containsKey(key)) {
98       synchronized (handlers) {
99         if (!handlers.containsKey(key)) {
100           handlers.put(key, new LRMPMessageHandler(ipaddress, port));
101         }
102       }
103     }
104     return (MessageHandler)handlers.get(key);
105   }
106   
107  /**
108   * get the default handler
109   */

110   public MessageHandler getDefaultHandler() throws Exception JavaDoc {
111     if (StringUtils.isEmpty(defaultAddress)) {
112       defaultAddress = resources.getString("default_address");
113       defaultPort = Integer.parseInt(resources.getString("default_port"));
114     }
115     return getHandler(defaultAddress, defaultPort);
116   }
117   
118   
119   // testing purposes
120
public static final void main(String JavaDoc[] args ) {
121     try {
122       MessageHandler nh = LRMPMessageHandlerFactory.getInstance().getDefaultHandler();
123       
124       MessageListener nl1 = new MessageListener() {
125         public void handle(Message event) {
126           System.out.println("received " + event);
127         }
128       };
129       
130       nh.addListener("test", nl1);
131       
132       Thread.sleep(1000);
133       if (args.length > 0) {
134         for (int i = 0; i < 1000; i++) {
135           nh.send(new Message(1, "test", "hello" + i));
136         }
137       }
138     }
139     catch (Exception JavaDoc e) {
140       e.printStackTrace();
141     }
142   }
143 }
144
Popular Tags