You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by da...@apache.org on 2006/02/12 10:31:27 UTC

svn commit: r377147 - in /cocoon/trunk/cocoon-blocks-fw: cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/ cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/

Author: danielf
Date: Sun Feb 12 01:31:25 2006
New Revision: 377147

URL: http://svn.apache.org/viewcvs?rev=377147&view=rev
Log:
Generalized the location in wiring.xml so that any (jdk) URL can be used. URLs are resolved relative to the BlocksManager context. Possibly a SourceResolver and some Sources should be used instead.

I have tested it both with unpackaged blocks (through the file: protocol) and with packaged blocks (through the jar: protocol e.g. "jar:file:/c:/blocks/test.jar!/").

Modified:
    cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockContext.java
    cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockWiring.java
    cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlockManager.java
    cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlocksManager.java

Modified: cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockContext.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockContext.java?rev=377147&r1=377146&r2=377147&view=diff
==============================================================================
--- cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockContext.java (original)
+++ cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockContext.java Sun Feb 12 01:31:25 2006
@@ -42,9 +42,11 @@
     private Hashtable attributes;
     private BlockWiring wiring;
     private Servlet servlet;
+    private URL contextURL;
     
-    public BlockContext(ServletContext parentContext, BlockWiring wiring) {
+    public BlockContext(ServletContext parentContext, URL contextURL, BlockWiring wiring) {
         super(parentContext);
+        this.contextURL = contextURL;
         this.wiring = wiring;
     }
 
@@ -91,8 +93,10 @@
      * @see javax.servlet.ServletContext#getResource(java.lang.String)
      */
     public URL getResource(String path) throws MalformedURLException {
-        String location = this.wiring.getLocation();
-        return super.servletContext.getResource(location + path);
+        if (path.length() == 0 || path.charAt(0) != '/')
+            throw new MalformedURLException("The path must start with '/' " + path);
+        path = path.substring(1);
+        return new URL(this.contextURL, path);
     }
 
     /*

Modified: cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockWiring.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockWiring.java?rev=377147&r1=377146&r2=377147&view=diff
==============================================================================
--- cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockWiring.java (original)
+++ cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-impl/src/main/java/org/apache/cocoon/blocks/BlockWiring.java Sun Feb 12 01:31:25 2006
@@ -22,8 +22,6 @@
 import java.util.Map;
 import java.util.Vector;
 
-import javax.servlet.ServletContext;
-
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
@@ -38,9 +36,8 @@
     extends AbstractLogEnabled
     implements Configurable{ 
 
-    private ServletContext servletContext;
+    private URL contextURL;
     private String id;
-    private String location;
     private Map connections = new HashMap();
     private Map properties = new HashMap();
     private Vector connectionNames;
@@ -51,12 +48,12 @@
     private boolean hasServlet;
     private String servletClass;
     private Configuration servletConfiguration;
-    
+        
     /**
-      * @param servletContext The servletContext to set.
-      */
-    public void setServletContext(ServletContext servletContext) {
-        this.servletContext = servletContext;
+     * @param contextURL The contextURL to set.
+     */
+    public void setContextURL(URL contextURL) {
+        this.contextURL = contextURL;
     }
 
     
@@ -67,15 +64,9 @@
         this.id = config.getAttribute("id");
         this.mountPath = config.getChild("mount").getAttribute("path", null);
 
-        this.location = config.getAttribute("location");
-        int length = this.location.length();
-        if (length > 0 && this.location.charAt(length - 1) == '/')
-            this.location = this.location.substring(0, length - 1);
-
-
         getLogger().debug("BlockWiring configure: " +
                           " id=" + this.id +
-                          " location=" + this.location +
+                          " location=" + this.contextURL +
                           " mountPath=" + this.mountPath);
 
         Configuration[] connections =
@@ -101,16 +92,13 @@
         }
 
         // Read the block.xml file
-        String blockPath = this.location + BlockConstants.BLOCK_CONF;
         URL blockURL;
+        String blockPath = this.contextURL + BlockConstants.BLOCK_CONF.substring(1);
         Configuration block = null;
 
         try {
-            blockURL = this.servletContext.getResource(blockPath);
-            if (blockURL == null)
-                throw new ConfigurationException("Couldn't find " + blockPath);
+            blockURL = new URL(this.contextURL, BlockConstants.BLOCK_CONF.substring(1));
             DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
-            //block = builder.build(source.getInputStream(), source.getURI());
             block = builder.build(blockURL.openStream(), blockURL.toExternalForm());
         } catch (IOException e) {
             String msg = "Exception while reading " + blockPath + ": " + e.getMessage();
@@ -153,13 +141,6 @@
         return this.id;
     }
     
-    /**
-     * Get the location of the block
-     */
-    public String getLocation() {
-        return this.location;
-    }
-
     /**
      * Get the names for the connections from this block. The name (super) of the super block is not included.
      */

Modified: cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlockManager.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlockManager.java?rev=377147&r1=377146&r2=377147&view=diff
==============================================================================
--- cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlockManager.java (original)
+++ cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlockManager.java Sun Feb 12 01:31:25 2006
@@ -17,6 +17,7 @@
 
 import java.io.IOException;
 import java.lang.reflect.Method;
+import java.net.URL;
 
 import javax.servlet.Servlet;
 import javax.servlet.ServletConfig;
@@ -59,6 +60,8 @@
     private BlockContext blockContext;
     private ServiceManagerRegistry serviceManagerRegistry;
 
+    private URL contextURL;
+
     public void enableLogging(Logger logger) {
         this.logger = logger;
     }
@@ -75,11 +78,15 @@
     public void setServiceManagerRegistry(ServiceManagerRegistry serviceManagerRegistry) {
         this.serviceManagerRegistry = serviceManagerRegistry;
     }
+    
+    public void setContextURL(URL contextURL) {
+        this.contextURL = contextURL;
+    }
 
     public void init(ServletConfig servletConfig) throws ServletException {
         super.init(servletConfig);
         this.blockWiring = new BlockWiring();
-        this.blockWiring.setServletContext(this.getServletContext());
+        this.blockWiring.setContextURL(this.contextURL);
         try {
             LifecycleHelper.setupComponent(this.blockWiring,
                                            this.getLogger(),
@@ -93,7 +100,7 @@
         getLogger().debug("Initializing new Block Manager: " + this.blockWiring.getId());
 
         this.blockContext =
-            new BlockContext(this.getServletContext(), this.blockWiring);
+            new BlockContext(this.getServletContext(), this.contextURL, this.blockWiring);
         ServletConfig blockServletConfig =
             new ServletConfigurationWrapper(this.getServletConfig(), this.blockContext);
 

Modified: cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlocksManager.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlocksManager.java?rev=377147&r1=377146&r2=377147&view=diff
==============================================================================
--- cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlocksManager.java (original)
+++ cocoon/trunk/cocoon-blocks-fw/cocoon-blocks-fw-servlet-impl/src/main/java/org/apache/cocoon/blocks/servlet/BlocksManager.java Sun Feb 12 01:31:25 2006
@@ -48,6 +48,7 @@
 import org.apache.cocoon.components.LifecycleHelper;
 import org.apache.cocoon.components.source.SourceUtil;
 import org.apache.cocoon.components.source.impl.DelayedRefreshSourceWrapper;
+import org.apache.cocoon.core.servlet.CoreUtil;
 import org.apache.cocoon.core.servlet.LoggerUtil;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.impl.URLSource;
@@ -65,6 +66,7 @@
 
     public static String ROLE = BlocksManager.class.getName();
     private BlocksContext blocksContext;
+    private URL contextURL;
 
     private Source wiringFile;
     private HashMap blocks = new HashMap();
@@ -76,6 +78,12 @@
     public void init(ServletConfig servletConfig) throws ServletException {
         super.init(servletConfig);
         this.blocksContext = new BlocksContext(this.getServletContext(), this);
+        String contextURL0 = CoreUtil.getContextURL(this.blocksContext, BlockConstants.WIRING);
+        try {
+            this.contextURL = new URL(contextURL0);
+        } catch (MalformedURLException e) {
+            throw new ServletException("Could not parse " + contextURL0, e);
+        }
         
         LoggerUtil loggerUtil =
             new LoggerUtil(this.getServletConfig(), BlockConstants.WIRING);
@@ -118,16 +126,17 @@
             } catch (ConfigurationException e) {
                 throw new ServletException("Couldn't get location from the wiring file");
             }
-            URL classesDir;
+            URL url;
             try {
-                classesDir = this.getServletContext().getResource(location);
+                url = this.resolve(location);
             } catch (MalformedURLException e) {
+                e.printStackTrace();
                 throw new ServletException("Couldn't get location of the classes of the block", e);
             }
-            if (classesDir != null) {
-                urlList.add(classesDir);
+            if (url != null) {
+                urlList.add(url);
                 if(this.logger.isDebugEnabled()) {
-                    this.logger.debug("added " + classesDir.toString());
+                    this.logger.debug("added " + url.toString());
                 }
             } else {
                 if(this.logger.isDebugEnabled()) {
@@ -158,6 +167,11 @@
                     " id=" + id +
                     " location=" + location);
             BlockManager blockManager = new BlockManager();
+            try {
+                blockManager.setContextURL(this.resolve(location));
+            } catch (MalformedURLException e) {
+                throw new ServletException("Could not resolve " + location, e);
+            }
             blockManager.setServiceManagerRegistry(this.serviceManagerRegistry);
             try {
                 LifecycleHelper.setupComponent(blockManager,
@@ -245,6 +259,27 @@
         return date < this.wiringFile.getLastModified();
     }
         
+    /**
+     * Resolve a path relative to the servlet context. Paths starting with '/' are
+     * supposed to be relative the servlet context to follow the behavior from 
+     * ServletContext.getResource. Use "file:" for file system paths instead.
+     * @param path
+     * @return
+     * @throws MalformedURLException
+     */
+    private URL resolve(String path) throws MalformedURLException {
+        if (path.charAt(0) == '/')
+            path = path.substring(1);
+        System.out.println("BlocksManager.resolve path=" + path +
+                " contextURL=" + this.contextURL);
+
+        URL result = new URL(this.contextURL, path);
+        
+        System.out.println("BlocksManager.resolve to=" + result);
+        
+        return result;
+    }
+
     /**
      * Utility function to ensure that the parts of the request URI not is null
      * and not ends with /