KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > proxy > ProxyConnector


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.proxy;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.activemq.Service;
26 import org.apache.activemq.transport.CompositeTransport;
27 import org.apache.activemq.transport.Transport;
28 import org.apache.activemq.transport.TransportAcceptListener;
29 import org.apache.activemq.transport.TransportFactory;
30 import org.apache.activemq.transport.TransportFilter;
31 import org.apache.activemq.transport.TransportServer;
32 import org.apache.activemq.util.ServiceStopper;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
37
38 /**
39  * @org.apache.xbean.XBean
40  *
41  * @version $Revision$
42  */

43 public class ProxyConnector implements Service {
44
45     private static final Log log = LogFactory.getLog(ProxyConnector.class);
46     private TransportServer server;
47     private URI JavaDoc bind;
48     private URI JavaDoc remote;
49     private URI JavaDoc localUri;
50     private String JavaDoc name;
51     
52     CopyOnWriteArrayList JavaDoc connections = new CopyOnWriteArrayList JavaDoc();
53        
54     public void start() throws Exception JavaDoc {
55         
56         this.getServer().setAcceptListener(new TransportAcceptListener() {
57             public void onAccept(Transport localTransport) {
58                 try {
59                     Transport remoteTransport = createRemoteTransport();
60                     ProxyConnection connection = new ProxyConnection(localTransport, remoteTransport);
61                     connections.add(connection);
62                     connection.start();
63                 }
64                 catch (Exception JavaDoc e) {
65                     onAcceptError(e);
66                 }
67             }
68
69             public void onAcceptError(Exception JavaDoc error) {
70                 log.error("Could not accept connection: " + error, error);
71             }
72         });
73         getServer().start();
74         log.info("Proxy Connector "+getName()+" Started");
75
76     }
77
78     public void stop() throws Exception JavaDoc {
79         ServiceStopper ss = new ServiceStopper();
80         if (this.server != null) {
81             ss.stop(this.server);
82         }
83         for (Iterator JavaDoc iter = connections.iterator(); iter.hasNext();) {
84             log.info("Connector stopped: Stopping proxy.");
85             ss.stop((Service) iter.next());
86         }
87         ss.throwFirstException();
88         log.info("Proxy Connector " + getName() + " Stopped");
89     }
90     
91     // Properties
92
// -------------------------------------------------------------------------
93

94     public URI JavaDoc getLocalUri() {
95         return localUri;
96     }
97
98     public void setLocalUri(URI JavaDoc localURI) {
99         this.localUri = localURI;
100     }
101
102     public URI JavaDoc getBind() {
103         return bind;
104     }
105
106     public void setBind(URI JavaDoc bind) {
107         this.bind = bind;
108     }
109
110     public URI JavaDoc getRemote() {
111         return remote;
112     }
113
114     public void setRemote(URI JavaDoc remote) {
115         this.remote = remote;
116     }
117
118     public TransportServer getServer() throws IOException JavaDoc, URISyntaxException JavaDoc {
119         if (server == null) {
120             server = createServer();
121         }
122         return server;
123     }
124     
125     public void setServer(TransportServer server) {
126         this.server = server;
127     }
128
129     protected TransportServer createServer() throws IOException JavaDoc, URISyntaxException JavaDoc {
130         if (bind == null) {
131             throw new IllegalArgumentException JavaDoc("You must specify either a server or the bind property");
132         }
133         return TransportFactory.bind(null, bind);
134     }
135
136     private Transport createRemoteTransport() throws Exception JavaDoc {
137         Transport transport = TransportFactory.compositeConnect(remote);
138         CompositeTransport ct = (CompositeTransport) transport.narrow(CompositeTransport.class);
139         if( ct !=null && localUri!=null ) {
140             ct.add(new URI JavaDoc[]{localUri});
141         }
142         
143         // Add a transport filter so that can track the transport life cycle
144
transport = new TransportFilter(transport) {
145             public void stop() throws Exception JavaDoc {
146                 System.out.println("Stopping proxy.");
147                 super.stop();
148                 connections.remove(this);
149             }
150         };
151         return transport;
152     }
153
154     public String JavaDoc getName() {
155         if( name == null ) {
156             if( server!=null ) {
157                 name = server.getConnectURI().toString();
158             } else {
159                 name = "proxy";
160             }
161         }
162         return name;
163     }
164
165     public void setName(String JavaDoc name) {
166         this.name = name;
167     }
168
169 }
170
Popular Tags