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.failover;
018    
019    import java.io.IOException;
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    import java.util.Map;
023    
024    import org.apache.activemq.transport.MutexTransport;
025    import org.apache.activemq.transport.ResponseCorrelator;
026    import org.apache.activemq.transport.Transport;
027    import org.apache.activemq.transport.TransportFactory;
028    import org.apache.activemq.transport.TransportServer;
029    import org.apache.activemq.util.IntrospectionSupport;
030    import org.apache.activemq.util.URISupport;
031    import org.apache.activemq.util.URISupport.CompositeData;
032    
033    public class FailoverTransportFactory extends TransportFactory {
034    
035        public Transport doConnect(URI location) throws IOException {
036            try {
037                Transport transport = createTransport(URISupport.parseComposite(location));
038                transport = new MutexTransport(transport);
039                transport = new ResponseCorrelator(transport);
040                return transport;
041            } catch (URISyntaxException e) {
042                throw new IOException("Invalid location: " + location);
043            }
044        }
045    
046        public Transport doCompositeConnect(URI location) throws IOException {
047            try {
048                return createTransport(URISupport.parseComposite(location));
049            } catch (URISyntaxException e) {
050                throw new IOException("Invalid location: " + location);
051            }
052        }
053    
054        /**
055         * @param location
056         * @return
057         * @throws IOException
058         */
059        public Transport createTransport(CompositeData compositData) throws IOException {
060            Map options = compositData.getParameters();
061            FailoverTransport transport = createTransport(options);
062            if (!options.isEmpty()) {
063                throw new IllegalArgumentException("Invalid connect parameters: " + options);
064            }
065            transport.add(compositData.getComponents());
066            return transport;
067        }
068    
069        public FailoverTransport createTransport(Map parameters) throws IOException {
070            FailoverTransport transport = new FailoverTransport();
071            IntrospectionSupport.setProperties(transport, parameters);
072            return transport;
073        }
074    
075        public TransportServer doBind(URI location) throws IOException {
076            throw new IOException("Invalid server URI: " + location);
077        }
078    
079    }