Author: lukaszlenart Date: Wed Oct 23 07:33:44 2013 New Revision: 1534936 URL: http://svn.apache.org/r1534936 Log: WW-4131 Implements new ActionProxyFactory to cooperate with PrefixBasedActionMapper
Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/PrefixBasedActionProxyFactory.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/StrutsActionProxyFactory.java struts/struts2/trunk/core/src/main/resources/struts-default.xml struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/DefaultActionProxyFactory.java Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/PrefixBasedActionProxyFactory.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/PrefixBasedActionProxyFactory.java?rev=1534936&view=auto ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/PrefixBasedActionProxyFactory.java (added) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/PrefixBasedActionProxyFactory.java Wed Oct 23 07:33:44 2013 @@ -0,0 +1,87 @@ +package org.apache.struts2.impl; + +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ActionProxyFactory; +import com.opensymphony.xwork2.DefaultActionProxyFactory; +import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.StrutsConstants; + +import java.util.HashMap; +import java.util.Map; + +/** + * <!-- START SNIPPET: description --> + * Prefix based factory should be used with {@link org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper} + * to use appropriate {@link com.opensymphony.xwork2.ActionProxyFactory} connected with given + * {@link org.apache.struts2.dispatcher.mapper.ActionMapper} + * + * Add below entry to struts.xml to enable the factory: + * <constant name="struts.actionProxyFactory" value="prefix"/> + * + * The factory will use the same set of patterns as defined with: + * <constant name="struts.mapper.prefixMapping" value="..."/> + * <!-- END SNIPPET: description --> + */ +public class PrefixBasedActionProxyFactory extends DefaultActionProxyFactory { + + private static final Logger LOG = LoggerFactory.getLogger(PrefixBasedActionProxyFactory.class); + + private Map<String, ActionProxyFactory> actionProxyFactories = new HashMap<String, ActionProxyFactory>(); + private ActionProxyFactory defaultFactory; + + @Inject + public void setContainer(Container container) { + this.container = container; + } + + @Inject(StrutsConstants.STRUTS_ACTIONPROXYFACTORY) + public void setActionProxyFactory(ActionProxyFactory factory) { + this.defaultFactory = factory; + } + + @Inject(StrutsConstants.PREFIX_BASED_MAPPER_CONFIGURATION) + public void setPrefixBasedActionProxyFactories(String list) { + if (list != null) { + String[] factories = list.split(","); + for (String factory : factories) { + String[] thisFactory = factory.split(":"); + if ((thisFactory != null) && (thisFactory.length == 2)) { + String factoryPrefix = thisFactory[0].trim(); + String factoryName = thisFactory[1].trim(); + ActionProxyFactory obj = container.getInstance(ActionProxyFactory.class, factoryName); + if (obj != null) { + actionProxyFactories.put(factoryPrefix, obj); + } else if (LOG.isWarnEnabled()) { + LOG.warn("Invalid PrefixBasedActionProxyFactory config entry: [#0]", factory); + } + } + } + } + } + + public ActionProxy createActionProxy(String namespace, String actionName, String methodName, + Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext) { + + String uri = namespace + (namespace.endsWith("/") ? actionName : "/" + actionName); + for (int lastIndex = uri.lastIndexOf('/'); lastIndex > (-1); lastIndex = uri.lastIndexOf('/', lastIndex - 1)) { + String key = uri.substring(0, lastIndex); + ActionProxyFactory actionProxyFactory = actionProxyFactories.get(key); + if (actionProxyFactory != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Using ActionProxyFactory [#0] for prefix [#1]", actionProxyFactory, key); + } + return actionProxyFactory.createActionProxy(namespace, actionName, methodName, extraContext, executeResult, cleanupContext); + } else if (LOG.isDebugEnabled()) { + LOG.debug("No ActionProxyFactory defined for [#1]", key); + } + } + if (LOG.isDebugEnabled()){ + LOG.debug("Cannot find any matching ActionProxyFactory, falling back to [#0]", defaultFactory); + } + return defaultFactory.createActionProxy(namespace, actionName, methodName, extraContext, executeResult, cleanupContext); + } + +} Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/StrutsActionProxyFactory.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/StrutsActionProxyFactory.java?rev=1534936&r1=1534935&r2=1534936&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/StrutsActionProxyFactory.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/impl/StrutsActionProxyFactory.java Wed Oct 23 07:33:44 2013 @@ -23,8 +23,6 @@ package org.apache.struts2.impl; -import java.util.Map; - import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.DefaultActionProxyFactory; Modified: struts/struts2/trunk/core/src/main/resources/struts-default.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/struts-default.xml?rev=1534936&r1=1534935&r2=1534936&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/struts-default.xml (original) +++ struts/struts2/trunk/core/src/main/resources/struts-default.xml Wed Oct 23 07:33:44 2013 @@ -48,6 +48,7 @@ <bean type="com.opensymphony.xwork2.FileManagerFactory" class="com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory" name="struts" scope="singleton"/> <bean type="com.opensymphony.xwork2.ActionProxyFactory" name="struts" class="org.apache.struts2.impl.StrutsActionProxyFactory"/> + <bean type="com.opensymphony.xwork2.ActionProxyFactory" name="prefix" class="org.apache.struts2.impl.PrefixBasedActionProxyFactory"/> <bean type="com.opensymphony.xwork2.conversion.ObjectTypeDeterminer" name="struts" class="com.opensymphony.xwork2.conversion.impl.DefaultObjectTypeDeterminer"/> Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/DefaultActionProxyFactory.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/DefaultActionProxyFactory.java?rev=1534936&r1=1534935&r2=1534936&view=diff ============================================================================== --- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/DefaultActionProxyFactory.java (original) +++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/DefaultActionProxyFactory.java Wed Oct 23 07:33:44 2013 @@ -71,4 +71,9 @@ public class DefaultActionProxyFactory i return proxy; } + @Override + public String toString() { + return getClass().getSimpleName(); + } + }