001: //========================================================================
002: //$Id: LifeCycleCallback.java 1448 2006-12-29 20:46:57Z janb $
003: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.plus.annotation;
017:
018: import java.lang.reflect.Method;
019: import java.lang.reflect.Modifier;
020:
021: import org.mortbay.util.IntrospectionUtil;
022: import org.mortbay.util.Loader;
023:
024: /**
025: * LifeCycleCallback
026: *
027: *
028: */
029: public abstract class LifeCycleCallback {
030: public static final Object[] __EMPTY_ARGS = new Object[] {};
031: private Method _target;
032: private String _className;
033:
034: public LifeCycleCallback() {
035: }
036:
037: /**
038: * @return the _className
039: */
040: public String getClassName() {
041: return _className;
042: }
043:
044: /**
045: * @param name the _className to set
046: */
047: public void setClassName(String name) {
048: _className = name;
049: }
050:
051: /**
052: * @return the target
053: */
054: public Method getTarget() {
055: return _target;
056: }
057:
058: /**
059: * @param target the target to set
060: */
061: public void setTarget(Method target) {
062: this ._target = target;
063: }
064:
065: public void setTarget(Class clazz, String methodName) {
066: try {
067: Method method = IntrospectionUtil.findMethod(clazz,
068: methodName, null, true, true);
069: validate(clazz, method);
070: _target = method;
071: _className = clazz.getName();
072: } catch (NoSuchMethodException e) {
073: throw new IllegalArgumentException("Method " + methodName
074: + " not found on class " + clazz.getName());
075: }
076: }
077:
078: public void callback(Object instance) throws Exception {
079: if (getTarget() != null) {
080: boolean accessibility = getTarget().isAccessible();
081: getTarget().setAccessible(true);
082: getTarget().invoke(instance, __EMPTY_ARGS);
083: getTarget().setAccessible(accessibility);
084: }
085: }
086:
087: /**
088: * Find a method of the given name either directly in the given
089: * class, or inherited.
090: *
091: * @param pack the package of the class under inspection
092: * @param clazz the class under inspection
093: * @param methodName the method to find
094: * @param checkInheritance false on first entry, true if a superclass is being introspected
095: * @return
096: */
097: public Method findMethod(Package pack, Class clazz,
098: String methodName, boolean checkInheritance) {
099: if (clazz == null)
100: return null;
101:
102: try {
103: Method method = clazz.getDeclaredMethod(methodName, null);
104: if (checkInheritance) {
105: int modifiers = method.getModifiers();
106: if (Modifier.isProtected(modifiers)
107: || Modifier.isPublic(modifiers)
108: || (!Modifier.isPrivate(modifiers) && (pack
109: .equals(clazz.getPackage()))))
110: return method;
111: else
112: return findMethod(clazz.getPackage(), clazz
113: .getSuperclass(), methodName, true);
114: }
115: return method;
116: } catch (NoSuchMethodException e) {
117: return findMethod(clazz.getPackage(),
118: clazz.getSuperclass(), methodName, true);
119: }
120: }
121:
122: public abstract void validate(Class clazz, Method m);
123: }
|