KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > AbstractEnhancedType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

29
30 package com.caucho.amber.type;
31
32 import com.caucho.amber.AmberRuntimeException;
33 import com.caucho.amber.entity.Listener;
34 import com.caucho.amber.field.StubMethod;
35 import com.caucho.amber.manager.AmberPersistenceUnit;
36 import com.caucho.bytecode.JClass;
37 import com.caucho.bytecode.JMethod;
38 import com.caucho.util.L10N;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Represents an abstract enhanced type.
46  * Known subclasses: EntityType and ListenerType.
47  */

48 public abstract class AbstractEnhancedType extends Type {
49   private static final Logger JavaDoc log = Logger.getLogger(AbstractEnhancedType.class.getName());
50   private static final L10N L = new L10N(AbstractEnhancedType.class);
51
52   AmberPersistenceUnit _amberPersistenceUnit;
53
54   JClass _beanClass;
55
56   private String JavaDoc _name;
57
58   private String JavaDoc _instanceClassName;
59
60   private ClassLoader JavaDoc _instanceLoader;
61   private Class JavaDoc _instanceClass;
62
63   // XXX: jpa/0u21
64
private boolean _isIdClass;
65
66   private boolean _isEnhanced;
67
68   private volatile boolean _isGenerated;
69
70   private Object JavaDoc _instance;
71
72   private Throwable JavaDoc _exception;
73
74   private ArrayList JavaDoc<StubMethod> _methods = new ArrayList JavaDoc<StubMethod>();
75
76   private ArrayList JavaDoc<JMethod> _postLoadCallbacks
77     = new ArrayList JavaDoc<JMethod>();
78
79   private ArrayList JavaDoc<JMethod> _prePersistCallbacks
80     = new ArrayList JavaDoc<JMethod>();
81
82   private ArrayList JavaDoc<JMethod> _postPersistCallbacks
83     = new ArrayList JavaDoc<JMethod>();
84
85   private ArrayList JavaDoc<JMethod> _preUpdateCallbacks
86     = new ArrayList JavaDoc<JMethod>();
87
88   private ArrayList JavaDoc<JMethod> _postUpdateCallbacks
89     = new ArrayList JavaDoc<JMethod>();
90
91   private ArrayList JavaDoc<JMethod> _preRemoveCallbacks
92     = new ArrayList JavaDoc<JMethod>();
93
94   private ArrayList JavaDoc<JMethod> _postRemoveCallbacks
95     = new ArrayList JavaDoc<JMethod>();
96
97
98   public AbstractEnhancedType(AmberPersistenceUnit amberPersistenceUnit)
99   {
100     _amberPersistenceUnit = amberPersistenceUnit;
101   }
102
103   /**
104    * Returns the persistence unit.
105    */

106   public AmberPersistenceUnit getPersistenceUnit()
107   {
108     return _amberPersistenceUnit;
109   }
110
111   public Throwable JavaDoc getConfigException()
112   {
113     return _exception;
114   }
115
116   public void setConfigException(Throwable JavaDoc e)
117   {
118     if (_exception == null)
119       _exception = e;
120   }
121
122   /**
123    * Sets the bean class.
124    */

125   public void setBeanClass(JClass beanClass)
126   {
127     _beanClass = beanClass;
128
129     if (getName() == null) {
130       String JavaDoc name = beanClass.getName();
131       int p = name.lastIndexOf('.');
132
133       if (p > 0)
134         name = name.substring(p + 1);
135
136       setName(name);
137     }
138   }
139
140   /**
141    * Gets the bean class.
142    */

143   public JClass getBeanClass()
144   {
145     return _beanClass;
146   }
147
148   /**
149    * Sets the name.
150    */

151   public void setName(String JavaDoc name)
152   {
153     _name = name;
154   }
155
156   /**
157    * Gets the name.
158    */

159   public String JavaDoc getName()
160   {
161     return _name;
162   }
163
164   /**
165    * Sets the instance.
166    */

167   public void setInstance(Object JavaDoc instance)
168   {
169     _instance = instance;
170   }
171
172   /**
173    * Gets the instance.
174    */

175   public Object JavaDoc getInstance()
176   {
177     if (_instance == null) {
178       try {
179         _instance = getInstanceClass().newInstance();
180       } catch (Exception JavaDoc e) {
181         throw new RuntimeException JavaDoc(e);
182       }
183     }
184
185     return _instance;
186   }
187
188   /**
189    * Sets the instance class loader
190    */

191   public void setInstanceClassLoader(ClassLoader JavaDoc loader)
192   {
193     _instanceLoader = loader;
194   }
195
196   public boolean isInit()
197   {
198     return _instanceClass != null;
199   }
200
201   /**
202    * Gets the instance class.
203    */

204   abstract public Class JavaDoc getInstanceClass();
205
206   /**
207    * Gets the instance class.
208    */

209   protected Class JavaDoc getInstanceClass(Class JavaDoc validationInterface)
210   {
211     if (_instanceClass == null) {
212       if (getInstanceClassName() == null) {
213         throw new RuntimeException JavaDoc("No instance class:" + this);
214       }
215
216       try {
217         if (isEnhanced()) {
218           ClassLoader JavaDoc loader = getPersistenceUnit().getEnhancedLoader();
219
220           if (log.isLoggable(Level.FINEST))
221             log.finest(L.l("loading bean class `{0}' from `{1}'", getBeanClass().getName(), loader));
222
223           _instanceClass = Class.forName(getBeanClass().getName(), false, loader);
224         }
225         else {
226           ClassLoader JavaDoc loader = _instanceLoader;
227
228           if (loader == null)
229             loader = getPersistenceUnit().getEnhancedLoader();
230
231           if (log.isLoggable(Level.FINEST))
232             log.finest(L.l("loading instance class `{0}' from `{1}'", getInstanceClassName(), loader));
233
234           _instanceClass = Class.forName(getInstanceClassName(), false, loader);
235         }
236       } catch (ClassNotFoundException JavaDoc e) {
237         throw new RuntimeException JavaDoc(e);
238       }
239
240       if (! validationInterface.isAssignableFrom(_instanceClass)) {
241         if (getConfigException() != null)
242           throw new AmberRuntimeException(getConfigException());
243         else if (_amberPersistenceUnit.getConfigException() != null)
244           throw new AmberRuntimeException(_amberPersistenceUnit.getConfigException());
245
246         throw new AmberRuntimeException(L.l("'{0}' with classloader {1} is an illegal instance class",
247                                             _instanceClass.getName(), _instanceClass.getClassLoader()));
248       }
249     }
250
251     return _instanceClass;
252   }
253
254   /**
255    * Sets the instance class name.
256    */

257   public void setInstanceClassName(String JavaDoc className)
258   {
259     _instanceClassName = className;
260   }
261
262   /**
263    * Gets the instance class name.
264    */

265   public String JavaDoc getInstanceClassName()
266   {
267     return _instanceClassName;
268   }
269
270   /**
271    * Sets true if the class is enhanced.
272    */

273   public void setEnhanced(boolean isEnhanced)
274   {
275     _isEnhanced = isEnhanced;
276   }
277
278   /**
279    * Returns true if the class is enhanced.
280    */

281   public boolean isEnhanced()
282   {
283     return _isEnhanced;
284   }
285
286   /**
287    * Sets true if the class is a key class, i.e.,
288    * some entity is annotated with @IdClass(this.class)
289    */

290   public void setIdClass(boolean isIdClass)
291   {
292     // jpa/0u21
293

294     _isIdClass = isIdClass;
295   }
296
297   /**
298    * Returns true if the class is a key class.
299    */

300   public boolean isIdClass()
301   {
302     return _isIdClass;
303   }
304
305   /**
306    * Returns true if generated.
307    */

308   public boolean isGenerated()
309   {
310     return _isGenerated;
311   }
312
313   /**
314    * Set true if generated.
315    */

316   public void setGenerated(boolean isGenerated)
317   {
318     // XXX: ejb/0600 vs ejb/0l00
319
if (isEnhanced())
320       _isGenerated = isGenerated;
321   }
322
323   /**
324    * Adds a stub method
325    */

326   public void addStubMethod(StubMethod method)
327   {
328     _methods.add(method);
329   }
330
331   /**
332    * Returns the methods
333    */

334   public ArrayList JavaDoc<StubMethod> getMethods()
335   {
336     return _methods;
337   }
338
339   /**
340    * Adds a @PostLoad callback.
341    */

342   public void addPostLoadCallback(JMethod callback)
343   {
344     _postLoadCallbacks.add(callback);
345   }
346
347   /**
348    * Gets the post-load callback.
349    */

350   public ArrayList JavaDoc<JMethod> getPostLoadCallbacks()
351   {
352     return _postLoadCallbacks;
353   }
354
355   /**
356    * Adds a pre-persist callback.
357    */

358   public void addPrePersistCallback(JMethod callback)
359   {
360     _prePersistCallbacks.add(callback);
361   }
362
363   /**
364    * Gets the pre-persist callback.
365    */

366   public ArrayList JavaDoc<JMethod> getPrePersistCallbacks()
367   {
368     return _prePersistCallbacks;
369   }
370
371   /**
372    * Adds a post-persist callback.
373    */

374   public void addPostPersistCallback(JMethod callback)
375   {
376     _postPersistCallbacks.add(callback);
377   }
378
379   /**
380    * Gets the post-persist callback.
381    */

382   public ArrayList JavaDoc<JMethod> getPostPersistCallbacks()
383   {
384     return _postPersistCallbacks;
385   }
386
387   /**
388    * Adds a pre-update callback.
389    */

390   public void addPreUpdateCallback(JMethod callback)
391   {
392     _preUpdateCallbacks.add(callback);
393   }
394
395   /**
396    * Gets the pre-update callback.
397    */

398   public ArrayList JavaDoc<JMethod> getPreUpdateCallbacks()
399   {
400     return _preUpdateCallbacks;
401   }
402
403   /**
404    * Adds a post-update callback.
405    */

406   public void addPostUpdateCallback(JMethod callback)
407   {
408     _postUpdateCallbacks.add(callback);
409   }
410
411   /**
412    * Gets the post-update callback.
413    */

414   public ArrayList JavaDoc<JMethod> getPostUpdateCallbacks()
415   {
416     return _postUpdateCallbacks;
417   }
418
419   /**
420    * Adds a pre-remove callback.
421    */

422   public void addPreRemoveCallback(JMethod callback)
423   {
424     _preRemoveCallbacks.add(callback);
425   }
426
427   /**
428    * Gets the pre-remove callback.
429    */

430   public ArrayList JavaDoc<JMethod> getPreRemoveCallbacks()
431   {
432     return _preRemoveCallbacks;
433   }
434
435   /**
436    * Adds a post-remove callback.
437    */

438   public void addPostRemoveCallback(JMethod callback)
439   {
440     _postRemoveCallbacks.add(callback);
441   }
442
443   /**
444    * Gets the post-remove callback.
445    */

446   public ArrayList JavaDoc<JMethod> getPostRemoveCallbacks()
447   {
448     return _postRemoveCallbacks;
449   }
450
451   /**
452    * Gets the callbacks.
453    */

454   public ArrayList JavaDoc<JMethod> getCallbacks(int callbackIndex)
455   {
456     switch (callbackIndex) {
457     case Listener.PRE_PERSIST:
458       return _prePersistCallbacks;
459     case Listener.POST_PERSIST:
460       return _postPersistCallbacks;
461     case Listener.PRE_REMOVE:
462       return _preRemoveCallbacks;
463     case Listener.POST_REMOVE:
464       return _postRemoveCallbacks;
465     case Listener.PRE_UPDATE:
466       return _preUpdateCallbacks;
467     case Listener.POST_UPDATE:
468       return _postUpdateCallbacks;
469     case Listener.POST_LOAD:
470       return _postLoadCallbacks;
471     }
472
473     return null;
474   }
475
476   /**
477    * Adds a callback.
478    */

479   public void addCallback(int callbackIndex,
480                           JMethod callback)
481   {
482     switch (callbackIndex) {
483     case Listener.PRE_PERSIST:
484       _prePersistCallbacks.add(callback);
485       break;
486     case Listener.POST_PERSIST:
487       _postPersistCallbacks.add(callback);
488       break;
489     case Listener.PRE_REMOVE:
490       _preRemoveCallbacks.add(callback);
491       break;
492     case Listener.POST_REMOVE:
493       _postRemoveCallbacks.add(callback);
494       break;
495     case Listener.PRE_UPDATE:
496       _preUpdateCallbacks.add(callback);
497       break;
498     case Listener.POST_UPDATE:
499       _postUpdateCallbacks.add(callback);
500       break;
501     case Listener.POST_LOAD:
502       _postLoadCallbacks.add(callback);
503       break;
504     }
505   }
506
507   /**
508    * Printable version of the listener.
509    */

510   public String JavaDoc toString()
511   {
512     return "AbstractEnhancedType[" + _beanClass.getName() + "]";
513   }
514 }
515
Popular Tags