001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence;
020:
021: import java.util.Collection;
022: import java.util.Map;
023: import javax.naming.Context;
024: import javax.naming.InitialContext;
025: import javax.naming.NamingException;
026: import javax.persistence.EntityManager;
027: import javax.persistence.EntityManagerFactory;
028: import javax.persistence.Query;
029: import javax.rmi.PortableRemoteObject;
030:
031: import org.apache.openjpa.enhance.PersistenceCapable;
032: import org.apache.openjpa.kernel.Bootstrap;
033: import org.apache.openjpa.kernel.Broker;
034: import org.apache.openjpa.lib.conf.ConfigurationProvider;
035: import org.apache.openjpa.lib.util.Localizer;
036: import org.apache.openjpa.util.ImplHelper;
037:
038: /**
039: * Static helper methods for JPA users.
040: *
041: * @author Abe White
042: * @published
043: * @since 0.4.0
044: */
045: public class OpenJPAPersistence {
046:
047: private static final Localizer _loc = Localizer
048: .forPackage(OpenJPAPersistence.class);
049:
050: /**
051: * Return the OpenJPA facade to the given entity manager factory.
052: */
053: public static OpenJPAEntityManagerFactory cast(
054: EntityManagerFactory emf) {
055: return (OpenJPAEntityManagerFactory) emf;
056: }
057:
058: /**
059: * Return the OpenJPA facade to the given entity manager.
060: */
061: public static OpenJPAEntityManager cast(EntityManager em) {
062: if (em instanceof OpenJPAEntityManager)
063: return (OpenJPAEntityManager) em;
064: return (OpenJPAEntityManager) em.getDelegate();
065: }
066:
067: /**
068: * Return the OpenJPA facade to the given query.
069: */
070: public static OpenJPAQuery cast(Query q) {
071: return (OpenJPAQuery) q;
072: }
073:
074: /**
075: * Returns the {@link OpenJPAEntityManagerFactory} specified by
076: * your OpenJPA defaults. This method will return the same logical factory
077: * for each invocation.
078: */
079: public static OpenJPAEntityManagerFactory getEntityManagerFactory() {
080: return getEntityManagerFactory(null);
081: }
082:
083: /**
084: * Returns the {@link OpenJPAEntityManagerFactory} specified by
085: * your OpenJPA defaults, using <code>map</code> as overrides. This method
086: * will return the same logical factory for invocations with the same
087: * overrides.
088: */
089: public static OpenJPAEntityManagerFactory getEntityManagerFactory(
090: Map map) {
091: ConfigurationProvider cp = new PersistenceProductDerivation.ConfigurationProviderImpl(
092: map);
093: try {
094: return JPAFacadeHelper.toEntityManagerFactory(Bootstrap
095: .getBrokerFactory(cp, null));
096: } catch (Exception e) {
097: throw PersistenceExceptions.toPersistenceException(e);
098: }
099: }
100:
101: /**
102: * Returns a new {@link OpenJPAEntityManagerFactory} specified by
103: * <code>name</code> in an XML configuration file at the resource location
104: * <code>resource</code>. If <code>name</code> is <code>null</code>, uses
105: * the first resource found in the specified location, regardless of the
106: * name specified in the XML resource or the name of the jar that the
107: * resource is contained in. If <code>resource</code> is <code>null</code>,
108: * uses the spec-defined <code>META-INF/persistence.xml</code> resource.
109: * This method only resolves {@link OpenJPAEntityManagerFactory} instances.
110: */
111: public static OpenJPAEntityManagerFactory createEntityManagerFactory(
112: String name, String resource) {
113: return createEntityManagerFactory(name, resource, null);
114: }
115:
116: /**
117: * Returns a new {@link OpenJPAEntityManagerFactory} specified by
118: * <code>name</code> in an XML configuration file at the resource location
119: * <code>resource</code>, applying the properties specified in
120: * <code>map</code> as overrides. If <code>name</code> is
121: * <code>null</code>, uses the first resource found in the specified
122: * location, regardless of the name specified in the XML resource or the
123: * name of the jar that the resource is contained in.
124: * If <code>resource</code> is <code>null</code>, uses the spec-defined
125: * <code>META-INF/persistence.xml</code> resource.
126: * This method only resolves {@link OpenJPAEntityManagerFactory} instances.
127: */
128: public static OpenJPAEntityManagerFactory createEntityManagerFactory(
129: String name, String resource, Map map) {
130: return (OpenJPAEntityManagerFactory) new PersistenceProviderImpl()
131: .createEntityManagerFactory(name, resource, map);
132: }
133:
134: /**
135: * Returns the {@link EntityManagerFactory} at the JNDI location specified
136: * by <code>jndiLocation</code> in the context <code>context</code>. If
137: * <code>context</code> is <code>null</code>,
138: * <code>new InitialContext()</code> will be used.
139: */
140: public static OpenJPAEntityManagerFactory createEntityManagerFactory(
141: String jndiLocation, Context context) {
142: if (jndiLocation == null)
143: throw new NullPointerException("jndiLocation == null");
144:
145: try {
146: if (context == null)
147: context = new InitialContext();
148:
149: Object o = context.lookup(jndiLocation);
150: return (OpenJPAEntityManagerFactory) PortableRemoteObject
151: .narrow(o, OpenJPAEntityManagerFactory.class);
152: } catch (NamingException ne) {
153: throw new ArgumentException(_loc.get("naming-exception",
154: jndiLocation), new Throwable[] { ne }, null, true);
155: }
156: }
157:
158: /**
159: * Return the entity manager for the given object, if one can be determined
160: * from just the object alone. This method will succeed for instances that
161: * are enhanced, that were loaded from the database (rather than
162: * being constructed with <code>new</code>), or that were created through
163: * {@link OpenJPAEntityManager#createInstance}.
164: */
165: public static OpenJPAEntityManager getEntityManager(Object o) {
166: try {
167: if (ImplHelper.isManageable(o)) {
168: PersistenceCapable pc = ImplHelper
169: .toPersistenceCapable(o, null);
170: if (pc != null)
171: return JPAFacadeHelper.toEntityManager((Broker) pc
172: .pcGetGenericContext());
173: }
174: return null;
175: } catch (Exception e) {
176: throw PersistenceExceptions.toPersistenceException(e);
177: }
178: }
179:
180: /**
181: * Close the given resource. The resource can be an extent iterator,
182: * query result, large result set relation, or any closeable OpenJPA
183: * component.
184: */
185: public static void close(Object o) {
186: try {
187: ImplHelper.close(o);
188: } catch (Exception e) {
189: throw PersistenceExceptions.toPersistenceException(e);
190: }
191: }
192:
193: /**
194: * Returns true if the specified class is an entity or embeddable type.
195: */
196: public static boolean isManagedType(EntityManager em, Class cls) {
197: try {
198: return ImplHelper.isManagedType(JPAFacadeHelper
199: .toBroker(em).getConfiguration(), cls);
200: } catch (Exception e) {
201: throw PersistenceExceptions.toPersistenceException(e);
202: }
203: }
204:
205: /**
206: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
207: * the published-API boundary, as does the JPAFacadeHelper utilization.
208: */
209: public static final String EM_KEY = "org.apache.openjpa.persistence.EntityManager";
210:
211: /**
212: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
213: * the published-API boundary, as does the JPAFacadeHelper utilization.
214: */
215: public static final String EMF_KEY = "org.apache.openjpa.persistence.EntityManagerFactory";
216:
217: /**
218: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
219: * the published-API boundary, as does the JPAFacadeHelper utilization.
220: */
221: public static OpenJPAEntityManagerFactory toEntityManagerFactory(
222: org.apache.openjpa.kernel.BrokerFactory factory) {
223: return JPAFacadeHelper.toEntityManagerFactory(factory);
224: }
225:
226: /**
227: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
228: * the published-API boundary, as does the JPAFacadeHelper utilization.
229: */
230: public static org.apache.openjpa.kernel.BrokerFactory toBrokerFactory(
231: EntityManagerFactory factory) {
232: return JPAFacadeHelper.toBrokerFactory(factory);
233: }
234:
235: /**
236: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
237: * the published-API boundary, as does the JPAFacadeHelper utilization.
238: */
239: public static OpenJPAEntityManager toEntityManager(
240: org.apache.openjpa.kernel.Broker broker) {
241: return JPAFacadeHelper.toEntityManager(broker);
242: }
243:
244: /**
245: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
246: * the published-API boundary, as does the JPAFacadeHelper utilization.
247: */
248: public static Broker toBroker(EntityManager em) {
249: return JPAFacadeHelper.toBroker(em);
250: }
251:
252: /**
253: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
254: * the published-API boundary, as does the JPAFacadeHelper utilization.
255: */
256: public static org.apache.openjpa.meta.ClassMetaData getMetaData(
257: Object o) {
258: return JPAFacadeHelper.getMetaData(o);
259: }
260:
261: /**
262: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
263: * the published-API boundary, as does the JPAFacadeHelper utilization.
264: */
265: public static org.apache.openjpa.meta.ClassMetaData getMetaData(
266: EntityManager em, Class cls) {
267: return JPAFacadeHelper.getMetaData(em, cls);
268: }
269:
270: /**
271: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
272: * the published-API boundary, as does the JPAFacadeHelper utilization.
273: */
274: public static org.apache.openjpa.meta.ClassMetaData getMetaData(
275: EntityManagerFactory factory, Class cls) {
276: return JPAFacadeHelper.getMetaData(factory, cls);
277: }
278:
279: /**
280: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
281: * the published-API boundary, as does the JPAFacadeHelper utilization.
282: */
283: public static Object fromOpenJPAObjectId(Object oid) {
284: return JPAFacadeHelper.fromOpenJPAObjectId(oid);
285: }
286:
287: /**
288: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
289: * the published-API boundary, as does the JPAFacadeHelper utilization.
290: */
291: public static Object toOpenJPAObjectId(
292: org.apache.openjpa.meta.ClassMetaData meta, Object oid) {
293: return JPAFacadeHelper.toOpenJPAObjectId(meta, oid);
294: }
295:
296: /**
297: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
298: * the published-API boundary, as does the JPAFacadeHelper utilization.
299: */
300: public static Object[] toOpenJPAObjectIds(
301: org.apache.openjpa.meta.ClassMetaData meta, Object... oids) {
302: return JPAFacadeHelper.toOpenJPAObjectIds(meta, oids);
303: }
304:
305: /**
306: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
307: * the published-API boundary, as does the JPAFacadeHelper utilization.
308: */
309: public static Collection toOpenJPAObjectIds(
310: org.apache.openjpa.meta.ClassMetaData meta, Collection oids) {
311: return JPAFacadeHelper.toOpenJPAObjectIds(meta, oids);
312: }
313:
314: /**
315: * @deprecated use {@link JPAFacadeHelper} instead. This method pierces
316: * the published-API boundary, as does the JPAFacadeHelper utilization.
317: */
318: public static Class fromOpenJPAObjectIdClass(Class oidClass) {
319: return JPAFacadeHelper.fromOpenJPAObjectIdClass(oidClass);
320: }
321: }
|