001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.util;
018
019import java.io.BufferedInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.Properties;
023import java.util.concurrent.ConcurrentHashMap;
024import java.util.concurrent.ConcurrentMap;
025
026/**
027 *
028 */
029public class FactoryFinder {
030
031    /**
032     * The strategy that the FactoryFinder uses to find load and instantiate Objects
033     * can be changed out by calling the
034     * {@link org.apache.activemq.util.FactoryFinder#setObjectFactory(org.apache.activemq.util.FactoryFinder.ObjectFactory)}
035     * method with a custom implementation of ObjectFactory.
036     *
037     * The default ObjectFactory is typically changed out when running in a specialized container
038     * environment where service discovery needs to be done via the container system.  For example,
039     * in an OSGi scenario.
040     */
041    public interface ObjectFactory {
042        /**
043         * @param path the full service path
044         * @return
045         */
046        public Object create(String path) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException;
047
048    }
049
050    /**
051     * The default implementation of Object factory which works well in standalone applications.
052     */
053    protected static class StandaloneObjectFactory implements ObjectFactory {
054        final ConcurrentMap<String, Class> classMap = new ConcurrentHashMap<String, Class>();
055
056        @Override
057        public Object create(final String path) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
058            Class clazz = classMap.get(path);
059            if (clazz == null) {
060                clazz = loadClass(loadProperties(path));
061                classMap.put(path, clazz);
062            }
063            return clazz.newInstance();
064        }
065
066        static public Class loadClass(Properties properties) throws ClassNotFoundException, IOException {
067
068            String className = properties.getProperty("class");
069            if (className == null) {
070                throw new IOException("Expected property is missing: class");
071            }
072            Class clazz = null;
073            ClassLoader loader = Thread.currentThread().getContextClassLoader();
074            if (loader != null) {
075                try {
076                    clazz = loader.loadClass(className);
077                } catch (ClassNotFoundException e) {
078                    // ignore
079                }
080            }
081            if (clazz == null) {
082                clazz = FactoryFinder.class.getClassLoader().loadClass(className);
083            }
084
085            return clazz;
086        }
087
088        static public Properties loadProperties(String uri) throws IOException {
089            // lets try the thread context class loader first
090            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
091            if (classLoader == null) {
092                classLoader = StandaloneObjectFactory.class.getClassLoader();
093            }
094            InputStream in = classLoader.getResourceAsStream(uri);
095            if (in == null) {
096                in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
097                if (in == null) {
098                    throw new IOException("Could not find factory class for resource: " + uri);
099                }
100            }
101
102            // lets load the file
103            BufferedInputStream reader = null;
104            try {
105                reader = new BufferedInputStream(in);
106                Properties properties = new Properties();
107                properties.load(reader);
108                return properties;
109            } finally {
110                try {
111                    reader.close();
112                } catch (Exception e) {
113                }
114            }
115        }
116    }
117
118    // ================================================================
119    // Class methods and properties
120    // ================================================================
121    private static ObjectFactory objectFactory = new StandaloneObjectFactory();
122
123    public static ObjectFactory getObjectFactory() {
124        return objectFactory;
125    }
126
127    public static void setObjectFactory(ObjectFactory objectFactory) {
128        FactoryFinder.objectFactory = objectFactory;
129    }
130
131    // ================================================================
132    // Instance methods and properties
133    // ================================================================
134    private final String path;
135
136    public FactoryFinder(String path) {
137        this.path = path;
138    }
139
140    /**
141     * Creates a new instance of the given key
142     *
143     * @param key is the key to add to the path to find a text file containing
144     *                the factory name
145     * @return a newly created instance
146     */
147    public Object newInstance(String key) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException {
148        return objectFactory.create(path+key);
149    }
150
151
152}