KickJava   Java API By Example, From Geeks To Geeks.

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


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.network;
19
20 import org.apache.activemq.transport.Transport;
21 import org.apache.activemq.transport.TransportFactory;
22 import org.apache.activemq.util.ServiceStopper;
23
24 import java.net.URI JavaDoc;
25
26 /**
27  * A network connector which uses some kind of multicast-like transport that
28  * communicates with potentially many remote brokers over a single logical
29  * {@link Transport} instance such as when using multicast.
30  *
31  * This implementation does not depend on multicast at all; any other group
32  * based transport could be used.
33  *
34  * @org.apache.xbean.XBean
35  *
36  * @version $Revision: 520814 $
37  */

38 public class MulticastNetworkConnector extends NetworkConnector {
39
40     private Transport localTransport;
41     private Transport remoteTransport;
42     private URI JavaDoc remoteURI;
43     private DemandForwardingBridgeSupport bridge;
44
45     public MulticastNetworkConnector() {
46     }
47
48     public MulticastNetworkConnector(URI JavaDoc remoteURI) {
49         this.remoteURI = remoteURI;
50     }
51
52     // Properties
53
// -------------------------------------------------------------------------
54

55     public DemandForwardingBridgeSupport getBridge() {
56         return bridge;
57     }
58
59     public void setBridge(DemandForwardingBridgeSupport bridge) {
60         this.bridge = bridge;
61     }
62
63     public Transport getLocalTransport() {
64         return localTransport;
65     }
66
67     public void setLocalTransport(Transport localTransport) {
68         this.localTransport = localTransport;
69     }
70
71     public Transport getRemoteTransport() {
72         return remoteTransport;
73     }
74
75     /**
76      * Sets the remote transport implementation
77      */

78     public void setRemoteTransport(Transport remoteTransport) {
79         this.remoteTransport = remoteTransport;
80     }
81
82     public URI JavaDoc getRemoteURI() {
83         return remoteURI;
84     }
85
86     /**
87      * Sets the remote transport URI to some group transport like
88      * <code>multicast://address:port</code>
89      */

90     public void setRemoteURI(URI JavaDoc remoteURI) {
91         this.remoteURI = remoteURI;
92     }
93
94     // Implementation methods
95
// -------------------------------------------------------------------------
96

97     protected void handleStart() throws Exception JavaDoc {
98         if (remoteTransport == null) {
99             if (remoteURI == null) {
100                 throw new IllegalArgumentException JavaDoc("You must specify the remoteURI property");
101             }
102             remoteTransport = TransportFactory.connect(remoteURI);
103         }
104
105         if (localTransport == null) {
106             localTransport = createLocalTransport();
107         }
108
109         bridge = createBridge(localTransport, remoteTransport);
110         configureBridge(bridge);
111         bridge.start();
112
113         // we need to start the transports after we've created the bridge
114
remoteTransport.start();
115         localTransport.start();
116
117         super.handleStart();
118     }
119
120     protected void handleStop(ServiceStopper stopper) throws Exception JavaDoc {
121         super.handleStop(stopper);
122         if (bridge != null) {
123             try {
124                 bridge.stop();
125             }
126             catch (Exception JavaDoc e) {
127                 stopper.onException(this, e);
128             }
129         }
130         if (remoteTransport != null) {
131             try {
132                 remoteTransport.stop();
133             }
134             catch (Exception JavaDoc e) {
135                 stopper.onException(this, e);
136             }
137         }
138         if (localTransport != null) {
139             try {
140                 localTransport.stop();
141             }
142             catch (Exception JavaDoc e) {
143                 stopper.onException(this, e);
144             }
145         }
146     }
147
148     public String JavaDoc getName() {
149         return remoteTransport.toString();
150     }
151
152     protected DemandForwardingBridgeSupport createBridge(Transport local, Transport remote) {
153         return new CompositeDemandForwardingBridge(this,local, remote);
154     }
155
156 }
157
Popular Tags