1 22 package org.jboss.test.testbeancluster.bean; 23 24 import java.rmi.RemoteException ; 25 import java.rmi.dgc.VMID ; 26 import javax.naming.InitialContext ; 27 import javax.naming.Context ; 28 import javax.ejb.CreateException ; 29 import javax.ejb.SessionContext ; 30 import javax.ejb.SessionBean ; 31 import javax.ejb.FinderException ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.test.testbean.interfaces.AComplexPK; 35 import org.jboss.test.testbeancluster.interfaces.EntityPK; 36 import org.jboss.test.testbeancluster.interfaces.EntityPKHome; 37 import org.jboss.test.testbeancluster.interfaces.NodeAnswer; 38 39 43 public class SessionToEntityBean implements SessionBean 44 { 45 private static Logger log = Logger.getLogger(SessionToEntityBean.class); 46 private static VMID nodeID = new VMID (); 47 48 private int accessCount; 49 private AComplexPK theKey; 50 51 public void ejbCreate(AComplexPK key) throws CreateException 52 { 53 log.debug("ejbCreate(AComplexPK) called, nodeID="+nodeID); 54 this.theKey = key; 55 } 56 public void ejbActivate() 57 { 58 log.debug("ejbActivate() called, nodeID="+nodeID); 59 } 60 public void ejbPassivate() 61 { 62 log.debug("ejbPassivate() called, nodeID="+nodeID); 63 } 64 public void ejbRemove() 65 { 66 log.debug("ejbRemove() called, nodeID="+nodeID); 67 try 68 { 69 InitialContext ctx = new InitialContext (); 70 Context enc = (Context ) ctx.lookup("java:comp/env"); 71 EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome"); 72 home.remove(theKey); 73 } 74 catch(Exception e) 75 { 76 log.error("Failed to remove EntityPK", e); 77 } 78 } 79 public void setSessionContext(SessionContext context) 80 { 81 } 82 83 public String createEntity() 84 throws CreateException 85 { 86 String msg = null; 87 EntityPKHome home = null; 88 log.info("Enter createEntity, theKey="+theKey); 89 try 90 { 91 InitialContext ctx = new InitialContext (); 92 Context enc = (Context ) ctx.lookup("java:comp/env"); 93 home = (EntityPKHome) enc.lookup("ejb/EntityPKHome"); 94 EntityPK bean = home.findByPrimaryKey(theKey); 95 msg = "Found EntityPK, bean="+bean; 96 log.info(msg); 97 } 98 catch(FinderException e) 99 { 100 EntityPK bean = home.create(theKey.aBoolean, theKey.anInt, theKey.aLong, 101 theKey.aDouble, theKey.aString); 102 msg = "Created EntityPK, bean="+bean; 103 log.info(msg); 104 } 105 catch(Exception e) 106 { 107 log.error("Failed to create EntityPK", e); 108 throw new CreateException ("Failed to create EntityPK: "+e.getMessage()); 109 } 110 return msg; 111 } 112 public NodeAnswer accessEntity() 113 { 114 accessCount ++; 115 log.debug("Enter accessEntity(), accessCount="+accessCount); 116 int beanCount = 0; 117 try 118 { 119 InitialContext ctx = new InitialContext (); 120 Context enc = (Context ) ctx.lookup("java:comp/env"); 121 EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome"); 122 EntityPK bean = home.findByPrimaryKey(theKey); 123 bean.setOtherField(accessCount); 124 log.debug("Set EntityPK.OtherField to: "+accessCount); 125 beanCount = bean.getOtherField(); 126 } 127 catch(Exception e) 128 { 129 log.debug("failed", e); 130 } 131 log.debug("Exit accessEntity()"); 132 return new NodeAnswer(nodeID, new Integer (beanCount)); 133 } 134 135 public int getAccessCount() 136 { 137 return accessCount; 138 } 139 public NodeAnswer validateAccessCount(int count) 140 throws RemoteException 141 { 142 if( accessCount != count ) 143 throw new RemoteException ("AccessCount: " + accessCount + " != " + count); 144 145 int beanCount = 0; 146 try 147 { 148 InitialContext ctx = new InitialContext (); 149 Context enc = (Context ) ctx.lookup("java:comp/env"); 150 EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome"); 151 EntityPK bean = home.findByPrimaryKey(theKey); 152 beanCount = bean.getOtherField(); 153 if( beanCount != count ) 154 throw new RemoteException ("BeanCount: " + beanCount + " != " + count); 155 } 156 catch(RemoteException e) 157 { 158 log.error("Failed to validate EntityPK", e); 159 throw e; 160 } 161 catch(Exception e) 162 { 163 log.error("Failed to validate EntityPK", e); 164 throw new RemoteException ("Failed to validate EntityPK"); 165 } 166 return new NodeAnswer(nodeID, new Integer (beanCount)); 167 } 168 } 169 | Popular Tags |