Skip to content

Commit

Permalink
WW-4903 Uses Initializable to properly initialise the factory
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Dec 28, 2017
1 parent 15e6be1 commit 6fc6959
Showing 1 changed file with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.DefaultActionProxyFactory;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Initializable;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* <!-- START SNIPPET: description -->
Expand All @@ -55,11 +59,12 @@
* </pre>
* <!-- END SNIPPET: description -->
*/
public class PrefixBasedActionProxyFactory extends DefaultActionProxyFactory {
public class PrefixBasedActionProxyFactory extends DefaultActionProxyFactory implements Initializable {

private static final Logger LOG = LogManager.getLogger(PrefixBasedActionProxyFactory.class);

private Map<String, ActionProxyFactory> actionProxyFactories = new HashMap<>();
private Set<String> prefixes = new HashSet<>();

@Inject
public void setContainer(Container container) {
Expand All @@ -69,18 +74,22 @@ public void setContainer(Container container) {
@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.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 {
LOG.warn("Invalid PrefixBasedActionProxyFactory config entry: [{}]", factory);
}
prefixes = new HashSet<>(Arrays.asList(list.split(",")));
}
}

@Override
public void init() {
for (String factory : prefixes) {
String[] thisFactory = factory.split(":");
if (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 {
LOG.warn("Invalid PrefixBasedActionProxyFactory config entry: [{}]", factory);
}
}
}
Expand All @@ -103,5 +112,4 @@ public ActionProxy createActionProxy(String namespace, String actionName, String
LOG.debug("Cannot find any matching ActionProxyFactory, falling back to [{}]", super.getClass().getName());
return super.createActionProxy(namespace, actionName, methodName, extraContext, executeResult, cleanupContext);
}

}

0 comments on commit 6fc6959

Please sign in to comment.