001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork;
006:
007: import com.opensymphony.xwork.config.ConfigurationException;
008: import com.opensymphony.xwork.config.ConfigurationManager;
009: import com.opensymphony.xwork.config.entities.ActionConfig;
010: import com.opensymphony.xwork.util.LocalizedTextUtil;
011: import com.opensymphony.util.TextUtils;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import java.io.Serializable;
016: import java.util.Locale;
017: import java.util.Map;
018:
019: /**
020: * The Default ActionProxy implementation
021: *
022: * @author Rainer Hermanns
023: * @author Revised by <a href="mailto:hu_pengfei@yahoo.com.cn">Henry Hu</a>
024: * @version $Revision: 1268 $
025: * @since 2005-8-6
026: */
027: public class DefaultActionProxy implements ActionProxy, Serializable {
028:
029: private static final long serialVersionUID = -7325819968808474533L;
030:
031: private static final Log LOG = LogFactory
032: .getLog(DefaultActionProxy.class);
033:
034: protected ActionConfig config;
035: protected ActionInvocation invocation;
036: protected Map extraContext;
037: protected String actionName;
038: protected String namespace;
039: protected String method;
040: protected boolean executeResult;
041: protected boolean cleanupContext;
042:
043: /**
044: * This constructor is private so the builder methods (create*) should be used to create an DefaultActionProxy.
045: * <p/>
046: * The reason for the builder methods is so that you can use a subclass to create your own DefaultActionProxy instance
047: * (like a RMIActionProxy).
048: */
049: protected DefaultActionProxy(String namespace, String actionName,
050: Map extraContext, boolean executeResult,
051: boolean cleanupContext) throws Exception {
052: this .cleanupContext = cleanupContext;
053: if (LOG.isDebugEnabled()) {
054: LOG.debug("Creating an DefaultActionProxy for namespace "
055: + namespace + " and action name " + actionName);
056: }
057:
058: this .actionName = actionName;
059: this .namespace = namespace;
060: this .executeResult = executeResult;
061: this .extraContext = extraContext;
062:
063: config = ConfigurationManager.getConfiguration()
064: .getRuntimeConfiguration().getActionConfig(namespace,
065: actionName);
066:
067: if (config == null) {
068: String message;
069:
070: if ((namespace != null) && (namespace.trim().length() > 0)) {
071: message = LocalizedTextUtil.findDefaultText(
072: XWorkMessages.MISSING_PACKAGE_ACTION_EXCEPTION,
073: Locale.getDefault(), new String[] { namespace,
074: actionName });
075: } else {
076: message = LocalizedTextUtil.findDefaultText(
077: XWorkMessages.MISSING_ACTION_EXCEPTION, Locale
078: .getDefault(),
079: new String[] { actionName });
080: }
081:
082: throw new ConfigurationException(message);
083: }
084:
085: prepare();
086: }
087:
088: public Object getAction() {
089: return invocation.getAction();
090: }
091:
092: public String getActionName() {
093: return actionName;
094: }
095:
096: public ActionConfig getConfig() {
097: return config;
098: }
099:
100: public void setExecuteResult(boolean executeResult) {
101: this .executeResult = executeResult;
102: }
103:
104: public boolean getExecuteResult() {
105: return executeResult;
106: }
107:
108: public ActionInvocation getInvocation() {
109: return invocation;
110: }
111:
112: public String getNamespace() {
113: return namespace;
114: }
115:
116: public String execute() throws Exception {
117: ActionContext nestedContext = ActionContext.getContext();
118: ActionContext.setContext(invocation.getInvocationContext());
119:
120: String retCode = null;
121:
122: try {
123: retCode = invocation.invoke();
124: } finally {
125: if (cleanupContext) {
126: ActionContext.setContext(nestedContext);
127: }
128: }
129:
130: return retCode;
131: }
132:
133: public String getMethod() {
134: return method;
135: }
136:
137: public void setMethod(String method) {
138: this .method = method;
139: resolveMethod();
140: }
141:
142: private void resolveMethod() {
143: // if the method is set to null, use the one from the configuration
144: // if the one from the configuration is also null, use "execute"
145: if (!TextUtils.stringSet(this .method)) {
146: this .method = config.getMethodName();
147: if (!TextUtils.stringSet(this .method)) {
148: this .method = "execute";
149: }
150: }
151: }
152:
153: protected void prepare() throws Exception {
154: invocation = ActionProxyFactory.getFactory()
155: .createActionInvocation(this, extraContext);
156: resolveMethod();
157: }
158: }
|