001: /**
002: * Copyright 2007 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.core;
016:
017: import java.lang.reflect.Method;
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.araneaframework.InputData;
021: import org.araneaframework.OutputData;
022: import org.araneaframework.core.util.ProxiedHandlerUtil;
023:
024: /**
025: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
026: * @author Taimo Peelo (taimo@araneaframework.org)
027: * @since 1.0.11
028: */
029: public class ProxyActionListener implements ActionListener {
030: private static final Log log = LogFactory
031: .getLog(ProxyActionListener.class);
032:
033: protected Object actionTarget;
034:
035: public ProxyActionListener(Object actionTarget) {
036: this .actionTarget = actionTarget;
037: }
038:
039: public void processAction(Object actionId, InputData input,
040: OutputData output) throws Exception {
041: String actionParameter = (String) input.getGlobalData().get(
042: ApplicationWidget.ACTION_PARAMETER_KEY);
043:
044: Method actionHandler;
045: // lets try to find a handle method with an empty argument
046: try {
047: actionHandler = ProxiedHandlerUtil.getActionHandler(
048: (String) actionId, actionTarget);
049:
050: if (log.isDebugEnabled()) {
051: String actionHandlerName = ProxiedHandlerUtil.ACTION_HANDLER_PREFIX
052: + ((String) actionId).substring(0, 1)
053: .toUpperCase()
054: + ((String) actionId).substring(1);
055: log.debug("Calling method '" + actionHandlerName
056: + "()' of class '"
057: + actionTarget.getClass().getName() + "'.");
058: }
059: actionHandler.invoke(actionTarget, new Object[] {});
060:
061: return;
062: } catch (NoSuchMethodException e) {/*OK*/
063: }
064:
065: // lets try to find a method with a String type argument
066: try {
067: actionHandler = ProxiedHandlerUtil.getActionHandler(
068: (String) actionId, actionTarget,
069: new Class[] { String.class });
070:
071: if (log.isDebugEnabled()) {
072: String actionHandlerName = ProxiedHandlerUtil.ACTION_HANDLER_PREFIX
073: + ((String) actionId).substring(0, 1)
074: .toUpperCase()
075: + ((String) actionId).substring(1);
076: log.debug("Calling method '" + actionHandlerName
077: + "(String)' of class '"
078: + actionTarget.getClass().getName() + "'.");
079: }
080: actionHandler.invoke(actionTarget,
081: new Object[] { actionParameter });
082:
083: return;
084: } catch (NoSuchMethodException e) {/*OK*/
085: }
086:
087: if (log.isWarnEnabled()) {
088: StringBuffer logMessage = new StringBuffer()
089: .append("ProxyActionListener")
090: .append(
091: actionTarget instanceof org.araneaframework.Component ? " '"
092: + ((org.araneaframework.Component) actionTarget)
093: .getScope() + "'"
094: : "");
095: logMessage
096: .append(" cannot deliver action as no action listeners were registered for the action id '");
097: logMessage.append(actionId).append("'!").append(
098: Assert.thisToString(actionTarget));
099: log.warn(logMessage);
100: }
101: }
102:
103: }
|