001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.io.BufferedReader;
021    import java.io.InputStream;
022    import java.io.InputStreamReader;
023    
024    import java.lang.reflect.Constructor;
025    
026    import java.net.URL;
027    
028    import java.util.ArrayList;
029    import java.util.Enumeration;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Miguel Pastor
035     * @author Raymond Augé
036     */
037    public class ServiceLoader {
038    
039            public static <S> List<S> load(Class<S> clazz) throws Exception {
040                    return load(clazz, _serviceLoaderCondition);
041            }
042    
043            public static <S> List<S> load(
044                            Class<S> clazz, ServiceLoaderCondition serviceLoaderCondition)
045                    throws Exception {
046    
047                    Thread currentThread = Thread.currentThread();
048    
049                    ClassLoader classLoader = currentThread.getContextClassLoader();
050    
051                    return load(classLoader, clazz, serviceLoaderCondition);
052            }
053    
054            public static <S> List<S> load(ClassLoader classLoader, Class<S> clazz)
055                    throws Exception {
056    
057                    return load(classLoader, clazz, _serviceLoaderCondition);
058            }
059    
060            public static <S> List<S> load(
061                            ClassLoader classLoader, Class<S> clazz,
062                            ServiceLoaderCondition serviceLoaderCondition)
063                    throws Exception {
064    
065                    Enumeration<URL> enu = classLoader.getResources(
066                            "META-INF/services/" + clazz.getName());
067    
068                    List<S> services = new ArrayList<S>();
069    
070                    while (enu.hasMoreElements()) {
071                            URL url = enu.nextElement();
072    
073                            if (!serviceLoaderCondition.isLoad(url)) {
074                                    continue;
075                            }
076    
077                            try {
078                                    _load(services, classLoader, clazz, url);
079                            }
080                            catch (Exception e) {
081                                    _log.error("Unable to load " + clazz + "with " + classLoader);
082                            }
083                    }
084    
085                    return services;
086            }
087    
088            private static <S> void _load(
089                            List<S> services, ClassLoader classLoader, Class<S> clazz, URL url)
090                    throws Exception {
091    
092                    InputStream inputStream = url.openStream();
093    
094                    try {
095                            BufferedReader bufferedReader = new BufferedReader(
096                                    new InputStreamReader(inputStream, StringPool.UTF8));
097    
098                            while (true) {
099                                    String line = bufferedReader.readLine();
100    
101                                    if (line == null) {
102                                            break;
103                                    }
104    
105                                    int comment = line.indexOf(CharPool.POUND);
106    
107                                    if (comment >= 0) {
108                                            line = line.substring(0, comment);
109                                    }
110    
111                                    String name = line.trim();
112    
113                                    if (name.length() == 0) {
114                                            continue;
115                                    }
116    
117                                    Class<?> serviceClass = Class.forName(name, true, classLoader);
118    
119                                    Class<? extends S> serviceImplClass = serviceClass.asSubclass(
120                                            clazz);
121    
122                                    Constructor<? extends S> constructor =
123                                            serviceImplClass.getConstructor();
124    
125                                    S service = constructor.newInstance();
126    
127                                    services.add(service);
128                            }
129                    }
130                    finally {
131                            inputStream.close();
132                    }
133            }
134    
135            private static Log _log = LogFactoryUtil.getLog(ServiceLoader.class);
136    
137            private static ServiceLoaderCondition _serviceLoaderCondition =
138                    new DefaultServiceLoaderCondition();
139    
140    }