cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dani...@apache.org
Subject svn commit: r178987 - in /cocoon/trunk/src/java/org/apache/cocoon: components/blocks/BlockManager.java components/blocks/BlocksManager.java components/source/impl/BlockSource.java components/source/impl/BlockSourceFactory.java environment/internal/BlockEnvironmentHelper.java
Date Sun, 29 May 2005 20:58:05 GMT
Author: danielf
Date: Sun May 29 13:58:05 2005
New Revision: 178987

URL: http://svn.apache.org/viewcvs?rev=178987&view=rev
Log:
Added the block: protocol, call to other block, call to own block and call to own block relative
to current sitemap. The relative call is not tested. Use of extended block and polymorphism
is not implemented yet.

Added:
    cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSource.java   (with
props)
    cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
  (with props)
    cocoon/trunk/src/java/org/apache/cocoon/environment/internal/BlockEnvironmentHelper.java
  (with props)
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=178987&r1=178986&r2=178987&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 29
13:58:05 2005
@@ -36,6 +36,7 @@
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.cocoon.Processor;
+import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.components.LifecycleHelper;
 import org.apache.cocoon.components.container.CocoonServiceManager;
@@ -54,12 +55,14 @@
     implements Configurable, Contextualizable, Disposable, Initializable, Processor, Serviceable
{ 
 
     public static String ROLE = BlockManager.class.getName();
+    public static String NAME = BlockManager.class.getName() + "-name";
 
     private ServiceManager parentServiceManager;
     private ServiceManager serviceManager;
     private SourceResolver sourceResolver;
     private DefaultContext context;
     private Processor processor;
+    private BlocksManager blocksManager;
     private EnvironmentHelper environmentHelper;
 
     private String id;
@@ -168,7 +171,7 @@
         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
+        // manager, otherwise the root path will be the one from the
         // parent manager
         DefaultConfiguration resolverConf =
             new DefaultConfiguration("source-resolver", "BlockManager source resolver: "
+ this.id);
@@ -218,17 +221,40 @@
         this.parentServiceManager = null;
     }
 
+    public void setBlocksManager(BlocksManager blocksManager) {
+        this.blocksManager = blocksManager;
+    }
+
     // 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();
+        String blockName = (String)environment.getAttribute(BlockManager.NAME);
+
+        if (blockName != null) {
+            // Request to other block.
+            // The block name should not be used in the recieving block.
+            environment.removeAttribute(BlockManager.NAME);
+            String blockId = (String)this.connections.get(blockName);
+            if (blockId == null) {
+                throw new ProcessingException("Unknown block name " + blockName);
+            }
+            getLogger().debug("Resolving block: " + blockName + " to " + blockId);
+            return this.blocksManager.process(blockId, environment);
+        } else {
+            getLogger().debug("Enter processing in block " + this.id);
+            // Request to the own block
+            EnvironmentHelper.enterProcessor(this, this.serviceManager, environment);
+            try {
+                return this.processor.process(environment);
+            } finally {
+                EnvironmentHelper.leaveProcessor();
+                getLogger().debug("Leaving processing in block " + this.id);
+            }
         }
     }
 
+    // FIXME: Not consistently supported for blocks yet. Most of the
+    // code just use process.
     public InternalPipelineDescription buildPipeline(Environment environment)
         throws Exception {
         return this.processor.buildPipeline(environment);

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=178987&r1=178986&r2=178987&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 29
13:58:05 2005
@@ -144,6 +144,7 @@
                                            this.context,
                                            blockServiceManager,
                                            blockConf);
