You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by da...@apache.org on 2005/09/27 22:31:22 UTC

svn commit: r292033 - in /cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl: Activator.java BlocksServlet.java

Author: danielf
Date: Tue Sep 27 13:31:19 2005
New Revision: 292033

URL: http://svn.apache.org/viewcvs?rev=292033&view=rev
Log:
Continued refactoring: now the Core and Processor are created outside the Servlet (in the Activator).

Modified:
    cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/Activator.java
    cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/BlocksServlet.java

Modified: cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/Activator.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/Activator.java?rev=292033&r1=292032&r2=292033&view=diff
==============================================================================
--- cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/Activator.java (original)
+++ cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/Activator.java Tue Sep 27 13:31:19 2005
@@ -1,133 +1,143 @@
-/*
- * 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.
- * 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.service.servlet.impl;
-
-import java.util.Hashtable;
-
-import org.apache.avalon.framework.logger.Logger;
-import org.apache.cocoon.core.BootstrapEnvironment;
-import org.apache.cocoon.core.CoreUtil;
-import org.apache.cocoon.core.osgi.OSGiBootstrapEnvironment;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.http.HttpService;
-
-/**
- * Activator which register a Cocoon servlet
- */
-
-public class Activator implements BundleActivator {
-
-    static BundleContext bc;
-    static final String  SERVLET_ALIAS = "/";     // the http server root
-    static final String  SITEMAP = "sitemap";
-
-    private Hashtable registrations = new Hashtable();
-    private ClassLoader classLoader = getClass().getClassLoader();;
-    private Logger logger;
-    private CoreUtil coreUtil;
-
-    public void start(BundleContext bc) throws BundleException {
-
-        this.bc  = bc;
-        try {
-            BootstrapEnvironment env = new OSGiBootstrapEnvironment(this.classLoader, this.bc);
-            env.log("OSGiBootstrapEnvironment created");
-            this.coreUtil = new CoreUtil(env);
-            env.log("CoreUtil created");
-            this.logger = env.getBootstrapLogger(null);
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new BundleException("Failed to create core util", e);
-        }
-
-        ServiceListener listener = new ServiceListener() {
-                public void serviceChanged(ServiceEvent ev) {
-                    ServiceReference sr = ev.getServiceReference();
-                    
-                    switch(ev.getType()) {
-                    case ServiceEvent.REGISTERED:
-                        setRoot(sr);
-                        break;
-                    case ServiceEvent.UNREGISTERING:
-                        unsetRoot(sr);
-                        break;
-                    }
-                }
-            };
-        
-        String filter = "(objectclass=" + HttpService.class.getName() + ")";
-        
-        try {
-            bc.addServiceListener(listener, filter);
-            
-            ServiceReference[] srl = bc.getServiceReferences(null, filter);
-            for(int i = 0; srl != null && i < srl.length; i++) {
-                listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
-                                                         srl[i]));
-            }
-        } catch (Exception e) {
-            this.logger.info("Failed to set up listener for http service", e);
-        }
-    }
-  
-    public void stop(BundleContext bc) throws BundleException {
-    }
-
-    private void setRoot(ServiceReference sr) {
-        
-        if(registrations.containsKey(sr)) {
-            return; // already done
-        }
-        
-        this.logger.info("set root for " + sr);
-
-        HttpService http = (HttpService)bc.getService(sr);
-
-        try {
-            http.registerServlet(SERVLET_ALIAS,
-                                 new BlocksServlet(this.classLoader,
-                                                   this.logger,
-                                                   this.coreUtil),
-                                 new Hashtable(),
-                                 null);
-
-            registrations.put(sr, null);
-        } catch (Exception e) {
-            this.logger.info("Failed to register resource", e);
-        }
-    } 
-
-    private void unsetRoot(ServiceReference sr) {
-        if(!registrations.containsKey(sr)) {
-            return; // nothing to do
-        }
-
-        this.logger.info("unset root for " + sr);
-    
-        HttpService http = (HttpService)bc.getService(sr);
-    
-        if(http != null) {
-            http.unregister(SERVLET_ALIAS);
-            bc.ungetService(sr);
-        }
-        registrations.remove(sr);
-    }
-}
+/*
+ * 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.
+ * 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.service.servlet.impl;
+
+import java.util.HashSet;
+
+import org.apache.avalon.excalibur.logger.LoggerManager;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.cocoon.Processor;
+import org.apache.cocoon.core.BootstrapEnvironment;
+import org.apache.cocoon.core.Core;
+import org.apache.cocoon.core.CoreUtil;
+import org.apache.cocoon.core.osgi.OSGiBootstrapEnvironment;
+import org.apache.cocoon.core.osgi.OSGiLoggerManager;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.log.LogService;
+
+/**
+ * Activator which register a Cocoon servlet
+ */
+
+public class Activator implements BundleActivator {
+
+    static BundleContext bc;
+    static final String  SERVLET_ALIAS = "/";     // the http server root
+    static final String  SITEMAP = "sitemap";
+
+    private HashSet registrations = new HashSet();
+    private ClassLoader classLoader = getClass().getClassLoader();;
+    private Logger logger;
+    private Core core;
+    private Processor processor;
+
+    public void start(BundleContext bc) throws BundleException {
+
+        this.bc  = bc;
+        try {
+            BootstrapEnvironment env = new OSGiBootstrapEnvironment(this.classLoader, this.bc);
+            env.log("OSGiBootstrapEnvironment created");
+            CoreUtil coreUtil = new CoreUtil(env);
+            env.log("CoreUtil created");
+            LoggerManager logManager = new OSGiLoggerManager(bc, LogService.LOG_DEBUG);
+            this.logger = logManager.getDefaultLogger();
+            this.core = coreUtil.getCore();
+            this.processor = coreUtil.createCocoon();
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new BundleException("Failed to create core util", e);
+        }
+
+        ServiceListener listener = new ServiceListener() {
+                public void serviceChanged(ServiceEvent ev) {
+                    ServiceReference sr = ev.getServiceReference();
+                    
+                    switch(ev.getType()) {
+                    case ServiceEvent.REGISTERED:
+                        setRoot(sr);
+                        break;
+                    case ServiceEvent.UNREGISTERING:
+                        unsetRoot(sr);
+                        break;
+                    }
+                }
+            };
+        
+        String filter = "(objectclass=" + HttpService.class.getName() + ")";
+        
+        try {
+            bc.addServiceListener(listener, filter);
+            
+            ServiceReference[] srl = bc.getServiceReferences(null, filter);
+            for(int i = 0; srl != null && i < srl.length; i++) {
+                listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
+                                                         srl[i]));
+            }
+        } catch (Exception e) {
+            this.logger.info("Failed to set up listener for http service", e);
+        }
+    }
+  
+    public void stop(BundleContext bc) throws BundleException {
+    }
+
+    private void setRoot(ServiceReference sr) {
+        
+        if(registrations.contains(sr)) {
+            return; // already done
+        }
+        
+        this.logger.info("set root for " + sr);
+
+        HttpService http = (HttpService)bc.getService(sr);
+
+        try {
+            http.registerServlet(SERVLET_ALIAS,
+                                 new BlocksServlet(this.classLoader,
+                                                   this.logger,
+                                                   this.core,
+                                                   this.processor),
+                                 null,
+                                 null);
+
+            registrations.add(sr);
+        } catch (Exception e) {
+            this.logger.info("Failed to register resource", e);
+        }
+    } 
+
+    private void unsetRoot(ServiceReference sr) {
+        if(!registrations.contains(sr)) {
+            return; // nothing to do
+        }
+
+        this.logger.info("unset root for " + sr);
+    
+        HttpService http = (HttpService)bc.getService(sr);
+    
+        if(http != null) {
+            http.unregister(SERVLET_ALIAS);
+            bc.ungetService(sr);
+        }
+        registrations.remove(sr);
+    }
+}

