001: package org.compass.gps.device.jpa.support;
002:
003: import java.util.HashSet;
004: import java.util.Set;
005: import javax.persistence.EntityManagerFactory;
006:
007: import org.compass.core.config.CompassSettings;
008: import org.compass.core.util.ClassUtils;
009: import org.compass.gps.device.jpa.JpaGpsDeviceException;
010: import org.compass.gps.device.jpa.NativeJpaExtractor;
011:
012: /**
013: * @author kimchy
014: */
015: public abstract class NativeJpaHelper {
016:
017: public static interface NativeJpaCallback<T> {
018:
019: T onHibernate();
020:
021: T onTopLinkEssentials();
022:
023: T onOpenJPA();
024:
025: T onEclipseLink();
026:
027: T onUnknown();
028: }
029:
030: public static <T> T detectNativeJpa(EntityManagerFactory emf,
031: CompassSettings settings, NativeJpaCallback<T> callback)
032: throws JpaGpsDeviceException {
033: EntityManagerFactory nativeEmf = extractNativeJpa(emf, settings);
034:
035: Set interfaces = ClassUtils.getAllInterfacesAsSet(nativeEmf);
036: Set<String> interfacesAsStrings = new HashSet<String>();
037: for (Object anInterface : interfaces) {
038: interfacesAsStrings.add(((Class) anInterface).getName());
039: }
040: interfacesAsStrings.add(nativeEmf.getClass().getName());
041:
042: T retVal;
043: if (interfacesAsStrings
044: .contains("org.hibernate.ejb.HibernateEntityManagerFactory")) {
045: retVal = callback.onHibernate();
046: } else if (interfacesAsStrings
047: .contains("oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerFactoryImpl")) {
048: retVal = callback.onTopLinkEssentials();
049: } else if (interfacesAsStrings
050: .contains("org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl")) {
051: retVal = callback.onEclipseLink();
052: } else if (interfacesAsStrings
053: .contains("org.apache.openjpa.persistence.OpenJPAEntityManagerFactory")) {
054: retVal = callback.onOpenJPA();
055: } else {
056: retVal = callback.onUnknown();
057: }
058: return retVal;
059: }
060:
061: public static EntityManagerFactory extractNativeJpa(
062: EntityManagerFactory emf, CompassSettings settings) {
063: Set interfaces = ClassUtils.getAllInterfacesAsSet(emf);
064: Set<String> interfacesAsStrings = new HashSet<String>();
065: for (Object anInterface : interfaces) {
066: interfacesAsStrings.add(((Class) anInterface).getName());
067: }
068: interfacesAsStrings.add(emf.getClass().getName());
069:
070: NativeJpaExtractor extractor = null;
071: if (interfacesAsStrings
072: .contains("org.springframework.orm.jpa.EntityManagerFactoryInfo")) {
073: try {
074: extractor = (NativeJpaExtractor) ClassUtils
075: .forName(
076: "org.compass.spring.device.jpa.SpringNativeJpaExtractor",
077: settings.getClassLoader())
078: .newInstance();
079: } catch (Exception e) {
080: throw new JpaGpsDeviceException(
081: "Failed to load/create spring native extractor",
082: e);
083: }
084: } else if (interfacesAsStrings
085: .contains("org.jboss.ejb3.entity.InjectedEntityManagerFactory")) {
086: try {
087: extractor = (NativeJpaExtractor) ClassUtils
088: .forName(
089: "org.compass.jboss.device.jpa.JBossNativeHibernateJpaExtractor",
090: settings.getClassLoader())
091: .newInstance();
092: } catch (Exception e) {
093: throw new JpaGpsDeviceException(
094: "Failed to load/create JBoss native extractor",
095: e);
096: }
097: }
098: // possible else if ...
099:
100: EntityManagerFactory nativeEmf = emf;
101: if (extractor != null) {
102: nativeEmf = extractor.extractNative(emf);
103: // recursivly call in order to find
104: nativeEmf = extractNativeJpa(nativeEmf, settings);
105: }
106: return nativeEmf;
107: }
108: }
|