001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.persistence;
031:
032: import javax.persistence.spi.PersistenceProvider;
033: import java.io.InputStream;
034: import java.net.URL;
035: import java.util.ArrayList;
036: import java.util.Enumeration;
037: import java.util.Map;
038: import java.util.WeakHashMap;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: * Bootstrap class to obtain an EntityManagerFactory.
044: */
045: public class Persistence {
046: private static final Logger log = Logger
047: .getLogger(Persistence.class.getName());
048:
049: private static final String SERVICE = "META-INF/services/javax.persistence.spi.PersistenceProvider";
050: private static WeakHashMap<ClassLoader, PersistenceProvider[]> _providerMap = new WeakHashMap<ClassLoader, PersistenceProvider[]>();
051:
052: private static final String AMBER_PROVIDER = "com.caucho.amber.manager.AmberPersistenceProvider";
053:
054: /**
055: * Create an return an EntityManagerFactory for the named unit.
056: *
057: * @param name - the name of the persistence unit
058: */
059: public static EntityManagerFactory createEntityManagerFactory(
060: String name) {
061: PersistenceProvider[] providers = getProviderList();
062:
063: for (int i = 0; i < providers.length; i++) {
064: EntityManagerFactory factory;
065:
066: factory = providers[i].createEntityManagerFactory(name,
067: null);
068:
069: if (factory != null)
070: return factory;
071: }
072:
073: return null;
074: }
075:
076: /**
077: * Create and return an EntityManagerFactory for the named unit.
078: *
079: * @param name - the name of the persistence unit
080: * @param props - persistence unit properties
081: */
082: public static EntityManagerFactory createEntityManagerFactory(
083: String name, Map props) {
084: for (PersistenceProvider provider : getProviderList()) {
085: EntityManagerFactory factory;
086:
087: factory = provider.createEntityManagerFactory(name, props);
088:
089: if (factory != null)
090: return factory;
091: }
092:
093: return null;
094: }
095:
096: private static PersistenceProvider[] getProviderList() {
097: ClassLoader loader = Thread.currentThread()
098: .getContextClassLoader();
099:
100: PersistenceProvider[] providers = _providerMap.get(loader);
101:
102: if (providers != null)
103: return providers;
104:
105: ArrayList<PersistenceProvider> list = new ArrayList<PersistenceProvider>();
106:
107: try {
108: Class cl = Class.forName(AMBER_PROVIDER, false, loader);
109:
110: PersistenceProvider provider = (PersistenceProvider) cl
111: .newInstance();
112:
113: list.add(provider);
114: } catch (Exception e) {
115: log.log(Level.FINE, e.toString(), e);
116: }
117:
118: try {
119: Enumeration e = loader.getResources(SERVICE);
120:
121: while (e.hasMoreElements()) {
122: URL url = (URL) e.nextElement();
123:
124: PersistenceProvider provider = loadProvider(url, loader);
125:
126: if (provider != null)
127: list.add(provider);
128: }
129: } catch (Exception e) {
130: log.log(Level.WARNING, e.toString(), e);
131: }
132:
133: providers = new PersistenceProvider[list.size()];
134: list.toArray(providers);
135:
136: _providerMap.put(loader, providers);
137:
138: return providers;
139: }
140:
141: private static PersistenceProvider loadProvider(URL url,
142: ClassLoader loader) {
143: InputStream is = null;
144: try {
145: is = url.openStream();
146: int ch;
147:
148: while ((ch = is.read()) >= 0) {
149: if (Character.isWhitespace((char) ch)) {
150: } else if (ch == '#') {
151: for (; ch >= 0 && ch != '\n' && ch != '\r'; ch = is
152: .read()) {
153: }
154: } else {
155: StringBuilder sb = new StringBuilder();
156:
157: for (; ch >= 0
158: && !Character.isWhitespace((char) ch); ch = is
159: .read()) {
160: sb.append((char) ch);
161: }
162:
163: String className = sb.toString();
164:
165: Class cl = Class.forName(className, false, loader);
166:
167: return (PersistenceProvider) cl.newInstance();
168: }
169: }
170: } catch (Exception e) {
171: log.log(Level.WARNING, e.toString(), e);
172: } finally {
173: try {
174: if (is != null)
175: is.close();
176: } catch (Throwable e) {
177: }
178: }
179:
180: return null;
181: }
182: }
|