KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.activemq.util.IntrospectionSupport;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 /**
33  * A {@link BrokerFactoryHandler} which uses a properties file to
34  * configure the broker's various policies.
35  *
36  * @version $Revision: 518590 $
37  */

38 public class PropertiesBrokerFactory implements BrokerFactoryHandler {
39
40     public BrokerService createBroker(URI JavaDoc brokerURI) throws Exception JavaDoc {
41
42         Map JavaDoc properties = loadProperties(brokerURI);
43         BrokerService brokerService = createBrokerService(brokerURI, properties);
44
45         IntrospectionSupport.setProperties(brokerService, properties);
46         return brokerService;
47     }
48
49     /**
50      * Lets load the properties from some external URL or a relative file
51      */

52     protected Map JavaDoc loadProperties(URI JavaDoc brokerURI) throws IOException JavaDoc {
53         // lets load a URI
54
String JavaDoc remaining = brokerURI.getSchemeSpecificPart();
55         Properties JavaDoc properties = new Properties JavaDoc();
56         File JavaDoc file = new File JavaDoc(remaining);
57
58         InputStream JavaDoc inputStream = null;
59         if (file.exists()) {
60             inputStream = new FileInputStream JavaDoc(file);
61         }
62         else {
63             URL JavaDoc url = null;
64             try {
65                 url = new URL JavaDoc(remaining);
66             }
67             catch (MalformedURLException JavaDoc e) {
68                 // lets now see if we can find the name on the classpath
69
inputStream = findResourceOnClassPath(remaining);
70                 if (inputStream == null) {
71                     throw new IOException JavaDoc("File does not exist: " + remaining + ", could not be found on the classpath and is not a valid URL: " + e);
72                 }
73             }
74             if (inputStream == null) {
75                 inputStream = url.openStream();
76             }
77         }
78         properties.load(inputStream);
79
80         // should we append any system properties?
81
try {
82             Properties JavaDoc systemProperties = System.getProperties();
83             properties.putAll(systemProperties);
84         }
85         catch (Exception JavaDoc e) {
86             // ignore security exception
87
}
88         return properties;
89     }
90
91     protected InputStream JavaDoc findResourceOnClassPath(String JavaDoc remaining) {
92         InputStream JavaDoc answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(remaining);
93         if (answer == null) {
94             answer = getClass().getClassLoader().getResourceAsStream(remaining);
95         }
96         return answer;
97     }
98
99     protected BrokerService createBrokerService(URI JavaDoc brokerURI, Map JavaDoc properties) {
100         return new BrokerService();
101     }
102 }
103
Popular Tags