KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > TcpTransportFactory


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.tcp;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.net.ServerSocketFactory;
28 import javax.net.SocketFactory;
29
30 import org.apache.activemq.openwire.OpenWireFormat;
31 import org.apache.activemq.transport.InactivityMonitor;
32 import org.apache.activemq.transport.Transport;
33 import org.apache.activemq.transport.TransportFactory;
34 import org.apache.activemq.transport.TransportLogger;
35 import org.apache.activemq.transport.TransportServer;
36 import org.apache.activemq.transport.WireFormatNegotiator;
37 import org.apache.activemq.util.IOExceptionSupport;
38 import org.apache.activemq.util.IntrospectionSupport;
39 import org.apache.activemq.util.URISupport;
40 import org.apache.activemq.wireformat.WireFormat;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 public class TcpTransportFactory extends TransportFactory {
45     private static final Log log = LogFactory.getLog(TcpTransportFactory.class);
46     public TransportServer doBind(String JavaDoc brokerId, final URI JavaDoc location) throws IOException JavaDoc {
47         try {
48             Map JavaDoc options = new HashMap JavaDoc(URISupport.parseParamters(location));
49
50             ServerSocketFactory serverSocketFactory = createServerSocketFactory();
51             TcpTransportServer server = createTcpTransportServer(location, serverSocketFactory);
52             server.setWireFormatFactory(createWireFormatFactory(options));
53             IntrospectionSupport.setProperties(server, options);
54             Map JavaDoc transportOptions = IntrospectionSupport.extractProperties(options, "transport.");
55             server.setTransportOption(transportOptions);
56             server.bind();
57             
58             return server;
59         }
60         catch (URISyntaxException JavaDoc e) {
61             throw IOExceptionSupport.create(e);
62         }
63     }
64
65     /**
66      * Allows subclasses of TcpTransportFactory to create custom instances of TcpTransportServer.
67      *
68      * @param location
69      * @param serverSocketFactory
70      * @return
71      * @throws IOException
72      * @throws URISyntaxException
73      */

74     protected TcpTransportServer createTcpTransportServer(final URI JavaDoc location, ServerSocketFactory serverSocketFactory) throws IOException JavaDoc, URISyntaxException JavaDoc {
75         return new TcpTransportServer(this, location, serverSocketFactory);
76     }
77
78     public Transport compositeConfigure(Transport transport, WireFormat format, Map JavaDoc options) {
79         
80         TcpTransport tcpTransport = (TcpTransport) transport.narrow(TcpTransport.class);
81         IntrospectionSupport.setProperties(tcpTransport, options);
82         
83         Map JavaDoc socketOptions = IntrospectionSupport.extractProperties(options, "socket.");
84         tcpTransport.setSocketOptions(socketOptions);
85
86         if (tcpTransport.isTrace()) {
87             transport = new TransportLogger(transport);
88         }
89
90         if (isUseInactivityMonitor(transport)) {
91             transport = new InactivityMonitor(transport);
92         }
93
94         // Only need the WireFormatNegotiator if using openwire
95
if( format instanceof OpenWireFormat ) {
96             transport = new WireFormatNegotiator(transport, (OpenWireFormat)format, tcpTransport.getMinmumWireFormatVersion());
97         }
98         
99         return transport;
100     }
101
102     /**
103      * Returns true if the inactivity monitor should be used on the transport
104      */

105     protected boolean isUseInactivityMonitor(Transport transport) {
106         return true;
107     }
108
109     protected Transport createTransport(URI JavaDoc location,WireFormat wf) throws UnknownHostException JavaDoc,IOException JavaDoc{
110         URI JavaDoc localLocation=null;
111         String JavaDoc path=location.getPath();
112         // see if the path is a local URI location
113
if (path != null && path.length() > 0) {
114             int localPortIndex = path.indexOf(':');
115             try {
116                 Integer.parseInt(path.substring((localPortIndex + 1), path.length()));
117                 String JavaDoc localString = location.getScheme() + ":/" + path;
118                 localLocation = new URI JavaDoc(localString);
119             }
120             catch (Exception JavaDoc e) {
121                 log.warn("path isn't a valid local location for TcpTransport to use", e);
122             }
123         }
124         SocketFactory socketFactory = createSocketFactory();
125         return createTcpTransport(wf, socketFactory, location, localLocation);
126     }
127
128     /**
129      * Allows subclasses of TcpTransportFactory to provide a create custom TcpTransport intances.
130      *
131      * @param location
132      * @param wf
133      * @param socketFactory
134      * @param localLocation
135      * @return
136      * @throws UnknownHostException
137      * @throws IOException
138      */

139     protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI JavaDoc location, URI JavaDoc localLocation) throws UnknownHostException JavaDoc, IOException JavaDoc {
140         return new TcpTransport(wf, socketFactory, location, localLocation);
141     }
142
143     protected ServerSocketFactory createServerSocketFactory() {
144         return ServerSocketFactory.getDefault();
145     }
146
147     protected SocketFactory createSocketFactory() {
148         return SocketFactory.getDefault();
149     }
150 }
151
Popular Tags