[jboss-cvs] JBossAS SVN: r99943 - projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jan 26 10:26:29 EST 2010


Author: thomas.diesler at jboss.com
Date: 2010-01-26 10:26:29 -0500 (Tue, 26 Jan 2010)
New Revision: 99943

Modified:
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java
Log:
Clarify ServiceLoader

Modified: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java	2010-01-26 15:25:35 UTC (rev 99942)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java	2010-01-26 15:26:29 UTC (rev 99943)
@@ -41,49 +41,55 @@
 {
    // Provide logging
    static final Logger log = LoggerFactory.getLogger(ServiceLoader.class);
-   
+
    /**
     * Loads a list of service implementations defined in META-INF/services/${serviceClass}
+    * 
+    * @param serviceClass The interface that is implemented by all loaded services
+    * @return The list of available service or an empty list
     */
    @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);
-      
+         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)
+            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());
+                     if (serviceClass.isAssignableFrom(implClass))
+                        services.add(implClass.newInstance());
+                     else
+                        log.warn("Not assignable: " + implClassName);
                   }
                   catch (Exception ex)
                   {
-                     log.debug ("Cannot load service: " + implClassName);
+                     log.debug("Cannot load service: " + implClassName);
                   }
                }
-               
+
                implClassName = br.readLine();
             }
             br.close();
@@ -93,16 +99,22 @@
             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}
+    * 
+    * @param serviceClass The interface that is implemented by all loaded services
+    * @return The first available service or null
     */
    public static <T> T loadService(Class<T> serviceClass)
    {
       List<T> services = loadServices(serviceClass);
-      return services.get(0); 
+      if (services.isEmpty())
+         return null;
+
+      return services.get(0);
    }
 }




More information about the jboss-cvs-commits mailing list