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    import com.liferay.portal.util.URLUtil;
020    
021    import java.io.BufferedReader;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    
025    import java.lang.reflect.Constructor;
026    
027    import java.net.URL;
028    
029    import java.util.ArrayList;
030    import java.util.Enumeration;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Miguel Pastor
036     * @author Raymond Aug??
037     */
038    public class ServiceLoader {
039    
040            public static <S> List<S> load(Class<S> clazz) throws Exception {
041                    return load(clazz, _serviceLoaderCondition);
042            }
043    
044            public static <S> List<S> load(
045                            Class<S> clazz, ServiceLoaderCondition serviceLoaderCondition)
046                    throws Exception {
047    
048                    Thread currentThread = Thread.currentThread();
049    
050                    ClassLoader classLoader = currentThread.getContextClassLoader();
051    
052                    return load(classLoader, clazz, serviceLoaderCondition);
053            }
054    
055            public static <S> List<S> load(ClassLoader classLoader, Class<S> clazz)
056                    throws Exception {
057    
058                    return load(classLoader, clazz, _serviceLoaderCondition);
059            }
060    
061            public static <S> List<S> load(
062                            ClassLoader classLoader, Class<S> clazz,
063                            ServiceLoaderCondition serviceLoaderCondition)
064                    throws Exception {
065    
066                    Enumeration<URL> enu = classLoader.getResources(
067                            "META-INF/services/" + clazz.getName());
068    
069                    List<S> services = new ArrayList<S>();
070    
071                    while (enu.hasMoreElements()) {
072                            URL url = enu.nextElement();
073    
074                            if (!serviceLoaderCondition.isLoad(url)) {
075                                    continue;
076                            }
077    
078                            try {
079                                    _load(services, classLoader, clazz, url);
080                            }
081                            catch (Exception e) {
082                                    _log.error(
083                                            "Unable to load " + clazz + " with " + classLoader, e);
084                            }
085                    }
086    
087                    return services;
088            }
089    
090            private static <S> void _load(
091                            List<S> services, ClassLoader classLoader, Class<S> clazz, URL url)
092                    throws Exception {
093    
094                    if (ServerDetector.isJBoss5()) {
095                            url = URLUtil.normalizeURL(url);
096                    }
097    
098                    InputStream inputStream = url.openStream();
099    
100                    try {
101                            BufferedReader bufferedReader = new BufferedReader(
102                                    new InputStreamReader(inputStream, StringPool.UTF8));
103    
104                            while (true) {
105                                    String line = bufferedReader.readLine();
106    
107                                    if (line == null) {
108                                            break;
109                                    }
110    
111                                    int comment = line.indexOf(CharPool.POUND);
112    
113                                    if (comment >= 0) {
114                                            line = line.substring(0, comment);
115                                    }
116    
117                                    String name = line.trim();
118    
119                                    if (name.length() == 0) {
120                                            continue;
121                                    }
122    
123                                    Class<?> serviceClass = Class.forName(name, true, classLoader);
124    
125                                    Class<? extends S> serviceImplClass = serviceClass.asSubclass(
126                                            clazz);
127    
128                                    Constructor<? extends S> constructor =
129                                            serviceImplClass.getConstructor();
130    
131                                    S service = constructor.newInstance();
132    
133                                    services.add(service);
134                            }
135                    }
136                    finally {
137                            inputStream.close();
138                    }
139            }
140    
141            private static Log _log = LogFactoryUtil.getLog(ServiceLoader.class);
142    
143            private static ServiceLoaderCondition _serviceLoaderCondition =
144                    new DefaultServiceLoaderCondition();
145    
146    }