Author: musachy
Date: Mon May 19 14:55:23 2008
New Revision: 658000
URL: http://svn.apache.org/viewvc?rev=658000&view=rev
Log:
Add @ExceptionMapping and @ExceptionMappings
Added:
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMapping.java
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMappings.java
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsActionLevelAction.java
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsMethodLevelAction.java
Modified:
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
Modified:
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java?rev=658000&r1=657999&r2=658000&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java
(original)
+++
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java
Mon May 19 14:55:23 2008
@@ -35,6 +35,8 @@
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.AnnotationTools;
+import org.apache.struts2.convention.annotation.ExceptionMapping;
+import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
@@ -43,11 +45,13 @@
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.config.entities.ActionConfig;
+import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
import com.opensymphony.xwork2.config.entities.InterceptorMapping;
import com.opensymphony.xwork2.config.entities.PackageConfig;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.config.providers.InterceptorBuilder;
import com.opensymphony.xwork2.inject.Inject;
+import com.opensymphony.xwork2.util.DomHelper;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
@@ -505,9 +509,36 @@
if (annotation != null)
actionConfig.addParams(StringTools.createParameterMap(annotation.params()));
+ //add exception mappings from annotation
+ if (annotation != null && annotation.exceptionMappings() != null)
+
actionConfig.addExceptionMappings(buildExceptionMappings(annotation.exceptionMappings(),
actionName));
+
+ //add exception mapping from class
+ ExceptionMappings exceptionMappings =
actionClass.getAnnotation(ExceptionMappings.class);
+ if (exceptionMappings != null)
+
actionConfig.addExceptionMappings(buildExceptionMappings(exceptionMappings.value(),
actionName));
+
+ //add
pkgCfg.addActionConfig(actionName, actionConfig.build());
}
+ private List<ExceptionMappingConfig>
buildExceptionMappings(ExceptionMapping[] exceptions, String actionName) {
+ List<ExceptionMappingConfig> exceptionMappings = new
ArrayList<ExceptionMappingConfig>();
+
+ for (ExceptionMapping exceptionMapping : exceptions) {
+ if (LOG.isTraceEnabled())
+ LOG.trace("Mapping exception [#0] to result [#1] for action
[#2]", exceptionMapping.exception(),
+ exceptionMapping.result(), actionName);
+ ExceptionMappingConfig.Builder builder = new
ExceptionMappingConfig.Builder(null, exceptionMapping
+ .exception(), exceptionMapping.result());
+ if (exceptionMapping.params() != null)
+
builder.addParams(StringTools.createParameterMap(exceptionMapping.params()));
+ exceptionMappings.add(builder.build());
+ }
+
+ return exceptionMappings;
+ }
+
private PackageConfig.Builder getPackageConfig(final Map<String,
PackageConfig.Builder> packageConfigs,
String actionNamespace, final String actionPackage, final Class<?>
actionClass,
Action action) {
Modified:
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java?rev=658000&r1=657999&r2=658000&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java
(original)
+++
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java
Mon May 19 14:55:23 2008
@@ -87,4 +87,9 @@
* <code>{"key", "value", "key2", "value2"}</code>.
*/
String[] params() default {};
+
+ /**
+ * @return Maps return codes to exceptions. The "exceptions" interceptor
must be applied to the action.
+ */
+ ExceptionMapping[] exceptionMappings() default {};
}
\ No newline at end of file
Added:
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMapping.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMapping.java?rev=658000&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMapping.java
(added)
+++
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMapping.java
Mon May 19 14:55:23 2008
@@ -0,0 +1,54 @@
+/*
+ * $ID$
+ *
+ * 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.
+ */
+package org.apache.struts2.convention.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * <p>
+ * Adds an exception mapping to an action
+ * </p>
+ * <!-- END SNIPPET: javadoc -->
+ */
[EMAIL PROTECTED](RetentionPolicy.RUNTIME)
[EMAIL PROTECTED](ElementType.TYPE)
+public @interface ExceptionMapping {
+ /**
+ * @return Result name
+ */
+ String result();
+
+ /**
+ * @return Class name of the exception to be thrown
+ */
+ String exception();
+
+ /**
+ * @return The parameters passed to the exception. This is a list of
strings that form a name/value
+ * pair chain since creating a Map for annotations is not
possible. An example would be:
+ * <code>{"key", "value", "key2", "value2"}</code>.
+ */
+ String[] params() default {};
+}
Added:
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMappings.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMappings.java?rev=658000&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMappings.java
(added)
+++
struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/ExceptionMappings.java
Mon May 19 14:55:23 2008
@@ -0,0 +1,44 @@
+/*
+ * $ID$
+ *
+ * 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.
+ */
+package org.apache.struts2.convention.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * <p>
+ * This annotation allows a class to define more than one [EMAIL PROTECTED]
ExceptionMapping}
+ * annotations. These exception mappings will be on all actions defined in the
annotated
+ * class, they are not global exception mappings.
+ * </p>
+ * <!-- END SNIPPET: javadoc -->
+ */
[EMAIL PROTECTED](RetentionPolicy.RUNTIME)
[EMAIL PROTECTED](ElementType.TYPE)
+public @interface ExceptionMappings {
+ /**
+ * @return Exception mappings
+ */
+ ExceptionMapping[] value();
+}
Modified:
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java?rev=658000&r1=657999&r2=658000&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
(original)
+++
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
Mon May 19 14:55:23 2008
@@ -41,6 +41,8 @@
import org.apache.struts2.convention.actions.action.ActionNamesAction;
import org.apache.struts2.convention.actions.action.SingleActionNameAction;
import org.apache.struts2.convention.actions.action.TestAction;
+import
org.apache.struts2.convention.actions.exception.ExceptionsActionLevelAction;
+import
org.apache.struts2.convention.actions.exception.ExceptionsMethodLevelAction;
import
org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptor2Action;
import
org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptor3Action;
import
org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptorAction;
@@ -67,6 +69,7 @@
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.entities.ActionConfig;
+import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
import com.opensymphony.xwork2.config.entities.InterceptorConfig;
import com.opensymphony.xwork2.config.entities.InterceptorMapping;
import com.opensymphony.xwork2.config.entities.InterceptorStackConfig;
@@ -119,6 +122,8 @@
"", strutsDefault, null);
PackageConfig paramsPkg =
makePackageConfig("org.apache.struts2.convention.actions.params#struts-default#/params",
"/params", strutsDefault, null);
+ PackageConfig exceptionPkg =
makePackageConfig("org.apache.struts2.convention.actions.exception#struts-default#/exception",
+ "/exception", strutsDefault, null);
PackageConfig actionPkg =
makePackageConfig("org.apache.struts2.convention.actions.action#struts-default#/action",
"/action", strutsDefault, null);
PackageConfig idxPkg =
makePackageConfig("org.apache.struts2.convention.actions.idx#struts-default#/idx",
@@ -166,6 +171,10 @@
/* org.apache.struts2.convention.actions.params */
expect(resultMapBuilder.build(ActionParamsMethodLevelAction.class,
getAnnotation(ActionParamsMethodLevelAction.class, "run1", Action.class),
"actionParam1", paramsPkg)).andReturn(results);
+ /* org.apache.struts2.convention.actions.exception */
+ expect(resultMapBuilder.build(ExceptionsMethodLevelAction.class,
getAnnotation(ExceptionsMethodLevelAction.class, "run1", Action.class),
"exception1", exceptionPkg)).andReturn(results);
+ expect(resultMapBuilder.build(ExceptionsActionLevelAction.class,
getAnnotation(ExceptionsActionLevelAction.class, "execute", Action.class),
"exceptions-action-level", exceptionPkg)).andReturn(results);
+
/* org.apache.struts2.convention.actions.interceptor */
expect(resultMapBuilder.build(InterceptorsAction.class,
getAnnotation(InterceptorsAction.class, "run1", Action.class), "action100",
interceptorRefsPkg)).andReturn(results);
expect(resultMapBuilder.build(InterceptorsAction.class,
getAnnotation(InterceptorsAction.class, "run2", Action.class), "action200",
interceptorRefsPkg)).andReturn(results);
@@ -265,6 +274,43 @@
assertEquals("val1", params.get("param1"));
assertEquals("val2", params.get("param2"));
+ /* org.apache.struts2.convention.actions.params */
+ pkgConfig =
configuration.getPackageConfig("org.apache.struts2.convention.actions.exception#struts-default#/exception");
+ assertNotNull(pkgConfig);
+ assertEquals(2, pkgConfig.getActionConfigs().size());
+
+ ac = pkgConfig.getAllActionConfigs().get("exception1");
+ assertNotNull(ac);
+ List<ExceptionMappingConfig> exceptions = ac.getExceptionMappings();
+ assertNotNull(exceptions);
+ assertEquals(2, exceptions.size());
+ ExceptionMappingConfig exception = exceptions.get(0);
+ assertEquals("NPE1", exception.getExceptionClassName());
+ assertEquals("success", exception.getResult());
+ exception = exceptions.get(1);
+ assertEquals("NPE2", exception.getExceptionClassName());
+ assertEquals("success", exception.getResult());
+ params = exception.getParams();
+ assertNotNull(params);
+ assertEquals(1, params.size());
+ assertEquals("val1", params.get("param1"));
+
+ ac = pkgConfig.getAllActionConfigs().get("exceptions-action-level");
+ assertNotNull(ac);
+ exceptions = ac.getExceptionMappings();
+ assertNotNull(exceptions);
+ assertEquals(2, exceptions.size());
+ exception = exceptions.get(0);
+ assertEquals("NPE1", exception.getExceptionClassName());
+ assertEquals("success", exception.getResult());
+ exception = exceptions.get(1);
+ assertEquals("NPE2", exception.getExceptionClassName());
+ assertEquals("success", exception.getResult());
+ params = exception.getParams();
+ assertNotNull(params);
+ assertEquals(1, params.size());
+ assertEquals("val1", params.get("param1"));
+
/* org.apache.struts2.convention.actions.idx */
pkgConfig =
configuration.getPackageConfig("org.apache.struts2.convention.actions.idx#struts-default#/idx");
assertNotNull(pkgConfig);
Added:
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsActionLevelAction.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsActionLevelAction.java?rev=658000&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsActionLevelAction.java
(added)
+++
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsActionLevelAction.java
Mon May 19 14:55:23 2008
@@ -0,0 +1,35 @@
+/*
+ * $ID$
+ *
+ * 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.
+ */
+package org.apache.struts2.convention.actions.exception;
+
+import org.apache.struts2.convention.annotation.ExceptionMapping;
+import org.apache.struts2.convention.annotation.ExceptionMappings;
+
[EMAIL PROTECTED]({
+ @ExceptionMapping(exception = "NPE1", result = "success"),
+ @ExceptionMapping(exception = "NPE2", result = "success", params =
{"param1", "val1"})
+})
+public class ExceptionsActionLevelAction {
+
+ public String execute() throws Exception {
+ return null;
+ }
+}
\ No newline at end of file
Added:
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsMethodLevelAction.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsMethodLevelAction.java?rev=658000&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsMethodLevelAction.java
(added)
+++
struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/exception/ExceptionsMethodLevelAction.java
Mon May 19 14:55:23 2008
@@ -0,0 +1,35 @@
+/*
+ * $ID$
+ *
+ * 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.
+ */
+package org.apache.struts2.convention.actions.exception;
+
+import org.apache.struts2.convention.annotation.Action;
+import org.apache.struts2.convention.annotation.ExceptionMapping;
+
+public class ExceptionsMethodLevelAction {
+
+ @Action(value = "exception1", exceptionMappings = {
+ @ExceptionMapping(exception = "NPE1", result = "success"),
+ @ExceptionMapping(exception = "NPE2", result = "success", params =
{"param1", "val1"})
+ })
+ public String run1() throws Exception {
+ return null;
+ }
+}
\ No newline at end of file