struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From musa...@apache.org
Subject svn commit: r656074 - in /struts/sandbox/trunk/struts2-convention-plugin/src: main/java/org/apache/struts2/convention/ main/java/org/apache/struts2/convention/annotation/ main/resources/ test/java/org/apache/struts2/convention/ test/java/org/apache/str...
Date Wed, 14 May 2008 01:21:17 GMT
Author: musachy
Date: Tue May 13 18:21:16 2008
New Revision: 656074

URL: http://svn.apache.org/viewvc?rev=656074&view=rev
Log:
Add params to Interceptors in @Action

Added:
    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/test/java/org/apache/struts2/convention/TestInterceptor.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/InterceptorRef.java
    struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml
    struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
    struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/InterceptorsAction.java

Added: 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=656074&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/DefaultInterceptorMapBuilder.java
(added)
+++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/DefaultInterceptorMapBuilder.java
Tue May 13 18:21:16 2008
@@ -0,0 +1,110 @@
+/*
+ * $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;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.struts2.convention.annotation.Action;
+import org.apache.struts2.convention.annotation.InterceptorRef;
+
+import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.config.Configuration;
+import com.opensymphony.xwork2.config.ConfigurationException;
+import com.opensymphony.xwork2.config.entities.InterceptorMapping;
+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.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+
+/**
+ * <p>
+ * Builds interceptor mappings from annotations.
+ * </p>
+ */
+public class DefaultInterceptorMapBuilder implements InterceptorMapBuilder {
+	private static final Logger LOG = LoggerFactory
+			.getLogger(DefaultInterceptorMapBuilder.class);
+
+	private Configuration configuration;
+
+	public List<InterceptorMapping> build(PackageConfig.Builder builder,
+			String actionName, Action annotation) {
+		List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(
+				10);
+
+		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));
+				}
+			}
+		}
+
+		return interceptorList;
+	}
+
+	protected Map<String, String> createParameterMap(String[] parms) {
+		Map<String, String> map = new HashMap<String, String>();
+		int subtract = parms.length % 2;
+		if (subtract != 0) {
+			throw new ConfigurationException(
+					"The InterceptorRef annotation uses an array of strings for"
+							+ " parameters and they must be in a key value pair configuration. It looks like you"
+							+ " have specified an odd number of parameters and there should only be an even number."
+							+ " (e.g. params = {\"key\", \"value\"})");
+		}
+
+		for (int i = 0; i < parms.length; i = i + 2) {
+			String key = parms[i];
+			String value = parms[i + 1];
+			map.put(key, value);
+			if (LOG.isTraceEnabled()) {
+				LOG.trace("Adding parmeter [#0:#1] to interceptor", key, value);
+			}
+		}
+
+		return map;
+	}
+
+	protected List<InterceptorMapping> buildInterceptorList(
+			PackageConfig.Builder builder, InterceptorRef ref, Map params) {
+		return InterceptorBuilder.constructInterceptorReference(builder, ref
+				.value(), params, builder.build().getLocation(),
+				(ObjectFactory) configuration.getContainer().getInstance(
+						ObjectFactory.class));
+	}
+
+	@Inject
+	public void setConfiguration(Configuration configuration) {
+		this.configuration = configuration;
+	}
+}

