1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.activemq.broker; 18 19 import java.io.IOException; 20 import java.net.URI; 21 22 import org.apache.activemq.util.FactoryFinder; 23 import org.apache.activemq.util.IOExceptionSupport; 24 25 /** 26 * A helper class to create a fully configured broker service using a URI. The 27 * list of currently supported URI syntaxes is described <a 28 * href="http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html">here</a> 29 * 30 * @version $Revision$ 31 */ 32 public final class BrokerFactory { 33 34 private static final FactoryFinder BROKER_FACTORY_HANDLER_FINDER = new FactoryFinder("META-INF/services/org/apache/activemq/broker/"); 35 36 private BrokerFactory() { 37 } 38 39 public static BrokerFactoryHandler createBrokerFactoryHandler(String type) throws IOException { 40 try { 41 return (BrokerFactoryHandler)BROKER_FACTORY_HANDLER_FINDER.newInstance(type); 42 } catch (Throwable e) { 43 throw IOExceptionSupport.create("Could not load " + type + " factory:" + e, e); 44 } 45 } 46 47 /** 48 * Creates a broker from a URI configuration 49 * 50 * @param brokerURI the URI scheme to configure the broker 51 * @throws Exception 52 */ 53 public static BrokerService createBroker(URI brokerURI) throws Exception { 54 return createBroker(brokerURI, false); 55 } 56 57 /** 58 * Creates a broker from a URI configuration 59 * 60 * @param brokerURI the URI scheme to configure the broker 61 * @param startBroker whether or not the broker should have its 62 * {@link BrokerService#start()} method called after 63 * construction 64 * @throws Exception 65 */ 66 public static BrokerService createBroker(URI brokerURI, boolean startBroker) throws Exception { 67 if (brokerURI.getScheme() == null) { 68 throw new IllegalArgumentException("Invalid broker URI, no scheme specified: " + brokerURI); 69 } 70 BrokerFactoryHandler handler = createBrokerFactoryHandler(brokerURI.getScheme()); 71 BrokerService broker = handler.createBroker(brokerURI); 72 if (startBroker) { 73 broker.start(); 74 } 75 return broker; 76 } 77 78 /** 79 * Creates a broker from a URI configuration 80 * 81 * @param brokerURI the URI scheme to configure the broker 82 * @throws Exception 83 */ 84 public static BrokerService createBroker(String brokerURI) throws Exception { 85 return createBroker(new URI(brokerURI)); 86 } 87 88 /** 89 * Creates a broker from a URI configuration 90 * 91 * @param brokerURI the URI scheme to configure the broker 92 * @param startBroker whether or not the broker should have its 93 * {@link BrokerService#start()} method called after 94 * construction 95 * @throws Exception 96 */ 97 public static BrokerService createBroker(String brokerURI, boolean startBroker) throws Exception { 98 return createBroker(new URI(brokerURI), startBroker); 99 } 100 101 }