KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > messaging > AbstractMessageHandler


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;
56
57 import java.io.Serializable JavaDoc;
58 import java.net.InetAddress JavaDoc;
59 import java.util.HashMap JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.LinkedList JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import org.apache.log4j.Logger;
66
67 import org.lateralnz.common.util.Constants;
68 import org.lateralnz.common.util.StringUtils;
69
70 /**
71  * an abstract message handler that implements the supporting regex methods
72  *
73  * @author J R Briggs
74  */

75 public abstract class AbstractMessageHandler implements MessageHandler, Constants, Serializable JavaDoc {
76   private static final Logger log = Logger.getLogger(AbstractMessageHandler.class.getName());
77   
78   protected Map JavaDoc listeners = new HashMap JavaDoc(); // a map of listeners (who want to receive events)
79

80   protected InetAddress JavaDoc addr = null;
81   protected int port;
82   private String JavaDoc allowedRegex = null;
83   private String JavaDoc disallowedRegex = null;
84   private int allowedPriority = 1;
85   private int disallowedPriority = 0;
86   private boolean allowed = true;
87
88   public synchronized void addListener(String JavaDoc group, MessageListener listener) {
89     List JavaDoc l = (List JavaDoc)listeners.get(group);
90     if (l == null) {
91       l = new LinkedList JavaDoc();
92       listeners.put(group, l);
93     }
94     l.add(listener);
95   }
96
97   public void notifyListeners(final Message msg) {
98     // if there are listeners registered for the group,
99
// send the event to them
100
if (listeners.containsKey(msg.getGroup())) {
101       Thread JavaDoc t = new Thread JavaDoc() {
102         public void run() {
103           List JavaDoc l = (List JavaDoc)listeners.get(msg.getGroup());
104           Iterator JavaDoc iter = l.iterator();
105           while (iter.hasNext()) {
106             MessageListener list = (MessageListener)iter.next();
107             list.handle(msg);
108           }
109         }
110       };
111       t.start();
112     }
113   }
114   
115  /**
116   * the inet address this handler is running against
117   */

118   public void setAddress(InetAddress JavaDoc addr, int port) {
119     this.addr = addr;
120     this.port = port;
121   }
122     
123   public void setAllowedGroupRegex(String JavaDoc regex, int priority) {
124     this.allowedRegex = regex;
125     this.allowedPriority = priority;
126     
127     if (this.allowedPriority > this.disallowedPriority) {
128       allowed = true;
129     }
130   }
131   
132   public void setDisallowedGroupRegex(String JavaDoc regex, int priority) {
133     this.disallowedRegex = regex;
134     this.disallowedPriority = priority;
135     
136     if (this.disallowedPriority > this.allowedPriority) {
137       allowed = false;
138     }
139   }
140
141   public boolean willTransmit(String JavaDoc group) {
142     // if allowed has a higher priority
143
if (allowed && allowedRegex != null && StringUtils.matches(group, allowedRegex)) {
144       return true;
145     }
146     // disallowed must have a higher priority, so that is checked
147
// first
148
else if (disallowedRegex != null && StringUtils.matches(group, disallowedRegex)) {
149       return false;
150     }
151     else {
152       // for everything else return true
153
return true;
154     }
155   }
156 }
Popular Tags