1 43 package org.objectweb.jotm; 44 45 import java.rmi.NoSuchObjectException ; 46 import java.rmi.RemoteException ; 47 48 import javax.naming.Context ; 49 import javax.naming.InitialContext ; 50 import javax.naming.NamingException ; 51 import javax.rmi.PortableRemoteObject ; 52 import javax.transaction.UserTransaction ; 53 54 import org.objectweb.carol.util.configuration.ConfigurationRepository; 55 import org.objectweb.carol.util.configuration.ConfigurationException; 56 import org.objectweb.transaction.jta.TMService; 57 import org.objectweb.transaction.jta.TransactionManager; 58 59 64 public class Jotm implements TMService { 65 66 69 Current current = null; 70 71 74 TransactionRecovery transactionrecovery = null; 75 76 79 private static final String TMFACTORY_NAME = "TMFactory"; 80 81 84 private TransactionFactory tf = null; 85 86 89 private boolean local; 90 91 94 private boolean bound; 95 96 99 private boolean boundFailed = false; 100 101 116 public Jotm(boolean local, boolean bound) throws NamingException { 117 if (TraceTm.jotm.isInfoEnabled()) { 118 TraceTm.jotm.info( 119 "JOTM started with a " 120 + (local ? "local" : "remote") 121 + " transaction factory" 122 + " which is " 123 + (bound ? "" : "not") 124 + " bound."); 125 } 126 127 this.local = local; 128 this.bound = bound; 129 TraceTm.jotm.info("CAROL initialization"); 131 132 try { 133 ConfigurationRepository.init(); 134 }catch (ConfigurationException e) { 135 TraceTm.jotm.error("CAROL initialization failed", e); 136 } 137 138 if (local) { 139 try { 140 tf = new TransactionFactoryImpl(); 141 } catch (RemoteException e) { 142 TraceTm.jotm.error( 145 "Instanciation of TransactionFactory failed", 146 e); 147 } 148 149 if (bound) { 150 Context ictx; 151 try { 152 ictx = new InitialContext (); 153 ictx.rebind(TMFACTORY_NAME, tf); 154 155 if (TraceTm.jotm.isDebugEnabled()) { 156 TraceTm.jotm.debug("TransactionFactory bound with name " + TMFACTORY_NAME); 157 } 158 } catch (NamingException e) { 159 TraceTm.jotm.error("TransactionFactory rebind failed", e); 160 boundFailed = true; 161 throw e; 162 } 163 } 164 } else { 165 Context ictx; 166 167 try { 168 ictx = new InitialContext (); 169 tf = (TransactionFactory) ictx.lookup(TMFACTORY_NAME); 170 } catch (NamingException e) { 171 TraceTm.jotm.error("TransactionFactory lookup failed", e); 172 throw e; 173 } 174 } 175 176 try { 177 current = new Current(tf); 178 } catch (Exception e){ 179 ; 180 } 181 182 } 183 184 187 public TransactionManager getTransactionManager() { 188 if (TraceTm.jotm.isDebugEnabled()) { 189 TraceTm.jotm.debug("TransactionManager=" + current); 190 } 191 return (TransactionManager) current; 192 } 193 194 197 public TransactionRecovery getTransactionRecovery() { 198 transactionrecovery = (TransactionRecovery) Current.getTransactionRecovery(); 199 if (TraceTm.jotm.isDebugEnabled()) { 200 TraceTm.jotm.debug("TransactionRecovery=" + transactionrecovery); 201 } 202 return transactionrecovery; 203 } 204 205 208 public UserTransaction getUserTransaction() { 209 if (TraceTm.jotm.isDebugEnabled()) { 210 TraceTm.jotm.debug("UserTransaction=" + current); 211 } 212 return (UserTransaction ) current; 213 } 214 215 218 public void stop() { 219 TraceTm.jotm.info("stop JOTM"); 220 222 try { 223 current.forget(); 224 } catch (Exception e) { 225 ; 226 } 227 228 try { 229 transactionrecovery.forget(); 230 } catch (Exception e) { 231 ; 232 } 233 234 if (local) { 236 if (bound && !boundFailed) { 237 try { 238 InitialContext ictx = new InitialContext (); 239 ictx.unbind(TMFACTORY_NAME); 240 241 if (TraceTm.jotm.isDebugEnabled()) { 242 TraceTm.jotm.debug("TransactionFactory unbound"); 243 } 244 } catch (Exception e) { 245 TraceTm.jotm.warn( 246 "an exception has prevented the TransactionFactory" 247 + " to be unbound", 248 e); 249 } 250 } 251 252 if (tf != null) { 253 try { 254 PortableRemoteObject.unexportObject(tf); 255 256 if (TraceTm.jotm.isDebugEnabled()) { 257 TraceTm.jotm.debug("TransactionFactory unexported"); 258 } 259 } catch (NoSuchObjectException e) { 260 TraceTm.jotm.warn( 261 "an exception has prevented the TransactionFactory" 262 + " to be unbound", 263 e); 264 } 265 } 266 } 267 tf = null; 268 } 269 } 270 | Popular Tags |