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.network;
018    
019    import java.net.URI;
020    import java.util.HashMap;
021    import org.apache.activemq.broker.Broker;
022    import org.apache.activemq.transport.Transport;
023    import org.apache.activemq.transport.TransportFactory;
024    import org.apache.activemq.util.URISupport;
025    
026    /**
027     * Factory for network bridges
028     * 
029     * 
030     */
031    public final class NetworkBridgeFactory {
032    
033        private NetworkBridgeFactory() {
034        }
035        
036        /**
037         * Create a network bridge
038         * 
039         * @param config
040         * @param localTransport
041         * @param remoteTransport
042         * @return the NetworkBridge
043         */
044        public static DemandForwardingBridge createBridge(NetworkBridgeConfiguration config,
045                                                          Transport localTransport, Transport remoteTransport) {
046            return createBridge(config, localTransport, remoteTransport, null);
047        }
048    
049        /**
050         * create a network bridge
051         * 
052         * @param configuration
053         * @param localTransport
054         * @param remoteTransport
055         * @param listener
056         * @return the NetworkBridge
057         */
058        public static DemandForwardingBridge createBridge(NetworkBridgeConfiguration configuration,
059                                                          Transport localTransport, Transport remoteTransport,
060                                                          final NetworkBridgeListener listener) {
061            DemandForwardingBridge result = null;
062            if (configuration.isConduitSubscriptions()) {
063                // dynamicOnly determines whether durables are auto bridged
064                result = new DurableConduitBridge(configuration, localTransport, remoteTransport);
065            } else {
066                result = new DemandForwardingBridge(configuration, localTransport, remoteTransport);
067            }
068            if (listener != null) {
069                result.setNetworkBridgeListener(listener);
070            }
071            return result;
072        }
073    
074        public static Transport createLocalTransport(Broker broker) throws Exception {
075            URI uri = broker.getVmConnectorURI();
076            HashMap<String, String> map = new HashMap<String, String>(URISupport.parseParameters(uri));
077            map.put("network", "true");
078            map.put("async", "false");
079            uri = URISupport.createURIWithQuery(uri, URISupport.createQueryString(map));
080            return TransportFactory.connect(uri);
081        }
082    }