/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.osgi.spi.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Loads service implementations from the requesters classpath.
 * 
 * @author Thomas.Diesler@jboss.com
 * @since 14-Dec-2006
 */
public abstract class ServiceLoader
{
   // Provide logging
   static final Logger log = LoggerFactory.getLogger(ServiceLoader.class);
   
   /**
    * Loads a list of service implementations defined in META-INF/services/${serviceClass}
    */
   @SuppressWarnings("unchecked")
   public static <T> List<T> loadServices(Class<T> serviceClass)
   {
      List<T> services = new ArrayList<T>();
      ClassLoader loader = serviceClass.getClassLoader();
      
      // Use the Services API (as detailed in the JAR specification), if available, to determine the classname.
      String filename = "META-INF/services/" + serviceClass.getName();
      InputStream inStream = loader.getResourceAsStream(filename);
      if (inStream == null)
         log.debug ("Cannot find resource: " + filename);
      
      if (inStream != null)
      {
         try
         {
            BufferedReader br = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
            String implClassName = br.readLine();
            while(implClassName != null)
            {
               int hashIndex = implClassName.indexOf("#");
               if (hashIndex > 0)
                  implClassName = implClassName.substring(0, hashIndex);
               
               implClassName = implClassName.trim();
               
               if (implClassName.length() > 0)
               {
                  try
                  {
                     Class<T> implClass = (Class<T>)loader.loadClass(implClassName);
                     services.add(implClass.newInstance());
                  }
                  catch (Exception ex)
                  {
                     log.debug ("Cannot load service: " + implClassName);
                  }
               }
               
               implClassName = br.readLine();
            }
            br.close();
         }
         catch (IOException ex)
         {
            throw new IllegalStateException("Failed to load services for: " + serviceClass.getName());
         }
      }
      
      return services;
   }

   /**
    * Loads the first of a list of service implementations defined in META-INF/services/${serviceClass}
    */
   public static <T> T loadService(Class<T> serviceClass)
   {
      List<T> services = loadServices(serviceClass);
      return services.get(0); 
   }
}

@@ -1559,20 +1559,17 @@
 class);%0A
-   
 %0A
+
    /**%0A 
@@ -1650,32 +1650,183 @@
 $%7BserviceClass%7D%0A
+    * %0A    * @param serviceClass The interface that is implemented by all loaded services%0A    * @return The list of available service or an empty list%0A
     */%0A   @Suppr
@@ -2023,23 +2023,17 @@
 ader();%0A
-      
 %0A
+
       //
@@ -2312,33 +2312,32 @@
        log.debug
- 
 (%22Cannot find re
@@ -2359,22 +2359,16 @@
 ename);%0A
-      
 %0A      i
@@ -2581,16 +2581,17 @@
    while
+ 
 (implCla
@@ -2786,32 +2786,17 @@
 Index);%0A
-               
 %0A
+
         
@@ -2840,32 +2840,17 @@
 trim();%0A
-               
 %0A
+
         
@@ -3057,44 +3057,210 @@
     
-services.add(implClass.newInstance()
+if (serviceClass.isAssignableFrom(implClass))%0A                        services.add(implClass.newInstance());%0A                     else%0A                        log.warn(%22Not assignable: %22 + implClassName
 );%0A 
@@ -3367,17 +3367,16 @@
 og.debug
- 
 (%22Cannot
@@ -3451,24 +3451,9 @@
   %7D%0A
-               
 %0A
+
     
@@ -3712,15 +3712,9 @@
   %7D%0A
-      
 %0A
+
     
@@ -3845,24 +3845,164 @@
 rviceClass%7D%0A
+    * %0A    * @param serviceClass The interface that is implemented by all loaded services%0A    * @return The first available service or null%0A
     */%0A   pu
@@ -4112,16 +4112,69 @@
 Class);%0A
+      if (services.isEmpty())%0A         return null;%0A%0A
       re
@@ -4194,17 +4194,16 @@
 .get(0);
- 
 %0A   %7D%0A%7D%0A

Clarify ServiceLoader