Author: pbenedict
Date: Wed Nov 26 07:19:38 2008
New Revision: 720898
URL: http://svn.apache.org/viewvc?rev=720898&view=rev
Log:
STR-3168: Use package message resource bundle
Added:
struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/
struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/LocalStrings.properties
(with props)
Modified:
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractDispatcher.java
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractMappingDispatcher.java
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletMappingDispatcher.java
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletParameterDispatcher.java
Modified:
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractDispatcher.java
URL:
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractDispatcher.java?rev=720898&r1=720897&r2=720898&view=diff
==============================================================================
---
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractDispatcher.java
(original)
+++
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractDispatcher.java
Wed Nov 26 07:19:38 2008
@@ -20,8 +20,6 @@
*/
package org.apache.struts.dispatcher;
-import org.apache.struts.Globals;
-import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.chain.contexts.ActionContext;
@@ -47,15 +45,13 @@
*/
public abstract class AbstractDispatcher implements Dispatcher {
- // Package constants
- static final String KEY_DISPATCH_ERROR = "dispatch.error";
- static final String KEY_MISSING_METHOD = "dispatch.method.user";
- static final String KEY_MISSING_HANDLER_PROPERTY = "dispatch.handler";
- static final String KEY_WRONG_RETURN_TYPE = "dispatch.return";
- static final String KEY_MISSING_NAMED_METHOD = "dispatch.method";
- static final String KEY_MISSING_PARAMETER = "dispatch.parameter";
- static final String KEY_RECURSIVE_DISPATCH = "dispatch.recursive";
- static final String LOCAL_STRINGS =
"org.apache.struts.actions.LocalStrings";
+ // Package message bundle keys
+ static final String LOCAL_STRINGS =
"org.apache.struts.dispatcher.LocalStrings";
+ static final String MSG_KEY_DISPATCH_ERROR = "dispatcher.error";
+ static final String MSG_KEY_MISSING_METHOD = "dispatcher.missingMethod";
+ static final String MSG_KEY_MISSING_METHOD_LOG =
"dispatcher.missingMethod.log";
+ static final String MSG_KEY_MISSING_MAPPING_PARAMETER =
"dispatcher.missingMappingParameter";
+ static final String MSG_KEY_UNSPECIFIED = "dispatcher.unspecified";
/**
* The name of the <code>cancelled</code> method.
@@ -78,7 +74,7 @@
static MessageResources messages =
MessageResources.getMessageResources(LOCAL_STRINGS);
/**
- * Commons Logging instance.
+ * Shared commons Logging instance among subclasses.
*/
protected final Log log;
@@ -91,10 +87,7 @@
private final HashMap methods;
/**
- * Construct an instance of this class from the supplied parameters.
- *
- * @param actionInstance the action instance to be invoked
- * @see #ActionDispatcher(Action, int)
+ * Construct a new dispatcher.
*/
public AbstractDispatcher() {
log = LogFactory.getLog(getClass());
@@ -116,23 +109,27 @@
}
}
- // Ensure there is a valid method name to call.
- // This may be null if the user hacks the query string.
String methodName = resolveMethodName(context);
+ if ((methodName == null) || "".equals(methodName)) {
+ methodName = getDefaultMethodName();
+ }
+
+ // Ensure there is a specified method name to invoke.
+ // This may be null if the user hacks the query string.
if (methodName == null) {
return unspecified(context);
}
- // Identify the method object to be dispatched to
+ // Identify the method object to dispatch
Method method;
try {
method = getMethod(context, methodName);
} catch (NoSuchMethodException e) {
String path = context.getActionConfig().getPath();
- String message = messages.getMessage(KEY_MISSING_NAMED_METHOD,
path, methodName);
+ String message = messages.getMessage(MSG_KEY_MISSING_METHOD_LOG,
path, methodName);
log.error(message, e);
- String userMsg = messages.getMessage(KEY_MISSING_METHOD, path);
+ String userMsg = messages.getMessage(MSG_KEY_MISSING_METHOD, path);
NoSuchMethodException e2 = new NoSuchMethodException(userMsg);
e2.initCause(e);
throw e2;
@@ -160,31 +157,44 @@
*/
protected abstract Object dispatchMethod(ActionContext context, Method
method, String name) throws Exception;
+ protected final void flushMethodCache() {
+ synchronized (methods) {
+ methods.clear();
+ }
+ }
+
/**
- * Retrieves the name of the method to fallback upon if no value can be
- * obtained from the parameter. The default implementation returns
+ * Retrieves the name of the method to fallback upon if no method name can
+ * be resolved. The default implementation returns
* [EMAIL PROTECTED] #UNSPECIFIED_METHOD_NAME}.
*
* @return the fallback method name; can be <code>null</code>
+ * @see #resolveMethodName(ActionContext)
* @see #UNSPECIFIED_METHOD_NAME
*/
- protected String getFallbackMethodName() {
+ protected String getDefaultMethodName() {
return UNSPECIFIED_METHOD_NAME;
}
/**
- * Introspect the action to identify a method of the specified name that
- * will receive the invocation of this dispatch. This implementation caches
- * the method instance for subsequent invocations.
+ * Introspects the action to identify a method of the specified name that
+ * will be the target of the dispatch. This implementation caches the
method
+ * instance for subsequent invocations.
*
* @param methodName the name of the method to be introspected
* @return the method of the specified name
* @throws NoSuchMethodException if no such method can be found
* @see #resolveMethod(ActionContext, String)
+ * @see #flushMethodCache()
*/
protected final Method getMethod(ActionContext context, String methodName)
throws NoSuchMethodException {
synchronized (methods) {
- String key = context.getAction().getClass().getName() + ":" +
methodName;
+ StringBuffer keyBuf = new StringBuffer(100);
+ keyBuf.append(context.getAction().getClass().getName());
+ keyBuf.append(":");
+ keyBuf.append(methodName);
+ String key = keyBuf.toString();
+
Method method = (Method) methods.get(key);
if (method == null) {
@@ -201,8 +211,8 @@
try {
return method.invoke(target, args);
} catch (IllegalAccessException e) {
- String message = messages.getMessage(KEY_DISPATCH_ERROR, path,
name);
- log.error(message, e);
+ String message = messages.getMessage(MSG_KEY_DISPATCH_ERROR, path);
+ log.error(message + ":" + name, e);
throw e;
} catch (InvocationTargetException e) {
// Rethrow the target exception if possible so that the
@@ -211,25 +221,23 @@
if (t instanceof Exception) {
throw ((Exception) t);
} else {
- String message = messages.getMessage(KEY_DISPATCH_ERROR, path,
name);
- log.error(message, e);
+ String message = messages.getMessage(MSG_KEY_DISPATCH_ERROR,
path);
+ log.error(message + ":" + name, e);
throw new ServletException(t);
}
}
}
/**
- * Determines whether the current form's cancel button was pressed. This
- * method will check if the [EMAIL PROTECTED] Globals#CANCEL_KEY} request
attribute has
- * been set, which normally occurs if the cancel button generated by
- * <strong>CancelTag</strong> was pressed by the user in the current
- * request. If <code>true</code>, validation performed by the
- * <code>validate()</code> method of the form; otherwise it will have been
- * skipped by the controller servlet.
+ * Determines whether the current form's cancel button was pressed. The
+ * default behavior method will check if the
+ * [EMAIL PROTECTED] ActionContext#getCancelled()} context property is set
, which
+ * normally occurs if the cancel button generated by
<strong>CancelTag</strong>
+ * was pressed by the user in the current request.
*
* @param context the current action context
- * @return <code>true</code> if the cancel button was pressed;
- * <code>false</code> otherwise
+ * @return <code>true</code> if the request is cancelled; otherwise
+ * <code>false</code>
* @see org.apache.struts.taglib.html.CancelTag
*/
protected boolean isCancelled(ActionContext context) {
@@ -239,8 +247,8 @@
/**
* Decides the appropriate method instance for the specified method name.
- * Implementations may introspect for any desired method signature. The
- * resolution is only needed if [EMAIL PROTECTED] #getMethod(String)} does
not find a
+ * Implementations may introspect for any desired method signature. This
+ * resolution is only invoked if [EMAIL PROTECTED] #getMethod(String)}
does not find a
* match in its method cache.
*
* @param methodName the method name to use for introspection
@@ -257,44 +265,24 @@
*
* @param context the current action context
* @return the method name or <code>null</code> if no name can be resolved
+ * @see #resolveMethod(String, ActionContext)
*/
protected abstract String resolveMethodName(ActionContext context);
/**
- * Invoked when a method cannot be resolved. The default behavior delegates
- * to the fallback method, if specified; otherwise throw an
- * [EMAIL PROTECTED] IllegalStateException}. Subclasses should override
this to
- * provide custom handling such as sending an HTTP 404 error.
+ * Services the case when the dispatch fails because the method name cannot
+ * be resolved. The default behavior throws an [EMAIL PROTECTED]
IllegalStateException}.
+ * Subclasses should override this to provide custom handling such as
+ * sending an HTTP 404 error.
*
* @param context the current action context
- * @throws NoSuchMethodException if the fallback method name is specified
- * but the corresponding method does not exist
* @throws IllegalStateException always unless supressed by subclass
- * @see #getFallbackMethodName()
+ * @see #resolveMethodName(ActionContext)
+ * @see #getDefaultMethodName()
*/
protected Object unspecified(ActionContext context) throws Exception {
- // Is the fallback method present?
- Method method = null;
- String name = getFallbackMethodName();
- if (name != null) {
- try {
- method = getMethod(context, name);
- } catch (NoSuchMethodException e) {
- String msg = messages.getMessage(KEY_MISSING_METHOD, name);
- NoSuchMethodException e2 = new NoSuchMethodException(msg);
- e2.initCause(e);
- throw e2;
- }
- }
-
- // Dispatch if fallback is available
- if (method != null) {
- return dispatchMethod(context, method, name);
- }
-
- // Otherwise the dispatch has failed
ActionConfig config = context.getActionConfig();
- String msg = messages.getMessage(KEY_MISSING_PARAMETER,
config.getPath(), config.getParameter());
+ String msg = messages.getMessage(MSG_KEY_UNSPECIFIED, config.getPath());
log.error(msg);
throw new IllegalStateException(msg);
}
Modified:
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractMappingDispatcher.java
URL:
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractMappingDispatcher.java?rev=720898&r1=720897&r2=720898&view=diff
==============================================================================
---
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractMappingDispatcher.java
(original)
+++
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/AbstractMappingDispatcher.java
Wed Nov 26 07:19:38 2008
@@ -43,7 +43,7 @@
}
if ((parameter == null)) {
- String message = messages.getMessage(KEY_MISSING_HANDLER_PROPERTY,
mapping.getPath());
+ String message =
messages.getMessage(MSG_KEY_MISSING_MAPPING_PARAMETER, mapping.getPath());
log.error(message);
throw new IllegalStateException(message);
}
Modified:
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletMappingDispatcher.java
URL:
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletMappingDispatcher.java?rev=720898&r1=720897&r2=720898&view=diff
==============================================================================
---
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletMappingDispatcher.java
(original)
+++
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletMappingDispatcher.java
Wed Nov 26 07:19:38 2008
@@ -27,6 +27,8 @@
import java.lang.reflect.Method;
+import javax.servlet.http.HttpServletResponse;
+
public class ServletMappingDispatcher extends AbstractMappingDispatcher {
protected Object dispatchMethod(ActionContext context, Method method,
String name) throws Exception {
@@ -40,4 +42,10 @@
return ServletDispatchUtils.resolveClassicExecuteMethod(context,
methodName);
}
+ protected Object unspecified(ActionContext context) throws Exception {
+ HttpServletResponse response = ((ServletActionContext)
context).getResponse();
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ return null;
+ }
+
}
Modified:
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletParameterDispatcher.java
URL:
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletParameterDispatcher.java?rev=720898&r1=720897&r2=720898&view=diff
==============================================================================
---
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletParameterDispatcher.java
(original)
+++
struts/struts1/trunk/core/src/main/java/org/apache/struts/dispatcher/servlet/ServletParameterDispatcher.java
Wed Nov 26 07:19:38 2008
@@ -27,6 +27,8 @@
import java.lang.reflect.Method;
+import javax.servlet.http.HttpServletResponse;
+
public class ServletParameterDispatcher extends AbstractParameterDispatcher {
protected Object dispatchMethod(ActionContext context, Method method,
String name) throws Exception {
@@ -45,4 +47,10 @@
return (String) servletContext.getParam().get(parameter);
}
+ protected Object unspecified(ActionContext context) throws Exception {
+ HttpServletResponse response = ((ServletActionContext)
context).getResponse();
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ return null;
+ }
+
}
Added:
struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/LocalStrings.properties
URL:
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/LocalStrings.properties?rev=720898&view=auto
==============================================================================
---
struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/LocalStrings.properties
(added)
+++
struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/LocalStrings.properties
Wed Nov 26 07:19:38 2008
@@ -0,0 +1,20 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+dispatcher.error=Dispatcher received an exception when invoking target method
for path '{0}'
+dispatcher.missingMethod=Dispatcher cannot resolve method (check logs)
+dispatcher.missingMethod.log=Dispatcher cannot resolve method named '{1}' for
path '{0}'
+dispatcher.missingMappingParameter=Dispatcher cannot resolve method name
because 'parameter' attribute for path '{0}' is not defined.
+dispatcher.unspecified=Dispatcher cannot resolve target method for path '{0}'
Propchange:
struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/LocalStrings.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
struts/struts1/trunk/core/src/main/resources/org/apache/struts/dispatcher/LocalStrings.properties
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL