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                            JmDNSFactory.onClose(getLocalAddress(), closeTarget);
096                        } catch (IOException e) {
097                            LOG.debug("Error closing JmDNS " + getLocalhost() + ". This exception will be ignored.", e);
098                        }
099                    }
100                };
101    
102                thread.setDaemon(true);
103                thread.start();
104    
105                jmdns = null;
106            }
107        }
108    
109        public void registerService(String name) throws IOException {
110            ServiceInfo si = createServiceInfo(name, new HashMap());
111            serviceInfos.add(si);
112            getJmdns().registerService(si);
113        }
114    
115        // ServiceListener interface
116        // -------------------------------------------------------------------------
117        public void addService(JmDNS jmDNS, String type, String name) {
118            if (LOG.isDebugEnabled()) {
119                LOG.debug("addService with type: " + type + " name: " + name);
120            }
121            if (listener != null) {
122                listener.onServiceAdd(new DiscoveryEvent(name));
123            }
124            jmDNS.requestServiceInfo(type, name);
125        }
126    
127        public void removeService(JmDNS jmDNS, String type, String name) {
128            if (LOG.isDebugEnabled()) {
129                LOG.debug("removeService with type: " + type + " name: " + name);
130            }
131            if (listener != null) {
132                listener.onServiceRemove(new DiscoveryEvent(name));
133            }
134        }
135    
136        public void serviceAdded(ServiceEvent event) {
137            addService(event.getDNS(), event.getType(), event.getName());
138        }
139    
140        public void serviceRemoved(ServiceEvent event) {
141            removeService(event.getDNS(), event.getType(), event.getName());
142        }
143    
144        public void serviceResolved(ServiceEvent event) {
145        }
146    
147        public void resolveService(JmDNS jmDNS, String type, String name, ServiceInfo serviceInfo) {
148        }
149    
150        public int getPriority() {
151            return priority;
152        }
153    
154        public void setPriority(int priority) {
155            this.priority = priority;
156        }
157    
158        public int getWeight() {
159            return weight;
160        }
161    
162        public void setWeight(int weight) {
163            this.weight = weight;
164        }
165    
166        public JmDNS getJmdns() throws IOException {
167            if (jmdns == null) {
168                jmdns = createJmDNS();
169            }
170            return jmdns;
171        }
172    
173        public void setJmdns(JmDNS jmdns) {
174            this.jmdns = jmdns;
175        }
176    
177        public InetAddress getLocalAddress() throws UnknownHostException {
178            if (localAddress == null) {
179                localAddress = createLocalAddress();
180            }
181            return localAddress;
182        }
183    
184        public void setLocalAddress(InetAddress localAddress) {
185            this.localAddress = localAddress;
186        }
187    
188        public String getLocalhost() {
189            return localhost;
190        }
191    
192        public void setLocalhost(String localhost) {
193            this.localhost = localhost;
194        }
195    
196        // Implementation methods
197        // -------------------------------------------------------------------------
198        protected ServiceInfo createServiceInfo(String name, Map map) {
199            int port = MapHelper.getInt(map, "port", 0);
200    
201            String type = getType();
202    
203            if (LOG.isDebugEnabled()) {
204                LOG.debug("Registering service type: " + type + " name: " + name + " details: " + map);
205            }
206            return ServiceInfo.create(type, name + "." + type, port, weight, priority, "");
207        }
208    
209        protected JmDNS createJmDNS() throws IOException {
210            return JmDNSFactory.create(getLocalAddress());
211        }
212    
213        protected InetAddress createLocalAddress() throws UnknownHostException {
214            if (localhost != null) {
215                return InetAddress.getByName(localhost);
216            }
217            return InetAddress.getLocalHost();
218        }
219    
220        public void setDiscoveryListener(DiscoveryListener listener) {
221            this.listener = listener;
222        }
223    
224        public String getGroup() {
225            return group;
226        }
227    
228        public void setGroup(String group) {
229            this.group = group;
230        }
231    
232        public String getType() {
233            return "_" + group + "." + TYPE_SUFFIX;
234        }
235    
236        public void serviceFailed(DiscoveryEvent event) throws IOException {
237            // TODO: is there a way to notify the JmDNS that the service failed?
238        }
239    
240    }