KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > BrokerFactory


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.broker;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22
23 import org.apache.activemq.util.FactoryFinder;
24 import org.apache.activemq.util.IOExceptionSupport;
25
26 /**
27  * A helper class to create a fully configured broker service using a URI.
28  * The list of currently supported URI syntaxes is described
29  * <a HREF="http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html">here</a>
30  *
31  * @version $Revision$
32  */

33 public class BrokerFactory {
34
35     static final private FactoryFinder brokerFactoryHandlerFinder = new FactoryFinder("META-INF/services/org/apache/activemq/broker/");
36
37     public static BrokerFactoryHandler createBrokerFactoryHandler(String JavaDoc type) throws IOException JavaDoc {
38         try {
39             return (BrokerFactoryHandler)brokerFactoryHandlerFinder.newInstance(type);
40         } catch (Throwable JavaDoc e) {
41             throw IOExceptionSupport.create("Could load "+type+" factory:"+e, e);
42         }
43     }
44
45     /**
46      * Creates a broker from a URI configuration
47      * @param brokerURI the URI scheme to configure the broker
48      * @throws Exception
49      */

50     public static BrokerService createBroker(URI JavaDoc brokerURI) throws Exception JavaDoc {
51         return createBroker(brokerURI, false);
52     }
53
54     /**
55      * Creates a broker from a URI configuration
56      * @param brokerURI the URI scheme to configure the broker
57      * @param startBroker whether or not the broker should have its {@link BrokerService#start()} method called after construction
58      * @throws Exception
59      */

60     public static BrokerService createBroker(URI JavaDoc brokerURI, boolean startBroker) throws Exception JavaDoc {
61         if( brokerURI.getScheme() == null )
62             throw new IllegalArgumentException JavaDoc("Invalid broker URI, no scheme specified: "+brokerURI);
63         
64         BrokerFactoryHandler handler = createBrokerFactoryHandler(brokerURI.getScheme());
65         BrokerService broker = handler.createBroker(brokerURI);
66         if (startBroker) {
67             broker.start();
68         }
69         return broker;
70     }
71
72
73     /**
74      * Creates a broker from a URI configuration
75      * @param brokerURI the URI scheme to configure the broker
76      * @throws Exception
77      */

78     public static BrokerService createBroker(String JavaDoc brokerURI) throws Exception JavaDoc {
79         return createBroker(new URI JavaDoc(brokerURI));
80     }
81
82
83     /**
84      * Creates a broker from a URI configuration
85      * @param brokerURI the URI scheme to configure the broker
86      * @param startBroker whether or not the broker should have its {@link BrokerService#start()} method called after construction
87      * @throws Exception
88      */

89     public static BrokerService createBroker(String JavaDoc brokerURI, boolean startBroker) throws Exception JavaDoc {
90         return createBroker(new URI JavaDoc(brokerURI), startBroker);
91     }
92
93
94 }
95
Popular Tags