[weld-commits] Weld SVN: r6047 - in core/trunk/impl/src/main: java/org/jboss/weld/util/serviceProvider and 1 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Mon Mar 15 08:40:11 EDT 2010


Author: nickarls
Date: 2010-03-15 08:40:10 -0400 (Mon, 15 Mar 2010)
New Revision: 6047

Modified:
   core/trunk/impl/src/main/java/org/jboss/weld/logging/messages/UtilMessage.java
   core/trunk/impl/src/main/java/org/jboss/weld/util/serviceProvider/DefaultServiceLoader.java
   core/trunk/impl/src/main/resources/org/jboss/weld/messages/util_en.properties
Log:
WELD-331

Modified: core/trunk/impl/src/main/java/org/jboss/weld/logging/messages/UtilMessage.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/logging/messages/UtilMessage.java	2010-03-14 23:05:02 UTC (rev 6046)
+++ core/trunk/impl/src/main/java/org/jboss/weld/logging/messages/UtilMessage.java	2010-03-15 12:40:10 UTC (rev 6047)
@@ -63,6 +63,9 @@
    @MessageId("000824") ACCESS_ERROR_ON_FIELD,
    @MessageId("000825") NO_SUCH_METHOD,
    @MessageId("000826") ANNOTATION_VALUES_INACCESSIBLE,
-   @MessageId("000827") INITIALIZER_METHOD_IS_GENERIC;
+   @MessageId("000827") INITIALIZER_METHOD_IS_GENERIC,
+   @MessageId("000827") COULD_NOT_READ_SERVICES_LIST,
+   @MessageId("000828") COULD_NOT_READ_SERVICES_FILE,
+   @MessageId("000829") EXTENSION_CLASS_NOT_FOUND;
    
 }

Modified: core/trunk/impl/src/main/java/org/jboss/weld/util/serviceProvider/DefaultServiceLoader.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/util/serviceProvider/DefaultServiceLoader.java	2010-03-14 23:05:02 UTC (rev 6046)
+++ core/trunk/impl/src/main/java/org/jboss/weld/util/serviceProvider/DefaultServiceLoader.java	2010-03-15 12:40:10 UTC (rev 6047)
@@ -16,26 +16,30 @@
  */
 package org.jboss.weld.util.serviceProvider;
 
+import static org.jboss.weld.logging.Category.UTIL;
+import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
+import static org.jboss.weld.logging.messages.UtilMessage.COULD_NOT_READ_SERVICES_FILE;
+import static org.jboss.weld.logging.messages.UtilMessage.COULD_NOT_READ_SERVICES_LIST;
 import static org.jboss.weld.logging.messages.UtilMessage.DECLARED_EXTENSION_DOES_NOT_IMPLEMENT_EXTENSION;
+import static org.jboss.weld.logging.messages.UtilMessage.EXTENSION_CLASS_NOT_FOUND;
+import static org.jboss.weld.logging.messages.UtilMessage.SECURITY_EXCEPTION_SCANNING;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.Enumeration;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 import org.jboss.weld.exceptions.ForbiddenStateException;
 import org.jboss.weld.exceptions.InvalidOperationException;
+import org.jboss.weld.util.collections.EnumerationList;
 import org.jboss.weld.util.reflection.SecureReflections;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.slf4j.ext.XLogger;
