01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.core.util;
16:
17: import java.lang.reflect.Method;
18: import org.araneaframework.core.ProxyActionListener;
19: import org.araneaframework.core.ProxyEventListener;
20:
21: /**
22: * Utility class for accessing methods that will be invocation targets of {@link ProxyEventListener} and
23: * {@link ProxyActionListener}.
24: *
25: * @author Taimo Peelo (taimo@araneaframework.org)
26: * @since 1.0.12
27: */
28: public abstract class ProxiedHandlerUtil {
29: private static final Class[] EMTPY_CLASS_ARRAY = new Class[] {};
30:
31: public static final String EVENT_HANDLER_PREFIX = "handleEvent";
32: public static final String ACTION_HANDLER_PREFIX = "handleAction";
33:
34: private ProxiedHandlerUtil() {
35: }
36:
37: public static Method getEventHandler(String eventId,
38: Object eventTarget) throws SecurityException,
39: NoSuchMethodException {
40: return getEventHandler(eventId, eventTarget, EMTPY_CLASS_ARRAY);
41: }
42:
43: public static Method getEventHandler(String eventId,
44: Object eventTarget, Class[] params)
45: throws SecurityException, NoSuchMethodException {
46: return getHandler(EVENT_HANDLER_PREFIX, eventId, eventTarget,
47: params);
48: }
49:
50: public static Method getActionHandler(String actionId,
51: Object eventTarget) throws SecurityException,
52: NoSuchMethodException {
53: return getActionHandler(actionId, eventTarget,
54: EMTPY_CLASS_ARRAY);
55: }
56:
57: public static Method getActionHandler(String eventId,
58: Object eventTarget, Class[] params)
59: throws SecurityException, NoSuchMethodException {
60: return getHandler(ACTION_HANDLER_PREFIX, eventId, eventTarget,
61: params);
62: }
63:
64: private static Method getHandler(String handlerPrefix,
65: String eventId, Object eventTarget, Class[] params)
66: throws SecurityException, NoSuchMethodException {
67: String eventHandlerName = handlerPrefix
68: + eventId.substring(0, 1).toUpperCase()
69: + eventId.substring(1);
70: Method result = null;
71: try {
72: result = eventTarget.getClass().getDeclaredMethod(
73: eventHandlerName, params);
74: } catch (NoSuchMethodException e) {
75: result = eventTarget.getClass().getMethod(eventHandlerName,
76: params);
77: }
78: if (!result.isAccessible())
79: result.setAccessible(true);
80: return result;
81: }
82: }
|