mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Adjusts class to the new Initializable mechanism
This commit is contained in:
@@ -59,11 +59,12 @@ import java.util.Set;
|
||||
* </pre>
|
||||
* <!-- END SNIPPET: description -->
|
||||
*/
|
||||
public class PrefixBasedActionProxyFactory extends StrutsActionProxyFactory {
|
||||
public class PrefixBasedActionProxyFactory extends StrutsActionProxyFactory 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) {
|
||||
@@ -73,18 +74,22 @@ public class PrefixBasedActionProxyFactory extends StrutsActionProxyFactory {
|
||||
@Inject(StrutsConstants.PREFIX_BASED_MAPPER_CONFIGURATION)
|
||||
public void setPrefixBasedActionProxyFactories(String list) {
|
||||
if (list != null) {
|
||||
Set<String> prefixes = new HashSet<>(Arrays.asList(list.split(",")));
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-5
@@ -41,7 +41,7 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase {
|
||||
private PrefixBasedActionProxyFactory factory;
|
||||
|
||||
public void testDifferentPrefixes() throws Exception {
|
||||
factory.setPrefixBasedActionProxyFactories("/ns1:prefix1,/ns2:prefix2");
|
||||
initFactory("/ns1:prefix1,/ns2:prefix2");
|
||||
|
||||
ActionProxy proxy1 = factory.createActionProxy("/ns1", "", "", Collections.<String, Object>emptyMap(), false, true);
|
||||
assertTrue(proxy1 instanceof Prefix1ActionProxy);
|
||||
@@ -51,7 +51,7 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase {
|
||||
}
|
||||
|
||||
public void testFallbackToDefault() throws Exception {
|
||||
factory.setPrefixBasedActionProxyFactories("/ns1:prefix1");
|
||||
initFactory("/ns1:prefix1");
|
||||
|
||||
ActionProxy proxy1 = factory.createActionProxy("/ns1", "", "", Collections.<String, Object>emptyMap(), false, true);
|
||||
assertTrue(proxy1 instanceof Prefix1ActionProxy);
|
||||
@@ -61,7 +61,7 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase {
|
||||
}
|
||||
|
||||
public void testEmptyPrefix() throws Exception {
|
||||
factory.setPrefixBasedActionProxyFactories(":prefix1");
|
||||
initFactory(":prefix1");
|
||||
|
||||
ActionProxy proxy1 = factory.createActionProxy("/ns1", "", "", Collections.<String, Object>emptyMap(), false, true);
|
||||
assertTrue(proxy1 instanceof Prefix1ActionProxy);
|
||||
@@ -81,7 +81,9 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase {
|
||||
public Object create(Context context) throws Exception {
|
||||
return new Prefix1Factory();
|
||||
}
|
||||
|
||||
public Class type() {
|
||||
return Prefix1Factory.class;
|
||||
}
|
||||
}, Scope.SINGLETON);
|
||||
}
|
||||
},
|
||||
@@ -92,7 +94,9 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase {
|
||||
public Object create(Context context) throws Exception {
|
||||
return new Prefix2Factory();
|
||||
}
|
||||
|
||||
public Class type() {
|
||||
return Prefix2Factory.class;
|
||||
}
|
||||
}, Scope.SINGLETON);
|
||||
}
|
||||
}
|
||||
@@ -104,6 +108,11 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase {
|
||||
factory.setContainer(container);
|
||||
}
|
||||
|
||||
void initFactory(String list) {
|
||||
factory.setPrefixBasedActionProxyFactories(list);
|
||||
factory.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
Reference in New Issue
Block a user