Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.pressgang.ccms.provider;
  
  import java.net.URL;
 import java.util.List;
 
 
 public abstract class DataProviderFactory {
     private List<ProviderListenerlisteners = new LinkedList<ProviderListener>();
 
     private 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;
             }
         });
     }

    
Initialise the Data Provider Factory with the required data.

Parameters:
args The arguments required to create the Data Provider Factory.
 
     @SuppressWarnings("unchecked")
     public static DataProviderFactory create(final Object... args) {
         // Find the implementation for the DataProviderFactory.
         final Class<? extends DataProviderFactorydataProviderClass = findDataProviderImpl();
 
         // Find the constructor that matches the arguments passed.
         final DataProviderFactory factory;
         try {
             Constructor<? extends DataProviderFactorydataProviderConstructor = null;
             final Constructor[] constructors = dataProviderClass.getDeclaredConstructors();
             for (final Constructor constructor : constructors) {
                 final Class<?>[] params = constructor.getParameterTypes();
                 boolean matches = false;
 
                 // Ensure that all the argument match
                 if (args.length == params.length) {
                     matches = true;
                     for (int i = 0; i < params.lengthi++) {
                         if (!params[i].isAssignableFrom(args[i].getClass())) {
                             matches = false;
                         }
                     }
                 }
 
                 // If the constructor matches then break the loop, as we've found the matching constructor
                 if (matches) {
                     dataProviderConstructor = constructor;
                     break;
                 }
             }
 
             // Make sure a constructor was found
             if (dataProviderConstructor != null) {
                 factory = dataProviderConstructor.newInstance(args);
             } else {
                 factory = null;
             }
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
 
         return factory;
     }
 
     @SuppressWarnings("unchecked")
     private static Class<? extends DataProviderFactoryfindDataProviderImpl() {
         // Get the class loader to be used to load the factory.
         final ClassLoader classLoader = getContextClassLoader();
 
         final String serviceId = "META-INF/services/" + DataProviderFactory.class.getName();
         // try to find services in CLASSPATH
         try {
             InputStream is;
             if (classLoader == null) {
                 is = ClassLoader.getSystemResourceAsStream(serviceId);
             } else {
                 is = classLoader.getResourceAsStream(serviceId);
             }
 
             if (is != null) {
                 // Read the class name from the services meta-inf
                final BufferedReader rd = new BufferedReader(new InputStreamReader(is"UTF-8"));
                final String factoryClassName = rd.readLine();
                rd.close();
                // Find the class for the specified class name
                if (factoryClassName != null && !"".equals(factoryClassName)) {
                    try {
                        return (Class<? extends DataProviderFactory>) findClass(factoryClassNameclassLoader);
                    } catch (ClassNotFoundException e) {
                        final URL url = classLoader.getResource(serviceId);
                        throw new ClassNotFoundException("Could not find from factory file " + urle);
                    }
                } else {
                    final URL url = classLoader.getResource(serviceId);
                    throw new ClassNotFoundException("Could not find from factory file " + url);
                }
            } else {
                final URL url = classLoader.getResource(serviceId);
                throw new ClassNotFoundException("Could not find from factory file " + url);
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    
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 Class<?> findClass(String classNameClassLoader 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;
        } catch (ClassNotFoundException x) {
            throw x;
        } catch (Exception x) {
            throw new ClassNotFoundException("Factory " + className + " could not be instantiated: " + xx);
        }
    }
    protected DataProviderFactory() {
    }

    
Get a Data Provider instance associated with the Application for the specified class.

Parameters:
clazz The class of the Data Provider to get an instance of.
<T> A DataProvider interface or implementation.
Returns:
The created data provider instance if one was able to be found.
    public <T> T getProvider(final Class<T> clazz) {
        return loadProvider(clazz);
    }

    
Load a provider for a specified class.

Parameters:
clazz The class of the Data Provider to load.
<T> A DataProvider interface or implementation.
Returns:
The created data provider instance if one was able to be found.
    protected abstract <T> T loadProvider(final Class<T> clazz);
    public abstract boolean isRollbackSupported();
    public abstract void rollback();
    public abstract boolean isNotificationsSupported();
    public void registerListener(ProviderListener listener) {
        if (isNotificationsSupported()) {
            .add(listener);
        } else {
            throw new UnsupportedOperationException("Notification events aren't supported");
        }
    }
    protected List<ProviderListenergetListeners() {
        return ;
    }
New to GrepCode? Check out our FAQ X