1 22 package org.jboss.ejb.plugins; 23 24 25 import java.lang.reflect.Field ; 26 import java.lang.reflect.Method ; 27 28 import java.io.IOException ; 29 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Collection ; 33 import java.util.Collections ; 34 35 import javax.ejb.EJBException ; 36 37 import org.jboss.ejb.Container; 38 import org.jboss.ejb.EntityEnterpriseContext; 39 import org.jboss.ejb.EntityPersistenceStore; 40 import org.jboss.ejb.EntityContainer; 41 import org.jboss.ejb.GenericEntityObjectFactory; 42 43 import org.jboss.metadata.EntityMetaData; 44 45 import org.jboss.system.ServiceMBeanSupport; 46 47 69 public class CMPInMemoryPersistenceManager 70 extends ServiceMBeanSupport 71 implements EntityPersistenceStore 72 { 73 75 protected EntityContainer con; 76 protected HashMap beans; 77 protected Field idField; 78 79 82 protected Method isModified; 83 84 86 public CMPInMemoryPersistenceManager () 87 { 88 } 89 90 97 public void setContainer (final Container con) 98 { 99 this.con = (EntityContainer)con; 100 } 101 102 105 protected void createService() throws Exception 106 { 107 this.beans = new HashMap (1000); 108 109 String ejbName = con.getBeanMetaData ().getEjbName (); 110 111 idField = con.getBeanClass ().getField ("id"); 112 log.debug("Using id field: " + idField); 113 114 try 116 { 117 isModified = con.getBeanClass().getMethod("isModified", new Class [0]); 118 if (!isModified.getReturnType().equals(Boolean.TYPE)) { 119 isModified = null; log.warn("Found isModified method, but return type is not boolean; ignoring"); 121 } 122 else { 123 log.debug("Using isModified method: " + isModified); 124 } 125 } 126 catch (NoSuchMethodException ignored) {} 127 } 128 129 protected void stopService() throws Exception 130 { 131 this.beans.clear(); 132 } 133 134 136 143 public Object createBeanClassInstance () throws Exception 144 { 145 return con.getBeanClass ().newInstance (); 146 } 147 148 156 public void initEntity (EntityEnterpriseContext ctx) 157 { 158 Object instance = ctx.getInstance (); 160 Class ejbClass = instance.getClass (); 161 Field cmpField; 162 Class cmpFieldType; 163 164 EntityMetaData metaData = (EntityMetaData)con.getBeanMetaData (); 165 Iterator i= metaData.getCMPFields (); 166 167 while(i.hasNext ()) 168 { 169 try 171 { 172 cmpField = ejbClass.getField ((String )i.next ()); 173 cmpFieldType = cmpField.getType (); 174 175 if (cmpFieldType.equals (boolean.class)) 178 { 179 cmpField.setBoolean (instance,false); 180 } 181 else if (cmpFieldType.equals (byte.class)) 182 { 183 cmpField.setByte (instance,(byte)0); 184 } 185 else if (cmpFieldType.equals (int.class)) 186 { 187 cmpField.setInt (instance,0); 188 } 189 else if (cmpFieldType.equals (long.class)) 190 { 191 cmpField.setLong (instance,0L); 192 } 193 else if (cmpFieldType.equals (short.class)) 194 { 195 cmpField.setShort (instance,(short)0); 196 } 197 else if (cmpFieldType.equals (char.class)) 198 { 199 cmpField.setChar (instance,'\u0000'); 200 } 201 else if (cmpFieldType.equals (double.class)) 202 { 203 cmpField.setDouble (instance,0d); 204 } 205 else if (cmpFieldType.equals (float.class)) 206 { 207 cmpField.setFloat (instance,0f); 208 } 209 else 210 { 211 cmpField.set (instance,null); 212 } 213 } 214 catch (NoSuchFieldException e) 215 { 216 } 219 catch (Exception e) 220 { 221 throw new EJBException (e); 222 } 223 } 224 } 225 226 239 public Object createEntity (Method m, Object [] args, EntityEnterpriseContext ctx) throws Exception 240 { 241 try 242 { 243 Object id = idField.get (ctx.getInstance ()); 244 245 if (this.beans.containsKey (id)) 247 throw new javax.ejb.DuplicateKeyException ("Already exists: "+id); 248 249 storeEntity (id, ctx.getInstance ()); 251 252 return id; 253 } 254 catch (IllegalAccessException e) 255 { 256 throw new javax.ejb.CreateException ("Could not create entity: "+e); 257 } 258 } 259 260 273 public Object postCreateEntity(final Method m, 274 final Object [] args, 275 final EntityEnterpriseContext ctx) 276 throws Exception 277 { 278 return null; 279 } 280 281 295 public Object findEntity (Method finderMethod, Object [] args, EntityEnterpriseContext instance, GenericEntityObjectFactory factory) 296 throws Exception 297 { 298 if (finderMethod.getName ().equals ("findByPrimaryKey")) 299 { 300 if (!this.beans.containsKey (args[0])) 301 throw new javax.ejb.FinderException (args[0]+" does not exist"); 302 303 return factory.getEntityEJBObject(args[0]); 304 } 305 306 return null; 307 } 308 309 324 public Collection findEntities(final Method finderMethod, 325 final Object [] args, 326 final EntityEnterpriseContext instance, 327 GenericEntityObjectFactory factory) 328 throws Exception 329 { 330 if (finderMethod.getName ().equals ("findAll")) 331 { 332 return GenericEntityObjectFactory.UTIL.getEntityCollection(factory, this.beans.keySet()); 333 } 334 else 335 { 336 return Collections.EMPTY_LIST; 338 } 339 } 340 341 344 public void activateEntity (EntityEnterpriseContext instance) 345 { 346 } 348 349 356 public void loadEntity (EntityEnterpriseContext ctx) 357 { 358 try 359 { 360 362 java.io.ObjectInputStream in = new CMPObjectInputStream 363 (new java.io.ByteArrayInputStream ((byte[])this.beans.get (ctx.getId ()))); 364 365 Object obj = ctx.getInstance (); 366 367 Field [] f = obj.getClass ().getFields (); 368 for (int i = 0; i < f.length; i++) 369 { 370 f[i].set (obj, in.readObject ()); 371 } 372 373 in.close (); 374 } 375 catch (Exception e) 376 { 377 throw new EJBException ("Load failed", e); 378 } 379 } 380 381 388 public boolean isStoreRequired (EntityEnterpriseContext ctx) throws Exception 389 { 390 if(isModified == null) 391 { 392 return true; 393 } 394 395 Boolean modified = (Boolean ) isModified.invoke (ctx.getInstance (), new Object [0]); 396 return modified.booleanValue (); 397 } 398 399 public boolean isModified(EntityEnterpriseContext ctx) throws Exception 400 { 401 return isStoreRequired(ctx); 402 } 403 404 411 public void storeEntity (EntityEnterpriseContext ctx) 412 { 413 storeEntity (ctx.getId (), ctx.getInstance ()); 414 } 415 416 419 public void passivateEntity (EntityEnterpriseContext instance) 420 { 421 } 423 424 433 public void removeEntity (EntityEnterpriseContext ctx) throws javax.ejb.RemoveException 434 { 435 if (this.beans.remove (ctx.getId ()) == null) 436 throw new javax.ejb.RemoveException ("Could not remove bean:" + ctx.getId ()); 437 } 438 439 441 protected void storeEntity (Object id, Object obj) 442 { 443 try 444 { 445 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 447 java.io.ObjectOutputStream out = new CMPObjectOutputStream (baos); 448 449 try { 450 Field [] f = obj.getClass ().getFields (); 451 for (int i = 0; i < f.length; i++) 452 { 453 out.writeObject (f[i].get (obj)); 454 } 455 } 456 finally { 457 out.close(); 458 } 459 460 this.beans.put (id, baos.toByteArray ()); 461 } 462 catch (Exception e) 463 { 464 throw new EJBException ("Store failed", e); 465 } 466 } 467 468 470 static class CMPObjectOutputStream extends java.io.ObjectOutputStream 471 { 472 public CMPObjectOutputStream (java.io.OutputStream out) throws IOException 473 { 474 super (out); 475 enableReplaceObject (true); 476 } 477 478 protected Object replaceObject (Object obj) 479 throws IOException 480 { 481 if (obj instanceof javax.ejb.EJBObject ) 482 return ((javax.ejb.EJBObject )obj).getHandle (); 483 484 return obj; 485 } 486 } 487 488 static class CMPObjectInputStream extends java.io.ObjectInputStream 489 { 490 public CMPObjectInputStream (java.io.InputStream in) throws IOException 491 { 492 super (in); 493 enableResolveObject (true); 494 } 495 496 protected Object resolveObject (Object obj) 497 throws IOException 498 { 499 if (obj instanceof javax.ejb.Handle ) 500 return ((javax.ejb.Handle )obj).getEJBObject (); 501 502 return obj; 503 } 504 } 505 506 } 507 | Popular Tags |