public static ServiceFactory newInstance ( ) throws ServiceException { // Create the factory singleton if needed if ( factory == null ) { PrivilegedAction action = new PropertyAccessAction ( SERVICEFACTORY_PROPERTY , DEFAULT_SERVICE_FACTORY ) ; String factoryName = ( String ) AccessController . doPrivileged ( action ) ; ClassLoader loader = Thread . currentThread ( ) . getContextClassLoader ( ) ; try { try { Class factoryClass = loader . loadClass ( factoryName ) ; factory = ( ServiceFactory ) factoryClass . newInstance ( ) ; } catch ( ClassNotFoundException e ) { // Throw the exception if the user asked for a specific factory if ( factoryName . equals ( DEFAULT_SERVICE_FACTORY ) == false ) throw e ; for ( int i = 0 ; i < alternativeFactories . length ; i ++ ) { factoryName = alternativeFactories [ i ] ; try { Class factoryClass = loader . loadClass ( factoryName ) ; return ( ServiceFactory ) factoryClass . newInstance ( ) ; } catch ( ClassNotFoundException e1 ) { log . severe ( "Cannot load factory: " + factoryName ) ; } } } } catch ( Throwable e ) { throw new ServiceException ( "Failed to create factory: " + factoryName , e ) ; } } if ( factory == null ) throw new ServiceException ( "Cannot find ServiceFactory implementation" ) ; return factory ; }
Gets an instance of the ServiceFactory Only one copy of a factory exists and is returned to the application each time this method is called .