Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * The contents of this file are subject to the terms
   * of the Common Development and Distribution License
   * (the "License").  You may not use this file except
   * in compliance with the License.
   *
   * You can obtain a copy of the license at
   * http://www.opensource.org/licenses/cddl1.php
   * See the License for the specific language governing
  * permissions and limitations under the License.
  */
 
 /*
  * FactoryFinder.java
  *
  * Created on November 16, 2007, 3:14 PM
  *
  */
 package javax.ws.rs.ext;
 
 import java.io.File;
 
 class FactoryFinder {
     
     static ClassLoader getContextClassLoader() { 
         return AccessController.doPrivileged
             new PrivilegedAction<ClassLoader>() { 
                 public ClassLoader run() { 
                     ClassLoader cl = null;
                     try { 
                         cl = Thread.currentThread().getContextClassLoader(); 
                     } catch (SecurityException ex) { } 
                     return cl
                 } 
         }); 
     }

    
Creates an instance of the specified class using the specified ClassLoader object.

Throws:
java.lang.ClassNotFoundException if the given class could not be found or could not be instantiated
 
     private static Object newInstance(String className,
             ClassLoader classLoaderthrows ClassNotFoundException {
         try {
             Class spiClass;
             if (classLoader == null) {
                 spiClass = Class.forName(className);
             } else {
                 try { 
                     spiClass = Class.forName(classNamefalseclassLoader); 
                 } catch (ClassNotFoundException ex) { 
                     spiClass = Class.forName(className); 
                 } 
             }
             return spiClass.newInstance();
         } catch (ClassNotFoundException x) {
             throw x;
         } catch (Exception x) {
             throw new ClassNotFoundException(
                     "Provider " + className + " could not be instantiated: " + x,
                     x);
         }
     }

    
Finds the implementation Class object for the given factory name, or if that fails, finds the Class object for the given fallback class name. The arguments supplied MUST be used in order. If using the first argument is successful, the second one will not be used.

This method is package private so that this code can be shared.

Parameters:
factoryId the name of the factory to find, which is a system property
fallbackClassName the implementation class name, which is to be used only if nothing else is found; null to indicate that there is no fallback class name
Returns:
the Class object of the specified message factory; may not be null
Throws:
WebServiceException if there is an error
 
     static Object find(String factoryIdString fallbackClassNamethrows ClassNotFoundException {
         ClassLoader classLoader = getContextClassLoader();
 
         String serviceId = "META-INF/services/" + factoryId;
         // try to find services in CLASSPATH
        try {
            InputStream is;
            if (classLoader == null) {
                is = ClassLoader.getSystemResourceAsStream(serviceId);
            } else {
                is = classLoader.getResourceAsStream(serviceId);
            }
            if (is != null) {
                BufferedReader rd =
                        new BufferedReader(new InputStreamReader(is"UTF-8"));
                String factoryClassName = rd.readLine();
                rd.close();
                if (factoryClassName != null &&
                        !"".equals(factoryClassName)) {
                    return newInstance(factoryClassNameclassLoader);
                }
            }
        } catch (Exception ex) {
        }
        // try to read from $java.home/lib/jaxrs.properties
        try {
            String javah = System.getProperty("java.home");
            String configFile = javah + . +
                    "lib" + . + "jaxrs.properties";
            File f = new File(configFile);
            if (f.exists()) {
                Properties props = new Properties();
                props.load(new FileInputStream(f));
                String factoryClassName = props.getProperty(factoryId);
                return newInstance(factoryClassNameclassLoader);
            }
        } catch (Exception ex) {
        }
        // Use the system property
        try {
            String systemProp =
                    System.getProperty(factoryId);
            if (systemProp != null) {
                return newInstance(systemPropclassLoader);
            }
        } catch (SecurityException se) {
        }
        if (fallbackClassName == null) {
            throw new ClassNotFoundException(
                    "Provider for " + factoryId + " cannot be found"null);
        }
        return newInstance(fallbackClassNameclassLoader);
    }
New to GrepCode? Check out our FAQ X