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