01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.gps.device.jpa.entities;
18:
19: import javax.persistence.EntityManagerFactory;
20:
21: import org.compass.core.config.CompassSettings;
22: import org.compass.core.util.ClassUtils;
23: import org.compass.gps.device.jpa.JpaGpsDeviceException;
24: import org.compass.gps.device.jpa.support.NativeJpaHelper;
25:
26: /**
27: * A {@link JpaEntitiesLocator} detector. Tries to check for the actual implementation of JPA
28: * <code>EntityManagerFactory</code>, and based on it, check if the current set of actual JPA
29: * implementations is one of compass supported ones (like Hibernate).
30: * <p/>
31: * If no implementation is found for the native <code>EntityManagerFactory</code> implementation,
32: * uses the {@link DefaultJpaEntitiesLocator}.
33: * <p/>
34: * Currently support the following JPA implementations: Hibernate, TopLink Essentials (Glassfish Persistence), OpenJPA.
35: * <p/>
36: * Assumes that the <code>EntityManagerFactory</code> is the native one, since the
37: * {@link org.compass.gps.device.jpa.NativeJpaExtractor} of the
38: * {@link org.compass.gps.device.jpa.JpaGpsDevice} was used to extract it.
39: *
40: * @author kimchy
41: */
42: public abstract class JpaEntitiesLocatorDetector {
43:
44: public static JpaEntitiesLocator detectLocator(
45: EntityManagerFactory entityManagerFactory,
46: CompassSettings settings) {
47:
48: String locatorClassName = NativeJpaHelper.detectNativeJpa(
49: entityManagerFactory, settings,
50: new NativeJpaHelper.NativeJpaCallback<String>() {
51:
52: public String onHibernate() {
53: return "org.compass.gps.device.jpa.entities.HibernateJpaEntitiesLocator";
54: }
55:
56: public String onTopLinkEssentials() {
57: return "org.compass.gps.device.jpa.entities.TopLinkEssentialsJpaEntitiesLocator";
58: }
59:
60: public String onEclipseLink() {
61: return "org.compass.gps.device.jpa.entities.EclipseLinkJpaEntitiesLocator";
62: }
63:
64: public String onOpenJPA() {
65: return "org.compass.gps.device.jpa.entities.OpenJPAJpaEntitiesLocator";
66: }
67:
68: public String onUnknown() {
69: return DefaultJpaEntitiesLocator.class
70: .getName();
71: }
72: });
73:
74: try {
75: Class locatorClass = ClassUtils.forName(locatorClassName,
76: settings.getClassLoader());
77: return (JpaEntitiesLocator) locatorClass.newInstance();
78: } catch (Exception e) {
79: throw new JpaGpsDeviceException(
80: "Failed to create locator class ["
81: + locatorClassName + "]", e);
82: }
83:
84: }
85: }
|