001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport.discovery.zeroconf;
018    
019    import java.io.IOException;
020    import java.net.InetAddress;
021    import java.net.UnknownHostException;
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.Map;
025    import java.util.concurrent.CopyOnWriteArrayList;
026    import javax.jmdns.JmDNS;
027    import javax.jmdns.ServiceEvent;
028    import javax.jmdns.ServiceInfo;
029    import javax.jmdns.ServiceListener;
030    
031    import org.apache.activemq.command.DiscoveryEvent;
032    import org.apache.activemq.transport.discovery.DiscoveryAgent;
033    import org.apache.activemq.transport.discovery.DiscoveryListener;
034    import org.apache.activemq.util.JMSExceptionSupport;
035    import org.apache.activemq.util.MapHelper;
036    import org.slf4j.Logger;
037    import org.slf4j.LoggerFactory;
038    
039    /**
040     * A {@link DiscoveryAgent} using <a href="http://www.zeroconf.org/">Zeroconf</a>
041     * via the <a href="http://jmdns.sf.net/">jmDNS</a> library
042     * 
043     * 
044     */
045    public class ZeroconfDiscoveryAgent implements DiscoveryAgent, ServiceListener {
046        private static final Logger LOG = LoggerFactory.getLogger(ZeroconfDiscoveryAgent.class);
047    
048        private static final String TYPE_SUFFIX = "ActiveMQ-5.";
049    
050        private JmDNS jmdns;
051        private InetAddress localAddress;
052        private String localhost;
053        private int weight;
054        private int priority;
055    
056        private DiscoveryListener listener;
057        private String group = "default";
058        private final CopyOnWriteArrayList<ServiceInfo> serviceInfos = new CopyOnWriteArrayList<ServiceInfo>();
059    
060        // DiscoveryAgent interface
061        // -------------------------------------------------------------------------
062        public void start() throws Exception {
063            if (group == null) {
064                throw new IOException("You must specify a group to discover");
065            }
066            String type = getType();
067            if (!type.endsWith(".")) {
068                LOG.warn("The type '" + type + "' should end with '.' to be a valid Rendezvous type");
069                type += ".";
070            }
071            try {
072                // force lazy construction
073                getJmdns();
074                if (listener != null) {
075                    LOG.info("Discovering service of type: " + type);
076                    jmdns.addServiceListener(type, this);
077                }
078            } catch (IOException e) {
079                JMSExceptionSupport.create("Failed to start JmDNS service: " + e, e);
080            }
081        }
082    
083        public void stop() {
084            if (jmdns != null) {
085                for (Iterator<ServiceInfo> iter = serviceInfos.iterator(); iter.hasNext();) {
086                    ServiceInfo si = iter.next();
087                    jmdns.unregisterService(si);
088                }
089    
090                // Close it down async since this could block for a while.
091                final JmDNS closeTarget = jmdns;
092                Thread thread = new Thread() {
093                    public void run() {
094                        try {
095                            if (JmDNSFactory.onClose(getLocalAddress())) {
096                                closeTarget.close();
097                            };
098                        } catch (IOException e) {
099                            LOG.debug("Error closing JmDNS " + getLocalhost() + ". This exception will be ignored.", e);
100                        }
101                    }
102                };
103    
104                thread.setDaemon(true);
105                thread.start();
106    
107                jmdns = null;
108            }
109        }
110    
111        public void registerService(String name) throws IOException {
112            ServiceInfo si = createServiceInfo(name, new HashMap());
113            serviceInfos.add(si);
114            getJmdns().registerService(si);
115        }
116    
117        // ServiceListener interface
118        // -------------------------------------------------------------------------
119        public void addService(JmDNS jmDNS, String type, String name) {
120            if (LOG.isDebugEnabled()) {
121                LOG.debug("addService with type: " + type + " name: " + name);
122            }
123            if (listener != null) {
124                listener.onServiceAdd(new DiscoveryEvent(name));
125            }
126            jmDNS.requestServiceInfo(type, name);
127        }
128    
129        public void removeService(JmDNS jmDNS, String type, String 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    
138        public void serviceAdded(ServiceEvent event) {
139            addService(event.getDNS(), event.getType(), event.getName());
140        }
141    
142        public void serviceRemoved(ServiceEvent event) {
143            removeService(event.getDNS(), event.getType(), event.getName());
144        }
145    
146        public void serviceResolved(ServiceEvent event) {
147        }
148    
149        public void resolveService(JmDNS jmDNS, String type, String name, ServiceInfo serviceInfo) {
150        }
151    
152        public int getPriority() {
153            return priority;
154        }
155    
156        public void setPriority(int priority) {
157            this.priority = priority;
158        }
159    
160        public int getWeight() {
161            return weight;
162        }
163    
164        public void setWeight(int weight) {
165            this.weight = weight;
166        }
167    
168        public JmDNS getJmdns() throws IOException {
169            if (jmdns == null) {
170                jmdns = createJmDNS();
171            }
172            return jmdns;
173        }
174    
175        public void setJmdns(JmDNS jmdns) {
176            this.jmdns = jmdns;
177        }
178    
179        public InetAddress getLocalAddress() throws UnknownHostException {
180            if (localAddress == null) {
181                localAddress = createLocalAddress();
182            }
183            return localAddress;
184        }
185    
186        public void setLocalAddress(InetAddress localAddress) {
187            this.localAddress = localAddress;
188        }
189    
190        public String getLocalhost() {
191            return localhost;
192        }
193    
194        public void setLocalhost(String localhost) {
195            this.localhost = localhost;
196        }
197    
198        // Implementation methods
199        // -------------------------------------------------------------------------
200        protected ServiceInfo createServiceInfo(String name, Map map) {
201            int port = MapHelper.getInt(map, "port", 0);
202    
203            String type = getType();
204    
205            if (LOG.isDebugEnabled()) {
206                LOG.debug("Registering service type: " + type + " name: " + name + " details: " + map);
207            }
208            return ServiceInfo.create(type, name + "." + type, port, weight, priority, "");
209        }
210    
211        protected JmDNS createJmDNS() throws IOException {
212            return JmDNSFactory.create(getLocalAddress());
213        }
214    
215        protected InetAddress createLocalAddress() throws UnknownHostException {
216            if (localhost != null) {
217                return InetAddress.getByName(localhost);
218            }
219            return InetAddress.getLocalHost();
220        }
221    
222        public void setDiscoveryListener(DiscoveryListener listener) {
223            this.listener = listener;
224        }
225    
226        public String getGroup() {
227            return group;
228        }
229    
230        public void setGroup(String group) {
231            this.group = group;
232        }
233    
234        public String getType() {
235            return "_" + group + "." + TYPE_SUFFIX;
236        }
237    
238        public void serviceFailed(DiscoveryEvent event) throws IOException {
239            // TODO: is there a way to notify the JmDNS that the service failed?
240        }
241    
242    }