You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/02/13 20:14:09 UTC

svn commit: r377469 - in /cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon: Cocoon.java SpringCocoon.java core/CoreUtil.java core/container/spring/ApplicationContextFactory.java

Author: cziegeler
Date: Mon Feb 13 11:14:08 2006
New Revision: 377469

URL: http://svn.apache.org/viewcvs?rev=377469&view=rev
Log:
Move spring container creation out of Cocoon object and start new spring based Cocoon implementation

Added:
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/SpringCocoon.java   (with props)
Modified:
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/Cocoon.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/CoreUtil.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ApplicationContextFactory.java

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/Cocoon.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/Cocoon.java?rev=377469&r1=377468&r2=377469&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/Cocoon.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/Cocoon.java Mon Feb 13 11:14:08 2006
@@ -22,8 +22,6 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.servlet.ServletConfig;
-
 import org.apache.avalon.excalibur.logger.LoggerManageable;
 import org.apache.avalon.excalibur.logger.LoggerManager;
 import org.apache.avalon.framework.activity.Disposable;
@@ -50,21 +48,15 @@
 import org.apache.cocoon.core.Core;
 import org.apache.cocoon.core.Settings;
 import org.apache.cocoon.core.container.RoleManager;
-import org.apache.cocoon.core.container.spring.ApplicationContextFactory;
-import org.apache.cocoon.core.container.spring.AvalonEnvironment;
-import org.apache.cocoon.core.container.spring.ConfigReader;
-import org.apache.cocoon.core.container.spring.ConfigurationInfo;
 import org.apache.cocoon.environment.Environment;
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.environment.Session;
 import org.apache.cocoon.environment.internal.EnvironmentHelper;
-import org.apache.cocoon.servlet.CocoonServlet;
 import org.apache.commons.lang.SystemUtils;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.impl.URLSource;
-import org.springframework.context.ApplicationContext;
 import org.xml.sax.InputSource;
 
 /**
@@ -174,19 +166,6 @@
         this.loggerManager = loggerManager;
     }
 
-    protected void testSpringContainer() throws Exception {
-        System.out.println("Setting up test Spring container...");
-        AvalonEnvironment env = new AvalonEnvironment();
-        env.context = this.context;
-        env.core = this.core;
-        env.logger = this.getLogger();
-        env.servletContext = ((ServletConfig)this.context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG)).getServletContext();
-        env.settings = this.core.getSettings();
-        ApplicationContext rootContext = ApplicationContextFactory.createRootApplicationContext(env);
-        ConfigurationInfo result = ConfigReader.readConfiguration(this.configurationFile.getURI(), env);
-        ApplicationContext mainContext = ApplicationContextFactory.createApplicationContext(env, result, rootContext);
-        System.out.println("Getting core cocoon processor context: " + mainContext.getBean(Core.ROLE));
-    }
     /**
      * @see org.apache.avalon.framework.activity.Initializable#initialize()
      */
@@ -203,9 +182,6 @@
             throw new ConfigurationException(
                     "Could not open configuration file: " + settings.getConfiguration(), e);
         }
-
-        // Test setup spring container
-        this.testSpringContainer();
 
         this.serviceManager = new CocoonServiceManager(this.parentServiceManager);
         ContainerUtil.enableLogging(this.serviceManager, this.rootLogger.getChildLogger("manager"));

