KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > peer > PeerTransportFactory


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.peer;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.activemq.broker.BrokerService;
27 import org.apache.activemq.broker.TransportConnector;
28 import org.apache.activemq.broker.BrokerFactoryHandler;
29 import org.apache.activemq.transport.Transport;
30 import org.apache.activemq.transport.TransportFactory;
31 import org.apache.activemq.transport.TransportServer;
32 import org.apache.activemq.transport.vm.VMTransportFactory;
33 import org.apache.activemq.util.IOExceptionSupport;
34 import org.apache.activemq.util.IdGenerator;
35 import org.apache.activemq.util.IntrospectionSupport;
36 import org.apache.activemq.util.URISupport;
37
38 import java.util.concurrent.ConcurrentHashMap JavaDoc;
39
40 public class PeerTransportFactory extends TransportFactory {
41
42     final public static ConcurrentHashMap JavaDoc brokers = new ConcurrentHashMap JavaDoc();
43
44     final public static ConcurrentHashMap JavaDoc connectors = new ConcurrentHashMap JavaDoc();
45
46     final public static ConcurrentHashMap JavaDoc servers = new ConcurrentHashMap JavaDoc();
47   
48     private IdGenerator idGenerator = new IdGenerator("peer-");
49
50     
51     public Transport doConnect(URI JavaDoc location) throws Exception JavaDoc {
52         VMTransportFactory vmTransportFactory = createTransportFactory(location);
53         return vmTransportFactory.doConnect(location);
54     }
55
56     public Transport doCompositeConnect(URI JavaDoc location) throws Exception JavaDoc {
57         VMTransportFactory vmTransportFactory = createTransportFactory(location);
58         return vmTransportFactory.doCompositeConnect(location);
59     }
60
61     /**
62      * @param location
63      * @return the converted URI
64      * @throws URISyntaxException
65      */

66     private VMTransportFactory createTransportFactory(URI JavaDoc location) throws IOException JavaDoc {
67         try {
68             String JavaDoc group = location.getHost();
69             String JavaDoc broker = URISupport.stripPrefix(location.getPath(), "/");
70             
71             if( group == null ) {
72                 group = "default";
73             }
74             if (broker == null || broker.length()==0){
75                 broker = idGenerator.generateSanitizedId();
76             }
77             
78             final Map JavaDoc brokerOptions = new HashMap JavaDoc(URISupport.parseParamters(location));
79             if (!brokerOptions.containsKey("persistent")){
80                 brokerOptions.put("persistent", "false");
81             }
82                         
83             final URI JavaDoc finalLocation = new URI JavaDoc("vm://"+broker);
84             final String JavaDoc finalBroker = broker;
85             final String JavaDoc finalGroup = group;
86             VMTransportFactory rc = new VMTransportFactory() {
87                 public Transport doConnect(URI JavaDoc ignore) throws Exception JavaDoc {
88                     return super.doConnect(finalLocation);
89                 };
90                 public Transport doCompositeConnect(URI JavaDoc ignore) throws Exception JavaDoc {
91                     return super.doCompositeConnect(finalLocation);
92                 };
93             };
94             rc.setBrokerFactoryHandler(new BrokerFactoryHandler(){
95                 public BrokerService createBroker(URI JavaDoc brokerURI) throws Exception JavaDoc {
96                     BrokerService service = new BrokerService();
97                     IntrospectionSupport.setProperties(service, brokerOptions);
98                     service.setBrokerName(finalBroker);
99                     TransportConnector c = service.addConnector("tcp://localhost:0");
100                     c.setDiscoveryUri(new URI JavaDoc("multicast://"+finalGroup));
101                     service.addNetworkConnector("multicast://"+finalGroup);
102                     return service;
103                 }
104             });
105             return rc;
106             
107         } catch (URISyntaxException JavaDoc e) {
108             throw IOExceptionSupport.create(e);
109         }
110     }
111
112
113     public TransportServer doBind(String JavaDoc brokerId,URI JavaDoc location) throws IOException JavaDoc {
114         throw new IOException JavaDoc("This protocol does not support being bound.");
115     }
116
117 }
118
Popular Tags