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.broker;
018
019import java.io.IOException;
020import java.net.URI;
021
022import org.apache.activemq.util.FactoryFinder;
023import org.apache.activemq.util.IOExceptionSupport;
024
025/**
026 * A helper class to create a fully configured broker service using a URI. The
027 * list of currently supported URI syntaxes is described <a
028 * href="http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html">here</a>
029 * 
030 * 
031 */
032public final class BrokerFactory {
033
034    private static final FactoryFinder BROKER_FACTORY_HANDLER_FINDER = new FactoryFinder("META-INF/services/org/apache/activemq/broker/");
035
036    private BrokerFactory() {        
037    }
038    
039    public static BrokerFactoryHandler createBrokerFactoryHandler(String type) throws IOException {
040        try {
041            return (BrokerFactoryHandler)BROKER_FACTORY_HANDLER_FINDER.newInstance(type);
042        } catch (Throwable e) {
043            throw IOExceptionSupport.create("Could not load " + type + " factory:" + e, e);
044        }
045    }
046
047    /**
048     * Creates a broker from a URI configuration
049     * 
050     * @param brokerURI the URI scheme to configure the broker
051     * @throws Exception
052     */
053    public static BrokerService createBroker(URI brokerURI) throws Exception {
054        return createBroker(brokerURI, false);
055    }
056
057    /**
058     * Creates a broker from a URI configuration
059     * 
060     * @param brokerURI the URI scheme to configure the broker
061     * @param startBroker whether or not the broker should have its
062     *                {@link BrokerService#start()} method called after
063     *                construction
064     * @throws Exception
065     */
066    public static BrokerService createBroker(URI brokerURI, boolean startBroker) throws Exception {
067        if (brokerURI.getScheme() == null) {
068            throw new IllegalArgumentException("Invalid broker URI, no scheme specified: " + brokerURI);
069        }
070        BrokerFactoryHandler handler = createBrokerFactoryHandler(brokerURI.getScheme());
071        BrokerService broker = handler.createBroker(brokerURI);
072        if (startBroker) {
073            broker.start();
074        }
075        return broker;
076    }
077
078    /**
079     * Creates a broker from a URI configuration
080     * 
081     * @param brokerURI the URI scheme to configure the broker
082     * @throws Exception
083     */
084    public static BrokerService createBroker(String brokerURI) throws Exception {
085        return createBroker(new URI(brokerURI));
086    }
087
088    /**
089     * Creates a broker from a URI configuration
090     * 
091     * @param brokerURI the URI scheme to configure the broker
092     * @param startBroker whether or not the broker should have its
093     *                {@link BrokerService#start()} method called after
094     *                construction
095     * @throws Exception
096     */
097    public static BrokerService createBroker(String brokerURI, boolean startBroker) throws Exception {
098        return createBroker(new URI(brokerURI), startBroker);
099    }
100
101    private static final ThreadLocal<Boolean> START_DEFAULT = new ThreadLocal<Boolean>();
102
103    public static void setStartDefault(boolean startDefault) {
104        START_DEFAULT.set(startDefault);
105    }
106    public static void resetStartDefault() {
107        START_DEFAULT.remove();
108    }
109
110    public static boolean getStartDefault() {
111        Boolean value = START_DEFAULT.get();
112        if( value==null ) {
113            return true;
114        }
115        return value.booleanValue();
116    }
117}