001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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     */
035    public class ServiceLoader {
036    
037            public static <S> List<S> load(Class<S> clazz) throws Exception {
038                    Thread currentThread = Thread.currentThread();
039    
040                    ClassLoader classLoader = currentThread.getContextClassLoader();
041    
042                    Enumeration<URL> enu = classLoader.getResources(
043                            "META-INF/services/" + clazz.getName());
044    
045                    List<S> services = new ArrayList<S>();
046    
047                    while (enu.hasMoreElements()) {
048                            URL url = enu.nextElement();
049    
050                            try {
051                                    _load(services, classLoader, clazz, url);
052                            }
053                            catch (Exception e) {
054                                    _log.error("Unable to load " + clazz + "with " + classLoader);
055                            }
056                    }
057    
058                    return services;
059            }
060    
061            private static <S> void _load(
062                            List<S> services, ClassLoader classLoader, Class<S> clazz, URL url)
063                    throws Exception {
064    
065                    InputStream inputStream = url.openStream();
066    
067                    try {
068                            BufferedReader bufferedReader = new BufferedReader(
069                                    new InputStreamReader(inputStream, StringPool.UTF8));
070    
071                            while (true) {
072                                    String line = bufferedReader.readLine();
073    
074                                    if (line == null) {
075                                            break;
076                                    }
077    
078                                    int comment = line.indexOf(CharPool.POUND);
079    
080                                    if (comment >= 0) {
081                                            line = line.substring(0, comment);
082                                    }
083    
084                                    String name = line.trim();
085    
086                                    if (name.length() == 0) {
087                                            continue;
088                                    }
089    
090                                    Class<?> serviceClass = Class.forName(name, true, classLoader);
091    
092                                    Class<? extends S> serviceImplClass = serviceClass.asSubclass(
093                                            clazz);
094    
095                                    Constructor<? extends S> constructor =
096                                            serviceImplClass.getConstructor();
097    
098                                    S service = constructor.newInstance();
099    
100                                    services.add(service);
101                            }
102                    }
103                    finally {
104                            inputStream.close();
105                    }
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(ServiceLoader.class);
109    
110    }