Author: danielf
Date: Sun May  1 15:01:25 2005
New Revision: 165550

URL: http://svn.apache.org/viewcvs?rev=165550&view=rev
Log:
Made the BlockManager a processor that uses its own sitemap. Started to work on 
the BlocksManager, it creates and stores the BlockManagers and create a minimal 
parent component manager for them.

Added:
    
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/core-components.xconf
Modified:
    cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlockManager.java
    cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlocksManager.java

Modified: 
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlockManager.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlockManager.java?rev=165550&r1=165549&r2=165550&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlockManager.java 
(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlockManager.java 
Sun May  1 15:01:25 2005
@@ -15,34 +15,54 @@
  */
 package org.apache.cocoon.components.blocks;
 
-import java.io.IOException;
+import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.activity.Initializable;
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+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.context.DefaultContext;
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
+
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.Processor;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.components.container.CocoonServiceManager;
+import org.apache.cocoon.components.container.ComponentContext;
+import org.apache.cocoon.components.treeprocessor.TreeProcessor;
+import org.apache.cocoon.environment.Environment;
+import org.apache.cocoon.environment.internal.EnvironmentHelper;
+
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
-import org.xml.sax.SAXException;
 
 /**
  * @version SVN $Id$
  */
 public class BlockManager
     extends AbstractLogEnabled
-    implements Configurable, Disposable, Serviceable { 
+    implements Configurable, Contextualizable, Disposable, Initializable, 
Processor, Serviceable { 
 
     public static String ROLE = BlockManager.class.getName();
 
-    private ServiceManager manager;
-    private SourceResolver resolver;
+    private ServiceManager parentServiceManager;
+    private ServiceManager serviceManager;
+    private SourceResolver sourceResolver;
+    private DefaultContext context;
+    private Processor processor;
+    private EnvironmentHelper environmentHelper;
 
     private String id;
     private String location;
@@ -52,8 +72,11 @@
     private Map properties = new HashMap();
 
     public void service(ServiceManager manager) throws ServiceException {
-        this.manager = manager;
-        this.resolver = (SourceResolver) 
this.manager.lookup(SourceResolver.ROLE);
+        this.parentServiceManager = manager;
+    }
+
+    public void contextualize(Context context) throws ContextException {
+        this.context = new ComponentContext(context);
     }
 
     public void configure(Configuration config)
@@ -91,31 +114,128 @@
 
         // Read the block.xml file
         String blockPath = this.location + "/COB-INF/block.xml";
+        SourceResolver resolver = null;
         Source source = null;
         Configuration block = null;
 
         try {
-            source = this.resolver.resolveURI(blockPath);
+            resolver = 
+                (SourceResolver) 
this.parentServiceManager.lookup(SourceResolver.ROLE);
+            source = resolver.resolveURI(blockPath);
             DefaultConfigurationBuilder builder = new 
DefaultConfigurationBuilder();
             block = builder.build( source.getInputStream() );
-        } catch (SAXException se) {
-            String msg = "SAXException while reading " + blockPath + ": " + 
se.getMessage();
-            throw new ConfigurationException(msg, se);
-        } catch (IOException ie) {
-              String msg = "IOException while reading " + blockPath + ": " + 
ie.getMessage();
-              throw new ConfigurationException(msg, ie);
+        } catch (Exception e) {
+            String msg = "Exception while reading " + blockPath + ": " + 
e.getMessage();
+            throw new ConfigurationException(msg, e);
         } finally {
-            this.resolver.release(source);
+            if (resolver != null) {
+                resolver.release(source);
+                this.parentServiceManager.release(resolver);
+            }
         }
         this.sitemapPath = block.getChild("sitemap").getAttribute("src");
         getLogger().debug("sitemapPath=" + this.sitemapPath);
     }
 
+    public void initialize() throws Exception {
+        getLogger().debug("Initializing new Block Manager: " + this.id);
+
+        // A block is supposed to be an isolated unit so it should not have
+        // any direct access to the global root context
+        getLogger().debug("Root URL " +
+                          ((URL) 
this.context.get(ContextHelper.CONTEXT_ROOT_URL)).toExternalForm());
+        String blockRoot =
+            ((URL) 
this.context.get(ContextHelper.CONTEXT_ROOT_URL)).toExternalForm() +
+            this.location;
+        this.context.put(ContextHelper.CONTEXT_ROOT_URL, new URL(blockRoot));
+        getLogger().debug("Block Root URL " +
+                          ((URL) 
this.context.get(ContextHelper.CONTEXT_ROOT_URL)).toExternalForm());
+        this.context.makeReadOnly();
+
+        // Create an own service manager
+        this.serviceManager = new 
CocoonServiceManager(this.parentServiceManager);
+        ContainerUtil.enableLogging(this.serviceManager, this.getLogger());
+        ContainerUtil.contextualize(this.serviceManager, this.context);
+
+        // Hack to put a sitemap configuration for the main sitemap of
+        // the block into the service manager
+        getLogger().debug("Block Manager: create sitemap " + this.sitemapPath);
+        DefaultConfiguration sitemapConf =
+            new DefaultConfiguration("sitemap", "BlockManager sitemap: " + 
this.id +
+                                     " for " + this.sitemapPath);
+        sitemapConf.setAttribute("file", this.sitemapPath);
+        sitemapConf.setAttribute("check-reload", "yes");
+        // The source resolver must be defined in this service
+        // manager, otherwise the root path will be the one tÂfrom the
+        // parent manager
+        DefaultConfiguration resolverConf =
+            new DefaultConfiguration("source-resolver", "BlockManager source 
resolver: " + this.id);
+        DefaultConfiguration conf =
+            new DefaultConfiguration("components", "BlockManager components: " 
+ this.id);
+        conf.addChild(sitemapConf);
+        conf.addChild(resolverConf);
+
+        ContainerUtil.configure(this.serviceManager, conf);
+        ContainerUtil.initialize(this.serviceManager);
+
+        this.sourceResolver = 
(SourceResolver)this.serviceManager.lookup(SourceResolver.ROLE);
+        final Processor processor = EnvironmentHelper.getCurrentProcessor();
+        if ( processor != null ) {
+            getLogger().debug("processor context" + processor.getContext());
+        }
+        Source sitemapSrc = this.sourceResolver.resolveURI(this.sitemapPath);
+        getLogger().debug("Sitemap Source " + sitemapSrc.getURI());
+        this.sourceResolver.release(sitemapSrc);
+
+        // Get the Processor and keep it
+        this.processor = (Processor)this.serviceManager.lookup(Processor.ROLE);
+
+        this.environmentHelper = new EnvironmentHelper(
+                (URL)this.context.get(ContextHelper.CONTEXT_ROOT_URL));
+        ContainerUtil.enableLogging(this.environmentHelper, this.getLogger());
+        ContainerUtil.service(this.environmentHelper, this.serviceManager);
+    }
+
     public void dispose() {
-        if (this.manager != null) {
-            this.manager.release(this.resolver);
-            this.resolver = null;
-            this.manager = null;
+        if (this.serviceManager != null) {
+            this.serviceManager.release(this.sourceResolver);
+            this.sourceResolver = null;
+            this.serviceManager = null;
+        }
+        this.parentServiceManager = null;
+    }
+
+    // The Processor methods
+
+    public boolean process(Environment environment) throws Exception {
+        EnvironmentHelper.enterProcessor(this, this.serviceManager, 
environment);
+        try {
+            return this.processor.process(environment);
+        } finally {
+            EnvironmentHelper.leaveProcessor();
         }
+    }
+
+    public InternalPipelineDescription buildPipeline(Environment environment)
+        throws Exception {
+        return this.processor.buildPipeline(environment);
+    }
+
+    public Configuration[] getComponentConfigurations() {
+        return null;
+    }
+
+    // A block is supposed to be an isolated unit so it should not have
+    // any direct access to the global root sitemap
+    public Processor getRootProcessor() {
+        return this;
+    }
+    
+    public org.apache.cocoon.environment.SourceResolver getSourceResolver() {
+        return this.environmentHelper;
+    }
+    
+    public String getContext() {
+        return this.environmentHelper.getContext();
     }
 }

Modified: 
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlocksManager.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlocksManager.java?rev=165550&r1=165549&r2=165550&view=diff
==============================================================================
--- 
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlocksManager.java 
(original)
+++ 
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/BlocksManager.java 
Sun May  1 15:01:25 2005
@@ -16,12 +16,20 @@
 package org.apache.cocoon.components.blocks;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 
 import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.activity.Initializable;
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+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.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
@@ -29,6 +37,9 @@
 import org.apache.avalon.framework.thread.ThreadSafe;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
+import org.apache.cocoon.components.container.CocoonServiceManager;
+import org.apache.cocoon.core.Core;
+import org.apache.cocoon.core.CoreUtil;
 import org.apache.cocoon.ProcessingException;
 import org.xml.sax.SAXException;
 
@@ -37,16 +48,25 @@
  */
 public class BlocksManager
     extends AbstractLogEnabled
-    implements Configurable, Disposable, Serviceable, ThreadSafe { 
+    implements Configurable, Contextualizable, Disposable, Initializable, 
Serviceable, ThreadSafe { 
 
     public static String ROLE = BlocksManager.class.getName();
-
-    private ServiceManager manager;
+    public static String CORE_COMPONENTS_XCONF =
+        "resource://org/apache/cocoon/components/blocks/core-components.xconf";
+    private ServiceManager serviceManager;
     private SourceResolver resolver;
+    private Context context;
+
+    private HashMap blockConfs = new HashMap();
+    private HashMap blocks = new HashMap();
 
     public void service(ServiceManager manager) throws ServiceException {
-        this.manager = manager;
-        this.resolver = (SourceResolver) 
this.manager.lookup(SourceResolver.ROLE);
+        this.serviceManager = manager;
+        this.resolver = (SourceResolver) 
this.serviceManager.lookup(SourceResolver.ROLE);
+    }
+
+    public void contextualize(Context context) throws ContextException {
+        this.context = context;
     }
 
     public void configure(Configuration config)
@@ -75,14 +95,58 @@
             getLogger().debug("BlocksManager configure: " + block.getName() +
                               " id=" + block.getAttribute("id") +
                               " location=" + block.getAttribute("location"));
+            this.blockConfs.put(block.getAttribute("id"), block);
+        }
+    }
+
+    public void initialize() throws Exception {
+        getLogger().debug("Initializing the Blocks Manager");
+
+        // Create a root service manager for blocks. This should be
+        // the minimal number of components that are needed for any
+        // block. Only components that not are context dependent
+        // should be defined here. Block that depends on e.g. the root
+        // context path should be defined in the BlockManager instead.
+
+        Core core = (Core)this.serviceManager.lookup(Core.ROLE);
+        ServiceManager blockParentServiceManager =
+            new CoreUtil.RootServiceManager(null, core);
+        CocoonServiceManager blockServiceManager =
+            new CocoonServiceManager(blockParentServiceManager);
+        ContainerUtil.enableLogging(blockServiceManager, this.getLogger());
+        ContainerUtil.contextualize(blockServiceManager, this.context);
+
+        Source coreComponentsSource =
+            this.resolver.resolveURI(CORE_COMPONENTS_XCONF);
+        DefaultConfigurationBuilder builder = new 
DefaultConfigurationBuilder();
+        Configuration coreComponentsConf =
+            builder.build( coreComponentsSource.getInputStream() );
+
+        ContainerUtil.configure(blockServiceManager, coreComponentsConf);
+        ContainerUtil.initialize(blockServiceManager);
+
+        // Create and store all blocks
+
+        Iterator confIter = this.blockConfs.entrySet().iterator();
+        while (confIter.hasNext()) {
+            Map.Entry entry = (Map.Entry)confIter.next();
+            Configuration blockConf = (Configuration)entry.getValue();
+            getLogger().debug("Creating " + blockConf.getName() +
+                              " id=" + blockConf.getAttribute("id"));
+            BlockManager blockManager = new BlockManager();
+            ContainerUtil.enableLogging(blockManager, this.getLogger());
+            ContainerUtil.contextualize(blockManager, this.context);
+            ContainerUtil.configure(blockManager, blockConf);
+            ContainerUtil.initialize(blockManager);
+            this.blocks.put(entry.getKey(), blockManager);
         }
     }
 
     public void dispose() {
-        if (this.manager != null) {
-            this.manager.release(this.resolver);
+        if (this.serviceManager != null) {
+            this.serviceManager.release(this.resolver);
             this.resolver = null;
-            this.manager = null;
+            this.serviceManager = null;
         }
     }
 }

Added: 
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/core-components.xconf
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/blocks/core-components.xconf?rev=165550&view=auto
==============================================================================
--- 
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/core-components.xconf 
(added)
+++ 
cocoon/trunk/src/java/org/apache/cocoon/components/blocks/core-components.xconf 
Sun May  1 15:01:25 2005
@@ -0,0 +1,43 @@
+<?xml version="1.0"?>
+<!--
+  Copyright 1999-2004 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.
+-->
+
+<cocoon version="2.2">
+
+  <include src="resource://org/apache/cocoon/cocoon.roles"/>
+
+  <xml-parser class="org.apache.excalibur.xml.impl.JaxpParser">
+    <parameter name="validate" value="false"/>
+    <parameter name="namespace-prefixes" value="false"/>
+    <parameter name="stop-on-warning" value="true"/>
+    <parameter name="stop-on-recoverable-error" value="true"/>
+    <parameter name="reuse-parsers" value="false"/>
+  </xml-parser>
+
+  <xmlizer/>
+
+  <source-resolver/>
+
+  <fam/>
+
+  <source-factories>
+    <component-instance 
class="org.apache.excalibur.source.impl.ResourceSourceFactory" name="resource"/>
+    <component-instance 
class="org.apache.cocoon.components.source.impl.ContextSourceFactory" 
name="context"/>
+    <component-instance 
class="org.apache.excalibur.source.impl.FileSourceFactory" name="file"/>
+    <component-instance 
class="org.apache.excalibur.source.impl.URLSourceFactory" name="*"/>
+  </source-factories>
+
+</cocoon>


Reply via email to