Modified: cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/BlocksServlet.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/BlocksServlet.java?rev=292033&r1=292032&r2=292033&view=diff
==============================================================================
--- cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/BlocksServlet.java (original)
+++ cocoon/trunk/src/osgi-servlet/org/apache/cocoon/service/servlet/impl/BlocksServlet.java Tue Sep 27 13:31:19 2005
@@ -29,15 +29,14 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.avalon.framework.logger.Logger;
-import org.apache.cocoon.Cocoon;
 import org.apache.cocoon.ConnectionResetException;
 import org.apache.cocoon.Constants;
+import org.apache.cocoon.Processor;
 import org.apache.cocoon.ResourceNotFoundException;
 import org.apache.cocoon.components.notification.DefaultNotifyingBuilder;
 import org.apache.cocoon.components.notification.Notifier;
 import org.apache.cocoon.components.notification.Notifying;
 import org.apache.cocoon.core.Core;
-import org.apache.cocoon.core.CoreUtil;
 import org.apache.cocoon.environment.Environment;
 import org.apache.cocoon.environment.http.HttpEnvironment;
 import org.apache.cocoon.servlet.multipart.MultipartHttpServletRequest;
@@ -67,9 +66,9 @@
     static final float HOUR   = 60 * MINUTE;
 
     /**
-     * The <code>Cocoon</code> instance
+     * The <code>Processor</code> instance
      */
