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