01: package com.bm.utils;
02:
03: import java.lang.reflect.Method;
04: import java.util.Set;
05:
06: import com.bm.ejb3metadata.annotations.metadata.ClassAnnotationMetadata;
07: import com.bm.ejb3metadata.annotations.metadata.MetaDataCache;
08: import com.bm.ejb3metadata.annotations.metadata.MethodAnnotationMetadata;
09: import com.bm.introspectors.IntrospectorFactory;
10:
11: /**
12: * Invokes the life cycle methods.
13: *
14: * @author Daniel Wiese
15: * @since Jul 19, 2007
16: */
17: public class LifeCycleMethodExecuter {
18:
19: /**
20: * Executes the life cycle methods after a object was created.
21: *
22: * @author Daniel Wiese
23: * @since Jul 19, 2007
24: * @param justCreated
25: * invokes life cycle methods.
26: */
27: public void executeLifeCycleMethodsForCreate(Object justCreated) {
28: ClassAnnotationMetadata classMeta = MetaDataCache
29: .getMetaData(justCreated.getClass());
30: if (classMeta != null
31: && (classMeta.isBean() || classMeta.isMdb())) {
32: final Set<MethodAnnotationMetadata> lifeCycleMethods = IntrospectorFactory
33: .createIntrospector(justCreated.getClass())
34: .getLifecycleMethods();
35: for (MethodAnnotationMetadata current : lifeCycleMethods) {
36: if (current.isPostConstruct()
37: || current.isPostActivate()) {
38: if (current.getJMethod().getSignature() != null) {
39: throw new IllegalArgumentException(
40: "The life cycle method ("
41: + current.getJMethod()
42: + ") has arguments");
43: }
44: Method toInvoke = Ejb3Utils
45: .getParameterlessMethodByName(current
46: .getMethodName(), justCreated
47: .getClass());
48: // dont inject lifecycle methods with this name
49: // 'injectGuiceDependencies' This method is reserved for
50: // guice integration
51: if (!toInvoke.getName().equalsIgnoreCase(
52: "injectGuiceDependencies")) {
53: toInvoke.setAccessible(true);
54: try {
55: toInvoke.invoke(justCreated,
56: (Object[]) null);
57: } catch (Exception e) {
58: throw new IllegalArgumentException(
59: "Can't invoke method ("
60: + current.getJMethod()
61: + ")", e);
62: }
63: }
64: }
65: }
66: }
67:
68: }
69: }
|