001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport.discovery;
018    
019    import java.io.IOException;
020    import java.net.URI;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.apache.activemq.transport.CompositeTransport;
025    import org.apache.activemq.transport.Transport;
026    import org.apache.activemq.transport.TransportServer;
027    import org.apache.activemq.transport.failover.FailoverTransport;
028    import org.apache.activemq.transport.failover.FailoverTransportFactory;
029    import org.apache.activemq.util.IntrospectionSupport;
030    import org.apache.activemq.util.URISupport.CompositeData;
031    
032    /**
033     * 
034     */
035    public class DiscoveryTransportFactory extends FailoverTransportFactory {
036            
037        public Transport createTransport(CompositeData compositeData) throws IOException {
038            Map<String, String> parameters = new HashMap<String, String>(compositeData.getParameters());
039            FailoverTransport failoverTransport = createTransport(parameters);
040            return createTransport(failoverTransport, compositeData, parameters);
041        }
042        
043        /**
044         * Creates a transport that reports discovered brokers to a specific composite transport.
045         * 
046         * @param compositeTransport transport to report discovered brokers to
047         * @param compositeData used to apply parameters to this transport 
048         * @return a transport that reports discovered brokers to a specific composite transport.
049         * @throws IOException
050         */
051        public static DiscoveryTransport createTransport(CompositeTransport compositeTransport, CompositeData compositeData, Map<String, String> parameters) throws IOException {
052            DiscoveryTransport transport = new DiscoveryTransport(compositeTransport);
053    
054            IntrospectionSupport.setProperties(transport, parameters);
055            transport.setParameters(parameters);
056            
057            URI discoveryAgentURI = compositeData.getComponents()[0];
058            DiscoveryAgent discoveryAgent = DiscoveryAgentFactory.createDiscoveryAgent(discoveryAgentURI);
059            transport.setDiscoveryAgent(discoveryAgent);
060            return transport;
061        }
062    
063        public TransportServer doBind(URI location) throws IOException {
064            throw new IOException("Invalid server URI: " + location);
065    // try{
066    //            CompositeData compositData=URISupport.parseComposite(location);
067    //            URI[] components=compositData.getComponents();
068    //            if(components.length!=1){
069    //                throw new IOException("Invalid location: "+location
070    //                                +", the location must have 1 and only 1 composite URI in it - components = "
071    //                                +components.length);
072    //            }
073    //            Map parameters=new HashMap(compositData.getParameters());
074    //            DiscoveryTransportServer server=new DiscoveryTransportServer(TransportFactory.bind(value,components[0]));
075    //            IntrospectionSupport.setProperties(server,parameters,"discovery");
076    //            DiscoveryAgent discoveryAgent=DiscoveryAgentFactory.createDiscoveryAgent(server.getDiscovery());
077    //            // Use the host name to configure the group of the discovery agent.
078    //            if(!parameters.containsKey("discovery.group")){
079    //                if(compositData.getHost()!=null){
080    //                    parameters.put("discovery.group",compositData.getHost());
081    //                }
082    //            }
083    //            if(!parameters.containsKey("discovery.brokerName")){
084    //                parameters.put("discovery.brokerName",value);
085    //            }
086    //            IntrospectionSupport.setProperties(discoveryAgent,parameters,"discovery.");
087    //            server.setDiscoveryAgent(discoveryAgent);
088    //            return server;
089    //        }catch(URISyntaxException e){
090    //            throw new IOException("Invalid location: "+location);
091    //        }
092        }
093    
094    }