Skip to content

Commit

Permalink
WW-5001 Adds support for struts-conversion.properties files
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Jan 15, 2019
1 parent aabed3e commit fc81832
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.conversion.StrutsConversionPropertiesProcessor;
import org.apache.struts2.conversion.StrutsTypeConverterHolder;
import org.apache.struts2.conversion.StrutsTypeConverterCreator;

Expand Down Expand Up @@ -256,7 +257,7 @@ protected Container createBootstrapContainer(List<ContainerProvider> providers)
builder.factory(ValueStackFactory.class, OgnlValueStackFactory.class, Scope.SINGLETON);

builder.factory(XWorkConverter.class, Scope.SINGLETON);
builder.factory(ConversionPropertiesProcessor.class, DefaultConversionPropertiesProcessor.class, Scope.SINGLETON);
builder.factory(ConversionPropertiesProcessor.class, StrutsConversionPropertiesProcessor.class, Scope.SINGLETON);
builder.factory(ConversionFileProcessor.class, DefaultConversionFileProcessor.class, Scope.SINGLETON);
builder.factory(ConversionAnnotationProcessor.class, DefaultConversionAnnotationProcessor.class, Scope.SINGLETON);
builder.factory(TypeConverterCreator.class, StrutsTypeConverterCreator.class, Scope.SINGLETON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import com.opensymphony.xwork2.conversion.impl.DateConverter;
import com.opensymphony.xwork2.conversion.impl.DefaultConversionAnnotationProcessor;
import com.opensymphony.xwork2.conversion.impl.DefaultConversionFileProcessor;
import com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor;
import org.apache.struts2.conversion.StrutsConversionPropertiesProcessor;
import com.opensymphony.xwork2.conversion.impl.DefaultObjectTypeDeterminer;
import org.apache.struts2.conversion.StrutsTypeConverterCreator;
import org.apache.struts2.conversion.StrutsTypeConverterHolder;
Expand Down Expand Up @@ -155,7 +155,7 @@ public void register(ContainerBuilder builder, LocatableProperties props)

.factory(XWorkConverter.class, Scope.SINGLETON)
.factory(XWorkBasicConverter.class, Scope.SINGLETON)
.factory(ConversionPropertiesProcessor.class, DefaultConversionPropertiesProcessor.class, Scope.SINGLETON)
.factory(ConversionPropertiesProcessor.class, StrutsConversionPropertiesProcessor.class, Scope.SINGLETON)
.factory(ConversionFileProcessor.class, DefaultConversionFileProcessor.class, Scope.SINGLETON)
.factory(ConversionAnnotationProcessor.class, DefaultConversionAnnotationProcessor.class, Scope.SINGLETON)
.factory(TypeConverterCreator.class, StrutsTypeConverterCreator.class, Scope.SINGLETON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,32 @@
* specific language governing permissions and limitations
* under the License.
*/
package com.opensymphony.xwork2.conversion.impl;
package org.apache.struts2.conversion;

import com.opensymphony.xwork2.XWorkException;
import com.opensymphony.xwork2.conversion.ConversionPropertiesProcessor;
import com.opensymphony.xwork2.conversion.TypeConverter;
import com.opensymphony.xwork2.conversion.TypeConverterCreator;
import com.opensymphony.xwork2.conversion.TypeConverterHolder;
import com.opensymphony.xwork2.inject.EarlyInitializable;
import com.opensymphony.xwork2.inject.Initializable;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.struts2.StrutsException;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class DefaultConversionPropertiesProcessor implements ConversionPropertiesProcessor, EarlyInitializable {
public class StrutsConversionPropertiesProcessor implements ConversionPropertiesProcessor, EarlyInitializable {

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

private static final String STRUTS_DEFAULT_CONVERSION_PROPERTIES = "struts-default-conversion.properties";
private static final String XWORK_CONVERSION_PROPERTIES = "xwork-conversion.properties";
private static final String STRUTS_CONVERSION_PROPERTIES = "struts-conversion.properties";

private TypeConverterCreator converterCreator;
private TypeConverterHolder converterHolder;
Expand All @@ -56,8 +59,9 @@ public void setTypeConverterHolder(TypeConverterHolder converterHolder) {
@Override
public void init() {
LOG.debug("Processing default conversion properties files");
processRequired("struts-default-conversion.properties");
process("xwork-conversion.properties");
processRequired(STRUTS_DEFAULT_CONVERSION_PROPERTIES);
process(STRUTS_CONVERSION_PROPERTIES);
process(XWORK_CONVERSION_PROPERTIES);
}

public void process(String propsName) {
Expand All @@ -72,6 +76,10 @@ public void loadConversionProperties(String propsName, boolean require) {
try {
Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true);
while (resources.hasNext()) {
if (XWORK_CONVERSION_PROPERTIES.equals(propsName)) {
LOG.warn("Instead of using deprecated {} please use the new file name {}",
XWORK_CONVERSION_PROPERTIES, STRUTS_CONVERSION_PROPERTIES);
}
URL url = resources.next();
Properties props = new Properties();
props.load(url.openStream());
Expand All @@ -95,7 +103,7 @@ public void loadConversionProperties(String propsName, boolean require) {
}
} catch (IOException ex) {
if (require) {
throw new XWorkException("Cannot load conversion properties file: "+propsName, ex);
throw new StrutsException("Cannot load conversion properties file: " + propsName, ex);
} else {
LOG.debug("Cannot load conversion properties file: {}", propsName, ex);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/struts-default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@

<bean type="com.opensymphony.xwork2.conversion.impl.XWorkConverter" name="struts" class="com.opensymphony.xwork2.conversion.impl.XWorkConverter" />

<bean type="com.opensymphony.xwork2.conversion.ConversionPropertiesProcessor" name="struts" class="com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor" />
<bean type="com.opensymphony.xwork2.conversion.ConversionPropertiesProcessor" name="struts" class="org.apache.struts2.conversion.StrutsConversionPropertiesProcessor" />
<bean type="com.opensymphony.xwork2.conversion.ConversionFileProcessor" name="struts" class="com.opensymphony.xwork2.conversion.impl.DefaultConversionFileProcessor" />
<bean type="com.opensymphony.xwork2.conversion.ConversionAnnotationProcessor" name="struts" class="com.opensymphony.xwork2.conversion.impl.DefaultConversionAnnotationProcessor" />
<bean type="com.opensymphony.xwork2.conversion.TypeConverterCreator" name="struts" class="org.apache.struts2.conversion.StrutsTypeConverterCreator" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public void testStringToCustomTypeUsingCustomConverterFromProperties() throws Ex
Thread.currentThread().setContextClassLoader(new ClassLoader(cl) {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
if ("xwork-conversion.properties".equals(name)) {
if ("struts-conversion.properties".equals(name)) {
return new Enumeration<URL>() {
boolean done = false;
public boolean hasMoreElements() {
Expand All @@ -481,7 +481,7 @@ public URL nextElement() {
}

done = true;
return getClass().getResource("/com/opensymphony/xwork2/conversion/impl/test-xwork-conversion.properties");
return getClass().getResource("/com/opensymphony/xwork2/conversion/impl/test-struts-conversion.properties");
}
};
} else {
Expand Down

0 comments on commit fc81832

Please sign in to comment.