cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r158127 - in cocoon/trunk/src: core/java/org/apache/cocoon/core/Core.java java/org/apache/cocoon/servlet/CocoonServlet.java
Date Fri, 18 Mar 2005 19:06:47 GMT
Author: cziegeler
Date: Fri Mar 18 11:06:45 2005
New Revision: 158127

URL: http://svn.apache.org/viewcvs?view=rev&rev=158127
Log:
Start moving code common to all environments to Core

Modified:
    cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.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=158126&r2=158127
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.java (original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/Core.java Fri Mar 18 11:06:45 2005
@@ -20,17 +20,22 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
 
 import org.apache.avalon.framework.CascadingRuntimeException;
+import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.context.ContextException;
 import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.Constants;
 import org.apache.cocoon.configuration.Settings;
+import org.apache.cocoon.util.ClassUtils;
 
 /**
  * This is the core Cocoon component.
@@ -59,6 +64,16 @@
     /** The component context. */
     private Context context;
 
+    private final Settings settings;
+    
+    public Core() {
+        this.settings = null;
+    }
+
+    public Core(Settings s) {
+        this.settings = s;
+    }
+
     /* (non-Javadoc)
      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
      */
@@ -97,6 +112,7 @@
      */
     public Settings getSettings() {
         return getSettings(this.context);
+        // return this.settings;
     }
 
     /**
@@ -217,5 +233,119 @@
         InputStream getInputStream(String path);
         
         void configure(Settings settings);
+        
+        ClassLoader getInitClassLoader();
+    }
+    
+    /**
+     * Bootstrap Cocoon Service Manager
+     */
+    public static ServiceManager createRootServiceManager(BootstrapEnvironment env) {   
    
+        // create settings
+        final Settings s = createSettings(env);
+        
+        if (s.isInitClassloader()) {
+            // Force context classloader so that JAXP can work correctly
+            // (see javax.xml.parsers.FactoryFinder.findClassLoader())
+            try {
+                Thread.currentThread().setContextClassLoader(env.getInitClassLoader());
+            } catch (Exception e) {
+                // ignore this
+            }
+        }
+
+        // create new Core
+        final Core cocoon = new Core(s);
+        
+        // create parent service manager
+        final ServiceManager parent = getParentServiceManager(s);
+
+        return new RootServiceManager(parent, cocoon);
+    }
+    
+    /**
+     * Instatiates the parent service manager, as specified in the
+     * parent-service-manager init parameter.
+     *
+     * If none is specified, the method returns <code>null</code>.
+     *
+     * @return the parent service manager, or <code>null</code>.
+     */
+    protected static ServiceManager getParentServiceManager(Settings s) {
+        String parentServiceManagerClass = s.getParentServiceManagerClassName();
+        String parentServiceManagerInitParam = null;
+        if (parentServiceManagerClass != null) {
+            int dividerPos = parentServiceManagerClass.indexOf('/');
+            if (dividerPos != -1) {
+                parentServiceManagerInitParam = parentServiceManagerInitParam.substring(dividerPos
+ 1);
+                parentServiceManagerClass = parentServiceManagerClass.substring(0, dividerPos);
+            }
+        }
+
+        ServiceManager parentServiceManager = null;
+        if (parentServiceManagerClass != null) {
+            try {
+                Class pcm = ClassUtils.loadClass(parentServiceManagerClass);
+                Constructor pcmc = pcm.getConstructor(new Class[]{String.class});
+                parentServiceManager = (ServiceManager) pcmc.newInstance(new Object[]{parentServiceManagerInitParam});
+
+                //ContainerUtil.enableLogging(parentServiceManager, getLogger());
+                //ContainerUtil.contextualize(parentServiceManager, this.appContext);
+                ContainerUtil.initialize(parentServiceManager);
+            } catch (Exception e) {
+                /*if (getLogger().isErrorEnabled()) {
+                    getLogger().error("Could not initialize parent component manager.", e);
+                }*/
+            }
+        }
+        return parentServiceManager;
+    }
+
+    public static final class RootServiceManager implements ServiceManager {
+        
+        protected final static String CORE_KEY = Core.class.getName();
+
+        protected final ServiceManager parent;
+        protected final Core cocoon;
+
+        public RootServiceManager(ServiceManager p, Core c) {
+            this.parent = p;
+            this.cocoon = c;
+        }
+
+        /* (non-Javadoc)
+         * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
+         */
+        public boolean hasService(String key) {
+            if ( CORE_KEY.equals(key) ) {
+                return true;
+            }
+            if ( this.parent != null ) {
+                return this.parent.hasService(key);
+            }
+            return false;
+        }
+
+        /* (non-Javadoc)
+         * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
+         */
+        public Object lookup(String key) throws ServiceException {
+            if ( CORE_KEY.equals(key) ) {
+                return this.cocoon;
+            }
+            if ( this.parent != null ) {
+                return this.parent.lookup(key);
+            }
+            throw new ServiceException("Cocoon", "Component for key '" + key + "' not found.");
+        }
+
+        /* (non-Javadoc)
+         * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
+         */
+        public void release(Object component) {
+            if ( component != this.cocoon && parent != null ) {
+                this.parent.release(component);
+            }
+        }
     }
 }

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=158126&r2=158127
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/servlet/CocoonServlet.java Fri Mar 18 11:06:45
2005
@@ -129,7 +129,7 @@
     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 final ClassLoader classLoader = this.getClass().getClassLoader();
 
     private String parentServiceManagerClass;
     private String parentServiceManagerInitParam;
@@ -178,7 +178,7 @@
         super.init(conf);
 
         // initialize settings
-        Core.BootstrapEnvironment env = new ServletBootstrapEnvironment(conf);
+        Core.BootstrapEnvironment env = new ServletBootstrapEnvironment(conf, this.classLoader);
 
         this.settings = Core.createSettings(env);
 
@@ -1300,9 +1300,11 @@
     implements Core.BootstrapEnvironment {
 
         private final ServletConfig config;
-
-        public ServletBootstrapEnvironment(ServletConfig config) {
+        private final ClassLoader   classLoader;
+        
+        public ServletBootstrapEnvironment(ServletConfig config, ClassLoader cl) {
             this.config = config;
+            this.classLoader = cl;
         }
 
         public void log(String message) {
@@ -1320,6 +1322,13 @@
         public void configure(Settings settings) {
             // fill from the servlet parameters
             SettingsHelper.fill(settings, this.config);                
+        }
+
+        /* (non-Javadoc)
+         * @see org.apache.cocoon.core.Core.BootstrapEnvironment#getInitClassLoader()
+         */
+        public ClassLoader getInitClassLoader() {
+            return this.classLoader;
         }
     }
 



Mime
View raw message