1 22 package org.jboss.ejb.plugins; 23 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Method ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.List ; 31 32 import javax.ejb.EJBException ; 33 34 import org.jboss.ejb.EntityEnterpriseContext; 35 import org.jboss.ejb.GenericEntityObjectFactory; 36 import org.jboss.ha.framework.interfaces.DistributedState; 37 import org.jboss.metadata.BeanMetaData; 38 import org.jboss.metadata.ClusterConfigMetaData; 39 import org.jboss.metadata.EntityMetaData; 40 41 53 public class CMPClusteredInMemoryPersistenceManager implements org.jboss.ejb.EntityPersistenceStore 54 { 55 57 59 protected org.jboss.ejb.EntityContainer con = null; 60 protected Field idField = null; 61 62 protected DistributedState ds = null; 63 64 protected String DS_CATEGORY = null; 65 66 69 protected Method isModified = null; 70 71 73 75 public CMPClusteredInMemoryPersistenceManager () 76 { 77 } 78 79 84 public void setContainer (org.jboss.ejb.Container con) 85 { 86 this.con = (org.jboss.ejb.EntityContainer)con; 87 } 88 89 92 public void create () throws Exception 93 { 94 BeanMetaData bmd = con.getBeanMetaData(); 95 ClusterConfigMetaData ccmd = bmd.getClusterConfigMetaData (); 96 String partitionName = ccmd.getPartitionName(); 97 String name = "jboss:service=DistributedState,partitionName="+partitionName; 98 ds = (DistributedState)org.jboss.system.Registry.lookup (name); 99 100 String ejbName = bmd.getEjbName(); 101 this.DS_CATEGORY = "CMPClusteredInMemoryPersistenceManager-" + ejbName; 102 103 idField = con.getBeanClass ().getField ("id"); 104 105 try 106 { 107 isModified = con.getBeanClass ().getMethod ("isModified", new Class [0]); 108 if (!isModified.getReturnType ().equals (Boolean.TYPE)) 109 isModified = null; } 111 catch (NoSuchMethodException ignored) 112 { 113 } 114 } 115 116 119 public void start () throws Exception 120 { 121 } 122 123 126 public void stop () 127 { 128 } 129 130 131 134 public void destroy () 135 { 136 } 137 138 140 142 149 public Object createBeanClassInstance () throws Exception 150 { 151 return con.getBeanClass ().newInstance (); 152 } 153 154 162 public void initEntity (EntityEnterpriseContext ctx) 163 { 164 Object instance = ctx.getInstance (); 166 Class ejbClass = instance.getClass (); 167 Field cmpField; 168 Class cmpFieldType; 169 170 EntityMetaData metaData = (EntityMetaData)con.getBeanMetaData (); 171 java.util.Iterator i= metaData.getCMPFields (); 172 173 while(i.hasNext ()) 174 { 175 try 176 { 177 try 179 { 180 cmpField = ejbClass.getField ((String )i.next ()); 181 cmpFieldType = cmpField.getType (); 182 if (cmpFieldType.equals (boolean.class)) 185 { 186 cmpField.setBoolean (instance,false); 187 } 188 else if (cmpFieldType.equals (byte.class)) 189 { 190 cmpField.setByte (instance,(byte)0); 191 } 192 else if (cmpFieldType.equals (int.class)) 193 { 194 cmpField.setInt (instance,0); 195 } 196 else if (cmpFieldType.equals (long.class)) 197 { 198 cmpField.setLong (instance,0L); 199 } 200 else if (cmpFieldType.equals (short.class)) 201 { 202 cmpField.setShort (instance,(short)0); 203 } 204 else if (cmpFieldType.equals (char.class)) 205 { 206 cmpField.setChar (instance,'\u0000'); 207 } 208 else if (cmpFieldType.equals (double.class)) 209 { 210 cmpField.setDouble (instance,0d); 211 } 212 else if (cmpFieldType.equals (float.class)) 213 { 214 cmpField.setFloat (instance,0f); 215 } 216 else 217 { 218 cmpField.set (instance,null); 219 } 220 } 221 catch (NoSuchFieldException e) 222 { 223 } 226 } 227 catch (Exception e) 228 { 229 throw new EJBException (e); 230 } 231 } 232 } 233 234 247 public Object createEntity (Method m, Object [] args, 248 EntityEnterpriseContext ctx) throws Exception 249 { 250 try 251 { 252 253 Object id = idField.get (ctx.getInstance ()); 254 255 if (this.ds.get (DS_CATEGORY, id.toString ()) != null) 257 throw new javax.ejb.DuplicateKeyException ("Already exists:"+id); 258 259 storeEntity (id, ctx.getInstance ()); 261 262 return id; 263 } 264 catch (IllegalAccessException e) 265 { 266 throw new javax.ejb.CreateException ("Could not create entity:"+e); 267 } 268 } 269 270 283 public Object postCreateEntity (Method m, Object [] args, 284 EntityEnterpriseContext ctx) throws Exception 285 { 286 return null; 287 } 288 289 304 public Object findEntity (Method finderMethod, Object [] args, 305 EntityEnterpriseContext instance, 306 GenericEntityObjectFactory factory) throws Exception 307 { 308 if (finderMethod.getName ().equals ("findByPrimaryKey")) 309 { 310 if (this.ds.get (DS_CATEGORY, args[0].toString ()) == null) 311 throw new javax.ejb.FinderException (args[0]+" does not exist"); 312 313 return factory.getEntityEJBObject(args[0]); 314 } 315 else 316 return null; 317 } 318 319 335 public Collection findEntities (Method finderMethod, Object [] args, 336 EntityEnterpriseContext instance, GenericEntityObjectFactory factory) throws Exception 337 { 338 Collection results = Collections.EMPTY_LIST; 339 if (finderMethod.getName ().equals ("findAll")) 340 { 341 Collection tmpColl = this.ds.getAllKeys (DS_CATEGORY); 342 if (tmpColl != null) 343 results = GenericEntityObjectFactory.UTIL.getEntityCollection(factory, tmpColl); 344 } 345 return results; 346 } 347 348 362 public void activateEntity (EntityEnterpriseContext instance) { } 363 364 373 public void loadEntity (EntityEnterpriseContext ctx) 374 { 375 try 376 { 377 byte[] content = (byte[])this.ds.get (this.DS_CATEGORY, ctx.getId ().toString ()); 379 380 if (content == null) 381 throw new javax.ejb.EJBException ("No entry exists (any more?) with this id: " + ctx.getId ()); 382 383 java.io.ObjectInputStream in = new org.jboss.ejb.plugins.CMPClusteredInMemoryPersistenceManager.CMPObjectInputStream ( 384 new java.io.ByteArrayInputStream (content)); 385 386 Object obj = ctx.getInstance (); 387 388 Field [] f = obj.getClass ().getFields (); 389 for (int i = 0; i < f.length; i++) 390 { 391 f[i].set (obj, in.readObject ()); 392 } 393 394 in.close (); 395 396 } 397 catch (javax.ejb.EJBException e) 398 { 399 throw e; 400 } 401 catch (Exception e) 402 { 403 throw new EJBException ("Load failed", e); 404 } 405 } 406 407 414 public boolean isStoreRequired (EntityEnterpriseContext ctx) throws Exception 415 { 416 if(isModified == null) 417 { 418 return true; 419 } 420 421 Object [] args = 422 {}; 423 Boolean modified = (Boolean ) isModified.invoke (ctx.getInstance (), args); 424 return modified.booleanValue (); 425 } 426 427 public boolean isModified (EntityEnterpriseContext ctx) throws Exception 428 { 429 return isStoreRequired(ctx); 430 } 431 432 441 public void storeEntity (EntityEnterpriseContext ctx) throws java.rmi.RemoteException 442 { 443 try 444 { 445 storeEntity (ctx.getId (), ctx.getInstance ()); 446 } 447 catch (Exception e) 448 { 449 throw new java.rmi.RemoteException (e.toString ()); 450 } 451 } 452 453 464 public void passivateEntity (EntityEnterpriseContext instance) 465 { 466 } 468 469 479 public void removeEntity (EntityEnterpriseContext ctx) throws javax.ejb.RemoveException 480 { 481 try 482 { 483 if (this.ds.remove (this.DS_CATEGORY, ctx.getId ().toString (), false) == null) 484 throw new javax.ejb.RemoveException ("Could not remove bean:" + 485 ctx.getId ()); 486 } 487 catch (Exception e) 488 { 489 throw new javax.ejb.RemoveException (e.toString ()); 490 } 491 } 492 493 495 497 499 protected void storeEntity (Object id, Object obj) throws Exception 500 { 501 try 502 { 503 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 505 java.io.ObjectOutputStream out = new org.jboss.ejb.plugins.CMPClusteredInMemoryPersistenceManager.CMPObjectOutputStream (baos); 506 507 Field [] f = obj.getClass ().getFields (); 508 for (int i = 0; i < f.length; i++) 509 { 510 out.writeObject (f[i].get (obj)); 511 } 512 513 out.close (); 514 515 this.ds.set (this.DS_CATEGORY, id.toString (), baos.toByteArray (), false); 516 517 } catch (Exception e) 518 { 519 throw new EJBException ("Store failed", e); 520 } 521 } 522 523 525 527 static class CMPObjectOutputStream extends java.io.ObjectOutputStream 528 { 529 public CMPObjectOutputStream (java.io.OutputStream out) throws IOException 530 { 531 super (out); 532 enableReplaceObject (true); 533 } 534 535 protected Object replaceObject (Object obj) 536 throws IOException 537 { 538 if (obj instanceof javax.ejb.EJBObject ) 539 return ((javax.ejb.EJBObject )obj).getHandle (); 540 541 return obj; 542 } 543 } 544 545 static class CMPObjectInputStream extends java.io.ObjectInputStream 546 { 547 public CMPObjectInputStream (java.io.InputStream in) throws IOException 548 { 549 super (in); 550 enableResolveObject (true); 551 } 552 553 protected Object resolveObject (Object obj) 554 throws IOException 555 { 556 if (obj instanceof javax.ejb.Handle ) 557 return ((javax.ejb.Handle )obj).getEJBObject (); 558 559 return obj; 560 } 561 } 562 563 } 564 | Popular Tags |