Commit 98bfafd1 authored by Samuel Trégouët's avatar Samuel Trégouët Committed by Mathieu Lirzin

Improved: Resolve classpath conflict on `freemarkerTransforms.properties`

(OFBIZ-11161)

All files in {compoment}/config directories are placed in jar
root (cf. sourceSets definition in build.gradle) but there are 4 different
`freemarkerTransforms.properties` in ofbiz (content, product, webapp, widget).
This works fine because we are adding config directories in classpath (through
classpath tag in ofbiz-component.xml), but this is not really intuitive. So to
make things clearer this patch move `freemarkerTransforms.properties` into
`src/main/resources` with a namespace package and `FreeMarkerWorker.java` is
modified accordingly.
parent 62c617ee
......@@ -23,19 +23,21 @@ import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Stream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.ofbiz.base.location.FlexibleLocation;
import org.apache.ofbiz.base.component.ComponentConfig;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilGenerics;
......@@ -68,6 +70,8 @@ import freemarker.template.Version;
* FreeMarkerWorker - Freemarker Template Engine Utilities.
*/
public final class FreeMarkerWorker {
/** The template used to retrieved Freemarker transforms from multiple component classpaths. */
private static final String TRANSFORMS_PROPERTIES = "org/apache/ofbiz/%s/freemarkerTransforms.properties";
public static final String module = FreeMarkerWorker.class.getName();
......@@ -122,27 +126,30 @@ public final class FreeMarkerWorker {
}
// Transforms properties file set up as key=transform name, property=transform class name
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> resources;
try {
resources = loader.getResources("freemarkerTransforms.properties");
} catch (IOException e) {
Debug.logError(e, "Could not load list of freemarkerTransforms.properties", module);
throw UtilMisc.initCause(new InternalError(e.getMessage()), e);
}
while (resources.hasMoreElements()) {
URL propertyURL = resources.nextElement();
Debug.logInfo("loading properties: " + propertyURL, module);
Properties props = UtilProperties.getProperties(propertyURL);
if (UtilValidate.isEmpty(props)) {
Debug.logError("Unable to locate properties file " + propertyURL, module);
transformsURL(loader).forEach(url -> {
Properties props = UtilProperties.getProperties(url);
if (props == null) {
Debug.logError("Unable to load properties file " + url, module);
} else {
Debug.logInfo("loading properties: " + url, module);
loadTransforms(loader, props, newConfig);
}
}
});
return newConfig;
}
/**
* Provides the sequence of existing {@code freemarkerTransforms.properties} files.
*
* @return a stream of resource location.
*/
private static Stream<URL> transformsURL(ClassLoader loader) {
return ComponentConfig.components()
.map(cc -> String.format(TRANSFORMS_PROPERTIES, cc.getComponentName()))
.map(loader::getResource)
.filter(Objects::nonNull);
}
private static void loadTransforms(ClassLoader loader, Properties props, Configuration config) {
for (Object object : props.keySet()) {
String key = (String) object;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment