KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > conf > EntityListenerAnnotationLoader


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.jpa.conf;
22
23 import java.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25
26 import javax.persistence.PostLoad;
27 import javax.persistence.PostPersist;
28 import javax.persistence.PostRemove;
29 import javax.persistence.PostUpdate;
30 import javax.persistence.PrePersist;
31 import javax.persistence.PreRemove;
32 import javax.persistence.PreUpdate;
33
34 import org.apache.cayenne.jpa.map.JpaEntityListener;
35 import org.apache.cayenne.jpa.map.JpaLifecycleCallback;
36
37 /**
38  * Loads annotations from the entity listener class. Only deals with non-entity formats of
39  * annotation methods.
40  * <h3>JPA Spec, 3.4.1:</h3>
41  * <p>
42  * Callback methods defined on an entity class have the following signature: <em>void
43  * <METHOD>()</em>
44  * Callback methods defined on an entity listener class have the following signature:
45  * <em>void <METHOD>(Object)</em> The Object argument is the entity instance for which
46  * the callback method is invoked. It maybe declared as the actual entity type. The
47  * callback methods can have public, private, protected, or package level access, but must
48  * not be static or final.
49  * </p>
50  *
51  * @author Andrus Adamchik
52  */

53 public class EntityListenerAnnotationLoader {
54
55     /**
56      * Returns a listener methods descriptor for the annotated listener, or null if none
57      * of the class methods are properly annotated.
58      */

59     public JpaEntityListener getEntityListener(Class JavaDoc listenerClass) {
60         JpaEntityListener listener = new JpaEntityListener();
61
62         boolean hasAnnotations = false;
63         Method JavaDoc[] methods = listenerClass.getDeclaredMethods();
64         for (int i = 0; i < methods.length; i++) {
65
66             if (isValidListenerMethod(methods[i])) {
67                 if (processAnnotations(methods[i], listener)) {
68                     hasAnnotations = true;
69                 }
70             }
71         }
72
73         if (hasAnnotations) {
74             listener.setClassName(listenerClass.getName());
75             return listener;
76         }
77
78         return null;
79     }
80
81     /**
82      * Checks that the method signature is one of a valid listener method,
83      * <em>void METHOD(Object)</em>.
84      */

85     protected boolean isValidListenerMethod(Method JavaDoc m) {
86         int modifiers = m.getModifiers();
87         if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
88             return false;
89         }
90
91         if (!Void.TYPE.equals(m.getReturnType())) {
92             return false;
93         }
94
95         Class JavaDoc[] params = m.getParameterTypes();
96         if (params.length != 1 || !Object JavaDoc.class.equals(params[0])) {
97             return false;
98         }
99
100         return true;
101     }
102
103     protected boolean processAnnotations(Method JavaDoc method, JpaEntityListener listener) {
104         boolean hasListenerAnnotations = false;
105
106         if (method.isAnnotationPresent(PrePersist.class)) {
107             listener.setPrePersist(new JpaLifecycleCallback(method.getName()));
108             hasListenerAnnotations = true;
109         }
110
111         if (method.isAnnotationPresent(PostPersist.class)) {
112             listener.setPostPersist(new JpaLifecycleCallback(method.getName()));
113             hasListenerAnnotations = true;
114         }
115
116         if (method.isAnnotationPresent(PreRemove.class)) {
117             listener.setPreRemove(new JpaLifecycleCallback(method.getName()));
118             hasListenerAnnotations = true;
119         }
120
121         if (method.isAnnotationPresent(PostRemove.class)) {
122             listener.setPostRemove(new JpaLifecycleCallback(method.getName()));
123             hasListenerAnnotations = true;
124         }
125
126         if (method.isAnnotationPresent(PreUpdate.class)) {
127             listener.setPreUpdate(new JpaLifecycleCallback(method.getName()));
128             hasListenerAnnotations = true;
129         }
130
131         if (method.isAnnotationPresent(PostUpdate.class)) {
132             listener.setPostUpdate(new JpaLifecycleCallback(method.getName()));
133             hasListenerAnnotations = true;
134         }
135
136         if (method.isAnnotationPresent(PostLoad.class)) {
137             listener.setPostLoad(new JpaLifecycleCallback(method.getName()));
138             hasListenerAnnotations = true;
139         }
140
141         return hasListenerAnnotations;
142     }
143 }
144
Popular Tags