001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.util;
018:
019: import java.io.BufferedInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Properties;
023: import java.util.concurrent.ConcurrentHashMap;
024:
025: public class FactoryFinder {
026: private final String path;
027: private final ConcurrentHashMap classMap = new ConcurrentHashMap();
028:
029: public FactoryFinder(String path) {
030: this .path = path;
031: }
032:
033: /**
034: * Creates a new instance of the given key
035: *
036: * @param key is the key to add to the path to find a text file
037: * containing the factory name
038: * @return a newly created instance
039: */
040: public Object newInstance(String key)
041: throws IllegalAccessException, InstantiationException,
042: IOException, ClassNotFoundException {
043: return newInstance(key, null);
044: }
045:
046: public Object newInstance(String key, String propertyPrefix)
047: throws IllegalAccessException, InstantiationException,
048: IOException, ClassNotFoundException {
049: if (propertyPrefix == null)
050: propertyPrefix = "";
051:
052: Class clazz = (Class) classMap.get(propertyPrefix + key);
053: if (clazz == null) {
054: clazz = newInstance(doFindFactoryProperies(key),
055: propertyPrefix);
056: classMap.put(propertyPrefix + key, clazz);
057: }
058: return clazz.newInstance();
059: }
060:
061: private Class newInstance(Properties properties,
062: String propertyPrefix) throws ClassNotFoundException,
063: IOException {
064:
065: String className = properties.getProperty(propertyPrefix
066: + "class");
067: if (className == null) {
068: throw new IOException("Expected property is missing: "
069: + propertyPrefix + "class");
070: }
071: Class clazz;
072: try {
073: clazz = Thread.currentThread().getContextClassLoader()
074: .loadClass(className);
075: } catch (ClassNotFoundException e) {
076: clazz = FactoryFinder.class.getClassLoader().loadClass(
077: className);
078: }
079:
080: return clazz;
081: }
082:
083: private Properties doFindFactoryProperies(String key)
084: throws IOException {
085: String uri = path + key;
086:
087: // lets try the thread context class loader first
088: InputStream in = Thread.currentThread().getContextClassLoader()
089: .getResourceAsStream(uri);
090: if (in == null) {
091: in = FactoryFinder.class.getClassLoader()
092: .getResourceAsStream(uri);
093: if (in == null) {
094: throw new IOException(
095: "Could not find factory class for resource: "
096: + uri);
097: }
098: }
099:
100: // lets load the file
101: BufferedInputStream reader = null;
102: try {
103: reader = new BufferedInputStream(in);
104: Properties properties = new Properties();
105: properties.load(reader);
106: return properties;
107: } finally {
108: try {
109: reader.close();
110: } catch (Exception e) {
111: }
112: }
113: }
114: }
|