Added: 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=656074&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/InterceptorMapBuilder.java
(added)
+++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/InterceptorMapBuilder.java
Tue May 13 18:21:16 2008
@@ -0,0 +1,48 @@
+/*
+ * $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;
+
+import java.util.List;
+
+import org.apache.struts2.convention.annotation.Action;
+
+import com.opensymphony.xwork2.config.entities.InterceptorMapping;
+import com.opensymphony.xwork2.config.entities.PackageConfig;
+
+/**
+ * <p>
+ * This interface defines how interceptors are built from
+ * annotations.
+ * </p>
+ */
+public interface InterceptorMapBuilder {
+	/**
+     * Builds the interceptor configurations given the action information.
+     *
+     * @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.
+     * @return  The mapping of the interceptors. If there were none found
+     *          than this should return an empty Map.
+     */
+	List<InterceptorMapping> build(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=656074&r1=656073&r2=656074&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
Tue May 13 18:21:16 2008
@@ -61,6 +61,7 @@
     private final Configuration configuration;
     private final ActionNameBuilder actionNameBuilder;
     private final ResultMapBuilder resultMapBuilder;
+    private final InterceptorMapBuilder interceptorMapBuilder;
     private final ObjectFactory objectFactory;
     private final String defaultParentPackage;
     private final boolean redirectToSlash;
@@ -77,6 +78,8 @@
      *          names.
      * @param   resultMapBuilder The result map builder used to create ResultConfig mappings
for each
      *          action.
+     * @param   interceptorMapBuilder The interceptor map builder used to create InterceptorConfig
mappings for each
+     *          action.
      * @param   objectFactory The ObjectFactory used to create the actions and such.
      * @param   redirectToSlash A boolean parameter that controls whether or not this will
create an
      *          action for indexes. If this is set to true, index actions are not created
because
@@ -86,7 +89,7 @@
      */
     @Inject
     public PackageBasedActionConfigBuilder(Configuration configuration, ActionNameBuilder
actionNameBuilder,
-            ResultMapBuilder resultMapBuilder, ObjectFactory objectFactory,
+            ResultMapBuilder resultMapBuilder, InterceptorMapBuilder interceptorMapBuilder,
ObjectFactory objectFactory,
             @Inject("struts.convention.redirect.to.slash") String redirectToSlash,
             @Inject("struts.convention.default.parent.package") String defaultParentPackage)
{
 
@@ -94,6 +97,7 @@
         this.configuration = configuration;
         this.actionNameBuilder = actionNameBuilder;
         this.resultMapBuilder = resultMapBuilder;
+        this.interceptorMapBuilder = interceptorMapBuilder;
         this.objectFactory = objectFactory;
         this.redirectToSlash = Boolean.parseBoolean(redirectToSlash);
 
@@ -426,7 +430,7 @@
         }
 
         //build interceptors
-        List<InterceptorMapping> interceptors = buildInterceptors(pkgCfg, actionName,
annotation);
+        List<InterceptorMapping> interceptors = interceptorMapBuilder.build(pkgCfg,
actionName, annotation);
         actionConfig.addInterceptors(interceptors);
 
         //build results
@@ -436,29 +440,6 @@
         pkgCfg.addActionConfig(actionName, actionConfig.build());
     }
 
-    private List<InterceptorMapping> buildInterceptors(PackageConfig.Builder builder,
String actionName, Action annotation) {
-        List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(10);
-
-        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);
-                    interceptorList.addAll(buildInterceptorList(builder, interceptor));
-                }
-            }
-        }
-
-        return interceptorList;
-    }
-
-    private List<InterceptorMapping> buildInterceptorList(PackageConfig.Builder builder,
InterceptorRef ref) {
-        return InterceptorBuilder.constructInterceptorReference(builder, ref.value(), new
LinkedHashMap(),
-                builder.build().getLocation(), (ObjectFactory) configuration.getContainer().getInstance(
-                        ObjectFactory.class));
-    }
-
     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/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=656074&r1=656073&r2=656074&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
Tue May 13 18:21:16 2008
@@ -35,5 +35,15 @@
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.TYPE)
 public @interface InterceptorRef {
+    /**
+     * @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:
+     *          <code>{"key", "value", "key2", "value2"}</code>.
+     */
+    String[] params() default {};
 }

Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml?rev=656074&r1=656073&r2=656074&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml (original)
+++ struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml Tue
May 13 18:21:16 2008
@@ -31,7 +31,8 @@
 
   <bean type="org.apache.struts2.convention.ActionConfigBuilder" class="org.apache.struts2.convention.PackageBasedActionConfigBuilder"/>
   <bean type="org.apache.struts2.convention.ActionNameBuilder" class="org.apache.struts2.convention.SEOActionNameBuilder"/>
-  <bean type="org.apache.struts2.convention.ResultMapBuilder" class="org.apache.struts2.convention.DefaultResultMapBuilder"/>
+  <bean type="org.apache.struts2.convention.ResultMapBuilder" class="org.apache.struts2.convention.DefaultResultMapBuilder"/>
+  <bean type="org.apache.struts2.convention.InterceptorMapBuilder" class="org.apache.struts2.convention.DefaultInterceptorMapBuilder"/>
   <bean type="org.apache.struts2.convention.ConventionsService" class="org.apache.struts2.convention.ConventionsServiceImpl"/>
 
   <bean type="com.opensymphony.xwork2.config.PackageProvider" class="org.apache.struts2.convention.ClasspathConfigurationProvider"/>

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=656074&r1=656073&r2=656074&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
Tue May 13 18:21:16 2008
@@ -98,8 +98,9 @@
 
         //setup interceptor stacks
         List<InterceptorStackConfig> defaultInterceptorStacks = new ArrayList<InterceptorStackConfig>();
-        defaultInterceptorStacks.add(makeInterceptorStackConfig("stack-1", "interceptor-1",
"interceptor-2"));
-        defaultInterceptorStacks.add(makeInterceptorStackConfig("stack-2", "interceptor-3",
"stack-1"));
+        InterceptorMapping interceptor1 = new InterceptorMapping("interceptor-1", new TestInterceptor());
+        InterceptorMapping interceptor2 = new InterceptorMapping("interceptor-2", new TestInterceptor());
+        defaultInterceptorStacks.add(makeInterceptorStackConfig("stack-1", interceptor1,
interceptor2));
 
         //setup results
         ResultTypeConfig[] defaultResults = new ResultTypeConfig[] { new ResultTypeConfig.Builder("dispatcher",
@@ -161,6 +162,7 @@
         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);
         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);
 
         /* org.apache.struts2.convention.actions.namespace */
         expect(resultMapBuilder.build(ActionLevelNamespaceAction.class, getAnnotation(ActionLevelNamespaceAction.class,
"execute", Action.class), "action", actionLevelNamespacePkg)).andReturn(results);
@@ -209,8 +211,10 @@
 
         ActionNameBuilder actionNameBuilder = new SEOActionNameBuilder("true", "-");
         ObjectFactory of = new ObjectFactory();
