KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > FactoryFinder


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.util;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import java.util.concurrent.ConcurrentHashMap JavaDoc;
26
27
28 public class FactoryFinder {
29
30     private final String JavaDoc path;
31     private final ConcurrentHashMap JavaDoc classMap = new ConcurrentHashMap JavaDoc();
32
33     public FactoryFinder(String JavaDoc path) {
34         this.path = path;
35     }
36
37     /**
38      * Creates a new instance of the given key
39      *
40      * @param key is the key to add to the path to find a text file
41      * containing the factory name
42      * @return a newly created instance
43      */

44     public Object JavaDoc newInstance(String JavaDoc key)
45             throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc
46     {
47         return newInstance(key, null);
48     }
49
50     public Object JavaDoc newInstance(String JavaDoc key, String JavaDoc propertyPrefix)
51             throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc
52     {
53         if (propertyPrefix == null)
54             propertyPrefix = "";
55
56         Class JavaDoc clazz = (Class JavaDoc) classMap.get(propertyPrefix + key);
57         if (clazz == null) {
58             clazz = newInstance(doFindFactoryProperies(key), propertyPrefix);
59             classMap.put(propertyPrefix + key, clazz);
60         }
61         return clazz.newInstance();
62     }
63
64     private Class JavaDoc newInstance(Properties JavaDoc properties, String JavaDoc propertyPrefix) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
65
66         String JavaDoc className = properties.getProperty(propertyPrefix + "class");
67         if (className == null) {
68             throw new IOException JavaDoc("Expected property is missing: " + propertyPrefix + "class");
69         }
70         Class JavaDoc clazz;
71         try {
72             clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
73         } catch (ClassNotFoundException JavaDoc e) {
74             clazz = FactoryFinder.class.getClassLoader().loadClass(className);
75         }
76
77         return clazz;
78     }
79
80     private Properties JavaDoc doFindFactoryProperies(String JavaDoc key) throws IOException JavaDoc {
81         String JavaDoc uri = path + key;
82
83         // lets try the thread context class loader first
84
ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
85         if (classLoader == null) classLoader = getClass().getClassLoader();
86         InputStream JavaDoc in = classLoader.getResourceAsStream(uri);
87         if (in == null) {
88             in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
89             if (in == null) {
90                 throw new IOException JavaDoc("Could not find factory class for resource: " + uri);
91             }
92         }
93
94         // lets load the file
95
BufferedInputStream JavaDoc reader = null;
96         try {
97             reader = new BufferedInputStream JavaDoc(in);
98             Properties JavaDoc properties = new Properties JavaDoc();
99             properties.load(reader);
100             return properties;
101         } finally {
102             try {
103                 reader.close();
104             } catch (Exception JavaDoc e) {
105             }
106         }
107     }
108 }
109
Popular Tags