KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > BMPPersistenceManager


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins;
23
24 import org.jboss.ejb.Container;
25 import org.jboss.ejb.EntityCache;
26 import org.jboss.ejb.EntityContainer;
27 import org.jboss.ejb.EntityEnterpriseContext;
28 import org.jboss.ejb.EntityPersistenceManager;
29 import org.jboss.ejb.AllowedOperationsAssociation;
30 import org.jboss.ejb.GenericEntityObjectFactory;
31 import org.jboss.logging.Logger;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.EntityBean JavaDoc;
36 import javax.ejb.FinderException JavaDoc;
37 import javax.ejb.RemoveException JavaDoc;
38 import java.lang.reflect.InvocationTargetException JavaDoc;
39 import java.lang.reflect.Method JavaDoc;
40 import java.rmi.RemoteException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 /**
48 * Persistence manager for BMP entites. All calls are simply deligated
49 * to the entity implementation class.
50 *
51 * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
52 * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
53 * @author <a HREF="mailto:andreas.schaefer@madplanet.com">Andreas Schaefer</a>
54 * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
55 * @version $Revision: 37459 $
56 */

57 public class BMPPersistenceManager
58    implements EntityPersistenceManager
59 {
60    // Constants -----------------------------------------------------
61
private final static Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
62    // Attributes ----------------------------------------------------
63
Logger log = Logger.getLogger(BMPPersistenceManager.class);
64
65    EntityContainer con;
66
67    Method JavaDoc ejbLoad;
68    Method JavaDoc ejbStore;
69    Method JavaDoc ejbActivate;
70    Method JavaDoc ejbPassivate;
71    Method JavaDoc ejbRemove;
72
73    /**
74     * Optional isModified method used by storeEntity
75     */

76    Method JavaDoc isModified;
77
78    HashMap JavaDoc createMethods = new HashMap JavaDoc();
79    HashMap JavaDoc postCreateMethods = new HashMap JavaDoc();
80    HashMap JavaDoc finderMethods = new HashMap JavaDoc();
81
82    // Static --------------------------------------------------------
83

84    // Constructors --------------------------------------------------
85

86    // Public --------------------------------------------------------
87
public void setContainer(Container c)
88    {
89       con = (EntityContainer)c;
90    }
91
92    public void create()
93    throws Exception JavaDoc
94    {
95       ejbLoad = EntityBean JavaDoc.class.getMethod("ejbLoad", new Class JavaDoc[0]);
96       ejbStore = EntityBean JavaDoc.class.getMethod("ejbStore", new Class JavaDoc[0]);
97       ejbActivate = EntityBean JavaDoc.class.getMethod("ejbActivate", new Class JavaDoc[0]);
98       ejbPassivate = EntityBean JavaDoc.class.getMethod("ejbPassivate", new Class JavaDoc[0]);
99       ejbRemove = EntityBean JavaDoc.class.getMethod("ejbRemove", new Class JavaDoc[0]);
100
101       // Create cache of create methods
102
if (con.getHomeClass() != null)
103       {
104          Method JavaDoc[] methods = con.getHomeClass().getMethods();
105          createMethodCache( methods );
106       }
107       if (con.getLocalHomeClass() != null)
108       {
109          Method JavaDoc[] methods = con.getLocalHomeClass().getMethods();
110          createMethodCache( methods );
111       }
112
113       try
114       {
115          isModified = con.getBeanClass().getMethod("isModified", new Class JavaDoc[0]);
116          if (!isModified.getReturnType().equals(Boolean.TYPE))
117             isModified = null; // Has to have "boolean" as return type!
118
}
119       catch (NoSuchMethodException JavaDoc ignored) {}
120    }
121
122     /**
123     * Returns a new instance of the bean class or a subclass of the bean class.
124     *
125     * @return the new instance
126     */

127     public Object JavaDoc createBeanClassInstance() throws Exception JavaDoc {
128        return con.getBeanClass().newInstance();
129     }
130
131    private void createMethodCache( Method JavaDoc[] methods )
132       throws NoSuchMethodException JavaDoc
133    {
134       for (int i = 0; i < methods.length; i++)
135       {
136          String JavaDoc name = methods[i].getName();
137          if (name.startsWith("create"))
138          {
139             String JavaDoc nameSuffix = name.substring(0, 1).toUpperCase() + name.substring(1);
140             try
141             {
142                createMethods.put(methods[i], con.getBeanClass().getMethod("ejb" + nameSuffix, methods[i].getParameterTypes()));
143             }
144             catch (NoSuchMethodException JavaDoc e)
145             {
146                log.error("Home Method " + methods[i] + " not implemented in bean class " + con.getBeanClass() + " looking for method named: ejb" + nameSuffix);
147
148                throw e;
149             }
150             try
151             {
152                postCreateMethods.put(methods[i], con.getBeanClass().getMethod("ejbPost" + nameSuffix, methods[i].getParameterTypes()));
153             }
154             catch (NoSuchMethodException JavaDoc e)
155             {
156                log.error("Home Method " + methods[i] + " not implemented in bean class " + con.getBeanClass() + " looking for method named: ejbPost" + nameSuffix);
157                throw e;
158             }
159          }
160       }
161
162       // Create cache of finder methods
163
for (int i = 0; i < methods.length; i++)
164       {
165          if (methods[i].getName().startsWith("find"))
166          {
167             try
168             {
169                finderMethods.put(methods[i], con.getBeanClass().getMethod("ejbF" + methods[i].getName().substring(1), methods[i].getParameterTypes()));
170             }
171             catch (NoSuchMethodException JavaDoc e)
172             {
173                log.error("Home Method " + methods[i] + " not implemented in bean class");
174                throw e;
175             }
176          }
177       }
178    }
179
180    public void start()
181    {
182    }
183
184    public void stop()
185    {
186    }
187
188    public void destroy()
189    {
190    }
191
192    public void createEntity(
193          Method JavaDoc m,
194          Object JavaDoc[] args,
195          EntityEnterpriseContext ctx)
196    throws Exception JavaDoc
197    {
198
199       Object JavaDoc id = null;
200       try
201       {
202          // Call ejbCreate<METHOD)
203
AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_CREATE);
204          Method JavaDoc createMethod = (Method JavaDoc)createMethods.get(m);
205          id = createMethod.invoke(ctx.getInstance(), args);
206       } catch (IllegalAccessException JavaDoc e)
207       {
208          // Throw this as a bean exception...(?)
209
throw new EJBException JavaDoc(e);
210       } catch (InvocationTargetException JavaDoc ite)
211       {
212          Throwable JavaDoc e = ite.getTargetException();
213          if (e instanceof CreateException JavaDoc)
214          {
215             // Rethrow exception
216
throw (CreateException JavaDoc)e;
217          }
218          else if (e instanceof RemoteException JavaDoc)
219          {
220             // Rethrow exception
221
throw (RemoteException JavaDoc)e;
222          }
223          else if (e instanceof EJBException JavaDoc)
224          {
225             // Rethrow exception
226
throw (EJBException JavaDoc)e;
227          }
228          else if (e instanceof RuntimeException JavaDoc)
229          {
230             // Wrap runtime exceptions
231
throw new EJBException JavaDoc((Exception JavaDoc)e);
232          }
233          else if(e instanceof Exception JavaDoc)
234          {
235             throw (Exception JavaDoc)e;
236          }
237          else
238          {
239             throw (Error JavaDoc)e;
240          }
241       }
242       finally{
243          AllowedOperationsAssociation.popInMethodFlag();
244       }
245
246       // set the id
247
ctx.setId(id);
248
249       // Create a new CacheKey
250
Object JavaDoc cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey( id );
251
252       // Give it to the context
253
ctx.setCacheKey(cacheKey);
254    }
255
256    public void postCreateEntity(
257          Method JavaDoc m,
258          Object JavaDoc[] args,
259          EntityEnterpriseContext ctx)
260    throws Exception JavaDoc
261    {
262       try
263       {
264          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_POST_CREATE);
265          Method JavaDoc postCreateMethod = (Method JavaDoc)postCreateMethods.get(m);
266          postCreateMethod.invoke(ctx.getInstance(), args);
267       } catch (IllegalAccessException JavaDoc e)
268       {
269          // Throw this as a bean exception...(?)
270
throw new EJBException JavaDoc(e);
271       } catch (InvocationTargetException JavaDoc ite)
272       {
273          Throwable JavaDoc e = ite.getTargetException();
274          if (e instanceof CreateException JavaDoc)
275          {
276             // Rethrow exception
277
throw (CreateException JavaDoc)e;
278          }
279          else if (e instanceof RemoteException JavaDoc)
280          {
281             // Rethrow exception
282
throw (RemoteException JavaDoc)e;
283          }
284          else if (e instanceof EJBException JavaDoc)
285          {
286             // Rethrow exception
287
throw (EJBException JavaDoc)e;
288          }
289          else if (e instanceof RuntimeException JavaDoc)
290          {
291             // Wrap runtime exceptions
292
throw new EJBException JavaDoc((Exception JavaDoc)e);
293          }
294          else if(e instanceof Exception JavaDoc)
295          {
296             throw (Exception JavaDoc)e;
297          }
298          else
299          {
300             throw (Error JavaDoc)e;
301          }
302       }
303       finally{
304          AllowedOperationsAssociation.popInMethodFlag();
305       }
306    }
307
308    public Object JavaDoc findEntity(Method JavaDoc finderMethod, Object JavaDoc[] args, EntityEnterpriseContext ctx, GenericEntityObjectFactory factory)
309    throws Exception JavaDoc
310    {
311       try
312       {
313          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_FIND);
314
315          // call the finder method
316
Object JavaDoc objectId = callFinderMethod(finderMethod, args, ctx);
317
318          final Object JavaDoc cacheKey = ((EntityCache)con.getInstanceCache()).createCacheKey( objectId );
319          return factory.getEntityEJBObject(cacheKey);
320       }
321       finally
322       {
323          AllowedOperationsAssociation.popInMethodFlag();
324       }
325    }
326
327    public Collection JavaDoc findEntities(Method JavaDoc finderMethod, Object JavaDoc[] args, EntityEnterpriseContext ctx, GenericEntityObjectFactory factory)
328    throws Exception JavaDoc
329    {
330       // call the finder method
331
Object JavaDoc result;
332       try
333       {
334          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_FIND);
335          result = callFinderMethod(finderMethod, args, ctx);
336       }
337       finally
338       {
339          AllowedOperationsAssociation.popInMethodFlag();
340       }
341
342       if (result == null)
343       {
344          // for EJB 1.0 compliance
345
// if the bean couldn't find any matching entities
346
// it returns null, so we return an empty collection
347
return new ArrayList JavaDoc();
348       }
349
350       if (result instanceof java.util.Enumeration JavaDoc)
351       {
352          // to preserve 1.0 spec compatiblity
353
ArrayList JavaDoc array = new ArrayList JavaDoc();
354          Enumeration JavaDoc e = (Enumeration JavaDoc) result;
355          while (e.hasMoreElements() == true)
356          {
357             // Wrap a cache key around the given object id/primary key
358
final Object JavaDoc cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey(e.nextElement());
359             Object JavaDoc o = factory.getEntityEJBObject(cacheKey);
360             array.add(o);
361          }
362          return array;
363       }
364       else if (result instanceof java.util.Collection JavaDoc)
365       {
366
367          ArrayList JavaDoc array = new ArrayList JavaDoc(((Collection JavaDoc) result).size());
368          Iterator JavaDoc i = ((Collection JavaDoc) result).iterator();
369          while (i.hasNext())
370          {
371             // Wrap a cache key around the given object id/primary key
372
final Object JavaDoc cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey(i.next());
373             Object JavaDoc o = factory.getEntityEJBObject(cacheKey);
374             array.add(o);
375          }
376          return array;
377       }
378       else
379       {
380          // so we received something that's not valid
381
// throw an exception reporting it
382
throw new EJBException JavaDoc("result of finder method is not a valid " +
383                "return type: " + result.getClass());
384       }
385    }
386
387    public void activateEntity(EntityEnterpriseContext ctx)
388       throws RemoteException JavaDoc
389    {
390       // Create a new CacheKey
391
Object JavaDoc id = ctx.getId();
392       Object JavaDoc cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey( id );
393
394       // Give it to the context
395
ctx.setCacheKey(cacheKey);
396
397       try
398       {
399          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_ACTIVATE);
400          ejbActivate.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY);
401       } catch (IllegalAccessException JavaDoc e)
402       {
403          // Throw this as a bean exception...(?)
404
throw new EJBException JavaDoc(e);
405       } catch (InvocationTargetException JavaDoc ite)
406       {
407          Throwable JavaDoc e = ite.getTargetException();
408          if (e instanceof RemoteException JavaDoc)
409          {
410             // Rethrow exception
411
throw (RemoteException JavaDoc)e;
412          }
413          else if (e instanceof EJBException JavaDoc)
414          {
415             // Rethrow exception
416
throw (EJBException JavaDoc)e;
417          }
418          else if (e instanceof RuntimeException JavaDoc)
419          {
420             // Wrap runtime exceptions
421
throw new EJBException JavaDoc((Exception JavaDoc)e);
422          }
423       }
424       finally{
425          AllowedOperationsAssociation.popInMethodFlag();
426       }
427    }
428
429    public void loadEntity(EntityEnterpriseContext ctx)
430    throws RemoteException JavaDoc
431    {
432       try
433       {
434          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_LOAD);
435          ejbLoad.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY);
436       } catch (IllegalAccessException JavaDoc e)
437       {
438          // Throw this as a bean exception...(?)
439
throw new EJBException JavaDoc(e);
440       } catch (InvocationTargetException JavaDoc ite)
441       {
442          Throwable JavaDoc e = ite.getTargetException();
443          if (e instanceof RemoteException JavaDoc)
444          {
445             // Rethrow exception
446
throw (RemoteException JavaDoc)e;
447          }
448          else if (e instanceof EJBException JavaDoc)
449          {
450             // Rethrow exception
451
throw (EJBException JavaDoc)e;
452          }
453          else if (e instanceof RuntimeException JavaDoc)
454          {
455             // Wrap runtime exceptions
456
throw new EJBException JavaDoc((Exception JavaDoc)e);
457          }
458       }
459       finally{
460          AllowedOperationsAssociation.popInMethodFlag();
461       }
462    }
463
464    public boolean isStoreRequired(EntityEnterpriseContext ctx) throws Exception JavaDoc
465    {
466       if(isModified == null)
467       {
468          return true;
469       }
470
471       Boolean JavaDoc modified = (Boolean JavaDoc) isModified.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY);
472       return modified.booleanValue();
473    }
474
475    public boolean isModified(EntityEnterpriseContext ctx) throws Exception JavaDoc
476    {
477       return isStoreRequired(ctx);
478    }
479
480    public void invokeEjbStore(EntityEnterpriseContext ctx)
481       throws RemoteException JavaDoc
482    {
483       try
484       {
485          if(!isStoreRequired(ctx))
486          {
487             return;
488          }
489       }
490       catch(Exception JavaDoc e)
491       {
492          throw new EJBException JavaDoc("Failed to invoke isModified().", e);
493       }
494
495       try
496       {
497          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_STORE);
498          ejbStore.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY);
499       } catch (IllegalAccessException JavaDoc e)
500       {
501          // Throw this as a bean exception...(?)
502
throw new EJBException JavaDoc(e);
503       } catch (InvocationTargetException JavaDoc ite)
504       {
505          Throwable JavaDoc e = ite.getTargetException();
506          if (e instanceof RemoteException JavaDoc)
507          {
508             // Rethrow exception
509
throw (RemoteException JavaDoc)e;
510          }
511          else if (e instanceof EJBException JavaDoc)
512          {
513             // Rethrow exception
514
throw (EJBException JavaDoc)e;
515          }
516          else if (e instanceof RuntimeException JavaDoc)
517          {
518             // Wrap runtime exceptions
519
throw new EJBException JavaDoc((Exception JavaDoc)e);
520          }
521       }
522       finally{
523          AllowedOperationsAssociation.popInMethodFlag();
524       }
525    }
526
527    public void storeEntity(EntityEnterpriseContext ctx)
528       throws RemoteException JavaDoc
529    {
530    }
531
532    public void passivateEntity(EntityEnterpriseContext ctx)
533    throws RemoteException JavaDoc
534    {
535       try
536       {
537          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_PASSIVATE);
538          ejbPassivate.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY);
539       } catch (IllegalAccessException JavaDoc e)
540       {
541          // Throw this as a bean exception...(?)
542
throw new EJBException JavaDoc(e);
543       } catch (InvocationTargetException JavaDoc ite)
544       {
545          Throwable JavaDoc e = ite.getTargetException();
546          if (e instanceof RemoteException JavaDoc)
547          {
548             // Rethrow exception
549
throw (RemoteException JavaDoc)e;
550          }
551          else if (e instanceof EJBException JavaDoc)
552          {
553             // Rethrow exception
554
throw (EJBException JavaDoc)e;
555          }
556          else if (e instanceof RuntimeException JavaDoc)
557          {
558             // Wrap runtime exceptions
559
throw new EJBException JavaDoc((Exception JavaDoc)e);
560          }
561       }
562       finally{
563          AllowedOperationsAssociation.popInMethodFlag();
564       }
565       ctx.setEJBObject(null);
566       ctx.setEJBLocalObject(null);
567    }
568
569    public void removeEntity(EntityEnterpriseContext ctx)
570    throws RemoteException JavaDoc, RemoveException JavaDoc
571    {
572       try
573       {
574          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_REMOVE);
575          ejbRemove.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY);
576       } catch (IllegalAccessException JavaDoc e)
577       {
578          // Throw this as a bean exception...(?)
579
throw new EJBException JavaDoc(e);
580       } catch (InvocationTargetException JavaDoc ite)
581       {
582          Throwable JavaDoc e = ite.getTargetException();
583          if (e instanceof RemoveException JavaDoc)
584          {
585             // Rethrow exception
586
throw (RemoveException JavaDoc)e;
587          }
588          else if (e instanceof RemoteException JavaDoc)
589          {
590             // Rethrow exception
591
throw (RemoteException JavaDoc)e;
592          }
593          else if (e instanceof EJBException JavaDoc)
594          {
595             // Rethrow exception
596
throw (EJBException JavaDoc)e;
597          }
598          else if (e instanceof RuntimeException JavaDoc)
599          {
600             // Wrap runtime exceptions
601
throw new EJBException JavaDoc((Exception JavaDoc)e);
602          }
603       }
604       finally{
605          AllowedOperationsAssociation.popInMethodFlag();
606       }
607    }
608
609    // Z implementation ----------------------------------------------
610

