KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > discovery > rendezvous > RendezvousDiscoveryAgent


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.discovery.rendezvous;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.jmdns.JmDNS;
28 import javax.jmdns.ServiceEvent;
29 import javax.jmdns.ServiceInfo;
30 import javax.jmdns.ServiceListener;
31
32 import org.apache.activemq.command.DiscoveryEvent;
33 import org.apache.activemq.transport.discovery.DiscoveryAgent;
34 import org.apache.activemq.transport.discovery.DiscoveryListener;
35 import org.apache.activemq.util.JMSExceptionSupport;
36 import org.apache.activemq.util.MapHelper;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
41
42 /**
43  * A {@link DiscoveryAgent} using <a HREF="http://www.zeroconf.org/">Zeroconf</a>
44  * via the <a HREF="http://jmdns.sf.net/">jmDNS</a> library
45  *
46  * @version $Revision$
47  */

48 public class RendezvousDiscoveryAgent implements DiscoveryAgent, ServiceListener {
49     private static final Log log = LogFactory.getLog(RendezvousDiscoveryAgent.class);
50
51     private static final String JavaDoc TYPE_SUFFIX = "ActiveMQ-4.";
52
53     private JmDNS jmdns;
54     private InetAddress JavaDoc localAddress;
55     private String JavaDoc localhost;
56     private int weight = 0;
57     private int priority = 0;
58
59     private DiscoveryListener listener;
60     private String JavaDoc group = "default";
61     private final CopyOnWriteArrayList JavaDoc serviceInfos = new CopyOnWriteArrayList JavaDoc();
62
63     private String JavaDoc brokerName;
64
65     // DiscoveryAgent interface
66
//-------------------------------------------------------------------------
67
public void start() throws Exception JavaDoc {
68         if (group == null) {
69             throw new IOException JavaDoc("You must specify a group to discover");
70         }
71         String JavaDoc type = getType();
72         if (!type.endsWith(".")) {
73             log.warn("The type '" + type + "' should end with '.' to be a valid Rendezvous type");
74             type += ".";
75         }
76         try {
77             // force lazy construction
78
getJmdns();
79             if (listener!=null) {
80                 log.info("Discovering service of type: " +type);
81                 jmdns.addServiceListener(type, this);
82             }
83         }
84         catch (IOException JavaDoc e) {
85             JMSExceptionSupport.create("Failed to start JmDNS service: " + e, e);
86         }
87     }
88
89     public void stop() {
90         if( jmdns!=null ) {
91             for (Iterator JavaDoc iter = serviceInfos.iterator(); iter.hasNext();) {
92                 ServiceInfo si = (ServiceInfo) iter.next();
93                 jmdns.unregisterService(si);
94             }
95             
96             // Close it down async since this could block for a while.
97
final JmDNS closeTarget = jmdns;
98             Thread JavaDoc thread = new Thread JavaDoc() {
99                 public void run() {
100                     closeTarget.close();
101                 }
102             };
103             
104             thread.setDaemon(true);
105             thread.start();
106             
107             jmdns=null;
108         }
109     }
110     
111     public void registerService(String JavaDoc name) throws IOException JavaDoc {
112         ServiceInfo si = createServiceInfo(name, new HashMap JavaDoc());
113         serviceInfos.add(si);
114         getJmdns().registerService(si);
115     }
116
117
118     // ServiceListener interface
119
//-------------------------------------------------------------------------
120
public void addService(JmDNS jmDNS, String JavaDoc type, String JavaDoc name) {
121         if (log.isDebugEnabled()) {
122             log.debug("addService with type: " + type + " name: " + name);
123         }
124         if( listener!=null )
125             listener.onServiceAdd(new DiscoveryEvent(name));
126         jmDNS.requestServiceInfo(type, name);
127     }
128
129     public void removeService(JmDNS jmDNS, String JavaDoc type, String JavaDoc name) {
130         if (log.isDebugEnabled()) {
131             log.debug("removeService with type: " + type + " name: " + name);
132         }
133         if( listener!=null )
134             listener.onServiceRemove(new DiscoveryEvent(name));
135     }
136
137     public void serviceAdded(ServiceEvent event) {
138         addService(event.getDNS(), event.getType(), event.getName());
139     }
140     public void serviceRemoved(ServiceEvent event) {
141         removeService(event.getDNS(), event.getType(), event.getName());
142     }
143     public void serviceResolved(ServiceEvent event) {
144     }
145     public void resolveService(JmDNS jmDNS, String JavaDoc type, String JavaDoc name, ServiceInfo serviceInfo) {
146     }
147
148     public int getPriority() {
149         return priority;
150     }
151
152     public void setPriority(int priority) {
153         this.priority = priority;
154     }
155
156     public int getWeight() {
157         return weight;
158     }
159
160     public void setWeight(int weight) {
161         this.weight = weight;
162     }
163
164     public JmDNS getJmdns() throws IOException JavaDoc {
165         if (jmdns == null) {
166             jmdns = createJmDNS();
167         }
168         return jmdns;
169     }
170
171     public void setJmdns(JmDNS jmdns) {
172         this.jmdns = jmdns;
173     }
174
175
176     public InetAddress JavaDoc getLocalAddress() throws UnknownHostException JavaDoc {
177         if (localAddress == null) {
178             localAddress = createLocalAddress();
179         }
180         return localAddress;
181     }
182
183     public void setLocalAddress(InetAddress JavaDoc localAddress) {
184         this.localAddress = localAddress;
185     }
186
187     public String JavaDoc getLocalhost() {
188         return localhost;
189     }
190
191     public void setLocalhost(String JavaDoc localhost) {
192         this.localhost = localhost;
193     }
194
195     // Implementation methods
196
//-------------------------------------------------------------------------
197
protected ServiceInfo createServiceInfo(String JavaDoc name, Map JavaDoc map) {
198         int port = MapHelper.getInt(map, "port", 0);
199
200         String JavaDoc type = getType();
201
202         if (log.isDebugEnabled()) {
203             log.debug("Registering service type: " + type + " name: " + name + " details: " + map);
204         }
205         return new ServiceInfo(type, name+"."+type, port, weight, priority, "");
206     }
207
208     protected JmDNS createJmDNS() throws IOException JavaDoc {
209         return JmDNSFactory.create(getLocalAddress());
210     }
211
212     protected InetAddress JavaDoc createLocalAddress() throws UnknownHostException JavaDoc {
213         if (localhost != null) {
214             return InetAddress.getByName(localhost);
215         }
216         return InetAddress.getLocalHost();
217     }
218
219     public void setDiscoveryListener(DiscoveryListener listener) {
220         this.listener = listener;
221     }
222
223     public String JavaDoc getGroup() {
224         return group;
225     }
226
227     public void setGroup(String JavaDoc group) {
228         this.group=group;
229     }
230
231     public String JavaDoc getType() {
232         return "_" + group+"."+TYPE_SUFFIX;
233     }
234
235     public void setBrokerName(String JavaDoc brokerName) {
236         this.brokerName = brokerName;
237     }
238
239     public void serviceFailed(DiscoveryEvent event) throws IOException JavaDoc {
240         // TODO: is there a way to notify the JmDNS that the service failed?
241
}
242 }
243
Popular Tags