KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > persistence > Persistence


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package javax.persistence;
24
25 // J2SE imports
26
import java.io.IOException JavaDoc;
27 import java.io.BufferedReader JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.regex.Matcher JavaDoc;
36 import java.util.regex.Pattern JavaDoc;
37
38 // persistence imports
39
import javax.persistence.spi.PersistenceProvider;
40
41 /**
42  * Bootstrap class that is used to obtain an {@link EntityManagerFactory}.
43  *
44  * @since Java Persistence 1.0
45  */

46 public class Persistence {
47
48     public static String JavaDoc PERSISTENCE_PROVIDER = "javax.persistence.spi.PeristenceProvider";
49     protected static final Set JavaDoc<PersistenceProvider> providers = new HashSet JavaDoc<PersistenceProvider>();
50   
51     /**
52      * Create and return an EntityManagerFactory for the
53      * named persistence unit.
54      *
55      * @param persistenceUnitName The name of the persistence unit
56      * @return The factory that creates EntityManagers configured
57      * according to the specified persistence unit
58      */

59     public static EntityManagerFactory createEntityManagerFactory(String JavaDoc persistenceUnitName) {
60         return createEntityManagerFactory(persistenceUnitName, null);
61     }
62
63     /**
64      * Create and return an EntityManagerFactory for the
65      * named persistence unit using the given properties.
66      *
67      * @param persistenceUnitName The name of the persistence unit
68      * @param properties Additional properties to use when creating the
69      * factory. The values of these properties override any values
70      * that may have been configured elsewhere.
71      * @return The factory that creates EntityManagers configured
72      * according to the specified persistence unit.
73      */

74     public static EntityManagerFactory createEntityManagerFactory(
75             String JavaDoc persistenceUnitName, Map JavaDoc properties) {
76         EntityManagerFactory emf = null;
77         if (providers.size() == 0) {
78             try{
79                 findAllProviders();
80             } catch (IOException JavaDoc exc){};
81         }
82         for (PersistenceProvider provider : providers) {
83             emf = provider.createEntityManagerFactory(persistenceUnitName, properties);
84             if (emf != null){
85                 break;
86             }
87         }
88         if (emf == null) {
89             throw new PersistenceException("No Persistence provider for EntityManager named " + persistenceUnitName);
90         }
91         return emf;
92     }
93   
94     // Helper methods
95

96     private static void findAllProviders() throws IOException JavaDoc {
97         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
98         Enumeration JavaDoc<URL JavaDoc> resources =
99             loader.getResources("META-INF/services/" + PersistenceProvider.class.getName());
100         Set JavaDoc<String JavaDoc> names = new HashSet JavaDoc<String JavaDoc>();
101         while (resources.hasMoreElements()) {
102             URL JavaDoc url = resources.nextElement();
103             InputStream JavaDoc is = url.openStream();
104             try {
105                 names.addAll(providerNamesFromReader(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is))));
106             } finally {
107                 is.close();
108             }
109         }
110         for (String JavaDoc s : names) {
111             try{
112                 providers.add((PersistenceProvider)loader.loadClass(s).newInstance());
113             } catch (ClassNotFoundException JavaDoc exc){
114             } catch (InstantiationException JavaDoc exc){
115             } catch (IllegalAccessException JavaDoc exc){
116             }
117         }
118     }
119
120     private static final Pattern JavaDoc nonCommentPattern = Pattern.compile("^([^#]+)");
121
122     private static Set JavaDoc<String JavaDoc> providerNamesFromReader(BufferedReader JavaDoc reader) throws IOException JavaDoc {
123         Set JavaDoc<String JavaDoc> names = new HashSet JavaDoc<String JavaDoc>();
124         String JavaDoc line;
125         while ((line = reader.readLine()) != null) {
126             line = line.trim();
127             Matcher JavaDoc m = nonCommentPattern.matcher(line);
128             if (m.find()) {
129                 names.add(m.group().trim());
130             }
131         }
132         return names;
133     }
134 }
135
Popular Tags