1 22 package org.jboss.test.perf.ejb; 23 import java.rmi.RemoteException ; 24 import java.rmi.ServerException ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import javax.ejb.CreateException ; 28 import javax.ejb.FinderException ; 29 import javax.ejb.RemoveException ; 30 import javax.ejb.SessionContext ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.rmi.PortableRemoteObject ; 35 36 import org.jboss.logging.Logger; 37 38 import org.jboss.test.perf.interfaces.EntityLocalHome; 39 import org.jboss.test.perf.interfaces.EntityLocal; 40 import org.jboss.test.perf.interfaces.EntityPK; 41 42 public class SessionBean implements javax.ejb.SessionBean 43 { 44 private static Logger log = Logger.getLogger(SessionBean.class); 45 private EntityLocalHome entityHome; 46 47 public void setSessionContext(SessionContext context) 48 { 49 } 50 51 public void ejbCreate(String entityName) throws CreateException 52 { 53 try 54 { 55 Context context = new InitialContext (); 56 Object ref = context.lookup("java:comp/env/"+entityName); 57 entityHome = (EntityLocalHome) PortableRemoteObject.narrow(ref, EntityLocalHome.class); 58 } 59 catch(NamingException e) 60 { 61 throw new CreateException ("Cound not resolve name: " + e); 62 } 63 } 64 65 private EntityLocal findByPrimaryKey(int key) throws FinderException 66 { 67 EntityPK primaryKey = new EntityPK(key); 68 return entityHome.findByPrimaryKey(primaryKey); 69 } 70 71 private Collection findInRange(int min, int max) throws FinderException 72 { 73 return entityHome.findInRange(min, max); 74 } 75 76 public void create(int low, int high) 77 throws CreateException 78 { 79 for(int i = low; i < high; i++) 80 { 81 entityHome.create(i, 0); 82 } 83 } 84 85 public void remove(int low, int high) 86 throws RemoveException 87 { 88 if(low + 1 == high) 89 { 90 try 91 { 92 EntityLocal entity = findByPrimaryKey(low); 93 entity.remove(); 94 } 95 catch(FinderException e) 96 { 97 log.error("Failed to find and remove entity", e); 98 throw new RemoveException ("Failed to find and remove entity"); 99 } 100 } 101 else 102 { 103 int count = 0; 106 for (int i = low; i < high; i++) 107 { 108 try 109 { 110 EntityLocal entity = findByPrimaryKey(i); 111 entity.remove(); 112 count++; 113 } 114 catch (Exception e) 115 { 116 } } 120 if( count != (high-low) ) 121 { 122 throw new RemoveException ("Removed "+count+" but should remove:"+(high-low)); 123 } 124 } 125 } 126 127 public void read(int id) throws RemoteException 128 { 129 try 130 { 131 EntityLocal entity = findByPrimaryKey(id); 132 entity.read(); 133 } 134 catch(FinderException e) 135 { 136 throw new ServerException ("findByPrimaryKey failed for id="+id, e); 137 } 138 } 139 140 public void read(int low, int high) throws RemoteException 141 { 142 Collection elements = null; 143 try 144 { 145 elements = findInRange(low, high); 146 } 147 catch(FinderException e) 148 { 149 throw new ServerException ("findInRange failed for low="+low+", high="+high, e); 150 } 151 152 Iterator iter = elements.iterator(); 153 while( iter.hasNext() ) 154 { 155 EntityLocal entity = (EntityLocal) iter.next(); 156 entity.read(); 157 } 158 } 159 160 public void write(int id) throws RemoteException 161 { 162 try 163 { 164 EntityLocal entity = findByPrimaryKey(id); 165 int value = entity.read(); 166 entity.write(value + 1); 167 } 168 catch(FinderException e) 169 { 170 throw new ServerException ("findByPrimaryKey failed for id="+id, e); 171 } 172 } 173 174 public void write(int low, int high) throws RemoteException 175 { 176 Collection elements = null; 177 try 178 { 179 elements = findInRange(low, high); 180 } 181 catch(FinderException e) 182 { 183 throw new ServerException ("findInRange failed for low="+low+", high="+high, e); 184 } 185 186 Iterator iter = elements.iterator(); 187 while( iter.hasNext() ) 188 { 189 EntityLocal entity = (EntityLocal) iter.next(); 190 int value = entity.read(); 191 entity.write(value + 1); 192 } 193 } 194 195 public void ejbRemove() 196 { 197 } 198 199 public void ejbActivate() 200 { 201 } 202 203 public void ejbPassivate() 204 { 205 } 206 207 } 208 209 | Popular Tags |