com.evasion.plugin.geoloc.compass.GlassfishTransactionFactory 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.compass;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.*;
import org.compass.core.CompassException;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.core.config.CompassEnvironment;
import org.compass.core.config.CompassSettings;
import org.compass.core.jndi.NamingHelper;
import org.compass.core.spi.InternalCompassSession;
import org.compass.core.transaction.*;
/**
*
* @author sglon
*/
public class GlassfishTransactionFactory extends JTASyncTransactionFactory {
private static final String DEFAULT_USER_TRANSACTION_NAME = "java:comp/UserTransaction";
private transient Map currentSessionMap = new ConcurrentHashMap();
private InitialContext context;
private String utName;
private UserTransaction userTransaction;
private TransactionManager transactionManager;
public void doConfigure(CompassSettings settings) throws CompassException {
try {
context = NamingHelper.getInitialContext(settings);
} catch (NamingException ne) {
throw new CompassException("Could not obtain initial context", ne);
}
utName = settings.getSetting(CompassEnvironment.Transaction.USER_TRANSACTION);
TransactionManagerLookup lookup = TransactionManagerLookupFactory.getTransactionManagerLookup(settings);
if (lookup != null) {
transactionManager = lookup.getTransactionManager(settings);
if (transactionManager == null) {
throw new CompassException("Failed to find transaction manager");
}
if (utName == null) {
utName = lookup.getUserTransactionName();
}
} else {
throw new CompassException("Must register a transaction manager lookup using the "
+ CompassEnvironment.Transaction.MANAGER_LOOKUP + " property");
}
if (utName == null) {
utName = DEFAULT_USER_TRANSACTION_NAME;
}
boolean cacheUserTransaction = settings.getSettingAsBoolean(CompassEnvironment.Transaction.CACHE_USER_TRANSACTION, true);
if (cacheUserTransaction) {
if (log.isDebugEnabled()) {
log.debug("Caching JTA UserTransaction from Jndi [" + utName + "]");
}
userTransaction = lookupUserTransaction();
}
}
protected boolean isWithinExistingTransaction(InternalCompassSession session) throws CompassException {
try {
return getUserTransaction().getStatus() != Status.STATUS_NO_TRANSACTION;
} catch (java.lang.IllegalStateException ex) {
return true;
} catch (SystemException e) {
throw new CompassException("Failed to get staus on JTA transaciton", e);
}
}
@Override
public CompassSession getTransactionBoundSession() throws CompassException {
UserTransaction ut = getUserTransaction();
Transaction tr = null;
try {
try {
if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) {
return null;
}
} catch (IllegalStateException ex) {
//
}
tr = transactionManager.getTransaction();
return currentSessionMap.get(tr);
} catch (SystemException e) {
throw new TransactionException("Failed to fetch transaction bound session", e);
}
}
protected void doBindSessionToTransaction(CompassTransaction tr, CompassSession session) throws CompassException {
try {
Transaction tx = transactionManager.getTransaction();
currentSessionMap.put(tx, session);
} catch (SystemException e) {
throw new TransactionException("Failed to bind session to transcation", e);
}
}
public void unbindSessionFromTransaction(Transaction tx, CompassSession session) {
((InternalCompassSession) session).unbindTransaction();
currentSessionMap.remove(tx);
}
public TransactionManager getTransactionManager() {
return this.transactionManager;
}
public UserTransaction getUserTransaction() throws TransactionException {
if (userTransaction != null) {
return userTransaction;
}
return lookupUserTransaction();
}
private UserTransaction lookupUserTransaction() throws TransactionException {
if (log.isDebugEnabled()) {
log.debug("Looking for UserTransaction under [" + utName + "]");
}
UserTransaction ut;
try {
ut = (UserTransaction) context.lookup(utName);
ut.getStatus();
} catch (NamingException ne) {
ut = null;
} catch (IllegalStateException e) {
ut = null;
} catch (SystemException e) {
ut = null;
}
if (ut == null) {
if (log.isInfoEnabled()) {
log.info("Failed to locate a UserTransaction under [" + utName + "], creating UserTransactionAdapter in its place");
}
ut = new UserTransactionAdapter(transactionManager);
}
return ut;
}
public InternalCompassTransaction doBeginTransaction(InternalCompassSession session) throws CompassException {
GlassfishSyncTransaction tx = new GlassfishSyncTransaction(getUserTransaction(), commitBeforeCompletion, this);
tx.begin(session, getTransactionManager());
return tx;
}
protected InternalCompassTransaction doContinueTransaction(InternalCompassSession session) throws CompassException {
GlassfishSyncTransaction tx = new GlassfishSyncTransaction(getUserTransaction(), commitBeforeCompletion, this);
tx.join(session);
return tx;
}
}