Save This Page
Home » hibernate-entity-src-20081106 » javax » persistence » [javadoc | source]
    1   // $Id: Persistence.java 11171 2007-02-08 03:40:51Z epbernard $
    2   package javax.persistence;
    3   
    4   import java.io.BufferedReader;
    5   import java.io.IOException;
    6   import java.io.InputStream;
    7   import java.io.InputStreamReader;
    8   import java.net.URL;
    9   import java.util.Enumeration;
   10   import java.util.HashSet;
   11   import java.util.Map;
   12   import java.util.Set;
   13   import java.util.regex.Matcher;
   14   import java.util.regex.Pattern;
   15   import javax.persistence.spi.PersistenceProvider;
   16   
   17   /**
   18    * Bootstrap class that provides access to an EntityManagerFactory.
   19    */
   20   public class Persistence {
   21   
   22   	public static String PERSISTENCE_PROVIDER = PersistenceProvider.class.getName();
   23   
   24   	protected static final Set<PersistenceProvider> providers = new HashSet<PersistenceProvider>();
   25   
   26   	/**
   27   	 * Create and return an EntityManagerFactory for the named persistence unit.
   28   	 *
   29   	 * @param persistenceUnitName The name of the persistence unit
   30   	 * @return The factory that creates EntityManagers configured according to the specified persistence unit
   31   	 */
   32   	public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName) {
   33   		return createEntityManagerFactory( persistenceUnitName, null );
   34   	}
   35   
   36   	/**
   37   	 * Create and return an EntityManagerFactory for the named persistence unit using the given properties.
   38   	 *
   39   	 * @param persistenceUnitName The name of the persistence unit
   40   	 * @param properties Additional properties to use when creating the factory. The values of these properties override
   41   	 * any values that may have been configured elsewhere
   42   	 * @return The factory that creates EntityManagers configured according to the specified persistence unit
   43   	 */
   44   	public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
   45   		EntityManagerFactory emf = null;
   46   
   47   		if ( providers.size() == 0 ) {
   48   			findAllProviders();
   49   		}
   50   		for ( PersistenceProvider provider : providers ) {
   51   			emf = provider.createEntityManagerFactory( persistenceUnitName, properties );
   52   			if ( emf != null ) break;
   53   		}
   54   		if ( emf == null ) {
   55   			throw new PersistenceException( "No Persistence provider for EntityManager named " + persistenceUnitName );
   56   		}
   57   		return emf;
   58   	}
   59   
   60   	// Helper methods
   61   
   62   	private static void findAllProviders() {
   63   		try {
   64   			ClassLoader loader = Thread.currentThread().getContextClassLoader();
   65   			Enumeration<URL> resources = loader.getResources( "META-INF/services/" + PersistenceProvider.class.getName() );
   66   			Set<String> names = new HashSet<String>();
   67   			while ( resources.hasMoreElements() ) {
   68   				URL url = resources.nextElement();
   69   				InputStream is = url.openStream();
   70   				try {
   71   					names.addAll( providerNamesFromReader( new BufferedReader( new InputStreamReader( is ) ) ) );
   72   				}
   73   				finally {
   74   					is.close();
   75   				}
   76   			}
   77   			for ( String s : names ) {
   78   				Class providerClass = loader.loadClass( s );
   79   				providers.add( (PersistenceProvider) providerClass.newInstance() );
   80   			}
   81   		}
   82   		catch (IOException e) {
   83   			throw new PersistenceException( e );
   84   		}
   85   		catch (InstantiationException e) {
   86   			throw new PersistenceException( e );
   87   		}
   88   		catch (IllegalAccessException e) {
   89   			throw new PersistenceException( e );
   90   		}
   91   		catch (ClassNotFoundException e) {
   92   			throw new PersistenceException( e );
   93   		}
   94   	}
   95   
   96   	private static final Pattern nonCommentPattern = Pattern.compile( "^([^#]+)" );
   97   
   98   	private static Set<String> providerNamesFromReader(BufferedReader reader) throws IOException {
   99   		Set<String> names = new HashSet<String>();
  100   		String line;
  101   		while ( ( line = reader.readLine() ) != null ) {
  102   			line = line.trim();
  103   			Matcher m = nonCommentPattern.matcher( line );
  104   			if ( m.find() ) {
  105   				names.add( m.group().trim() );
  106   			}
  107   		}
  108   		return names;
  109   	}
  110   }

Save This Page
Home » hibernate-entity-src-20081106 » javax » persistence » [javadoc | source]