Added: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/SpringCocoon.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/SpringCocoon.java?rev=377469&view=auto
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/SpringCocoon.java (added)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/SpringCocoon.java Mon Feb 13 11:14:08 2006
@@ -0,0 +1,325 @@
+/*
+ * Copyright 1999-2006 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon;
+
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.core.Core;
+import org.apache.cocoon.environment.Environment;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.Session;
+import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.commons.lang.SystemUtils;
+
+/**
+ * The Cocoon Object is the main Kernel for the entire Cocoon system.
+ *
+ * @version $Id$
+ */
+public class SpringCocoon
+       implements Processor {
+
+    /** Active request count. */
+    private volatile int activeRequestCount;
+
+    /** The processor used to process the requests. */
+    protected final Processor processor;
+
+    /** The environment helper. */
+    protected final EnvironmentHelper environmentHelper;
+
+    /** An optional component that is called before and after processing all requests. */
+    protected RequestListener requestListener;
+
+    /** Processor attributes. */
+    protected Map processorAttributes = new HashMap();
+
+    /** The service manager. */
+    protected final ServiceManager serviceManager;
+
+    /** The logger. */
+    protected final Logger logger;
+
+    /**
+     * Creates a new <code>Cocoon</code> instance.
+     */
+    public SpringCocoon(Processor      processor,
+                        ServiceManager serviceManager,
+                        Context        context,
+                        Logger         logger)
+    throws Exception {
+        this.processor = processor;
+        this.logger = logger;
+        this.serviceManager = serviceManager;
+        this.environmentHelper = new EnvironmentHelper(
+                (URL) context.get(ContextHelper.CONTEXT_ROOT_URL));
+        ContainerUtil.enableLogging(this.environmentHelper, logger);
+        ContainerUtil.service(this.environmentHelper, this.serviceManager);
+    }
+
+    /**
+     * Set the (optional) request listener.
+     */
+    public void setRequestListener(RequestListener listener) {
+        this.requestListener = listener;
+    }
+
+    /**
+     * Log debug information about the current environment.
+     *
+     * @param environment an <code>Environment</code> value
+     */
+    protected void debug(Environment environment, boolean internal) {
+        String lineSeparator = SystemUtils.LINE_SEPARATOR;
+        Map objectModel = environment.getObjectModel();
+        Request request = ObjectModelHelper.getRequest(objectModel);
+        Session session = request.getSession(false);
+        StringBuffer msg = new StringBuffer();
+        msg.append("DEBUGGING INFORMATION:").append(lineSeparator);
+        if (internal) {
+            msg.append("INTERNAL ");
+        }
+        msg.append("REQUEST: ").append(request.getRequestURI()).append(lineSeparator).append(lineSeparator);
+        msg.append("CONTEXT PATH: ").append(request.getContextPath()).append(lineSeparator);
+        msg.append("SERVLET PATH: ").append(request.getServletPath()).append(lineSeparator);
+        msg.append("PATH INFO: ").append(request.getPathInfo()).append(lineSeparator).append(lineSeparator);
+
+        msg.append("REMOTE HOST: ").append(request.getRemoteHost()).append(lineSeparator);
+        msg.append("REMOTE ADDRESS: ").append(request.getRemoteAddr()).append(lineSeparator);
+        msg.append("REMOTE USER: ").append(request.getRemoteUser()).append(lineSeparator);
+        msg.append("REQUEST SESSION ID: ").append(request.getRequestedSessionId()).append(lineSeparator);
+        msg.append("REQUEST PREFERRED LOCALE: ").append(request.getLocale().toString()).append(lineSeparator);
+        msg.append("SERVER HOST: ").append(request.getServerName()).append(lineSeparator);
+        msg.append("SERVER PORT: ").append(request.getServerPort()).append(lineSeparator).append(lineSeparator);
+
+        msg.append("METHOD: ").append(request.getMethod()).append(lineSeparator);
+        msg.append("CONTENT LENGTH: ").append(request.getContentLength()).append(lineSeparator);
+        msg.append("PROTOCOL: ").append(request.getProtocol()).append(lineSeparator);
+        msg.append("SCHEME: ").append(request.getScheme()).append(lineSeparator);
+        msg.append("AUTH TYPE: ").append(request.getAuthType()).append(lineSeparator).append(lineSeparator);
+        msg.append("CURRENT ACTIVE REQUESTS: ").append(activeRequestCount).append(lineSeparator);
+
+        // log all of the request parameters
+        Enumeration e = request.getParameterNames();
+
+        msg.append("REQUEST PARAMETERS:").append(lineSeparator).append(lineSeparator);
+
+        while (e.hasMoreElements()) {
+            String p = (String) e.nextElement();
+
+            msg.append("PARAM: '").append(p).append("' ")
+               .append("VALUES: '");
+            String[] params = request.getParameterValues(p);
+            for (int i = 0; i < params.length; i++) {
+                msg.append("[" + params[i] + "]");
+                if (i != (params.length - 1)) {
+                    msg.append(", ");
+                }
+            }
+
+            msg.append("'").append(lineSeparator);
+        }
+
+        // log all of the header parameters
+        Enumeration e2 = request.getHeaderNames();
+
+        msg.append("HEADER PARAMETERS:").append(lineSeparator).append(lineSeparator);
+
+        while (e2.hasMoreElements()) {
+            String p = (String) e2.nextElement();
+
+            msg.append("PARAM: '").append(p).append("' ")
+               .append("VALUES: '");
+            Enumeration e3 = request.getHeaders(p);
+            while (e3.hasMoreElements()) {
+                msg.append("[" + e3.nextElement() + "]");
+                if (e3.hasMoreElements()) {
+                    msg.append(", ");
+                }
+            }
+
+            msg.append("'").append(lineSeparator);
+        }
+
+        msg.append(lineSeparator).append("SESSION ATTRIBUTES:").append(lineSeparator).append(lineSeparator);
+
+        // log all of the session attributes
+        if (session != null) {
+            // Fix bug #12139: Session can be modified while still
+            // being enumerated here
+            synchronized (session) {
+                e = session.getAttributeNames();
+                while (e.hasMoreElements()) {
+                    String p = (String) e.nextElement();
+                    msg.append("PARAM: '").append(p).append("' ")
+                       .append("VALUE: '").append(session.getAttribute(p)).append("'")
+                       .append(lineSeparator);
+                }
+            }
+        }
+
+        this.logger.debug(msg.toString());
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#process(org.apache.cocoon.environment.Environment)
+     */
+    public boolean process(Environment environment)
+    throws Exception {
+        environment.startingProcessing();
+        final int environmentDepth = EnvironmentHelper.markEnvironment();
+        // FIXME
+        //EnvironmentHelper.enterProcessor(this, this.serviceManager, environment);
+        try {
+            boolean result;
+            if (this.logger.isDebugEnabled()) {
+                ++activeRequestCount;
+                this.debug(environment, false);
+            }
+
+            if (this.requestListener != null) {
+                try {
+                    requestListener.onRequestStart(environment);
+                } catch (Exception e) {
+                    this.logger.error("Error encountered monitoring request start: " + e.getMessage());
+                }
+            }
+
+            result = this.processor.process(environment);
+
+            if (this.requestListener != null) {
+                try {
+                    requestListener.onRequestEnd(environment);
+                } catch (Exception e) {
+                    this.logger.error("Error encountered monitoring request start: " + e.getMessage());
+                }
+            }
+
+            // commit response on success
+            environment.commitResponse();
+
+            return result;
+        } catch (Exception any) {
+            if (this.requestListener != null) {
+                try {
+                    requestListener.onRequestException(environment, any);
+                } catch (Exception e) {
+                    this.logger.error("Error encountered monitoring request start: " + e.getMessage());
+                }
+            }
+            // reset response on error
+            environment.tryResetResponse();
+            throw any;
+        } finally {
+            EnvironmentHelper.leaveProcessor();
+            environment.finishingProcessing();
+            if (this.logger.isDebugEnabled()) {
+                --activeRequestCount;
+            }
+            Core.cleanup();
+
+            EnvironmentHelper.checkEnvironment(environmentDepth, this.logger);
+        }
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#buildPipeline(org.apache.cocoon.environment.Environment)
+     */
+    public InternalPipelineDescription buildPipeline(Environment environment)
+    throws Exception {
+        try {
+            if (this.logger.isDebugEnabled()) {
+                ++activeRequestCount;
+                debug(environment, true);
+            }
+
+            return this.processor.buildPipeline(environment);
+
+        } finally {
+            if (this.logger.isDebugEnabled()) {
+                --activeRequestCount;
+            }
+        }
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#getComponentConfigurations()
+     */
+    public Configuration[] getComponentConfigurations() {
+        return null;
+    }
+
+    /**
+     * Return this (Cocoon is always at the root of the processing chain).
+     * @since 2.1.1
+     */
+    public Processor getRootProcessor() {
+        return this;
+    }
+
+    /**
+     * Accessor for active request count
+     */
+    public int getActiveRequestCount() {
+        return activeRequestCount;
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#getSourceResolver()
+     */
+    public org.apache.cocoon.environment.SourceResolver getSourceResolver() {
+        return this.environmentHelper;
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#getContext()
+     */
+    public String getContext() {
+        return this.environmentHelper.getContext();
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#getAttribute(java.lang.String)
+     */
+    public Object getAttribute(String name) {
+        return this.processorAttributes.get(name);
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#removeAttribute(java.lang.String)
+     */
+    public Object removeAttribute(String name) {
+        return this.processorAttributes.remove(name);
+    }
+
+    /**
+     * @see org.apache.cocoon.Processor#setAttribute(java.lang.String, java.lang.Object)
+     */
+    public void setAttribute(String name, Object value) {
+        this.processorAttributes.put(name, value);
+    }
+
+}

Propchange: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/SpringCocoon.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/SpringCocoon.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/CoreUtil.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/CoreUtil.java?rev=377469&r1=377468&r2=377469&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/CoreUtil.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/CoreUtil.java Mon Feb 13 11:14:08 2006
@@ -31,6 +31,8 @@
 import java.util.Map;
 import java.util.Properties;
 
+import javax.servlet.ServletConfig;
+
 import org.apache.avalon.excalibur.logger.Log4JConfLoggerManager;
 import org.apache.avalon.excalibur.logger.LoggerManageable;
 import org.apache.avalon.excalibur.logger.LoggerManager;
@@ -43,7 +45,6 @@
 import org.apache.avalon.framework.context.ContextException;
 import org.apache.avalon.framework.context.DefaultContext;
 import org.apache.avalon.framework.logger.Logger;
-import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.Cocoon;
 import org.apache.cocoon.Constants;
@@ -53,12 +54,17 @@
 import org.apache.cocoon.components.container.ComponentContext;
 import org.apache.cocoon.configuration.ConfigurationBuilder;
 import org.apache.cocoon.core.container.SingleComponentServiceManager;
+import org.apache.cocoon.core.container.spring.ApplicationContextFactory;
+import org.apache.cocoon.core.container.spring.AvalonEnvironment;
+import org.apache.cocoon.core.container.spring.ConfigReader;
+import org.apache.cocoon.core.container.spring.ConfigurationInfo;
 import org.apache.cocoon.core.logging.CocoonLogKitLoggerManager;
 import org.apache.cocoon.core.logging.PerRequestLoggerManager;
 import org.apache.cocoon.core.logging.SettingsContext;
 import org.apache.cocoon.core.source.SimpleSourceResolver;
 import org.apache.cocoon.environment.Environment;
 import org.apache.cocoon.matching.helpers.WildcardHelper;
+import org.apache.cocoon.servlet.CocoonServlet;
 import org.apache.cocoon.util.ClassUtils;
 import org.apache.cocoon.util.StringUtils;
 import org.apache.cocoon.util.location.Location;
@@ -67,6 +73,7 @@
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.TraversableSource;
+import org.springframework.context.ApplicationContext;
 
 /**
  * This is an utility class to create a new Cocoon instance.
@@ -107,6 +114,9 @@
     
     protected ClassLoader classloader;
 
+    /** The core object. */
+    protected Core core;
+
     // Register the location finder for Avalon configuration objects and exceptions
     // and keep a strong reference to it.
     private static final LocationUtils.LocationFinder confLocFinder = new LocationUtils.LocationFinder() {
@@ -313,26 +323,28 @@
         this.dumpSystemProperties();
 
         // create the Core object
-        final Core core = this.createCore();
+        this.core = this.createCore();
 
         // create parent service manager
-        this.parentManager = new SingleComponentServiceManager(null, core, Core.ROLE);
+        this.parentManager = new SingleComponentServiceManager(null, this.core, Core.ROLE);
 
         // settings can't be changed anymore
         settings.makeReadOnly();
 
         // put the core into the context - this is for internal use only
         // The Cocoon container fetches the Core object using the context.
+        // FIXME - We shouldn't need this - check where it is used
         this.appContext.put(Core.ROLE, core);
+
+        // test the setup of the spring based container
+        this.testSpringContainer();
     }
 
+    /**
+     * Return the core object.
+     */
     public Core getCore() {
-        try {
-            return (Core)this.parentManager.lookup(Core.ROLE);
-        } catch (ServiceException neverIgnore) {
-            // this should never happen!
-            throw new CoreFatalException("Fatal exception: no Cocoon core available.", neverIgnore);
-        }
+        return this.core;
     }
 
     /**
@@ -832,9 +844,7 @@
             }
             ContainerUtil.contextualize(p, this.appContext);
 
-            // create the Core object
-            final Core core = this.createCore();
-            this.parentManager = new SingleComponentServiceManager(null, core, Core.ROLE);
+            this.parentManager = new SingleComponentServiceManager(null, this.core, Core.ROLE);
             ContainerUtil.service(p, this.parentManager);
 
             ContainerUtil.initialize(p);
@@ -910,6 +920,20 @@
             return this.loggerManager.getLoggerForCategory(rootlogger);
         }
         return this.log;
+    }
+
+    protected void testSpringContainer() throws Exception {
+        System.out.println("Setting up test Spring container...");
+        AvalonEnvironment env = new AvalonEnvironment();
+        env.context = this.appContext;
+        env.core = this.core;
+        env.logger = this.log;
+        env.servletContext = ((ServletConfig)this.appContext.get(CocoonServlet.CONTEXT_SERVLET_CONFIG)).getServletContext();
+        env.settings = this.core.getSettings();
+        ApplicationContext rootContext = ApplicationContextFactory.createRootApplicationContext(env);
+        ConfigurationInfo result = ConfigReader.readConfiguration(settings.getConfiguration(), env);
+        ApplicationContext mainContext = ApplicationContextFactory.createApplicationContext(env, result, rootContext);
+        System.out.println("Getting core cocoon processor context: " + mainContext.getBean(Core.ROLE));
     }
 
     /**

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ApplicationContextFactory.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ApplicationContextFactory.java?rev=377469&r1=377468&r2=377469&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ApplicationContextFactory.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ApplicationContextFactory.java Mon Feb 13 11:14:08 2006
@@ -141,17 +141,17 @@
         public Object postProcessBeforeInitialization(Object bean, String beanName)
         throws BeansException {
             final ComponentInfo info = (ComponentInfo)this.components.get(beanName);
-            if ( info == null ) {
-                // no info so just return as this is not an Avalon component
-                return bean;
-            }
             try {
+                if ( info == null ) {
+                    // no info so we just return the bean and don't apply any lifecycle interfaces
+                    return bean;
+                }
                 if ( info.getLoggerCategory() != null ) {
                     ContainerUtil.enableLogging(bean, this.logger.getChildLogger(info.getLoggerCategory()));
                 } else {
                     ContainerUtil.enableLogging(bean, this.logger);
                 }
-                ContainerUtil.contextualize(bean, context);
+                ContainerUtil.contextualize(bean, this.context);
                 ContainerUtil.service(bean, (ServiceManager)this.beanFactory.getBean(ServiceManager.class.getName()));
                 if ( info != null ) {
                     Configuration config = info.getConfiguration();