+            blockManager.setBlocksManager(this);
             this.blocks.put(entry.getKey(), blockManager);
             String mountPath = blockConf.getChild("mount").getAttribute("path", null);
             if (mountPath != null) {

Added: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSource.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSource.java?rev=178987&view=auto
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSource.java (added)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSource.java Sun May
29 13:58:05 2005
@@ -0,0 +1,193 @@
+/*
+ * 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.components.source.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map;
+
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.ResourceNotFoundException;
+import org.apache.cocoon.components.blocks.BlockManager;
+import org.apache.cocoon.components.blocks.BlocksManager;
+import org.apache.cocoon.environment.Environment;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.internal.BlockEnvironmentHelper;
+import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.cocoon.environment.wrapper.EnvironmentWrapper;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.impl.AbstractSource;
+
+/**
+ * Implementation of a {@link Source} that gets its content by
+ * invoking the BlockManager. 
+ *
+ * @version $Id$ */
+public final class BlockSource
+    extends AbstractSource {
+
+    /** The current ServiceManager */
+    private final ServiceManager manager;
+
+    /** The environment */
+    private final EnvironmentWrapper environment;
+
+    /** The name of the called block */
+    private String blockName;
+
+    /**
+     * Construct a new object
+     */
+    public BlockSource(ServiceManager manager,
+                       String         uri,
+                       Map            parameters,
+                       Logger         logger)
+        throws MalformedURLException {
+
+        Environment env = EnvironmentHelper.getCurrentEnvironment();
+        if ( env == null ) {
+            throw new MalformedURLException("The block protocol can not be used outside an
environment.");
+        }
+        this.manager = manager;
+
+        SitemapSourceInfo info = null;
+        try {
+            info = parseBlockURI(env, uri);
+        } catch (URISyntaxException e) {
+            throw new MalformedURLException("Malformed URI in block source " +
+                                            e.getMessage());
+        }
+        setScheme(info.protocol);
+        setSystemId(info.systemId);
+
+        // create environment...
+        this.environment = new EnvironmentWrapper(env, info, logger);
+
+        // ...and put information passed from the parent request to the internal request
+        if ( null != parameters ) {
+            this.environment.getObjectModel().put(ObjectModelHelper.PARENT_CONTEXT, parameters);
+        } else {
+            this.environment.getObjectModel().remove(ObjectModelHelper.PARENT_CONTEXT);
+        }
+
+        this.environment.setURI(info.prefix, info.uri);
+        this.environment.setAttribute(BlockManager.NAME, this.blockName);
+    }
+
+    /**
+     * Return an <code>InputStream</code> object to read from the source.
+     */
+    public InputStream getInputStream()
+        throws IOException, SourceException {
+
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        this.environment.setOutputStream(os);
+
+        BlockManager block = BlockEnvironmentHelper.getCurrentBlock();
+        if (block == null)
+            throw new SourceNotFoundException("Must be used in a block context " + this.getURI());
+
+        try {
+            block.process(this.environment);
+            
+            return new ByteArrayInputStream(os.toByteArray());
+
+        } catch (Exception e) {
+            throw new SourceException("Exception during processing of " + this.getURI(),
e);
+        } finally {
+            // Unhide wrapped environment output stream
+            this.environment.setOutputStream(null);
+        }
+    }
+
+    /**
+     * Returns true always.
+     * @see org.apache.excalibur.source.Source#exists()
+     */
+    public boolean exists() {
+        return true;
+    }
+
+    /**
+     * Recyclable
+     */
+    public void recycle() {
+    }
+
+    // Parse the blocks protocol.
+    private SitemapSourceInfo parseBlockURI(Environment env, String blockURI) 
+        throws URISyntaxException {
+
+        SitemapSourceInfo info = new SitemapSourceInfo();
+        // Maybe rawMode should be available for the block protocol.
+        info.rawMode = false;
+
+        URI uri = new URI(blockURI);
+
+        // Can't happen
+        if (!uri.isAbsolute()) {
+            throw new URISyntaxException(blockURI,
+                                         "Only absolute URIs are allowed for the block protocol.");
+        }
+        info.protocol = uri.getScheme();
+        
+        if (uri.isOpaque()) {
+            // Sub protocol or relative self reference
+            uri = new URI(uri.getSchemeSpecificPart());
+            if (uri.isAbsolute()) {
+                // Sub protocol refering to other block, e.g. block:foo:/bar
+                if (uri.isOpaque()) {
+                    throw new URISyntaxException(blockURI,
+                                                 "The protocol must have the form block:foo:/bar");
+                }
+                this.blockName = uri.getScheme();
+                info.prefix = "";
+                info.uri = uri.getPath();
+                info.processFromRoot = true;
+            } else {
+                // self reference relative to the current sitemap, e.g. block:./foo
+                this.blockName = null;
+                info.prefix = env.getURIPrefix();
+                info.uri = uri.getPath();
+                info.processFromRoot = false;
+            }
+        } else {
+            // reference to the base sitemap of the own block, block:/foo
+            this.blockName = null;
+            info.prefix = "";
+            info.uri = uri.getPath();
+            info.processFromRoot = true;
+        }
+
+        info.requestURI = info.prefix + info.uri;
+        info.queryString = uri.getQuery();
+        info.view = SitemapSourceInfo.getView(info.queryString, env);
+        
+        // FIXME: This will not be a system global id, as the blockName is block local.
+        info.systemId =
+            info.protocol + ":" + (this.blockName != null ? this.blockName + ":" : "") +
+            info.requestURI + (info.queryString != null ? "?" + info.queryString : "");
+        
+        return info;
+    }
+}

Propchange: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java?rev=178987&view=auto
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
(added)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
Sun May 29 13:58:05 2005
@@ -0,0 +1,87 @@
+/*
+ * 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.
+ */
+package org.apache.cocoon.components.source.impl;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+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.avalon.framework.thread.ThreadSafe;
+
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.URIAbsolutizer;
+import org.apache.excalibur.source.SourceUtil;
+
+/**
+ * This class implements the block: protocol.
+ *
+ *
+ * @version $Id$ */
+public final class BlockSourceFactory
+    extends AbstractLogEnabled
+    implements SourceFactory, ThreadSafe, Serviceable, URIAbsolutizer
+{
+    
+    /** The <code>ServiceManager</code> */
+    private ServiceManager manager;
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
+     */
+    public Source getSource( String location, Map parameters )
+        throws MalformedURLException, IOException {
+        if( getLogger().isDebugEnabled() ) {
+            getLogger().debug( "Creating source object for " + location );
+        }
+
+        return new BlockSource( this.manager,
+                                location,
+                                parameters,
+                                getLogger());
+    }
+    
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
+     */
+    public void release( Source source ) {
+        if ( null != source ) {
+            if ( this.getLogger().isDebugEnabled() ) {
+                this.getLogger().debug("Releasing source " + source.getURI());
+            }
+            ((BlockSource)source).recycle();
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.URIAbsolutizer#absolutize(java.lang.String, java.lang.String)
+     */
+    public String absolutize(String baseURI, String location) {
+        return SourceUtil.absolutize(baseURI, location, true);
+    }
+
+}

Propchange: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/trunk/src/java/org/apache/cocoon/environment/internal/BlockEnvironmentHelper.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/environment/internal/BlockEnvironmentHelper.java?rev=178987&view=auto
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/environment/internal/BlockEnvironmentHelper.java
(added)
+++ cocoon/trunk/src/java/org/apache/cocoon/environment/internal/BlockEnvironmentHelper.java
Sun May 29 13:58:05 2005
@@ -0,0 +1,49 @@
+/*
+ * 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.environment.internal;
+
+import java.net.URL;
+
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.Processor;
+import org.apache.cocoon.components.blocks.BlockManager;
+
+/**
+ * Hack used for geting hold on the current block manager without
+ * making core code dependent of the experimental blocks code.
+ *
+ * @version $Id$ */
+public class BlockEnvironmentHelper
+    extends EnvironmentHelper {
+
+    // Hack for getting it to compile
+    private BlockEnvironmentHelper(URL context) {
+        super(context);
+    }
+
+    public static BlockManager getCurrentBlock() {
+        final EnvironmentStack stack = (EnvironmentStack)environmentStack.get();
+        if ( stack != null && !stack.isEmpty()) {
+            for (int i = stack.getOffset(); i >= 0; i--) {
+                final EnvironmentInfo info = (EnvironmentInfo)stack.get(i);
+                if (info.processor instanceof BlockManager) {
+                    return (BlockManager)info.processor;
+                }
+            }
+        }
+        return null;
+    }
+}

Propchange: cocoon/trunk/src/java/org/apache/cocoon/environment/internal/BlockEnvironmentHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message