KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > discovery > DiscoveryTransport


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;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23
24 import org.apache.activemq.command.DiscoveryEvent;
25 import org.apache.activemq.transport.CompositeTransport;
26 import org.apache.activemq.transport.TransportFilter;
27 import org.apache.activemq.util.ServiceStopper;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import java.util.concurrent.ConcurrentHashMap JavaDoc;
32
33 /**
34  * A {@link ReliableTransportChannel} which uses a {@link DiscoveryAgent} to
35  * discover remote broker instances and dynamically connect to them.
36  *
37  * @version $Revision$
38  */

39 public class DiscoveryTransport extends TransportFilter implements DiscoveryListener {
40
41     private static final Log log = LogFactory.getLog(DiscoveryTransport.class);
42
43     private final CompositeTransport next;
44     private DiscoveryAgent discoveryAgent;
45     private final ConcurrentHashMap JavaDoc serviceURIs = new ConcurrentHashMap JavaDoc();
46
47     public DiscoveryTransport(CompositeTransport next) {
48         super(next);
49         this.next = next;
50     }
51
52     public void start() throws Exception JavaDoc {
53         if (discoveryAgent == null) {
54             throw new IllegalStateException JavaDoc("discoveryAgent not configured");
55         }
56
57         // lets pass into the agent the broker name and connection details
58
discoveryAgent.setDiscoveryListener(this);
59         discoveryAgent.start();
60         next.start();
61     }
62
63     public void stop() throws Exception JavaDoc {
64         ServiceStopper ss = new ServiceStopper();
65         ss.stop(discoveryAgent);
66         ss.stop(next);
67         ss.throwFirstException();
68     }
69
70     public void onServiceAdd(DiscoveryEvent event) {
71         String JavaDoc url = event.getServiceName();
72         if (url != null) {
73             try {
74                 URI JavaDoc uri = new URI JavaDoc(url);
75                 serviceURIs.put(event.getServiceName(), uri);
76                 log.info("Adding new broker connection URL: " + uri );
77                 next.add(new URI JavaDoc[]{uri});
78             } catch (URISyntaxException JavaDoc e) {
79                 log.warn("Could not connect to remote URI: " + url + " due to bad URI syntax: " + e, e);
80             }
81         }
82     }
83
84     public void onServiceRemove(DiscoveryEvent event) {
85         URI JavaDoc uri = (URI JavaDoc) serviceURIs.get(event.getServiceName());
86         if (uri != null) {
87             next.remove(new URI JavaDoc[]{uri});
88         }
89     }
90
91     public DiscoveryAgent getDiscoveryAgent() {
92         return discoveryAgent;
93     }
94
95     public void setDiscoveryAgent(DiscoveryAgent discoveryAgent) {
96         this.discoveryAgent = discoveryAgent;
97     }
98
99 }
100
Popular Tags