-    protected Cocoon cocoon;
+    protected Processor processor;
 
     /**
      * Holds exception happened during initialization (if any)
@@ -90,19 +89,17 @@
     /** Core */
     protected Core core;
 
-    /** CoreUtil */
-    protected CoreUtil coreUtil;
-
     /** The logger */
     protected Logger log;
 
     public BlocksServlet(ClassLoader classLoader,
                          Logger logger,
-                         CoreUtil coreUtil) {
+                         Core core,
+                         Processor processor) {
         this.classLoader = classLoader;
         this.log = logger;
-        this.coreUtil = coreUtil;
-        this.core = coreUtil.getCore();
+        this.core = core;
+        this.processor = processor;
     }
 
     /**
@@ -130,35 +127,17 @@
                                                  this.core.getSettings().isSilentlyRename(),
                                                  this.core.getSettings().getMaxUploadSize(),
                                                  this.containerEncoding);
-
-        try {
-            this.exception = null;
-            this.cocoon = this.coreUtil.createCocoon();
-        } catch (Exception e) {
-            this.exception = e;
-        }
-        if (this.exception == null) {
-            this.getLogger().info("Apache Cocoon " + Constants.VERSION + " is up and ready.");
-        } else {
-            final String message = "Errors during initializing Apache Cocoon " + Constants.VERSION + " : " + this.exception.getMessage();
-            this.getLogger().error(message, this.exception);
-        }
     }
 
     /**
      * Dispose Cocoon when servlet is destroyed
      */
     public void destroy() {
-        this.getLogger().info("Destroying Cocoon Servlet.");
-        if (this.coreUtil != null) {
-            this.coreUtil = null;
-            // coreUtil will dispose it.
-            this.cocoon = null;
-        }
-
-        this.requestFactory = null;
         this.classLoader = null;
         this.log = null;
+        this.core = null;
+        this.processor = null;
+        this.requestFactory = null;
         super.destroy();
     }
 
@@ -206,25 +185,6 @@
             return;
         }
 
-        // Get the cocoon engine instance
-        try {
-            this.exception = null;
-            this.cocoon = this.coreUtil.getCocoon(request.getPathInfo(), request.getParameter(Constants.RELOAD_PARAM));
-        } catch (Exception e) {
-            this.exception = e;
-        }
-
-        // Check if cocoon was initialized
-        if (this.cocoon == null) {
-            manageException(request, res, null, null,
-                            HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
-                            "Initialization Problem",
-                            null /* "Cocoon was not initialized" */,
-                            null /* "Cocoon was not initialized, cannot process request" */,
-                            this.exception);
-            return;
-        }
-
         // We got it... Process the request
         String uri = request.getServletPath();
         if (uri == null) {
@@ -279,9 +239,10 @@
 
         try {
             try {
-                handle = this.coreUtil.initializeRequest(env);
+                // FIXME: don't want the Servlet depend on coreUtil, find other way to initialized request
+                // handle = this.coreUtil.initializeRequest(env);
 
-                if (this.cocoon.process(env)) {
+                if (this.processor.process(env)) {
                     contentType = env.getContentType();
                 } else {
                     // We reach this when there is nothing in the processing change that matches
@@ -364,7 +325,8 @@
                 }
             }
         } finally {
-            this.coreUtil.cleanUpRequest(handle);
+            // FIXME: don't want the Servlet depend on coreUtil, find other way to clean up request
+            // this.coreUtil.cleanUpRequest(handle);
 
             try {
                 if (request instanceof MultipartHttpServletRequest) {