Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.myfaces.shared.util;
 
 
 
 import java.net.URL;
 
 import java.util.List;
 
 import java.util.Set;
 
Utility methods for accessing classes and resources using an appropriate class loader.

Version:
$Revision: 1299845 $ $Date: 2012-03-12 15:46:16 -0500 (Mon, 12 Mar 2012) $
 
 public final class ClassLoaderUtils
 {
   // Utility class only, no instances
   private ClassLoaderUtils()
   {
   }
  
  
Loads the class with the specified name. For Java 2 callers, the current thread's context class loader is preferred, falling back on the system class loader of the caller when the current thread's context is not set, or the caller is pre Java 2.

Parameters:
name the name of the class
Returns:
the resulting Class object
Throws:
java.lang.ClassNotFoundException if the class was not found
 
   public static Class<?> loadClass(
     String namethrows ClassNotFoundException
   {
     return loadClass(namenull);
   }

  
Locates the resource with the specified name. For Java 2 callers, the current thread's context class loader is preferred, falling back on the system class loader of the caller when the current thread's context is not set, or the caller is pre Java 2.

Parameters:
name the name of the resource
Returns:
the resulting URL object
 
   public static URL getResource(
     String name)
   {
     return getResource(namenull);
   }

  
Locates the stream resource with the specified name. For Java 2 callers, the current thread's context class loader is preferred, falling back on the system class loader of the caller when the current thread's context is not set, or the caller is pre Java 2.

Parameters:
name the name of the resource
Returns:
the resulting InputStream object
 
   public static InputStream getResourceAsStream(
     String name)
   {
    return getResourceAsStream(namenull);
  }

  
Loads the class with the specified name. For Java 2 callers, the current thread's context class loader is preferred, falling back on the class loader of the caller when the current thread's context is not set, or the caller is pre Java 2. If the callerClassLoader is null, then fall back on the system class loader.

Parameters:
name the name of the class
callerClassLoader the calling class loader context
Returns:
the resulting Class object
Throws:
java.lang.ClassNotFoundException if the class was not found
  public static Class<?> loadClass(
    String      name,
    ClassLoader callerClassLoaderthrows ClassNotFoundException
  {
    Class<?> clazz = null;
    try
    {
      ClassLoader loader = getContextClassLoader();
      if (loader != null)
      {
          clazz = loader.loadClass(name);
      }
    }
    catch (ClassNotFoundException e)
    {
      // treat as though loader not set
    }
    if (clazz == null)
    {
      if (callerClassLoader != null)
      {
          clazz = callerClassLoader.loadClass(name);
      }
      else
      {
          clazz = Class.forName(name);
      }
    }
    return clazz;
  }

  
Locates the resource with the specified name. For Java 2 callers, the current thread's context class loader is preferred, falling back on the class loader of the caller when the current thread's context is not set, or the caller is pre Java 2. If the callerClassLoader is null, then fall back on the system class loader.

Parameters:
name the name of the resource
callerClassLoader the calling class loader context
Returns:
the resulting URL object
  public static URL getResource(
    String      name,
    ClassLoader callerClassLoader)
  {
    _checkResourceName(name);
    URL url = null;
    ClassLoader loader = getContextClassLoader();
    if (loader != null)
    {
        url = loader.getResource(name);
    }
    if (url == null)
    {
      if (callerClassLoader != null)
      {
          url = callerClassLoader.getResource(name);
      }
      else
      {
          url = ClassLoader.getSystemResource(name);
      }
    }
    return url;
  }

  
Locates the resource stream with the specified name. For Java 2 callers, the current thread's context class loader is preferred, falling back on the class loader of the caller when the current thread's context is not set, or the caller is pre Java 2. If the callerClassLoader is null, then fall back on the system class loader.

Parameters:
name the name of the resource
callerClassLoader the calling class loader context
Returns:
the resulting InputStream object
  public static InputStream getResourceAsStream(
    String      name,
    ClassLoader callerClassLoader)
  {
    _checkResourceName(name);
    InputStream stream = null;
    ClassLoader loader = getContextClassLoader();
    if (loader != null)
    {
        stream = loader.getResourceAsStream(name);
    }
    if (stream == null)
    {
      if (callerClassLoader != null)
      {
          stream = callerClassLoader.getResourceAsStream(name);
      }
      else
      {
          stream = ClassLoader.getSystemResourceAsStream(name);
      }
    }
    return stream;
  }

  
Dynamically accesses the current context class loader. Includes a check for priviledges against java2 security to ensure no security related exceptions are encountered. Returns null if there is no per-thread context class loader.
  public static ClassLoader getContextClassLoader()
  {
      if (System.getSecurityManager() != null
      {
          try 
          {
              ClassLoader cl = AccessController.doPrivileged(new PrivilegedExceptionAction<ClassLoader>()
                      {
                          public ClassLoader run() throws PrivilegedActionException
                          {
                              return Thread.currentThread().getContextClassLoader();
                          }
                      });
              return cl;
          }
          catch (PrivilegedActionException pae)
          {
              throw new FacesException(pae);
          }
      }
      else
      {
          return Thread.currentThread().getContextClassLoader();
      }
  }

  
Instantiate a service from a file in /META-INF/services.

The following is an excerpt from the JAR File specification: A service provider identifies itself by placing a provider-configuration file in the resource directory META-INF/services. The file's name should consist of the fully-qualified name of the abstract service class. The file should contain a newline-separated list of unique concrete provider-class names. Space and tab characters, as well as blank lines, are ignored. The comment character is '#' (0x23); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8.

Parameters:
service the classname of the abstract service class. eg: javax.servlet.Filter
  @SuppressWarnings("unchecked")
  public static <T> List<T> getServices(String service)
  {
    String serviceUri ="META-INF/services/" + service;
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try
    {
      Enumeration<URLurls = loader.getResources(serviceUri);
      if (urls.hasMoreElements())
      {
        List<T> services = new ArrayList<T>(1);
        Set<Stringkeys = new HashSet<String>(20);
        do
        {
          URL url = urls.nextElement();
          
          if (.isLoggable(.))
          {
            .finest("Processing: " + url);
          }
          try
          {
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            try
            {
              while(true)
              {
                String line = in.readLine();
                if (line == null)
                {
                    break;
                }
                
                String className = _parseLine(line);
                
                if(className!=null && keys.add(className))
                {
                  T instance = (T) _getClass(loaderclassName);
                  services.add(instance);
                }                
              }
            }
            finally
            {
              in.close();
            }
          }
          catch (Exception e)
          {
            if (.isLoggable(.))
            {
              .log(."Error parsing URL: " + urle);
            }
          }
        } 
        while(urls.hasMoreElements());
        
        if (services.size() == 1)
        {
            return Collections.singletonList(services.get(0));
        }
        
        return Collections.unmodifiableList(services);
      }
    }
    catch (IOException e)
    {
      if (.isLoggable(.))
      {
        .log(."Error loading Resource: " + serviceUrie);
      }
    }
    return Collections.emptyList();
  }
  
  private static String _parseLine(String line)
  {
    // Eliminate any comments
    int hashIndex = line.indexOf('#');
    if (hashIndex >= 0)
    {
        line = line.substring(0, hashIndex);
    }
    // and any whitespace
    line = line.trim();
    if (line.length() > 0)
    {
      return line;
    }
    
    return null;
  }
  
  private static Object _getClass(ClassLoader loaderString className)
           IllegalAccessException
  {
    Class<?> clazz = loader.loadClass(className);
    return clazz.newInstance();
  }
  private static void _checkResourceName(String name)
  {
    if ((name != null) && name.startsWith("/"))
    {
      if (.isLoggable(.))
      {
        .log(."Resource name not portable: " +name);
      }
    }
  }
  private static final Logger _LOG = Logger.getLogger(ClassLoaderUtils.class.getName());
New to GrepCode? Check out our FAQ X