Author: cziegeler Date: Mon Apr 4 07:12:40 2005 New Revision: 160046 URL: http://svn.apache.org/viewcvs?view=rev&rev=160046 Log: Fix bugs in init code
Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.java cocoon/trunk/src/core/java/org/apache/cocoon/core/CoreUtil.java cocoon/trunk/src/core/java/org/apache/cocoon/util/log/LoggingHelper.java cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.java?view=diff&r1=160045&r2=160046 ============================================================================== --- cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.java (original) +++ cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.java Mon Apr 4 07:12:40 2005 @@ -17,15 +17,12 @@ package org.apache.cocoon.core; import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Properties; import org.apache.avalon.framework.CascadingRuntimeException; import org.apache.avalon.framework.context.Context; @@ -71,8 +68,9 @@ this.settings = null; } - public Core(Settings s) { + public Core(Settings s, Context c) { this.settings = s; + this.context = c; } /* (non-Javadoc) @@ -185,53 +183,6 @@ } catch (ContextException ce) { throw new CascadingRuntimeException("Unable to get the settings object from the context.", ce); } - } - - /** - * Get the settings for Cocoon - * @param env This provides access to various parts of the used environment. - */ - public static Settings createSettings(BootstrapEnvironment env) { - // create an empty settings objects - final Settings s = new Settings(); - - String additionalPropertyFile = System.getProperty(Settings.PROPERTY_USER_SETTINGS); - - // read cocoon-settings.properties - if available - InputStream propsIS = env.getInputStream("cocoon-settings.properties"); - if ( propsIS != null ) { - env.log("Reading settings from 'cocoon-settings.properties'"); - final Properties p = new Properties(); - try { - p.load(propsIS); - propsIS.close(); - s.fill(p); - additionalPropertyFile = p.getProperty(Settings.PROPERTY_USER_SETTINGS, additionalPropertyFile); - } catch (IOException ignore) { - env.log("Unable to read 'cocoon-settings.properties'.", ignore); - env.log("Continuing initialization."); - } - } - // fill from the environment configuration, like web.xml etc. - env.configure(s); - - // read additional properties file - if ( additionalPropertyFile != null ) { - env.log("Reading user settings from '" + additionalPropertyFile + "'"); - final Properties p = new Properties(); - try { - FileInputStream fis = new FileInputStream(additionalPropertyFile); - p.load(fis); - fis.close(); - } catch (IOException ignore) { - env.log("Unable to read '" + additionalPropertyFile + "'.", ignore); - env.log("Continuing initialization."); - } - } - // now overwrite with system properties - s.fill(System.getProperties()); - - return s; } public static interface BootstrapEnvironment { Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/CoreUtil.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/CoreUtil.java?view=diff&r1=160045&r2=160046 ============================================================================== --- cocoon/trunk/src/core/java/org/apache/cocoon/core/CoreUtil.java (original) +++ cocoon/trunk/src/core/java/org/apache/cocoon/core/CoreUtil.java Mon Apr 4 07:12:40 2005 @@ -17,13 +17,16 @@ package org.apache.cocoon.core; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Constructor; import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; import java.util.Iterator; import java.util.Map; +import java.util.Properties; import org.apache.avalon.excalibur.logger.Log4JLoggerManager; import org.apache.avalon.excalibur.logger.LogKitLoggerManager; @@ -44,6 +47,7 @@ import org.apache.cocoon.components.ContextHelper; import org.apache.cocoon.configuration.ConfigurationBuilder; import org.apache.cocoon.configuration.Settings; +import org.apache.cocoon.core.Core.BootstrapEnvironment; import org.apache.cocoon.core.source.SimpleSourceResolver; import org.apache.cocoon.matching.helpers.WildcardHelper; import org.apache.cocoon.util.ClassUtils; @@ -66,6 +70,8 @@ /** Parameter map for the context protocol */ protected static final Map CONTEXT_PARAMETERS = Collections.singletonMap("force-traversable", Boolean.TRUE); + protected final static String CORE_KEY = Core.class.getName(); + /** The callback to the real environment */ protected final Core.BootstrapEnvironment env; @@ -75,26 +81,22 @@ /** The settings */ protected final Settings settings; - private Logger log; - private LoggerManager loggerManager; + /** The parent service manager TODO This will be made protected*/ + public final ServiceManager parentManager; + + /** TODO This will be made protected */ + public Logger log; + /** TODO This will be made protected */ + public LoggerManager loggerManager; - public CoreUtil(Core.BootstrapEnvironment e) + public CoreUtil(Core.BootstrapEnvironment environment) throws Exception { - this.env = e; + this.env = environment; // create settings - this.settings = Core.createSettings(this.env); + this.settings = createSettings(this.env); this.appContext.put(Core.CONTEXT_SETTINGS, this.settings); - this.createRootServiceManager(); - } - - /** - * Bootstrap Cocoon Service Manager. - */ - public ServiceManager createRootServiceManager() - throws Exception { - if (this.settings.isInitClassloader()) { // Force context classloader so that JAXP can work correctly // (see javax.xml.parsers.FactoryFinder.findClassLoader()) @@ -108,7 +110,7 @@ // add root url try { appContext.put(ContextHelper.CONTEXT_ROOT_URL, - new URL(env.getContextURL())); + new URL(this.env.getContextURL())); } catch (MalformedURLException ignore) { // we simply ignore this } @@ -151,8 +153,8 @@ // Output some debug info if (this.log.isDebugEnabled()) { - this.log.debug("Context URL: " + env.getContextURL()); - this.log.debug("Writeable Context: " + env.getContextForWriting()); + this.log.debug("Context URL: " + this.env.getContextURL()); + this.log.debug("Writeable Context: " + this.env.getContextForWriting()); if (workDirParam != null) { this.log.debug("Using work-directory " + workDir); } else { @@ -163,7 +165,7 @@ final String uploadDirParam = this.settings.getUploadDirectory(); File uploadDir; if (uploadDirParam != null) { - if (env.getContextForWriting() == null) { + if (this.env.getContextForWriting() == null) { uploadDir = new File(uploadDirParam); } else { // Context path exists : is upload-directory absolute ? @@ -173,7 +175,7 @@ uploadDir = uploadDirParamFile; } else { // No : consider it relative to context path - uploadDir = new File(env.getContextForWriting(), uploadDirParam); + uploadDir = new File(this.env.getContextForWriting(), uploadDirParam); } } if (this.log.isDebugEnabled()) { @@ -192,7 +194,7 @@ String cacheDirParam = this.settings.getCacheDirectory(); File cacheDir; if (cacheDirParam != null) { - if (env.getContextForWriting() == null) { + if (this.env.getContextForWriting() == null) { cacheDir = new File(cacheDirParam); } else { // Context path exists : is cache-directory absolute ? @@ -202,7 +204,7 @@ cacheDir = cacheDirParamFile; } else { // No : consider it relative to context path - cacheDir = new File(env.getContextForWriting(), cacheDirParam); + cacheDir = new File(this.env.getContextForWriting(), cacheDirParam); } } if (this.log.isDebugEnabled()) { @@ -227,18 +229,30 @@ this.settings.setConfiguration(u.toExternalForm()); this.appContext.put(Constants.CONTEXT_CONFIG_URL, u); + // set encoding + this.appContext.put(Constants.CONTEXT_DEFAULT_ENCODING, settings.getFormEncoding()); + // create parent service manager final ServiceManager parent = this.getParentServiceManager(); - // set encoding - this.appContext.put(Constants.CONTEXT_DEFAULT_ENCODING, settings.getFormEncoding()); + // create a service manager + this.parentManager = new RootServiceManager(parent, this.createCore()); + } - // create new Core - final Core cocoon = new Core(this.settings); + public Core getCore() { + try { + return (Core)this.parentManager.lookup(CORE_KEY); + } catch (ServiceException ignore) { + // this can never happen! + throw new RuntimeException("Fatal error: no Cocoon core available."); + } + } - return new RootServiceManager(parent, cocoon); + protected Core createCore() { + final Core c = new Core(this.settings, this.appContext); + return c; } - + /** * Instatiates the parent service manager, as specified in the * parent-service-manager init parameter. @@ -277,15 +291,67 @@ return parentServiceManager; } + /** + * Get the settings for Cocoon + * @param env This provides access to various parts of the used environment. + * TODO Make a non-static, protected method out of this + */ + public static Settings createSettings(BootstrapEnvironment env) { + // create an empty settings objects + final Settings s = new Settings(); + + String additionalPropertyFile = System.getProperty(Settings.PROPERTY_USER_SETTINGS); + + // read cocoon-settings.properties - if available + InputStream propsIS = env.getInputStream("cocoon-settings.properties"); + if ( propsIS != null ) { + env.log("Reading settings from 'cocoon-settings.properties'"); + final Properties p = new Properties(); + try { + p.load(propsIS); + propsIS.close(); + s.fill(p); + additionalPropertyFile = p.getProperty(Settings.PROPERTY_USER_SETTINGS, additionalPropertyFile); + } catch (IOException ignore) { + env.log("Unable to read 'cocoon-settings.properties'.", ignore); + env.log("Continuing initialization."); + } + } + // fill from the environment configuration, like web.xml etc. + env.configure(s); + + // read additional properties file + if ( additionalPropertyFile != null ) { + env.log("Reading user settings from '" + additionalPropertyFile + "'"); + final Properties p = new Properties(); + try { + FileInputStream fis = new FileInputStream(additionalPropertyFile); + p.load(fis); + fis.close(); + } catch (IOException ignore) { + env.log("Unable to read '" + additionalPropertyFile + "'.", ignore); + env.log("Continuing initialization."); + } + } + // now overwrite with system properties + s.fill(System.getProperties()); + + return s; + } + protected void initLogger() { final DefaultContext subcontext = new DefaultContext(this.appContext); subcontext.put("context-work", new File(this.settings.getWorkDirectory())); - if (this.env.getContextURL() == null) { + if (this.env.getContextForWriting() == null) { File logSCDir = new File(this.settings.getWorkDirectory(), "log"); logSCDir.mkdirs(); subcontext.put("context-root", logSCDir.toString()); } else { - subcontext.put("context-root", this.env.getContextURL()); + try { + subcontext.put("context-root", this.env.getContextForWriting().toURL().toExternalForm()); + } catch (MalformedURLException ignore) { + // we simply ignore this + } } this.env.configureLoggingContext(subcontext); @@ -306,8 +372,7 @@ defaultHierarchy.setErrorHandler(errorHandler); defaultHierarchy.setDefaultLogTarget(this.env.getDefaultLogTarget()); defaultHierarchy.setDefaultPriority(logPriority); - final Logger logger = new LogKitLogger(Hierarchy.getDefaultHierarchy() - .getLoggerFor("")); + final Logger logger = new LogKitLogger(Hierarchy.getDefaultHierarchy().getLoggerFor("")); // we can't pass the context-root to our resolver Object value = null; @@ -490,8 +555,6 @@ public static final class RootServiceManager implements ServiceManager { - protected final static String CORE_KEY = Core.class.getName(); - protected final ServiceManager parent; protected final Core cocoon; Modified: cocoon/trunk/src/core/java/org/apache/cocoon/util/log/LoggingHelper.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/util/log/LoggingHelper.java?view=diff&r1=160045&r2=160046 ============================================================================== --- cocoon/trunk/src/core/java/org/apache/cocoon/util/log/LoggingHelper.java (original) +++ cocoon/trunk/src/core/java/org/apache/cocoon/util/log/LoggingHelper.java Mon Apr 4 07:12:40 2005 @@ -48,7 +48,7 @@ import org.apache.log4j.LogManager; /** -* +* TODO Delete this class as soon as the CoreUtil is used * @version SVN $Id$ */ public class LoggingHelper { 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&r1=160045&r2=160046 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java Mon Apr 4 07:12:40 2005 @@ -31,6 +31,7 @@ import org.apache.cocoon.components.notification.Notifying; import org.apache.cocoon.configuration.Settings; import org.apache.cocoon.core.Core; +import org.apache.cocoon.core.CoreUtil; import org.apache.cocoon.environment.Context; import org.apache.cocoon.environment.Environment; import org.apache.cocoon.environment.http.HttpContext; @@ -125,7 +126,7 @@ /** * Avalon application context */ - protected DefaultContext appContext = new DefaultContext(); + protected DefaultContext appContext; private String containerEncoding; @@ -213,11 +214,40 @@ } } + try { + // FIXME (VG): We shouldn't have to specify these. Need to override + // jaxp implementation of weblogic before initializing logger. + // This piece of code is also required in the Cocoon class. + String value = System.getProperty("javax.xml.parsers.SAXParserFactory"); + if (value != null && value.startsWith("weblogic")) { + System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl"); + System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); + } + } catch (Exception e) { + // Ignore security exception + System.out.println("CocoonServlet: Could not check system properties, got: " + e); + } + // initialize settings Core.BootstrapEnvironment env = new ServletBootstrapEnvironment(conf, this.classLoader, this.servletContextPath, this.servletContextURL); - this.settings = Core.createSettings(env); - + /* + try { + CoreUtil util = new CoreUtil(env); + this.settings = util.getCore().getSettings(); + this.appContext = (DefaultContext)util.getCore().getContext(); + this.log = util.log; + this.loggerManager = util.loggerManager; + this.parentServiceManager = util.parentManager; + } catch (Exception e) { + if ( e instanceof ServletException ) { + throw (ServletException)e; + } + throw new ServletException(e); + } + */ + this.settings = CoreUtil.createSettings(env); + this.appContext = new DefaultContext(); this.appContext.put(Core.CONTEXT_SETTINGS, this.settings); if (this.settings.isInitClassloader()) { @@ -230,20 +260,6 @@ } } - try { - // FIXME (VG): We shouldn't have to specify these. Need to override - // jaxp implementation of weblogic before initializing logger. - // This piece of code is also required in the Cocoon class. - String value = System.getProperty("javax.xml.parsers.SAXParserFactory"); - if (value != null && value.startsWith("weblogic")) { - System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl"); - System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); - } - } catch (Exception e) { - // Ignore security exception - System.out.println("CocoonServlet: Could not check system properties, got: " + e); - } - this.appContext.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new HttpContext(this.servletContext)); // first init the work-directory for the logger. @@ -380,9 +396,9 @@ } } - this.containerEncoding = getInitParameter("container-encoding", "ISO-8859-1"); this.appContext.put(Constants.CONTEXT_DEFAULT_ENCODING, settings.getFormEncoding()); + this.containerEncoding = getInitParameter("container-encoding", "ISO-8859-1"); this.requestFactory = new RequestFactory(settings.isAutosaveUploads(), new File(settings.getUploadDirectory()), settings.isAllowOverwrite(), @@ -390,7 +406,7 @@ settings.getMaxUploadSize(), this.containerEncoding); // Add the servlet configuration - this.appContext.put(CONTEXT_SERVLET_CONFIG, conf); + //this.appContext.put(CONTEXT_SERVLET_CONFIG, conf); createCocoon(); if (this.exception == null) { this.servletContext.log("Apache Cocoon " + Constants.VERSION + " is up and ready."); @@ -1349,14 +1365,14 @@ * @see org.apache.cocoon.core.Core.BootstrapEnvironment#configureLoggingContext(org.apache.avalon.framework.context.DefaultContext) */ public void configureLoggingContext(DefaultContext context) { - context.put(CONTEXT_SERVLET_CONFIG, this.config.getServletContext()); + context.put(CONTEXT_SERVLET_CONFIG, this.config); } /** * @see org.apache.cocoon.core.Core.BootstrapEnvironment#configure(org.apache.avalon.framework.context.DefaultContext) */ public void configure(DefaultContext context) { - context.put(CONTEXT_SERVLET_CONFIG, this.config.getServletContext()); + context.put(CONTEXT_SERVLET_CONFIG, this.config); } /**