611    // Package protected ---------------------------------------------
612

613    // Protected -----------------------------------------------------
614

615    // Private -------------------------------------------------------
616
private Object JavaDoc callFinderMethod(Method JavaDoc finderMethod, Object JavaDoc[] args, EntityEnterpriseContext ctx)
617    throws Exception JavaDoc
618    {
619       // get the finder method
620
Method JavaDoc callMethod = (Method JavaDoc)finderMethods.get(finderMethod);
621
622       if (callMethod == null)
623       {
624          throw new EJBException JavaDoc("couldn't find finder method in bean class. " +
625                finderMethod.toString());
626       }
627
628       // invoke the finder method
629
Object JavaDoc result = null;
630       try
631       {
632          result = callMethod.invoke(ctx.getInstance(), args);
633       } catch (IllegalAccessException JavaDoc e)
634         {
635             // Throw this as a bean exception...(?)
636
throw new EJBException JavaDoc(e);
637       } catch (InvocationTargetException JavaDoc ite)
638         {
639             Throwable JavaDoc e = ite.getTargetException();
640          if (e instanceof FinderException JavaDoc)
641          {
642             // Rethrow exception
643
throw (FinderException JavaDoc)e;
644          }
645          else if (e instanceof RemoteException JavaDoc)
646          {
647             // Rethrow exception
648
throw (RemoteException JavaDoc)e;
649          }
650          else if (e instanceof EJBException JavaDoc)
651          {
652             // Rethrow exception
653
throw (EJBException JavaDoc)e;
654          }
655          else if (e instanceof RuntimeException JavaDoc)
656          {
657             // Wrap runtime exceptions
658
throw new EJBException JavaDoc((Exception JavaDoc)e);
659          }
660          else if(e instanceof Exception JavaDoc)
661          {
662             throw (Exception JavaDoc)e;
663          }
664          else
665          {
666             throw (Error JavaDoc)e;
667          }
668       }
669
670       return result;
671    }
672
673
674    // Inner classes -------------------------------------------------
675
}
676
677
678
679
680
Popular Tags