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     */
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    }