Author: cbrisson Date: Wed Jun 20 09:21:22 2018 New Revision: 1833898 URL: http://svn.apache.org/viewvc?rev=1833898&view=rev Log: [tools] Review configuration:
* default tools not loaded by default by VelocityView * default config locations not checked if user provided a custom location (for velocity.properties and tools.xml) * dropped autoloading feature Modified: velocity/tools/trunk/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/WEB-INF/web.xml velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/EasyFactoryConfiguration.java velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java Modified: velocity/tools/trunk/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/WEB-INF/web.xml?rev=1833898&r1=1833897&r2=1833898&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/WEB-INF/web.xml (original) +++ velocity/tools/trunk/velocity-tools-examples/velocity-tools-examples-showcase/src/main/webapp/WEB-INF/web.xml Wed Jun 20 09:21:22 2018 @@ -27,6 +27,10 @@ <servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.VelocityLayoutServlet</servlet-class> <init-param> + <param-name>org.apache.velocity.tools.loadDefaults</param-name> + <param-value>true</param-value> + </init-param> + <init-param> <param-name>org.apache.velocity.tools.cleanConfiguration</param-name> <param-value>true</param-value> </init-param> Modified: velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java?rev=1833898&r1=1833897&r2=1833898&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java (original) +++ velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java Wed Jun 20 09:21:22 2018 @@ -48,8 +48,7 @@ public class ToolManager /** * Constructs an instance already configured to use the - * {@link ConfigurationUtils#getAutoLoaded()} configuration - * and any configuration specified via a "org.apache.velocity.tools" + * any configuration specified via a "org.apache.velocity.tools" * system property. */ public ToolManager() @@ -74,16 +73,12 @@ public class ToolManager public void autoConfigure(boolean includeDefaults) { - FactoryConfiguration config = - ConfigurationUtils.getAutoLoaded(includeDefaults); - // look for any specified via system property FactoryConfiguration sys = ConfigurationUtils.findFromSystemProperty(); if (sys != null) { - config.addConfiguration(sys); + configure(sys); } - configure(config); } public void configure(FactoryConfiguration config) Modified: velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java?rev=1833898&r1=1833897&r2=1833898&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java (original) +++ velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java Wed Jun 20 09:21:22 2018 @@ -48,12 +48,8 @@ public class ConfigurationUtils { public static final String GENERIC_DEFAULTS_PATH = "/org/apache/velocity/tools/generic/tools.xml"; - public static final String XML_DEFAULTS_PATH = - "/org/apache/velocity/tools/xml/tools.xml"; public static final String VIEW_DEFAULTS_PATH = "/org/apache/velocity/tools/view/tools.xml"; - public static final String STRUTS_DEFAULTS_PATH = - "/org/apache/velocity/tools/struts/tools.xml"; public static final String AUTOLOADED_XML_PATH = "tools.xml"; public static final String AUTOLOADED_PROPS_PATH = "tools.properties"; @@ -86,10 +82,8 @@ public class ConfigurationUtils new XmlFactoryConfiguration("ConfigurationUtils.getDefaultTools()"); config.read(GENERIC_DEFAULTS_PATH); - // xml tools, view tools and struts tools may not be available - config.read(XML_DEFAULTS_PATH, false); + // view tools may not be available config.read(VIEW_DEFAULTS_PATH, false); - config.read(STRUTS_DEFAULTS_PATH, false); // defaults should *always* be clean! clean(config); @@ -130,120 +124,6 @@ public class ConfigurationUtils } /** - * Returns a {@link FactoryConfiguration} including all default - * "VelocityStruts" tools available as well as the default "VelocityView" - * tools and "GenericTools". - * @throws {@link ConfigurationException} if a tools.xml is not found - * at the {@link #VIEW_DEFAULTS_PATH} or {@link #STRUTS_DEFAULTS_PATH}. - */ - public static FactoryConfiguration getVelocityStruts() - { - FileFactoryConfiguration config = - new XmlFactoryConfiguration("ConfigurationUtils.getVelocityStruts()"); - config.read(GENERIC_DEFAULTS_PATH); - config.read(VIEW_DEFAULTS_PATH); - config.read(STRUTS_DEFAULTS_PATH); - - // defaults should *always* be clean! - clean(config); - return config; - } - - /** - * Returns a {@link FactoryConfiguration} including all - * {@link #getDefaultTools()} as well as any tools that can be - * automatically loaded from "tools.xml" or "tools.properties" found - * at the root of the classpath or in the current directory. - * - * @see #getAutoLoaded(boolean includeDefaults) - */ - public static FactoryConfiguration getAutoLoaded() - { - return getAutoLoaded(true); - } - - /** - * Returns a {@link FactoryConfiguration} including all - * {@link #getDefaultTools()} (only if includeDefaults is {@code true}) as well as any tools that can be - * automatically loaded from "tools.xml" or "tools.properties" found - * at the root of the classpath or in the current directory. - * - * @see #getAutoLoaded(boolean includeDefaults) - */ - public static FactoryConfiguration getAutoLoaded(boolean includeDefaults) - { - return getAutoLoaded(includeDefaults, true, true); - } - - /** - * Returns a {@link FactoryConfiguration} composed, in order of the - * following configurations: - * <ul> - * <li>{@link #getDefaultTools()} (only if includeDefaults is {@code true})</li> - * <li>All "tools.xml" configurations found in the classpath root, in the order found (only if searchClasspath is true)</li> - * <li>All "tools.properties" configurations found in the classpath root, in the order found (only if searchClasspath is true)</li> - * <li>The "tools.xml" file in the current directory (if any, and only if searchCurrentDirectory is true)</li> - * <li>The "tools.properties" file in the current directory (if any, and only if searchCurrentDirectory is true)</li> - * </ul> - * If the includeDefaults parameter is null and no such files described above - * can be found, then the configuration returned by this method will be - * empty, but it should never be {@code null}. - */ - public static FactoryConfiguration getAutoLoaded(boolean includeDefaults, boolean searchClasspath, boolean searchCurrentDirectory) - { - FactoryConfiguration auto; - if (includeDefaults) - { - // start with the available defaults - auto = getDefaultTools(); - } - else - { - // start out blank - auto = new FactoryConfiguration("ConfigurationUtils.getAutoLoaded(false)"); - } - - //TODO: look for any Tools classes in the root of the classpath - - if (searchClasspath) - { - // look for all tools.xml in the classpath - FactoryConfiguration cpXml = findInClasspath(AUTOLOADED_XML_PATH); - if (cpXml != null) - { - auto.addConfiguration(cpXml); - } - - // look for all tools.properties in the classpath - FactoryConfiguration cpProps = findInClasspath(AUTOLOADED_PROPS_PATH); - if (cpProps != null) - { - auto.addConfiguration(cpProps); - } - } - - if (searchCurrentDirectory) - { - // look for tools.xml in the current file system - FactoryConfiguration fsXml = findInFileSystem(AUTOLOADED_XML_PATH); - if (fsXml != null) - { - auto.addConfiguration(fsXml); - } - - // look for tools.properties in the file system - FactoryConfiguration fsProps = findInFileSystem(AUTOLOADED_PROPS_PATH); - if (fsProps != null) - { - auto.addConfiguration(fsProps); - } - } - - // return the config we've accumulated - return auto; - } - - /** * Returns a {@link FactoryConfiguration} loaded from the path specified * in the "org.apache.velocity.tools" system property (if any). * If no such property has been set {@code null} will be returned. @@ -262,23 +142,18 @@ public class ConfigurationUtils /** * Returns a new, standard {@link ToolboxFactory} configured - * with the results of both {@link #getAutoLoaded()} and - * {@link #findFromSystemProperty()}. + * with the results of {@link #findFromSystemProperty()}. */ public static ToolboxFactory createFactory() { - // get the automatically loaded config(s) - FactoryConfiguration auto = getAutoLoaded(); - // include any config specified via system property FactoryConfiguration sys = findFromSystemProperty(); + + ToolboxFactory factory = new ToolboxFactory(); if (sys != null) { - auto.addConfiguration(sys); + factory.configure(sys); } - - ToolboxFactory factory = new ToolboxFactory(); - factory.configure(auto); return factory; } Modified: velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/EasyFactoryConfiguration.java URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/EasyFactoryConfiguration.java?rev=1833898&r1=1833897&r2=1833898&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/EasyFactoryConfiguration.java (original) +++ velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/EasyFactoryConfiguration.java Wed Jun 20 09:21:22 2018 @@ -145,7 +145,7 @@ public class EasyFactoryConfiguration ex public EasyFactoryConfiguration autoLoad(boolean includeDefaults) { - addConfiguration(ConfigurationUtils.getAutoLoaded(includeDefaults)); + addConfiguration(ConfigurationUtils.getDefaultTools()); addedDefaults = true; return this; } Modified: velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java?rev=1833898&r1=1833897&r2=1833898&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java (original) +++ velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java Wed Jun 20 09:21:22 2018 @@ -156,22 +156,9 @@ public class ConfigTests { public @Test void testAutoConfig() { - FactoryConfiguration autoMinusDef = ConfigurationUtils.getAutoLoaded(false); - assertValid(autoMinusDef); - - assertValid(autoMinusDef); - assertConfigEquals(getBaseConfig(), autoMinusDef); - - FactoryConfiguration auto = ConfigurationUtils.getAutoLoaded(); - assertValid(auto); - // get the default tools FactoryConfiguration def = ConfigurationUtils.getDefaultTools(); assertValid(def); - // add the autoloaded ones (sans defaults) onto the default - def.addConfiguration(autoMinusDef); - // and see that it comes out the same - assertConfigEquals(auto, def); } public @Test void testBadData() Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java?rev=1833898&r1=1833897&r2=1833898&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java (original) +++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java Wed Jun 20 09:21:22 2018 @@ -149,7 +149,7 @@ public class VelocityView extends ViewTo /** * Controls loading of available default tool configurations - * provided by VelocityTools. The default is true. + * provided by VelocityTools. The default is false. */ public static final String LOAD_DEFAULTS_KEY = "org.apache.velocity.tools.loadDefaults"; @@ -350,9 +350,19 @@ public class VelocityView extends ViewTo velocity.setProperties(appProperties); } + // check for a custom location for servlet-wide user props + String servletPropsPath = config.getInitParameter(PROPERTIES_KEY); + if (servletPropsPath != null && !USER_PROPERTIES_PATH.equals(servletPropsPath) && (appPropsPath == null || !appPropsPath.equals(servletPropsPath))) + { + boolean isInWebInf = servletPropsPath.startsWith("/WEB-INF") || servletPropsPath.startsWith("WEB-INF"); + Properties servletProperties = getProperties(DEFAULT_PROPERTIES_PATH, true, !isInWebInf, isInWebInf); + getLog().debug("Configuring Velocity with properties at: {}", servletPropsPath); + velocity.setProperties(servletProperties); + } + // check for servlet-wide user props in the config init params at the // conventional location, and be silent if they're missing - if (!USER_PROPERTIES_PATH.equals(appPropsPath)) + if (appPropsPath == null && servletPropsPath == null) { Properties appProperties = getProperties(USER_PROPERTIES_PATH, false, false, true); if (appProperties != null) @@ -362,16 +372,6 @@ public class VelocityView extends ViewTo } } - // check for a custom location for servlet-wide user props - String servletPropsPath = config.getInitParameter(PROPERTIES_KEY); - if (servletPropsPath != null && !USER_PROPERTIES_PATH.equals(servletPropsPath) && (appPropsPath == null || !appPropsPath.equals(servletPropsPath))) - { - boolean isInWebInf = servletPropsPath.startsWith("/WEB-INF") || servletPropsPath.startsWith("WEB-INF"); - Properties servletProperties = getProperties(DEFAULT_PROPERTIES_PATH, true, !isInWebInf, isInWebInf); - getLog().debug("Configuring Velocity with properties at: {}", servletPropsPath); - velocity.setProperties(servletProperties); - } - /* now that velocity engine is initialized, re-initialize our logger so that it takes potentially provided configuration attributes into account @@ -383,7 +383,6 @@ public class VelocityView extends ViewTo * Here's the configuration lookup/loading order: * <ol> * <li>If loadDefaults is true, {@link ConfigurationUtils#getDefaultTools()}</li> - * <li>{@link ConfigurationUtils#getAutoLoaded}(false)</li> * <li>Config file optionally specified by servletContext {@code org.apache.velocity.tools} init-param</li> * <li>Config file optionally at {@code /WEB-INF/tools.xml} (conventional location)</li> * <li>Config file optionally specified by servlet {@code org.apache.velocity.tools} init-param</li> @@ -400,41 +399,54 @@ public class VelocityView extends ViewTo FactoryConfiguration factoryConfig = new FactoryConfiguration("VelocityView.configure(config,factory)"); String loadDefaults = config.findInitParameter(LOAD_DEFAULTS_KEY); - if (loadDefaults == null || "true".equalsIgnoreCase(loadDefaults)) + if (loadDefaults == null || "false".equalsIgnoreCase(loadDefaults)) { - // add all available default tools - getLog().trace("Loading default tools configuration..."); - factoryConfig.addConfiguration(ConfigurationUtils.getDefaultTools()); + // let the user know that the defaults were suppressed + getLog().debug("Default tools not loaded."); } else { - // let the user know that the defaults were suppressed - getLog().debug("Default tools configuration has been suppressed."); + // add all available default tools + getLog().trace("Loading default tools configuration..."); + factoryConfig.addConfiguration(ConfigurationUtils.getDefaultTools()); } - // this gets the auto loaded config from the classpath - // this doesn't include defaults since they're handled already - FactoryConfiguration autoLoaded = ConfigurationUtils.getAutoLoaded(false, true, false); - factoryConfig.addConfiguration(autoLoaded); - // check for application-wide user config in the context init params String appToolsPath = servletContext.getInitParameter(TOOLS_KEY); - setConfig(factoryConfig, appToolsPath, true); - - // check for user configuration at the conventional location, - // and be silent if they're missing - setConfig(factoryConfig, USER_TOOLS_PATH, false); + if (appToolsPath != null) + { + FactoryConfiguration appToolsConfig = getConfiguration(appToolsPath, true); + factoryConfig.addConfiguration(appToolsConfig); + getLog().debug("Loaded configuration from: {}", appToolsPath); + } // check for a custom location for servlet-wide user props String servletToolsPath = config.getInitParameter(TOOLS_KEY); - setConfig(factoryConfig, servletToolsPath, true); + if (servletToolsPath != null) + { + FactoryConfiguration servletToolsConfig = getConfiguration(appToolsPath, true); + factoryConfig.addConfiguration(servletToolsConfig); + getLog().debug("Loaded configuration from: {}", servletToolsPath); + } + + if (appToolsPath == null && servletToolsPath == null) + { + // check for user configuration at the conventional location, + // and be silent if they're missing + FactoryConfiguration standardLocationConfiguration = getConfiguration(USER_TOOLS_PATH, false); + if (standardLocationConfiguration != null) + { + factoryConfig.addConfiguration(standardLocationConfiguration); + getLog().debug("Loaded configuration from: {}", USER_TOOLS_PATH); + } + } // check for "injected" configuration in application attributes FactoryConfiguration injected = ServletUtils.getConfiguration(servletContext); if (injected != null) { - getLog().debug("Adding configuration instance in servletContext attributes as '{}'", TOOLS_KEY); factoryConfig.addConfiguration(injected); + getLog().debug("Added configuration instance in servletContext attributes as '{}'", TOOLS_KEY); } // see if we should only keep valid tools, data, and properties Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java?rev=1833898&r1=1833897&r2=1833898&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java (original) +++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java Wed Jun 20 09:21:22 2018 @@ -62,9 +62,8 @@ public class ViewToolManager extends Too private String toolboxKey = DEFAULT_TOOLBOX_KEY; /** - * Constructs an instance already configured to use the - * {@link ConfigurationUtils#getAutoLoaded()()} configuration - * and any configuration specified via a "org.apache.velocity.tools" + * Constructs an instance already configured to use + * any configuration specified via a "org.apache.velocity.tools" * system property. */ public ViewToolManager(ServletContext app)