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.camel.impl;
018
019import java.io.BufferedInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025import java.util.Properties;
026import java.util.concurrent.ConcurrentHashMap;
027import java.util.concurrent.ConcurrentMap;
028
029import org.apache.camel.NoFactoryAvailableException;
030import org.apache.camel.spi.ClassResolver;
031import org.apache.camel.spi.FactoryFinder;
032import org.apache.camel.spi.Injector;
033import org.apache.camel.util.CastUtils;
034import org.apache.camel.util.IOHelper;
035
036/**
037 * Default factory finder.
038 */
039public class DefaultFactoryFinder implements FactoryFinder {
040
041    protected final ConcurrentMap<String, Class<?>> classMap = new ConcurrentHashMap<String, Class<?>>();
042    private final ClassResolver classResolver;
043    private final String path;
044
045    public DefaultFactoryFinder(ClassResolver classResolver, String resourcePath) {
046        this.classResolver = classResolver;
047        this.path = resourcePath;
048    }
049
050    public String getResourcePath() {
051        return path;
052    }
053
054    public Object newInstance(String key) throws NoFactoryAvailableException {
055        try {
056            return newInstance(key, null);
057        } catch (Exception e) {
058            throw new NoFactoryAvailableException(key, e);
059        }
060    }
061
062    public <T> List<T> newInstances(String key, Injector injector, Class<T> type) throws ClassNotFoundException, IOException {
063        List<Class<T>> list = CastUtils.cast(findClasses(key));
064        List<T> answer = new ArrayList<T>(list.size());
065        answer.add(newInstance(key, injector, type));
066        return answer;
067    }
068
069    public Class<?> findClass(String key) throws ClassNotFoundException, IOException {
070        return findClass(key, null);
071    }
072
073    public Class<?> findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException {
074        String prefix = propertyPrefix != null ? propertyPrefix : "";
075
076        Class<?> clazz = classMap.get(prefix + key);
077        if (clazz == null) {
078            clazz = newInstance(doFindFactoryProperties(key), prefix);
079            if (clazz != null) {
080                classMap.put(prefix + key, clazz);
081            }
082        }
083        return clazz;
084    }
085
086    public Class<?> findClass(String key, String propertyPrefix, Class<?> clazz) throws ClassNotFoundException, IOException {
087        // Just ignore clazz which is only useful for OSGiFactoryFinder
088        return findClass(key, propertyPrefix);
089    }
090
091    private Object newInstance(String key, String propertyPrefix) throws IllegalAccessException,
092        InstantiationException, IOException, ClassNotFoundException {
093        Class<?> clazz = findClass(key, propertyPrefix);
094        return clazz.newInstance();
095    }
096
097    private <T> T newInstance(String key, Injector injector, Class<T> expectedType) throws IOException,
098        ClassNotFoundException {
099        return newInstance(key, injector, null, expectedType);
100    }
101
102    private <T> T newInstance(String key, Injector injector, String propertyPrefix, Class<T> expectedType)
103        throws IOException, ClassNotFoundException {
104        Class<?> type = findClass(key, propertyPrefix);
105        Object value = injector.newInstance(type);
106        if (expectedType.isInstance(value)) {
107            return expectedType.cast(value);
108        } else {
109            throw new ClassCastException("Not instanceof " + expectedType.getName() + " value: " + value);
110        }
111    }
112
113    private List<Class<?>> findClasses(String key) throws ClassNotFoundException, IOException {
114        return findClasses(key, null);
115    }
116
117    private List<Class<?>> findClasses(String key, String propertyPrefix) throws ClassNotFoundException, IOException {
118        Class<?> type = findClass(key, propertyPrefix);
119        return Collections.<Class<?>>singletonList(type);
120    }
121
122    private Class<?> newInstance(Properties properties, String propertyPrefix) throws ClassNotFoundException, IOException {
123        String className = properties.getProperty(propertyPrefix + "class");
124        if (className == null) {
125            throw new IOException("Expected property is missing: " + propertyPrefix + "class");
126        }
127
128        Class<?> clazz = classResolver.resolveClass(className);
129        if (clazz == null) {
130            throw new ClassNotFoundException(className);
131        }
132        return clazz;
133    }
134
135    private Properties doFindFactoryProperties(String key) throws IOException {
136        String uri = path + key;
137
138        InputStream in = classResolver.loadResourceAsStream(uri);
139        if (in == null) {
140            throw new NoFactoryAvailableException(uri);
141        }
142
143        // lets load the file
144        BufferedInputStream reader = null;
145        try {
146            reader = IOHelper.buffered(in);
147            Properties properties = new Properties();
148            properties.load(reader);
149            return properties;
150        } finally {
151            IOHelper.close(reader, key, null);
152            IOHelper.close(in, key, null);
153        }
154    }
155}