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.lifecycle;
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 JpaEntityLifecycleInjector} 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: * Currently support the following JPA implementations: Hibernate, TopLink Essentials (Glassfish Persistence), OpenJPA.
32: * <p/>
33: * Assumes that the <code>EntityManagerFactory</code> is the native one, since the
34: * {@link org.compass.gps.device.jpa.NativeJpaExtractor} of the
35: * {@link org.compass.gps.device.jpa.JpaGpsDevice} was used to extract it.
36: *
37: * @author kimchy
38: */
39: public abstract class JpaEntityLifecycleInjectorDetector {
40:
41: public static JpaEntityLifecycleInjector detectInjector(
42: EntityManagerFactory entityManagerFactory,
43: CompassSettings settings) throws JpaGpsDeviceException {
44: String injectorClassName = NativeJpaHelper.detectNativeJpa(
45: entityManagerFactory, settings,
46: new NativeJpaHelper.NativeJpaCallback<String>() {
47:
48: public String onHibernate() {
49: return "org.compass.gps.device.jpa.lifecycle.HibernateJpaEntityLifecycleInjector";
50: }
51:
52: public String onTopLinkEssentials() {
53: return "org.compass.gps.device.jpa.lifecycle.TopLinkEssentialsJpaEntityLifecycleInjector";
54: }
55:
56: public String onEclipseLink() {
57: return "org.compass.gps.device.jpa.lifecycle.EclipseLinkJpaEntityLifecycleInjector";
58: }
59:
60: public String onOpenJPA() {
61: return "org.compass.gps.device.jpa.lifecycle.OpenJPAJpaEntityLifecycleInjector";
62: }
63:
64: public String onUnknown() {
65: return null;
66: }
67: });
68:
69: if (injectorClassName == null) {
70: return null;
71: }
72:
73: try {
74: Class injectorClass = ClassUtils.forName(injectorClassName,
75: settings.getClassLoader());
76: return (JpaEntityLifecycleInjector) injectorClass
77: .newInstance();
78: } catch (Exception e) {
79: throw new JpaGpsDeviceException(
80: "Failed to create injector class ["
81: + injectorClassName + "]", e);
82: }
83: }
84: }
|