Author: musachy Date: Wed May 14 13:24:35 2008 New Revision: 656410 URL: http://svn.apache.org/viewvc?rev=656410&view=rev Log: Add @InterceptorRefs, which can be applied at the class level
Added: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRefs.java struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor2Action.java struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor3Action.java struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptorAction.java Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/DefaultInterceptorMapBuilder.java struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/InterceptorMapBuilder.java 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/InterceptorRef.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/DefaultInterceptorMapBuilder.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/DefaultInterceptorMapBuilder.java?rev=656410&r1=656409&r2=656410&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/DefaultInterceptorMapBuilder.java (original) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/DefaultInterceptorMapBuilder.java Wed May 14 13:24:35 2008 @@ -26,7 +26,9 @@ import java.util.Map; import org.apache.struts2.convention.annotation.Action; +import org.apache.struts2.convention.annotation.AnnotationTools; import org.apache.struts2.convention.annotation.InterceptorRef; +import org.apache.struts2.convention.annotation.InterceptorRefs; import com.opensymphony.xwork2.ObjectFactory; import com.opensymphony.xwork2.config.Configuration; @@ -35,6 +37,7 @@ import com.opensymphony.xwork2.config.entities.PackageConfig; import com.opensymphony.xwork2.config.providers.InterceptorBuilder; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.AnnotationUtils; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; @@ -49,29 +52,48 @@ private Configuration configuration; - public List<InterceptorMapping> build(PackageConfig.Builder builder, + public List<InterceptorMapping> build(Class<?> actionClass, PackageConfig.Builder builder, String actionName, Action annotation) { List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>( 10); + //from @InterceptorRefs annotation + InterceptorRefs interceptorRefs = AnnotationTools.findAnnotation(actionClass, InterceptorRefs.class); + if (interceptorRefs != null) + interceptorList.addAll(build(interceptorRefs.value(), actionName, builder)); + + //from @InterceptorRef annotation + InterceptorRef interceptorRef = AnnotationTools.findAnnotation(actionClass, InterceptorRef.class); + if (interceptorRef != null) + interceptorList.addAll(build(new InterceptorRef[] {interceptorRef}, actionName, builder)); + + //from @Action annotation if (annotation != null) { InterceptorRef[] interceptors = annotation.interceptorRefs(); if (interceptors != null) { - for (InterceptorRef interceptor : interceptors) { - if (LOG.isTraceEnabled()) - LOG.trace("Adding interceptor [#0] to [#1]", - interceptor.value(), actionName); - Map<String, String> params = createParameterMap(interceptor - .params()); - interceptorList.addAll(buildInterceptorList(builder, - interceptor, params)); - } + interceptorList.addAll(build(interceptors, actionName, builder)); } } return interceptorList; } + protected List<InterceptorMapping> build(InterceptorRef[] interceptors, String actionName, PackageConfig.Builder builder) { + List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>( + 10); + for (InterceptorRef interceptor : interceptors) { + if (LOG.isTraceEnabled()) + LOG.trace("Adding interceptor [#0] to [#1]", + interceptor.value(), actionName); + Map<String, String> params = createParameterMap(interceptor + .params()); + interceptorList.addAll(buildInterceptorList(builder, + interceptor, params)); + } + + return interceptorList; + } + protected Map<String, String> createParameterMap(String[] parms) { Map<String, String> map = new HashMap<String, String>(); int subtract = parms.length % 2; Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/InterceptorMapBuilder.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/InterceptorMapBuilder.java?rev=656410&r1=656409&r2=656410&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/InterceptorMapBuilder.java (original) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/InterceptorMapBuilder.java Wed May 14 13:24:35 2008 @@ -40,9 +40,9 @@ * @param actionClass The class of the action. * @param annotation The action annotation. * @param actionName The action name. - * @param packageConfig The package configuration that the action will be added to. + * @param builder The package configuration builder. * @return The mapping of the interceptors. If there were none found - * than this should return an empty Map. + * then this should return an empty List. */ - List<InterceptorMapping> build(PackageConfig.Builder builder, String actionName, Action annotation); + List<InterceptorMapping> build(Class<?> actionClass, PackageConfig.Builder builder, String actionName, Action annotation); } 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=656410&r1=656409&r2=656410&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 Wed May 14 13:24:35 2008 @@ -430,7 +430,7 @@ } //build interceptors - List<InterceptorMapping> interceptors = interceptorMapBuilder.build(pkgCfg, actionName, annotation); + List<InterceptorMapping> interceptors = interceptorMapBuilder.build(actionClass, pkgCfg, actionName, annotation); actionConfig.addInterceptors(interceptors); //build results Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRef.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRef.java?rev=656410&r1=656409&r2=656410&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRef.java (original) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRef.java Wed May 14 13:24:35 2008 @@ -28,7 +28,10 @@ /** * <!-- START SNIPPET: javadoc --> * <p> - * This annotation allows interceptor to be applied to acctions + * This annotation allows an interceptor to be applied to an action. If + * this annotation is used at the class level, then the interceptor + * will be applied to all actions defined on that class, and will be applied + * before the ones defined at the method level. * </p> * <!-- END SNIPPET: javadoc --> */ @@ -39,7 +42,7 @@ * @return name of the interceptor or interceptor stack */ String value(); - + /** * @return The parameters passed to the interceptor. 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: Added: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRefs.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRefs.java?rev=656410&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRefs.java (added) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/InterceptorRefs.java Wed May 14 13:24:35 2008 @@ -0,0 +1,40 @@ +/* + * $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] InterceptorRef} + * annotations. + * </p> + * <!-- END SNIPPET: javadoc --> + */ [EMAIL PROTECTED](RetentionPolicy.RUNTIME) [EMAIL PROTECTED]({ElementType.TYPE}) +public @interface InterceptorRefs { + InterceptorRef[] 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=656410&r1=656409&r2=656410&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 Wed May 14 13:24:35 2008 @@ -20,6 +20,12 @@ */ package org.apache.struts2.convention; +import static org.apache.struts2.convention.ReflectionTools.getAnnotation; +import static org.easymock.EasyMock.checkOrder; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.verify; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -28,7 +34,6 @@ import junit.framework.TestCase; -import static org.apache.struts2.convention.ReflectionTools.*; import org.apache.struts2.convention.actions.DefaultResultPathAction; import org.apache.struts2.convention.actions.NoAnnotationAction; import org.apache.struts2.convention.actions.Skip; @@ -36,6 +41,9 @@ 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.interceptor.ActionLevelInterceptor2Action; +import org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptor3Action; +import org.apache.struts2.convention.actions.interceptor.ActionLevelInterceptorAction; import org.apache.struts2.convention.actions.interceptor.InterceptorsAction; import org.apache.struts2.convention.actions.namespace.ActionLevelNamespaceAction; import org.apache.struts2.convention.actions.namespace.ClassLevelNamespaceAction; @@ -54,9 +62,7 @@ import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.dispatcher.ServletDispatcherResult; import org.easymock.EasyMock; -import static org.easymock.EasyMock.*; -import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ObjectFactory; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.entities.ActionConfig; @@ -69,11 +75,7 @@ import com.opensymphony.xwork2.config.impl.DefaultConfiguration; import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.Scope.Strategy; -import com.opensymphony.xwork2.interceptor.AbstractInterceptor; -import com.opensymphony.xwork2.interceptor.Interceptor; import com.opensymphony.xwork2.ognl.OgnlReflectionProvider; -import com.opensymphony.xwork2.util.reflection.ReflectionProvider; -import com.opensymphony.xwork2.validator.ValidationInterceptor; /** * <p> @@ -164,6 +166,13 @@ expect(resultMapBuilder.build(InterceptorsAction.class, getAnnotation(InterceptorsAction.class, "run3", Action.class), "action300", interceptorRefsPkg)).andReturn(results); expect(resultMapBuilder.build(InterceptorsAction.class, getAnnotation(InterceptorsAction.class, "run4", Action.class), "action400", interceptorRefsPkg)).andReturn(results); + expect(resultMapBuilder.build(ActionLevelInterceptorAction.class, getAnnotation(ActionLevelInterceptorAction.class, "run1", Action.class), "action500", interceptorRefsPkg)).andReturn(results); + expect(resultMapBuilder.build(ActionLevelInterceptorAction.class, getAnnotation(ActionLevelInterceptorAction.class, "run2", Action.class), "action600", interceptorRefsPkg)).andReturn(results); + expect(resultMapBuilder.build(ActionLevelInterceptorAction.class, getAnnotation(ActionLevelInterceptorAction.class, "run3", Action.class), "action700", interceptorRefsPkg)).andReturn(results); + + expect(resultMapBuilder.build(ActionLevelInterceptor2Action.class, getAnnotation(ActionLevelInterceptor2Action.class, "run1", Action.class), "action800", interceptorRefsPkg)).andReturn(results); + expect(resultMapBuilder.build(ActionLevelInterceptor3Action.class, getAnnotation(ActionLevelInterceptor3Action.class, "run1", Action.class), "action900", interceptorRefsPkg)).andReturn(results); + /* org.apache.struts2.convention.actions.namespace */ expect(resultMapBuilder.build(ActionLevelNamespaceAction.class, getAnnotation(ActionLevelNamespaceAction.class, "execute", Action.class), "action", actionLevelNamespacePkg)).andReturn(results); expect(resultMapBuilder.build(ClassLevelNamespaceAction.class, null, "class-level-namespace", classLevelNamespacePkg)).andReturn(results); @@ -309,12 +318,21 @@ /* org.apache.struts2.convention.actions.interceptorRefs */ pkgConfig = configuration.getPackageConfig("org.apache.struts2.convention.actions.interceptor#struts-default#/interceptor"); assertNotNull(pkgConfig); - assertEquals(4, pkgConfig.getActionConfigs().size()); + assertEquals(9, pkgConfig.getActionConfigs().size()); verifyActionConfigInterceptors(pkgConfig, "action100", "interceptor-1"); verifyActionConfigInterceptors(pkgConfig, "action200", "interceptor-1", "interceptor-2"); verifyActionConfigInterceptors(pkgConfig, "action300", "interceptor-1", "interceptor-2"); verifyActionConfigInterceptors(pkgConfig, "action400", "interceptor-1", "interceptor-1", "interceptor-2"); - + + // Interceptors at class level + verifyActionConfigInterceptors(pkgConfig, "action500", "interceptor-1"); + verifyActionConfigInterceptors(pkgConfig, "action600", "interceptor-1", "interceptor-2"); + verifyActionConfigInterceptors(pkgConfig, "action700", "interceptor-1", "interceptor-1", "interceptor-2"); + + //multiple interceptor at class level + verifyActionConfigInterceptors(pkgConfig, "action800", "interceptor-1", "interceptor-2"); + verifyActionConfigInterceptors(pkgConfig, "action900", "interceptor-1", "interceptor-1", "interceptor-2"); + /* org.apache.struts2.convention.actions */ pkgConfig = configuration.getPackageConfig("org.apache.struts2.convention.actions#struts-default#"); assertNotNull(pkgConfig); Added: struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor2Action.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor2Action.java?rev=656410&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor2Action.java (added) +++ struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor2Action.java Wed May 14 13:24:35 2008 @@ -0,0 +1,42 @@ +/* + * $Id: ActionLevelResultAction.java 655902 2008-05-13 15:15:12Z bpontarelli $ + * + * 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.interceptor; + +import org.apache.struts2.convention.annotation.Action; +import org.apache.struts2.convention.annotation.InterceptorRef; +import org.apache.struts2.convention.annotation.InterceptorRefs; + +/** + * <p> + * This is a test action with 2 interceptors at the action level. + * </p> + */ [EMAIL PROTECTED]({ + @InterceptorRef("interceptor-1"), + @InterceptorRef("interceptor-2") +}) +public class ActionLevelInterceptor2Action { + + @Action(value = "action800") + public String run1() 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/interceptor/ActionLevelInterceptor3Action.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor3Action.java?rev=656410&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor3Action.java (added) +++ struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptor3Action.java Wed May 14 13:24:35 2008 @@ -0,0 +1,42 @@ +/* + * $Id: ActionLevelResultAction.java 655902 2008-05-13 15:15:12Z bpontarelli $ + * + * 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.interceptor; + +import org.apache.struts2.convention.annotation.Action; +import org.apache.struts2.convention.annotation.InterceptorRef; +import org.apache.struts2.convention.annotation.InterceptorRefs; + +/** + * <p> + * This is a test action with 1 interceptor and 1 stack at the action level. + * </p> + */ [EMAIL PROTECTED]({ + @InterceptorRef("interceptor-1"), + @InterceptorRef("stack-1") +}) +public class ActionLevelInterceptor3Action { + + @Action(value = "action900") + public String run1() 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/interceptor/ActionLevelInterceptorAction.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptorAction.java?rev=656410&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptorAction.java (added) +++ struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/ActionLevelInterceptorAction.java Wed May 14 13:24:35 2008 @@ -0,0 +1,48 @@ +/* + * $Id: ActionLevelResultAction.java 655902 2008-05-13 15:15:12Z bpontarelli $ + * + * 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.interceptor; + +import org.apache.struts2.convention.annotation.Action; +import org.apache.struts2.convention.annotation.InterceptorRef; + +/** + * <p> + * This is a test action with one interceptor at the action level. + * </p> + */ [EMAIL PROTECTED]("interceptor-1") +public class ActionLevelInterceptorAction { + + @Action(value = "action500") + public String run1() throws Exception { + return null; + } + + @Action(value = "action600", interceptorRefs = @InterceptorRef("interceptor-2")) + public String run2() throws Exception { + return null; + } + + @Action(value = "action700", interceptorRefs = @InterceptorRef("stack-1")) + public String run3() throws Exception { + return null; + } +}