001    /**
002     * Copyright (c) 2000-2012 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 java.io.BufferedReader;
018    import java.io.InputStream;
019    import java.io.InputStreamReader;
020    
021    import java.lang.reflect.Constructor;
022    
023    import java.net.URL;
024    
025    import java.util.ArrayList;
026    import java.util.Enumeration;
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class ServiceLoader {
033    
034            public static <S> List<S> load(Class<S> clazz) throws Exception {
035                    Thread currentThread = Thread.currentThread();
036    
037                    ClassLoader classLoader = currentThread.getContextClassLoader();
038    
039                    Enumeration<URL> enu = classLoader.getResources(
040                            "META-INF/services/" + clazz.getName());
041    
042                    List<S> services = new ArrayList<S>();
043    
044                    while (enu.hasMoreElements()) {
045                            URL url = enu.nextElement();
046    
047                            _load(services, classLoader, clazz, url);
048                    }
049    
050                    return services;
051            }
052    
053            private static <S> void _load(
054                            List<S> services, ClassLoader classLoader, Class<S> clazz, URL url)
055                    throws Exception {
056    
057                    InputStream inputStream = url.openStream();
058    
059                    try {
060                            BufferedReader bufferedReader = new BufferedReader(
061                                    new InputStreamReader(inputStream, StringPool.UTF8));
062    
063                            while (true) {
064                                    String line = bufferedReader.readLine();
065    
066                                    if (line == null) {
067                                            break;
068                                    }
069    
070                                    int comment = line.indexOf(CharPool.POUND);
071    
072                                    if (comment >= 0) {
073                                            line = line.substring(0, comment);
074                                    }
075    
076                                    String name = line.trim();
077    
078                                    if (name.length() == 0) {
079                                            continue;
080                                    }
081    
082                                    Class<?> serviceClass = Class.forName(name, true, classLoader);
083    
084                                    Class<? extends S> serviceImplClass = serviceClass.asSubclass(
085                                            clazz);
086    
087                                    Constructor<? extends S> constructor =
088                                            serviceImplClass.getConstructor();
089    
090                                    S service = constructor.newInstance();
091    
092                                    services.add(service);
093                            }
094                    }
095                    finally {
096                            inputStream.close();
097                    }
098            }
099    
100    }