KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > network > NetworkConnector


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

14
15 package org.apache.activemq.network;
16
17 import java.net.URI JavaDoc;
18 import java.net.URISyntaxException JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
22 import org.apache.activemq.Service;
23 import org.apache.activemq.command.ActiveMQDestination;
24 import org.apache.activemq.transport.Transport;
25 import org.apache.activemq.transport.TransportFactory;
26 import org.apache.activemq.util.ServiceStopper;
27 import org.apache.activemq.util.ServiceSupport;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * @version $Revision$
33  */

34 public abstract class NetworkConnector extends NetworkBridgeConfiguration implements Service{
35
36     protected static final Log log=LogFactory.getLog(NetworkConnector.class);
37     protected URI JavaDoc localURI;
38     private Set JavaDoc durableDestinations;
39     private List JavaDoc excludedDestinations=new CopyOnWriteArrayList JavaDoc();
40     private List JavaDoc dynamicallyIncludedDestinations=new CopyOnWriteArrayList JavaDoc();
41     private List JavaDoc staticallyIncludedDestinations=new CopyOnWriteArrayList JavaDoc();
42     protected ConnectionFilter connectionFilter;
43     protected ServiceSupport serviceSupport=new ServiceSupport(){
44
45         protected void doStart() throws Exception JavaDoc{
46            handleStart();
47         }
48
49         protected void doStop(ServiceStopper stopper) throws Exception JavaDoc{
50             handleStop(stopper);
51         }
52     };
53
54     public NetworkConnector(){
55     }
56
57     public NetworkConnector(URI JavaDoc localURI){
58         this.localURI=localURI;
59     }
60
61     public URI JavaDoc getLocalUri() throws URISyntaxException JavaDoc{
62         return localURI;
63     }
64
65     public void setLocalUri(URI JavaDoc localURI){
66         this.localURI=localURI;
67     }
68
69     
70     /**
71      * @return Returns the durableDestinations.
72      */

73     public Set JavaDoc getDurableDestinations(){
74         return durableDestinations;
75     }
76
77     /**
78      * @param durableDestinations The durableDestinations to set.
79      */

80     public void setDurableDestinations(Set JavaDoc durableDestinations){
81         this.durableDestinations=durableDestinations;
82     }
83
84     /**
85      * @return Returns the excludedDestinations.
86      */

87     public List JavaDoc getExcludedDestinations(){
88         return excludedDestinations;
89     }
90
91     /**
92      * @param excludedDestinations The excludedDestinations to set.
93      */

94     public void setExcludedDestinations(List JavaDoc excludedDestinations){
95         this.excludedDestinations=excludedDestinations;
96     }
97
98     public void addExcludedDestination(ActiveMQDestination destiantion){
99         this.excludedDestinations.add(destiantion);
100     }
101
102     /**
103      * @return Returns the staticallyIncludedDestinations.
104      */

105     public List JavaDoc getStaticallyIncludedDestinations(){
106         return staticallyIncludedDestinations;
107     }
108
109     /**
110      * @param staticallyIncludedDestinations The staticallyIncludedDestinations to set.
111      */

112     public void setStaticallyIncludedDestinations(List JavaDoc staticallyIncludedDestinations){
113         this.staticallyIncludedDestinations=staticallyIncludedDestinations;
114     }
115
116     public void addStaticallyIncludedDestination(ActiveMQDestination destiantion){
117         this.staticallyIncludedDestinations.add(destiantion);
118     }
119
120     /**
121      * @return Returns the dynamicallyIncludedDestinations.
122      */

123     public List JavaDoc getDynamicallyIncludedDestinations(){
124         return dynamicallyIncludedDestinations;
125     }
126
127     /**
128      * @param dynamicallyIncludedDestinations The dynamicallyIncludedDestinations to set.
129      */

130     public void setDynamicallyIncludedDestinations(List JavaDoc dynamicallyIncludedDestinations){
131         this.dynamicallyIncludedDestinations=dynamicallyIncludedDestinations;
132     }
133
134     public void addDynamicallyIncludedDestination(ActiveMQDestination destiantion){
135         this.dynamicallyIncludedDestinations.add(destiantion);
136     }
137     
138     public ConnectionFilter getConnectionFilter(){
139         return connectionFilter;
140     }
141
142     public void setConnectionFilter(ConnectionFilter connectionFilter){
143         this.connectionFilter=connectionFilter;
144     }
145
146
147     // Implementation methods
148
// -------------------------------------------------------------------------
149
protected NetworkBridge configureBridge(DemandForwardingBridgeSupport result){
150         List JavaDoc destsList=getDynamicallyIncludedDestinations();
151         ActiveMQDestination dests[]=(ActiveMQDestination[])destsList.toArray(new ActiveMQDestination[destsList.size()]);
152         result.setDynamicallyIncludedDestinations(dests);
153         destsList=getExcludedDestinations();
154         dests=(ActiveMQDestination[])destsList.toArray(new ActiveMQDestination[destsList.size()]);
155         result.setExcludedDestinations(dests);
156         destsList=getStaticallyIncludedDestinations();
157         dests=(ActiveMQDestination[])destsList.toArray(new ActiveMQDestination[destsList.size()]);
158         result.setStaticallyIncludedDestinations(dests);
159         if(durableDestinations!=null){
160             ActiveMQDestination[] dest=new ActiveMQDestination[durableDestinations.size()];
161             dest=(ActiveMQDestination[])durableDestinations.toArray(dest);
162             result.setDurableDestinations(dest);
163         }
164         return result;
165     }
166
167     protected Transport createLocalTransport() throws Exception JavaDoc{
168         return TransportFactory.connect(localURI);
169     }
170
171     public void start() throws Exception JavaDoc{
172         serviceSupport.start();
173     }
174
175     public void stop() throws Exception JavaDoc{
176         serviceSupport.stop();
177     }
178     
179     public abstract String JavaDoc getName();
180     
181     protected void handleStart() throws Exception JavaDoc{
182         if(localURI==null){
183             throw new IllegalStateException JavaDoc("You must configure the 'localURI' property");
184         }
185         log.info("Network Connector "+getName()+" Started");
186     }
187
188     protected void handleStop(ServiceStopper stopper) throws Exception JavaDoc{
189         log.info("Network Connector "+getName()+" Stopped");
190     }
191 }
192
Popular Tags