+        DefaultInterceptorMapBuilder interceptorBuilder = new DefaultInterceptorMapBuilder();
+        interceptorBuilder.setConfiguration(configuration);
         PackageBasedActionConfigBuilder builder = new PackageBasedActionConfigBuilder(configuration,
-            actionNameBuilder, resultMapBuilder, of, "false", "struts-default");
+            actionNameBuilder, resultMapBuilder, interceptorBuilder ,of, "false", "struts-default");
         if (actionPackages != null) {
             builder.setActionPackages(actionPackages);
         }
@@ -305,12 +309,12 @@
         /* org.apache.struts2.convention.actions.interceptorRefs */
         pkgConfig = configuration.getPackageConfig("org.apache.struts2.convention.actions.interceptor#struts-default#/interceptor");
         assertNotNull(pkgConfig);
-        assertEquals(3, pkgConfig.getActionConfigs().size());
+        assertEquals(4, pkgConfig.getActionConfigs().size());
         verifyActionConfigInterceptors(pkgConfig, "action100", "interceptor-1");
         verifyActionConfigInterceptors(pkgConfig, "action200", "interceptor-1", "interceptor-2");
-        verifyActionConfigInterceptors(pkgConfig, "action300", "interceptor-3", "stack-1");
-
-
+        verifyActionConfigInterceptors(pkgConfig, "action300", "interceptor-1", "interceptor-2");
+        verifyActionConfigInterceptors(pkgConfig, "action400", "interceptor-1", "interceptor-1",
"interceptor-2");
+        
         /* org.apache.struts2.convention.actions */
         pkgConfig = configuration.getPackageConfig("org.apache.struts2.convention.actions#struts-default#");
         assertNotNull(pkgConfig);
@@ -381,14 +385,14 @@
     }
 
     private InterceptorConfig makeInterceptorConfig(String name) {
-        InterceptorConfig.Builder builder = new InterceptorConfig.Builder(name, "com.opensymphony.xwork2.validator.ValidationInterceptor");
+        InterceptorConfig.Builder builder = new InterceptorConfig.Builder(name, "org.apache.struts2.convention.TestInterceptor");
         return builder.build();
     }
 
-    private InterceptorStackConfig makeInterceptorStackConfig(String name, String... interceptors)
{
+    private InterceptorStackConfig makeInterceptorStackConfig(String name, InterceptorMapping...
interceptors) {
         InterceptorStackConfig.Builder builder = new InterceptorStackConfig.Builder(name);
-        for (String interceptor : interceptors)
-            builder.addInterceptor(new InterceptorMapping(interceptor, new ValidationInterceptor()));
+        for (InterceptorMapping interceptor : interceptors)
+            builder.addInterceptor(interceptor);
         return builder.build();
     }
 

Added: struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/TestInterceptor.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/TestInterceptor.java?rev=656074&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/TestInterceptor.java
(added)
+++ struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/TestInterceptor.java
Tue May 13 18:21:16 2008
@@ -0,0 +1,21 @@
+package org.apache.struts2.convention;
+
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
+
+public class TestInterceptor extends AbstractInterceptor {
+	private String string1;
+	
+	@Override
+	public String intercept(ActionInvocation invocation) throws Exception {
+		return null;
+	}
+
+	public String getString1() {
+		return string1;
+	}
+
+	public void setString1(String string1) {
+		this.string1 = string1;
+	}
+}

Modified: struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/InterceptorsAction.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/InterceptorsAction.java?rev=656074&r1=656073&r2=656074&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/InterceptorsAction.java
(original)
+++ struts/sandbox/trunk/struts2-convention-plugin/src/test/java/org/apache/struts2/convention/actions/interceptor/InterceptorsAction.java
Tue May 13 18:21:16 2008
@@ -39,8 +39,13 @@
         return null;
     }
 
-    @Action(value = "action300", interceptorRefs = @InterceptorRef("stack-2"))
+    @Action(value = "action300", interceptorRefs = {@InterceptorRef("interceptor-1"), @InterceptorRef("interceptor-2")})
     public String run3() {
         return null;
     }
+    
+    @Action(value = "action400", interceptorRefs = {@InterceptorRef("interceptor-1"), @InterceptorRef("stack-1")})
+    public String run4() {
+        return null;
+    }
 }
\ No newline at end of file



Mime
View raw message