KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > CallbackOnListener


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 package org.apache.cayenne.reflect;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.util.Util;
26
27 /**
28  * Defines a generic callback operation executed via reflection on an arbitrary listener
29  * object. Note that the method must be declared in the class itself. Callback will not
30  * look up the class hierarchy.
31  *
32  * @since 3.0
33  * @author Andrus Adamchik
34  */

35 class CallbackOnListener extends AbstractCallback {
36
37     private Method JavaDoc callbackMethod;
38     private Object JavaDoc listener;
39
40     CallbackOnListener(Object JavaDoc listener, String JavaDoc methodName)
41             throws IllegalArgumentException JavaDoc {
42         this(listener, methodName, Object JavaDoc.class);
43     }
44
45     CallbackOnListener(Object JavaDoc listener, String JavaDoc methodName, Class JavaDoc entityType)
46             throws IllegalArgumentException JavaDoc {
47
48         if (listener == null) {
49             throw new IllegalArgumentException JavaDoc("Null listener");
50         }
51
52         this.callbackMethod = findMethod(listener.getClass(), methodName, entityType);
53         this.listener = listener;
54     }
55
56     public void performCallback(Object JavaDoc entity) {
57         try {
58             callbackMethod.invoke(listener, new Object JavaDoc[] {
59                 entity
60             });
61         }
62         catch (Exception JavaDoc e) {
63             throw new CayenneRuntimeException(
64                     "Error invoking entity listener callback method "
65                             + callbackMethod.getName(),
66                     e);
67         }
68     }
69
70     private Method JavaDoc findMethod(Class JavaDoc objectClass, String JavaDoc methodName, Class JavaDoc entityType)
71             throws IllegalArgumentException JavaDoc {
72
73         Method JavaDoc[] methods = objectClass.getDeclaredMethods();
74         for (int i = 0; i < methods.length; i++) {
75             if (methodName.equals(methods[i].getName())) {
76
77                 // must be non-static, void, with a single arg assinable to entity type
78
// JPA spec also requires it to be non-final, but we don't care
79
int modifiers = methods[i].getModifiers();
80                 Class JavaDoc[] parameters = methods[i].getParameterTypes();
81                 if (!Modifier.isStatic(modifiers)
82                         && Void.TYPE.isAssignableFrom(methods[i].getReturnType())
83                         && parameters.length == 1
84                         && parameters[0].isAssignableFrom(entityType)) {
85
86                     if (!Util.isAccessible(methods[i])) {
87                         methods[i].setAccessible(true);
88                     }
89
90                     return methods[i];
91                 }
92             }
93         }
94
95         throw new IllegalArgumentException JavaDoc("Class "
96                 + objectClass.getName()
97                 + " has no valid listener callback method '"
98                 + methodName
99                 + "'");
100     }
101 }
102
Popular Tags