cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dani...@apache.org
Subject svn commit: r473701 - in /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon: components/source/impl/BlockContextSource.java core/deployment/DeploymentUtil.java
Date Sat, 11 Nov 2006 12:24:39 GMT
Author: danielf
Date: Sat Nov 11 04:24:37 2006
New Revision: 473701

URL: http://svn.apache.org/viewvc?view=rev&rev=473701
Log:
Make Cocoon runnable inside IDEs (or at least inside Eclipse).
* Extended the DeploymentUtil so that it handles the case when the blocks in the classpath
has the URL type file:.
* Refactored BlockContextSource to create map with child sources in the constructor instead
of in method invocations.
* Added a dynamic proxy to child sources in the BlockContextSource, so that the child source
get the block name as name. The previous code only worked in e.g. the TraversableGenerator
if the source that the block name was resolved to happened to have the block name as collection
name.

Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/deployment/DeploymentUtil.java

Modified: 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=diff&rev=473701&r1=473700&r2=473701
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockContextSource.java
Sat Nov 11 04:24:37 2006
@@ -17,18 +17,25 @@
 package org.apache.cocoon.components.source.impl;
 
 import java.io.IOException;
-import java.util.ArrayList;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.net.MalformedURLException;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.CascadingIOException;
+import org.apache.commons.collections.map.HashedMap;
 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;
+import org.springframework.util.ClassUtils;
 
 /**
  * This source makes the immediate children to the blockcontext:/ uri
@@ -42,15 +49,22 @@
     
     private Map blockContexts;
     private ServiceManager manager;
+    private Map children;
 
     /**
+     * @throws IOException 
+     * @throws MalformedURLException 
+     * @throws IOException 
+     * @throws MalformedURLException 
      * 
      */
-    public BlockContextSource(String location, Map blockContexts, ServiceManager manager)
{
+    public BlockContextSource(String location, Map blockContexts, ServiceManager manager)
+    throws MalformedURLException, IOException {
         this.setScheme(location.substring(0, location.indexOf(":/")));
         this.setSystemId(location);
         this.blockContexts = blockContexts;
         this.manager = manager;
+        this.children = this.createChildren();
     }
 
     /* (non-Javadoc)
@@ -64,49 +78,62 @@
      * @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)
+        Source child = (Source) this.children.get(name);
+        if (child == 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);
-        }
+        return child;
     }
 
     /* (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());
+        return this.children.values();
+    }
+
+    private Map createChildren() throws MalformedURLException, IOException  {
+        Map children = new HashedMap(this.blockContexts.size());
         SourceResolver resolver = null;
-        String contextPath = null;
         try {
             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
-            Iterator i = contextPaths.iterator();
+            Iterator i = this.blockContexts.entrySet().iterator();
             while (i.hasNext()) {
-                contextPath = (String) i.next();
+                Entry entry = (Entry) i.next();
+                String blockName = (String) entry.getKey();
+                String contextPath = (String) entry.getValue();
                 Source child = resolver.resolveURI(contextPath);
-                children.add(child);
+                if (child instanceof TraversableSource)
+                    child = adjustName((TraversableSource) child, blockName);
+                children.put(blockName, 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);
+            throw new CascadingIOException("SourceResolver is not available.", se);
         } finally {
             this.manager.release(resolver);
         }
+    }
+    
+    /*
+     * Adjust the traversable source so that it use the block name as name instead of
+     * e.g. the directory name from the source that the block name was resolved to.
+     */
+    private static Source adjustName(final TraversableSource source, final String blockName)
{
+        TraversableSource adjustedSource =
+            (TraversableSource) Proxy.newProxyInstance(source.getClass().getClassLoader(),
ClassUtils.getAllInterfaces(source),
+                new InvocationHandler() {
+
+                    /* (non-Javadoc)
+                     * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
java.lang.reflect.Method, java.lang.Object[])
+                     */
+                    public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable {
+                        if (method.equals(TraversableSource.class.getMethod("getName", new
Class[]{})))
+                            return blockName;
+                        else
+                            return method.invoke(source, args);
+                    }
+        });
+        return adjustedSource;
     }
 
     /* (non-Javadoc)

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=473701&r1=473700&r2=473701
==============================================================================
--- 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
Sat Nov 11 04:24:37 2006
@@ -80,12 +80,21 @@
         final Enumeration jarUrls = this.getClass().getClassLoader().getResources(resourcePattern);
         while ( jarUrls.hasMoreElements() ) {
             final URL resourceUrl = (URL)jarUrls.nextElement();
-            // we only handle jar files!
-            // TODO - Should we throw an exception if it's not a jar file? (or log?)
-            if ( "jar".equals(resourceUrl.getProtocol()) ) {
+
+            String url = resourceUrl.toExternalForm();
+            if ( "file".equals(resourceUrl.getProtocol()) ) {
+                // FIXME: This only cover the siuation when the project is Maven generated
+                // if this is a file url generated by the Maven,
+                // it has this form "file:{url}/{block name}/target/classes/COB-INF
+                int pos = url.indexOf("/target/classes/COB-INF");
+                // extract block name
+                String blockName = url.substring(0, pos);
+                blockName = blockName.substring(blockName.lastIndexOf('/') + 1);
+                // register the root URL for the block resources
+                DeploymentUtil.blockContexts.put(blockName, url);
+            } else if ( "jar".equals(resourceUrl.getProtocol()) ) {
                 // if this is a jar url, it has this form "jar:{url-to-jar}!/{resource-path}
                 // to open the jar, we can simply remove everything after "!/"
-                String url = resourceUrl.toExternalForm();
                 int pos = url.indexOf('!');
                 url = url.substring(0, pos+2); // +2 as we include "!/"
                 final URL jarUrl = new URL(url);
@@ -110,6 +119,8 @@
                 // register the root URL for the block resources
                 DeploymentUtil.blockContexts.put(blockName, destination);
             }
+            // we only handle jar files and ordinary files
+            // TODO - Should we throw an exception if it's some other protocol type? (or
log?)
         }        
     }
 



Mime
View raw message