1 55 package org.lateralnz.panther.wrapper; 56 57 import java.io.Serializable ; 58 import java.rmi.RemoteException ; 59 import java.rmi.server.UnicastRemoteObject ; 60 import javax.ejb.EJBHome ; 61 import javax.ejb.EJBObject ; 62 import javax.ejb.Handle ; 63 import javax.ejb.RemoveException ; 64 import javax.ejb.SessionContext ; 65 import javax.transaction.Status ; 66 import javax.transaction.TransactionManager ; 67 68 import org.apache.log4j.Logger; 69 70 import org.lateralnz.common.util.Constants; 71 import org.lateralnz.common.util.JNDIUtils; 72 import org.lateralnz.common.util.StringUtils; 73 import org.lateralnz.common.util.TransUtils; 74 import org.lateralnz.common.util.StackThreadLocal; 75 76 86 public abstract class SessionBeanWrapper extends UnicastRemoteObject implements Serializable , Handle , Constants { 87 private static final Logger log = Logger.getLogger(SessionBeanWrapper.class.getName()); 88 89 private static TransactionManager tm = null; 90 protected SessionContext ctx; 91 92 protected SessionBeanWrapper() throws RemoteException { 93 super(); 94 95 if (tm == null) { 96 synchronized (SessionBeanWrapper.class) { 97 if (tm == null) { 98 try { 99 tm = (TransactionManager )JNDIUtils.get("java:/TransactionManager"); 100 } 101 catch (Exception e) { 102 log.error(e); 103 } 104 } 105 } 106 } 107 } 108 109 protected boolean beginTrans(String transactionType) { 110 try { 111 if (log.isDebugEnabled()) { 112 log.debug("transaction manager status: " + TransUtils.getStatus(tm)); 113 } 114 if (tm.getStatus() != Status.STATUS_ACTIVE && !transactionType.equalsIgnoreCase(SUPPORTS)) { 115 tm.begin(); 116 return true; 117 } 118 } 119 catch (Exception e) { 120 e.printStackTrace(); 122 } 123 return false; 124 } 125 126 protected void checkTransEnd(boolean transStartedByCaller) { 127 try { 128 if (StringUtils.isEmpty((String )StackThreadLocal.peek()) || transStartedByCaller) { 133 int status = tm.getStatus(); 134 if (log.isDebugEnabled()) { 135 log.debug("status: " + TransUtils.getStatus(tm)); 136 } 137 138 if (status == Status.STATUS_MARKED_ROLLBACK) { 139 if (log.isDebugEnabled()) { 140 log.debug("rolling back transaction"); 141 } 142 tm.rollback(); 143 } 144 else { 145 if (log.isDebugEnabled()) { 146 log.debug("committing transaction"); 147 } 148 tm.commit(); 149 } 150 } 151 else if (log.isDebugEnabled()) { 152 log.debug("not at end of transaction chain"); 153 } 154 } 155 catch (Exception e) { 156 e.printStackTrace(); 159 } 160 } 161 162 public EJBHome getEJBHome() throws RemoteException { 163 return ctx.getEJBHome(); 164 } 165 166 public void remove() throws RemoteException , RemoveException { 167 ctx.getEJBHome().remove(this.getHandle()); 168 } 169 170 public boolean isIdentical(EJBObject obj) throws RemoteException { 171 if (obj == null) { 172 return false; 173 } 174 else if (getEJBHome().getEJBMetaData().isStatelessSession()) { 175 return this.getClass().equals(obj.getClass()); 176 } 177 else { 178 return this.equals(obj); 179 } 180 } 181 182 public Handle getHandle() throws RemoteException { 183 return this; 184 } 185 186 public Object getPrimaryKey() throws RemoteException { 187 throw new RemoteException ("primary key method not supported for session beans"); 188 } 189 190 }
| Popular Tags
|