-import org.slf4j.ext.XLoggerFactory;
-import org.slf4j.ext.XLogger.Level;
+import org.slf4j.cal10n.LocLogger;
 
 /**
  * This class handles looking up service providers on the class path. It
@@ -51,12 +55,11 @@
  * 
  * @author Pete Muir
  * @author <a href="mailto:dev at avalon.apache.org">Avalon Development Team</a>
+ * @author Nicklas Karlsson
  */
 public class DefaultServiceLoader<S> implements Iterable<S>
 {
-   
-   private static Logger log = LoggerFactory.getLogger(DefaultServiceLoader.class);
-   private static XLogger xLog = XLoggerFactory.getXLogger(DefaultServiceLoader.class);
+   private static LocLogger log = loggerFactory().getLogger(UTIL);
 
    private static final String SERVICES = "META-INF/services";
 
@@ -80,12 +83,12 @@
    {
       return load(SERVICES, service, Thread.currentThread().getContextClassLoader());
    }
-   
+
    public static <S> DefaultServiceLoader<S> load(String directoryName, Class<S> service)
    {
       return load(directoryName, service, Thread.currentThread().getContextClassLoader());
    }
-   
+
    public static <S> DefaultServiceLoader<S> load(String directoryName, Class<S> service, ClassLoader loader)
    {
       if (loader == null)
@@ -134,11 +137,11 @@
    {
       throw new InvalidOperationException();
    }
-   
+
    private final String serviceFile;
    private Class<S> expectedType;
    private final ClassLoader loader;
-   
+
    private Set<S> providers;
 
    private DefaultServiceLoader(String prefix, Class<S> service, ClassLoader loader)
@@ -147,7 +150,7 @@
       this.serviceFile = prefix + "/" + service.getName();
       this.expectedType = service;
    }
-   
+
    /**
     * Clear this loader's provider cache so that all providers will be reloaded.
     * 
@@ -161,106 +164,114 @@
    public void reload()
    {
       providers = new HashSet<S>();
-      Enumeration<URL> enumeration = null;
-      boolean errorOccurred = false;
 
+      for (URL serviceFile : loadServiceFiles())
+      {
+         loadServiceFile(serviceFile);
+      }
+   }
+
+   @SuppressWarnings("unchecked")
+   private List<URL> loadServiceFiles()
+   {
       try
       {
-         enumeration = loader.getResources(serviceFile);
+         return new EnumerationList(loader.getResources(serviceFile));
       }
-      catch (IOException ioe)
+      catch (IOException e)
       {
-         errorOccurred = true;
+         log.warn(COULD_NOT_READ_SERVICES_LIST, serviceFile, e);
+         return Collections.emptyList();
       }
+   }
 
-      if (!errorOccurred)
+   private void loadServiceFile(URL serviceFile)
+   {
+      InputStream is = null;
+      try
       {
-         while (enumeration.hasMoreElements())
+         is = serviceFile.openStream();
+         BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+         String serviceClassName = null;
+         while ((serviceClassName = reader.readLine()) != null)
          {
+            serviceClassName = trim(serviceClassName);
+            if (serviceClassName.length() > 0)
+            {
+               loadService(serviceClassName);
+            }
+         }
+      }
+      catch (IOException e)
+      {
+         throw new ForbiddenStateException(COULD_NOT_READ_SERVICES_FILE, serviceFile, e);
+      }
+      finally
+      {
+         if (is != null)
+         {
             try
             {
-               final URL url = enumeration.nextElement();
-               final InputStream is = url.openStream();
-               final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+               is.close();
+            }
+            catch (IOException e)
+            {
+               log.warn(COULD_NOT_READ_SERVICES_FILE, serviceFile, e);
+            }
+         }
+      }
+   }
 
-               String line = reader.readLine();
-               while (null != line)
-               {
-                  try
-                  {
-                     final int comment = line.indexOf('#');
+   private String trim(String line)
+   {
+      final int comment = line.indexOf('#');
 
-                     if (comment > -1)
-                     {
-                        line = line.substring(0, comment);
-                     }
+      if (comment > -1)
+      {
+         line = line.substring(0, comment);
+      }
+      return line.trim();
+   }
 
-                     line.trim();
+   private void loadService(String serviceClassName)
+   {
+      Class<? extends S> serviceClass = loadClass(serviceClassName);
+      S serviceInstance = prepareInstance(serviceClass);
+      providers.add(serviceInstance);
+   }
 
-                     if (line.length() > 0)
-                     {
-                        try
-                        {
-                           Class<?> clazz = loader.loadClass(line);
-                           Class<? extends S> serviceClass;
-                           try
-                           {
-                              serviceClass = clazz.asSubclass(expectedType);
-                           }
-                           catch (ClassCastException e)
-                           {
-                              throw new ForbiddenStateException(DECLARED_EXTENSION_DOES_NOT_IMPLEMENT_EXTENSION, line);
-                           }
-                           Object object = SecureReflections.ensureAccessible(SecureReflections.getDeclaredConstructor(serviceClass)).newInstance();
-                           
-                           @SuppressWarnings("unchecked")
-                           S instance = (S) object;
-                           
-                           providers.add(instance);
-                        }
-                        catch (NoClassDefFoundError e)
-                        {
-                           log.warn("Error loading line", line);
-                           xLog.throwing(Level.DEBUG, e);
-                           throw e;
-                        }
-                        catch (InstantiationException e)
-                        {
-                           log.warn("Error loading line", line);
-                           xLog.throwing(Level.DEBUG, e);
-                           throw e;
-                        }
-                        catch (IllegalAccessException e)
-                        {
-                           log.warn("Error loading line", line);
-                           xLog.throwing(Level.DEBUG, e);
-                           throw e;
-                        }
-                        catch (NoSuchMethodException e) 
-                        {
-                           log.warn("Error loading line", line);
-                           xLog.throwing(Level.DEBUG, e);
-                           throw e;
-                        }
-                     }
-                  }
-                  catch (Exception e)
-                  {
-                     // try next line
-                  }
+   private Class<? extends S> loadClass(String serviceClassName)
+   {
+      Class<?> clazz = null;
+      Class<? extends S> serviceClass = null;
+      try
+      {
+         clazz = loader.loadClass(serviceClassName);
+         serviceClass = clazz.asSubclass(expectedType);
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw new ForbiddenStateException(EXTENSION_CLASS_NOT_FOUND, serviceClassName);
+      }
+      catch (ClassCastException e)
+      {
+         throw new ForbiddenStateException(DECLARED_EXTENSION_DOES_NOT_IMPLEMENT_EXTENSION, serviceClassName);
+      }
+      return serviceClass;
+   }
 
-                  line = reader.readLine();
-               }
-            }
-            catch (Exception e)
-            {
-               // try the next file
-            }
-         }
+   @SuppressWarnings("unchecked")
+   private S prepareInstance(Class<? extends S> serviceClass)
+   {
+      try
+      {
+         return (S) SecureReflections.ensureAccessible(SecureReflections.getDeclaredConstructor(serviceClass)).newInstance();
       }
+      catch (Exception e)
+      {
+         throw new ForbiddenStateException(SECURITY_EXCEPTION_SCANNING, serviceClass, e);
+      }
    }
-   
-   
 
    /**
     * Lazily loads the available providers of this loader's service.
@@ -317,5 +328,4 @@
    {
       return "Services for " + serviceFile;
    }
-
 }

Modified: core/trunk/impl/src/main/resources/org/jboss/weld/messages/util_en.properties
===================================================================
--- core/trunk/impl/src/main/resources/org/jboss/weld/messages/util_en.properties	2010-03-14 23:05:02 UTC (rev 6046)
+++ core/trunk/impl/src/main/resources/org/jboss/weld/messages/util_en.properties	2010-03-15 12:40:10 UTC (rev 6047)
@@ -26,3 +26,6 @@
 NO_SUCH_METHOD=Method {0} not implemented by instance {1}
 ANNOTATION_VALUES_INACCESSIBLE=Cannot access values() on annotation
 INITIALIZER_METHOD_IS_GENERIC=Initializer method {0} declared on {1} may not be a generic method
+COULD_NOT_READ_SERVICES_LIST=Could not read services list from {0}
+COULD_NOT_READ_SERVICES_FILE=Could not read services file {0}
+EXTENSION_CLASS_NOT_FOUND=Extension class {0} not found
\ No newline at end of file



More information about the weld-commits mailing list