/**
*
* Title: TransactionHelper
* Description: Helps to get handle to a Transaction. Created using standard Jini lookup.
* This class supplays an interface to get a transaction manager service
* (Started before running the application using "startJiniTXNMngr.bat/sh")
* and a transaction proxy connected to it.
*
*/
package com.gigaspaces.examples.tutorials.plainjavaspaces;
import net.jini.core.transaction.Transaction;
import net.jini.core.transaction.TransactionFactory;
import net.jini.core.transaction.server.TransactionManager;
import java.rmi.RMISecurityManager;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.entry.Entry;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.lookup.entry.Name;
import java.net.*;
import java.io.*;
import java.rmi.*;
public class TransactionHelper {
private static TransactionHelper me = null;
private TransactionManager trManager=null;
public TransactionHelper() {
}
public static TransactionHelper getInstance(){
if(me==null){
me = new TransactionHelper();
}
return me;
}
/**
* getJiniTransaction
* Returns a transaction manager proxy.
*
* @param timeout - The length of time our transaction should live before timing out.
* @return Transaction.Created
* @throws Exception
*/
public Transaction.Created getJiniTransaction(long timeout) throws Exception {
if (null == trManager) {
trManager = findTransactionManager();
}
Transaction.Created tCreated = TransactionFactory.create(trManager,timeout);
return tCreated;
}
private TransactionManager findTransactionManager(){
if ( System.getSecurityManager() == null ){
System.setSecurityManager(new RMISecurityManager());
}
Class [] classes = new Class[]{net.jini.core.transaction.server.TransactionManager.class};
Name sn = new Name( "*" );
ServiceTemplate tmpl = new ServiceTemplate(null,classes,
new Entry[] { } );
LookupLocator locator = null;
try {
locator = new LookupLocator("jini:);
} catch (MalformedURLException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
ServiceRegistrar sr = null;
try {
sr = locator.getRegistrar();
} catch (ClassNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
TransactionManager tm = null;
try {
tm = (TransactionManager) sr.lookup(tmpl);
} catch (RemoteException ex2) {
ex2.printStackTrace();
}
return tm;
}
}