cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dani...@apache.org
Subject svn commit: r471587 - in /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon: components/source/impl/BlockContextSource.java components/source/impl/BlockContextSourceFactory.java core/deployment/DeploymentUtil.java
Date Mon, 06 Nov 2006 00:43:31 GMT
Author: danielf
Date: Sun Nov  5 16:43:30 2006
New Revision: 471587

URL: http://svn.apache.org/viewvc?view=rev&rev=471587
Log:
A block context source where the uris has the form blockcontext:/blockname/path.
The block names are resolved to the paths to the COB-INF directories. Block name - path associations
are made available from the DeployerUtil.

The code is not tested yet.

Added:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
  (with props)
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSourceFactory.java
  (with props)
Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/deployment/DeploymentUtil.java

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java?view=auto&rev=471587
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
(added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
Sun Nov  5 16:43:30 2006
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.excalibur.source.TraversableSource;
+import org.apache.excalibur.source.impl.AbstractSource;
+
+/**
+ * This source makes the immediate children to the blockcontext:/ uri
+ * traversable
+ * 
+ * @version $Id$
+ */
+public class BlockContextSource extends AbstractSource implements
+        TraversableSource {
+    
+    private Map blockContexts;
+    private ServiceManager manager;
+    private Logger logger;
+
+    /**
+     * 
+     */
+    public BlockContextSource(String location, Map blockContexts, ServiceManager manager,
Logger logger) {
+        this.setScheme(location.substring(0, location.indexOf(":/")));
+        this.setSystemId(location);
+        this.blockContexts = blockContexts;
+        this.manager = manager;
+        this.logger = logger;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.Source#exists()
+     */
+    public boolean exists() {
+        return true;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.TraversableSource#getChild(java.lang.String)
+     */
+    public Source getChild(String name) throws SourceException {
+        String blockContext = (String) this.blockContexts.get(name);
+        if (blockContext == null)
+            throw new SourceException("The block named " + name + "doesn't exist");
+
+        SourceResolver resolver = null;
+        try {
+            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+            return resolver.resolveURI(blockContext);
+        } catch (ServiceException se) {
+            throw new SourceException("SourceResolver is not available.", se);
+        } catch (IOException e) {
+            throw new SourceException("The source for block " + name +
+                    " could not be resolved ", e);
+        } finally {
+            this.manager.release(resolver);
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.TraversableSource#getChildren()
+     */
+    public Collection getChildren() throws SourceException {
+        Collection contextPaths = this.blockContexts.values();
+        ArrayList children = new ArrayList(contextPaths.size());
+        SourceResolver resolver = null;
+        String contextPath = null;
+        try {
+            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+            Iterator i = contextPaths.iterator();
+            while (i.hasNext()) {
+                contextPath = (String) i.next();
+                Source child = resolver.resolveURI(contextPath);
+                children.add(child);
+            }
+            return children;
+        } catch (ServiceException se) {
+            throw new SourceException("SourceResolver is not available.", se);
+        } catch (IOException e) {
+            throw new SourceException("The block context path " + contextPath +
+                    " could not be resolved ", e);
+        } finally {
+            this.manager.release(resolver);
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.TraversableSource#getName()
+     */
+    public String getName() {
+        // FIXME: what name is the root directory supposed to have?
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.TraversableSource#getParent()
+     */
+    public Source getParent() throws SourceException {
+        // this is the root node so there is no parent
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.TraversableSource#isCollection()
+     */
+    public boolean isCollection() {
+        // this source is always a directory
+        return true;
+    }
+
+}

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

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSourceFactory.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSourceFactory.java?view=auto&rev=471587
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSourceFactory.java
(added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSourceFactory.java
Sun Nov  5 16:43:30 2006
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.cocoon.core.deployment.DeploymentUtil;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.SourceResolver;
+
+/**
+ * Create a <code>BlockContextSource</code> that makes the resources of a block
available.
+ * The form of the URL is blockcontext:/blockname/path.
+ * 
+ * @version $Id$
+ */
+public class BlockContextSourceFactory extends AbstractLogEnabled implements
+        SourceFactory, Serviceable, ThreadSafe {
+    
+    private ServiceManager serviceManager;
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+     */
+    public void service(ServiceManager serviceManager) throws ServiceException {
+        this.serviceManager = serviceManager;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
+     */
+    public Source getSource(String location, Map parameters) throws IOException,
+            MalformedURLException {
+
+        Map blockContexts = DeploymentUtil.getBlockContexts();
+
+        // the root "directory" of the blocks
+        if (location.endsWith(":/"))
+            return new BlockContextSource(location, blockContexts, this.serviceManager, this.getLogger());
+        
+        // Remove the protocol and the first '/'
+        int pos = location.indexOf(":/");
+        String path = location.substring(pos+1);
+        
+        pos = path.indexOf('/');
+        if (pos != -1) {
+            // extract the block name and get the block context path
+            String blockName = path.substring(0, pos);
+            path = path.substring(pos);
+            String blockContext = (String) blockContexts.get(blockName);
+            if (blockContext == null)
+                throw new MalformedURLException("Unknown block name " + blockName +
+                        " in block context uri " + location);
+
+            // construct the path relative the block context
+            String resolvedPath = blockContext + path;
+            if (this.getLogger().isDebugEnabled())
+                this.getLogger().debug("block context source " + location +
+                        " is resolved to " + resolvedPath);
+
+            SourceResolver resolver = null;
+            try {
+                resolver = (SourceResolver) this.serviceManager.lookup(SourceResolver.ROLE);
+                return resolver.resolveURI(resolvedPath);
+            } catch (ServiceException se) {
+                throw new SourceException("SourceResolver is not available.", se);
+            } finally {
+                this.serviceManager.release(resolver);
+            }
+        } else {
+            throw new MalformedURLException("The block name part of a block context uri must
end with a '/':" + location);
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
+     */
+    public void release(Source source) {
+    }
+
+}

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

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSourceFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/deployment/DeploymentUtil.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/deployment/DeploymentUtil.java?view=diff&rev=471587&r1=471586&r2=471587
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/deployment/DeploymentUtil.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/deployment/DeploymentUtil.java
Sun Nov  5 16:43:30 2006
@@ -24,6 +24,8 @@
 import java.net.JarURLConnection;
 import java.net.URL;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.jar.JarFile;
 import java.util.zip.ZipEntry;
 
@@ -46,6 +48,8 @@
     protected static final String RESOURCES_PATH = "COB-INF";
 
     protected final String destinationDirectory;
+    
+    protected static final Map blockContexts = new HashMap(); 
 
     public DeploymentUtil(ServletContext servletContext) {
         // TODO how do we handle non servlet container environment?
@@ -112,7 +116,10 @@
                 buffer.append(relativeDirectory);
                 buffer.append(File.separatorChar);
                 buffer.append(blockName);
-                this.deploy(jarFile, resourcePattern, buffer.toString());
+                String destination = buffer.toString();
+                this.deploy(jarFile, resourcePattern, destination);
+                // register the root URL for the block resources
+                DeploymentUtil.blockContexts.put(blockName, destination);
             }
         }        
     }
@@ -125,4 +132,15 @@
             this.deployBlockResources(DeploymentUtil.RESOURCES_PATH, "blocks");
         }
     }
+
+    /**
+     * Get a map that associates a block name with the root URL of the blocks resources
+     *  
+     * @return the blockContexts
+     */
+    // FIXME: It would be better to make the block contexts mapping available as a
+    // component instead of as a static method
+    public static Map getBlockContexts() {
+        return blockContexts;
+    }    
 }



Mime
View raw message