com.evasion.plugin.geoloc.GlassfishV3NativeJpaExtractor Maven / Gradle / Ivy
The newest version!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.plugin.geoloc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.compass.core.CompassException;
import org.compass.gps.device.jpa.JpaGpsDeviceException;
import org.compass.gps.device.jpa.extractor.NativeJpaExtractor;
/**
*
* @author sglon
*/
public class GlassfishV3NativeJpaExtractor implements NativeJpaExtractor {
private static final String EMF_WRAPPER_CLASS = "com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper";
private static final String EM_WRAPPER_CLASS = "com.sun.enterprise.container.common.impl.EntityManagerWrapper";
private Class emfWrapperClass;
private Class emWrapperClass;
private Method emfGetDelegateMethod;
private Method emGetDelegateMethod;
public GlassfishV3NativeJpaExtractor() {
try {
emfWrapperClass = GlassfishV3NativeJpaExtractor.class.getClassLoader().loadClass(EMF_WRAPPER_CLASS);
emfGetDelegateMethod = emfWrapperClass.getDeclaredMethod("getDelegate");
emfGetDelegateMethod.setAccessible(true);
} catch (ClassNotFoundException e) {
throw new CompassException("Failed to find Glassfish EMF wrapper class [" + EMF_WRAPPER_CLASS + "]");
} catch (NoSuchMethodException e) {
throw new CompassException("Failed to find Glassfish getDelegate method within wrapper class [" + EMF_WRAPPER_CLASS + "]");
}
try {
emWrapperClass = GlassfishV3NativeJpaExtractor.class.getClassLoader().loadClass(EM_WRAPPER_CLASS);
emGetDelegateMethod = emWrapperClass.getDeclaredMethod("getDelegate");
emGetDelegateMethod.setAccessible(true);
} catch (ClassNotFoundException e) {
throw new CompassException("Failed to find Glassfish EM wrapper class [" + EM_WRAPPER_CLASS + "]");
} catch (NoSuchMethodException e) {
throw new CompassException("Failed to find Glassfish getDelegate method within wrapper class [" + EM_WRAPPER_CLASS + "]");
}
}
@Override
public EntityManagerFactory extractNative(EntityManagerFactory entityManagerFactory) throws JpaGpsDeviceException {
if (emfWrapperClass == null) {
return entityManagerFactory;
}
if (emfWrapperClass.isAssignableFrom(entityManagerFactory.getClass())) {
try {
EntityManagerFactory emf = (EntityManagerFactory) emfGetDelegateMethod.invoke(entityManagerFactory);
return emf;
} catch (InvocationTargetException e) {
throw new JpaGpsDeviceException("Failed to invoke getDelegate method on [" + entityManagerFactory + "]", e.getTargetException());
} catch (Exception e) {
throw new JpaGpsDeviceException("Failed to invoke getDelegate method on [" + entityManagerFactory + "]", e);
}
}
return entityManagerFactory;
}
@Override
public EntityManager extractNative(EntityManager entityManager) throws JpaGpsDeviceException {
if (emWrapperClass == null) {
return entityManager;
}
if (emWrapperClass.isAssignableFrom(entityManager.getClass())) {
try {
return (EntityManager) emGetDelegateMethod.invoke(entityManager);
} catch (InvocationTargetException e) {
throw new JpaGpsDeviceException("Failed to invoke getDelegate method on [" + entityManager + "]", e.getTargetException());
} catch (Exception e) {
throw new JpaGpsDeviceException("Failed to invoke getDelegate method on [" + entityManager + "]", e);
}
}
return entityManager;
}
}