Author: cziegeler Date: Fri Jan 21 11:12:09 2005 New Revision: 125953 URL: http://svn.apache.org/viewcvs?view=rev&rev=125953 Log: Use Settings object Modified: cocoon/trunk/src/core/java/org/apache/cocoon/configuration/Settings.java cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java cocoon/trunk/src/java/org/apache/cocoon/servlet/SettingsHelper.java
Modified: cocoon/trunk/src/core/java/org/apache/cocoon/configuration/Settings.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/configuration/Settings.java?view=diff&rev=125953&p1=cocoon/trunk/src/core/java/org/apache/cocoon/configuration/Settings.java&r1=125952&p2=cocoon/trunk/src/core/java/org/apache/cocoon/configuration/Settings.java&r2=125953 ============================================================================== --- cocoon/trunk/src/core/java/org/apache/cocoon/configuration/Settings.java (original) +++ cocoon/trunk/src/core/java/org/apache/cocoon/configuration/Settings.java Fri Jan 21 11:12:09 2005 @@ -26,8 +26,19 @@ */ public class Settings { - protected String classloaderClassName; - protected boolean initClassloader; + /** + * Default value for [EMAIL PROTECTED] #isAllowReload()} parameter (false) + */ + public static final boolean ALLOW_RELOAD = false; + + /** + * Default value for [EMAIL PROTECTED] #isEnableUploads()} parameter (false) + */ + public static final boolean ENABLE_UPLOADS = false; + public static final boolean SAVE_UPLOADS_TO_DISK = true; + public static final int MAX_UPLOAD_SIZE = 10000000; // 10Mb + + protected boolean initClassloader = false; protected String[] forceProperties; protected String configuration; protected String loggingConfiguration; @@ -35,19 +46,41 @@ protected String accessLogger; protected String bootstrapLogLevel; protected String loggerClassName; - protected boolean allowReload; - protected String[] loadClasses; - protected boolean enableUploads; + + /** + * Allow reloading of cocoon by specifying the <code>cocoon-reload=true</code> parameter with a request + */ + protected boolean allowReload = ALLOW_RELOAD; + + protected String[] loadClasses; + + /** + * Allow processing of upload requests (mime/multipart) + */ + protected boolean enableUploads = ENABLE_UPLOADS; protected String uploadDirectory; - protected boolean autosaveUploads; + protected boolean autosaveUploads = SAVE_UPLOADS_TO_DISK; + // accepted values are deny|allow|rename - rename is default. protected String overwriteUploads; - protected int maxUploadSize; + protected int maxUploadSize = MAX_UPLOAD_SIZE; protected String cacheDirectory; protected String workDirectory; protected String[] extraClasspaths; protected String parentServiceManagerClassName; - protected boolean showTime; - protected boolean manageExceptions; + + /** + * Allow adding processing time to the response + */ + protected boolean showTime = false; + /** + * If true, processing time will be added as an HTML comment + */ + protected boolean hideShowTime = false; + /** + * If true or not set, this class will try to catch and handle all Cocoon exceptions. + * If false, it will rethrow them to the servlet container. + */ + protected boolean manageExceptions = true; protected String formEncoding; protected String log4jConfiguration; protected String overrideLogLevel; @@ -79,6 +112,18 @@ } /** + * @return Returns the hideShowTime. + */ + public boolean isHideShowTime() { + return this.hideShowTime; + } + /** + * @param hideShowTime The hideShowTime to set. + */ + public void setHideShowTime(boolean hideShowTime) { + this.hideShowTime = hideShowTime; + } + /** * @return Returns the allowReload. */ public boolean isAllowReload() { @@ -115,18 +160,6 @@ this.cacheDirectory = cacheDirectory; } /** - * @return Returns the classloaderClassName. - */ - public String getClassloaderClassName() { - return this.classloaderClassName; - } - /** - * @param classloaderClassName The classloaderClassName to set. - */ - public void setClassloaderClassName(String classloaderClassName) { - this.classloaderClassName = classloaderClassName; - } - /** * @return Returns the cocoonLogger. */ public String getCocoonLogger() { @@ -389,14 +422,35 @@ public void setOverrideLogLevel(String overrideLogLevel) { this.overrideLogLevel = overrideLogLevel; } + + public boolean isAllowOverwrite() { + if ("deny".equalsIgnoreCase(this.overwriteUploads)) { + return false; + } else if ("allow".equalsIgnoreCase(this.overwriteUploads)) { + return true; + } else { + // either rename is specified or unsupported value - default to rename. + return false; + } + } + public boolean isSilentlyRename() { + if ("deny".equalsIgnoreCase(this.overwriteUploads)) { + return false; + } else if ("allow".equalsIgnoreCase(this.overwriteUploads)) { + return false; // ignored in this case + } else { + // either rename is specified or unsupported value - default to rename. + return true; + } + } + /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return "Settings:\n"+ "- Configuration: " + this.configuration + "\n" + - "- Classloader: " + ( this.classloaderClassName == null ? "-" : this.classloaderClassName ) + "\n" + "- InitClassloader: " + this.initClassloader + "\n" + "- ForceProperties: " + ( this.forceProperties == null ? "-" : this.forceProperties.toString() ) + "\n" + "- Logging-Configuration: " + this.loggingConfiguration + "\n" + Modified: cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java?view=diff&rev=125953&p1=cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java&r1=125952&p2=cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java&r2=125953 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java Fri Jan 21 11:12:09 2005 @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,7 +65,6 @@ import org.apache.cocoon.util.IOUtils; import org.apache.cocoon.util.StringUtils; import org.apache.cocoon.util.log.CocoonLogFormatter; -import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.SystemUtils; import org.apache.log.ContextMap; import org.apache.log.output.ServletOutputLogTarget; @@ -122,56 +121,15 @@ */ protected DefaultContext appContext = new DefaultContext(); - - /** - * Default value for [EMAIL PROTECTED] #allowReload} parameter (false) - */ - protected static final boolean ALLOW_RELOAD = false; - - /** - * Allow reloading of cocoon by specifying the <code>cocoon-reload=true</code> parameter with a request - */ - protected boolean allowReload; - - - /** - * Allow adding processing time to the response - */ - protected boolean showTime; - - /** - * If true, processing time will be added as an HTML comment - */ - protected boolean hiddenShowTime; - - - /** - * Default value for [EMAIL PROTECTED] #enableUploads} parameter (false) - */ - private static final boolean ENABLE_UPLOADS = false; - private static final boolean SAVE_UPLOADS_TO_DISK = true; - private static final int MAX_UPLOAD_SIZE = 10000000; // 10Mb - - /** - * Allow processing of upload requests (mime/multipart) - */ - private boolean enableUploads; - private boolean autoSaveUploads; - private boolean allowOverwrite; - private boolean silentlyRename; - private int maxUploadSize; - private File uploadDir; private File workDir; private File cacheDir; private String containerEncoding; - private String defaultFormEncoding; protected ServletContext servletContext; /** The classloader that will be set as the context classloader if init-classloader is true */ protected ClassLoader classLoader = this.getClass().getClassLoader(); - protected boolean initClassLoader = false; private String parentServiceManagerClass; private String parentServiceManagerInitParam; @@ -183,12 +141,6 @@ protected String forceSystemProperty; /** - * If true or not set, this class will try to catch and handle all Cocoon exceptions. - * If false, it will rethrow them to the servlet container. - */ - private boolean manageExceptions; - - /** * This is the path to the servlet context (or the result * of calling getRealPath('/') on the ServletContext. * Note, that this can be null. @@ -210,6 +162,15 @@ protected Settings settings; /** + * Get the settings for Cocoon + * If a subclass uses different default values for settings, this method + * can be overwritten + */ + protected Settings getSettings() { + return SettingsHelper.getSettings(this.getServletConfig()); + } + + /** * Initialize this <code>CocoonServlet</code> instance. You will * notice that I have broken the init into sub methods to make it * easier to maintain (BL). The context is passed to a couple of @@ -226,14 +187,10 @@ super.init(conf); - // Check the init-classloader parameter only if it's not already true. - // This is useful for subclasses of this servlet that override the value - // initially set by this class (i.e. false). - if (!this.initClassLoader) { - this.initClassLoader = getInitParameterAsBoolean("init-classloader", false); - } - - if (this.initClassLoader) { + // initialize settings + this.settings = this.getSettings(); + + if (this.settings.isInitClassloader()) { // Force context classloader so that JAXP can work correctly // (see javax.xml.parsers.FactoryFinder.findClassLoader()) try { @@ -260,7 +217,7 @@ // first init the work-directory for the logger. // this is required if we are running inside a war file! - final String workDirParam = getInitParameter("work-directory"); + final String workDirParam = this.settings.getWorkDirectory(); if (workDirParam != null) { if (this.servletContextPath == null) { // No context path : consider work-directory as absolute @@ -317,8 +274,6 @@ this.appContext.put(ContextHelper.CONTEXT_ROOT_URL, this.servletContextURL); // Init logger - this.settings = SettingsHelper.getSettings(this.getServletConfig()); - initLogger(); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug(this.settings.toString()); @@ -341,7 +296,7 @@ } } - final String uploadDirParam = conf.getInitParameter("upload-directory"); + final String uploadDirParam = this.settings.getUploadDirectory(); if (uploadDirParam != null) { if (this.servletContextPath == null) { this.uploadDir = new File(uploadDirParam); @@ -368,27 +323,7 @@ this.uploadDir.mkdirs(); this.appContext.put(Constants.CONTEXT_UPLOAD_DIR, this.uploadDir); - this.enableUploads = getInitParameterAsBoolean("enable-uploads", ENABLE_UPLOADS); - - this.autoSaveUploads = getInitParameterAsBoolean("autosave-uploads", SAVE_UPLOADS_TO_DISK); - - String overwriteParam = getInitParameter("overwrite-uploads", "rename"); - // accepted values are deny|allow|rename - rename is default. - if ("deny".equalsIgnoreCase(overwriteParam)) { - this.allowOverwrite = false; - this.silentlyRename = false; - } else if ("allow".equalsIgnoreCase(overwriteParam)) { - this.allowOverwrite = true; - this.silentlyRename = false; // ignored in this case - } else { - // either rename is specified or unsupported value - default to rename. - this.allowOverwrite = false; - this.silentlyRename = true; - } - - this.maxUploadSize = getInitParameterAsInteger("upload-max-size", MAX_UPLOAD_SIZE); - - String cacheDirParam = conf.getInitParameter("cache-directory"); + String cacheDirParam = this.settings.getCacheDirectory(); if (cacheDirParam != null) { if (this.servletContextPath == null) { this.cacheDir = new File(cacheDirParam); @@ -423,17 +358,6 @@ } } - // get allow reload parameter, default is true - this.allowReload = getInitParameterAsBoolean("allow-reload", ALLOW_RELOAD); - - value = conf.getInitParameter("show-time"); - this.showTime = BooleanUtils.toBoolean(value) || (this.hiddenShowTime = "hide".equals(value)); - if (value == null) { - if (getLogger().isDebugEnabled()) { - getLogger().debug("show-time was not set - defaulting to false"); - } - } - parentServiceManagerClass = getInitParameter("parent-service-manager", null); if (parentServiceManagerClass != null) { int dividerPos = parentServiceManagerClass.indexOf('/'); @@ -444,16 +368,13 @@ } this.containerEncoding = getInitParameter("container-encoding", "ISO-8859-1"); - this.defaultFormEncoding = getInitParameter("form-encoding", "ISO-8859-1"); - this.appContext.put(Constants.CONTEXT_DEFAULT_ENCODING, this.defaultFormEncoding); - - this.manageExceptions = getInitParameterAsBoolean("manage-exceptions", true); + this.appContext.put(Constants.CONTEXT_DEFAULT_ENCODING, settings.getFormEncoding()); - this.requestFactory = new RequestFactory(this.autoSaveUploads, + this.requestFactory = new RequestFactory(settings.isAutosaveUploads(), this.uploadDir, - this.allowOverwrite, - this.silentlyRename, - this.maxUploadSize, + settings.isAllowOverwrite(), + settings.isSilentlyRename(), + settings.getMaxUploadSize(), this.containerEncoding); // Add the servlet configuration this.appContext.put(CONTEXT_SERVLET_CONFIG, conf); @@ -464,7 +385,7 @@ * Dispose Cocoon when servlet is destroyed */ public void destroy() { - if (this.initClassLoader) { + if (this.settings.isInitClassloader()) { try { Thread.currentThread().setContextClassLoader(this.classLoader); } catch (Exception e) { @@ -887,7 +808,7 @@ /* HACK for reducing class loader problems. */ /* example: xalan extensions fail if someone adds xalan jars in tomcat3.2.1/lib */ - if (this.initClassLoader) { + if (this.settings.isInitClassloader()) { try { Thread.currentThread().setContextClassLoader(this.classLoader); } catch (Exception e) { @@ -903,7 +824,7 @@ // get the request (wrapped if contains multipart-form data) HttpServletRequest request; try{ - if (this.enableUploads) { + if (this.settings.isEnableUploads()) { request = requestFactory.getServletRequest(req); } else { request = req; @@ -1059,12 +980,12 @@ if (contentType != null && contentType.equals("text/html")) { String showTime = request.getParameter(Constants.SHOWTIME_PARAM); - boolean show = this.showTime; + boolean show = this.settings.isShowTime(); if (showTime != null) { show = !showTime.equalsIgnoreCase("no"); } if (show) { - boolean hide = this.hiddenShowTime; + boolean hide = this.settings.isHideShowTime(); if (showTime != null) { hide = showTime.equalsIgnoreCase("hide"); } @@ -1115,8 +1036,8 @@ String uri, int errorStatus, String title, String message, String description, Exception e) - throws IOException { - if (this.manageExceptions) { + throws IOException { + if (this.settings.isManageExceptions()) { if (env != null) { env.tryResetResponse(); } else { @@ -1174,7 +1095,7 @@ String formEncoding = req.getParameter("cocoon-form-encoding"); if (formEncoding == null) { - formEncoding = this.defaultFormEncoding; + formEncoding = this.settings.getFormEncoding(); } env = new HttpEnvironment(uri, this.servletContextURL, @@ -1226,7 +1147,7 @@ /* HACK for reducing class loader problems. */ /* example: xalan extensions fail if someone adds xalan jars in tomcat3.2.1/lib */ - if (this.initClassLoader) { + if (this.settings.isInitClassloader()) { try { Thread.currentThread().setContextClassLoader(this.classLoader); } catch (Exception e) { @@ -1309,7 +1230,7 @@ */ private void getCocoon(final String pathInfo, final String reloadParam) throws ServletException { - if (this.allowReload) { + if (this.settings.isAllowReload()) { boolean reload = false; if (this.cocoon != null) { @@ -1374,31 +1295,6 @@ return defaultValue; } else { return result; - } - } - - /** Convenience method to access boolean servlet parameters */ - protected boolean getInitParameterAsBoolean(String name, boolean defaultValue) { - String value = getInitParameter(name); - if (value == null) { - if (getLogger() != null && getLogger().isDebugEnabled()) { - getLogger().debug(name + " was not set - defaulting to '" + defaultValue + "'"); - } - return defaultValue; - } - - return BooleanUtils.toBoolean(value); - } - - protected int getInitParameterAsInteger(String name, int defaultValue) { - String value = getInitParameter(name); - if (value == null) { - if (getLogger() != null && getLogger().isDebugEnabled()) { - getLogger().debug(name + " was not set - defaulting to '" + defaultValue + "'"); - } - return defaultValue; - } else { - return Integer.parseInt(value); } } Modified: cocoon/trunk/src/java/org/apache/cocoon/servlet/SettingsHelper.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/servlet/SettingsHelper.java?view=diff&rev=125953&p1=cocoon/trunk/src/java/org/apache/cocoon/servlet/SettingsHelper.java&r1=125952&p2=cocoon/trunk/src/java/org/apache/cocoon/servlet/SettingsHelper.java&r2=125953 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/servlet/SettingsHelper.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/servlet/SettingsHelper.java Fri Jan 21 11:12:09 2005 @@ -52,21 +52,27 @@ s.setLog4jConfiguration("context:/" + value); } - s.setClassloaderClassName(config.getInitParameter("classloader-class")); s.setInitClassloader(getInitParameterAsBoolean(config, "init-classloader", false)); s.setForceProperties(getInitParameterAsArray(config, "force-property")); s.setConfiguration(config.getInitParameter("configurations")); - s.setInitClassloader(getInitParameterAsBoolean(config, "allow-reload", false)); + s.setAllowReload(getInitParameterAsBoolean(config, "allow-reload", Settings.ALLOW_RELOAD)); s.setLoadClasses(getInitParameterAsArray(config, "load-class")); - s.setEnableUploads(getInitParameterAsBoolean(config, "init-classloader", false)); + s.setEnableUploads(getInitParameterAsBoolean(config, "enable-uploads", Settings.ENABLE_UPLOADS)); s.setUploadDirectory(config.getInitParameter("upload-directory")); - s.setAutosaveUploads(getInitParameterAsBoolean(config, "autosave-uploads", true)); + s.setAutosaveUploads(getInitParameterAsBoolean(config, "autosave-uploads", Settings.SAVE_UPLOADS_TO_DISK)); s.setOverwriteUploads(config.getInitParameter("overwrite-uploads")); - s.setMaxUploadSize(getInitParameterAsInteger(config, "upload-max-size", 10000000)); + s.setMaxUploadSize(getInitParameterAsInteger(config, "upload-max-size", Settings.MAX_UPLOAD_SIZE)); s.setCacheDirectory(config.getInitParameter("cache-directory")); s.setWorkDirectory(config.getInitParameter("work-directory")); s.setParentServiceManagerClassName(config.getInitParameter("parent-service-manager")); - s.setShowTime(getInitParameterAsBoolean(config, "show-time", false)); + value = config.getInitParameter("show-time"); + if ( value != null && value.equalsIgnoreCase("hide") ) { + s.setShowTime(true); + s.setHideShowTime(true); + } else { + s.setShowTime(getInitParameterAsBoolean(config, "show-time", false)); + s.setHideShowTime(false); + } s.setManageExceptions(getInitParameterAsBoolean(config, "manage-exceptions", true)); s.setFormEncoding(config.getInitParameter("form-encoding"));