You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2007/10/04 15:51:31 UTC

svn commit: r581907 - in /cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon: SitemapElementParser.java SourceIOInputStream.java SourceResourceLoader.java

Author: cziegeler
Date: Thu Oct  4 06:51:29 2007
New Revision: 581907

URL: http://svn.apache.org/viewvc?rev=581907&view=rev
Log:
Possible fix for COCOON-2138: Wrap excalibur source into spring resource.

Added:
    cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceIOInputStream.java   (with props)
Modified:
    cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SitemapElementParser.java
    cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceResourceLoader.java

Modified: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SitemapElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SitemapElementParser.java?rev=581907&r1=581906&r2=581907&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SitemapElementParser.java (original)
+++ cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SitemapElementParser.java Thu Oct  4 06:51:29 2007
@@ -19,6 +19,7 @@
 package org.apache.cocoon.core.container.spring.avalon;
 
 import org.apache.cocoon.spring.configurator.WebAppContextUtils;
+import org.apache.excalibur.source.SourceResolver;
 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.core.io.ResourceLoader;
@@ -66,7 +67,7 @@
         WebApplicationContext parentContext = WebAppContextUtils.getCurrentWebApplicationContext();
         return ConfigurationReader.readSitemap((ConfigurationInfo)parentContext.getBean(ConfigurationInfo.class.getName()),
                                                location,
-                                               new SourceResourceLoader(resourceLoader));
+                                               new SourceResourceLoader(resourceLoader, (SourceResolver)parentContext.getBean(SourceResolver.ROLE)));
     }
 
     protected String getConfigurationLocation() {

Added: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceIOInputStream.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceIOInputStream.java?rev=581907&view=auto
==============================================================================
--- cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceIOInputStream.java (added)
+++ cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceIOInputStream.java Thu Oct  4 06:51:29 2007
@@ -0,0 +1,129 @@
+/*
+ * 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.core.container.spring.avalon;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
+
+public class SourceIOInputStream extends InputStream {
+
+    protected final InputStream delegate;
+
+    protected final Source source;
+
+    protected boolean closed;
+
+    protected final SourceResolver resolver;
+
+    public SourceIOInputStream(SourceResolver resolver, Source source) throws IOException {
+        this.source = source;
+        this.delegate = source.getInputStream();
+        this.resolver = resolver;
+        this.closed = false;
+    }
+
+    /**
+     * @see java.io.InputStream#available()
+     */
+    public int available() throws IOException {
+        this.check();
+        return this.delegate.available();
+    }
+
+    /**
+     * @see java.io.InputStream#close()
+     */
+    public void close() throws IOException {
+        if ( !this.closed ) {
+            this.closed = true;
+            IOException e = null;
+            try {
+                this.delegate.close();
+            } catch (IOException i) {
+                e = i;
+            } finally {
+                this.resolver.release(this.source);
+            }
+            if ( e != null ) {
+                throw e;
+            }
+        }
+    }
+
+    /**
+     * @see java.io.InputStream#mark(int)
+     */
+    public void mark(int arg0) {
+        this.delegate.mark(arg0);
+    }
+
+    /**
+     * @see java.io.InputStream#markSupported()
+     */
+    public boolean markSupported() {
+        return this.delegate.markSupported();
+    }
+
+    /**
+     * @see java.io.InputStream#read()
+     */
+    public int read() throws IOException {
+        this.check();
+        return this.delegate.read();
+    }
+
+    /**
+     * @see java.io.InputStream#read(byte[], int, int)
+     */
+    public int read(byte[] arg0, int arg1, int arg2) throws IOException {
+        this.check();
+        return this.delegate.read(arg0, arg1, arg2);
+    }
+
+    /**
+     * @see java.io.InputStream#read(byte[])
+     */
+    public int read(byte[] arg0) throws IOException {
+        this.check();
+        return this.delegate.read(arg0);
+    }
+
+    /**
+     * @see java.io.InputStream#reset()
+     */
+    public void reset() throws IOException {
+        this.check();
+        this.delegate.reset();
+    }
+
+    /**
+     * @see java.io.InputStream#skip(long)
+     */
+    public long skip(long arg0) throws IOException {
+        this.check();
+        return this.delegate.skip(arg0);
+    }
+
+    protected void check() throws IOException {
+        if ( this.closed ) {
+            throw new IOException("Input stream has already been closed.");
+        }
+    }
+}

Propchange: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceIOInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceIOInputStream.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Modified: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceResourceLoader.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceResourceLoader.java?rev=581907&r1=581906&r2=581907&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceResourceLoader.java (original)
+++ cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/core/container/spring/avalon/SourceResourceLoader.java Thu Oct  4 06:51:29 2007
@@ -18,8 +18,15 @@
  */
 package org.apache.cocoon.core.container.spring.avalon;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
+import java.net.URL;
 
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.excalibur.source.SourceUtil;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
 import org.springframework.core.io.UrlResource;
@@ -28,8 +35,11 @@
 
     protected final ResourceLoader wrappedLoader;
 
-    public SourceResourceLoader(ResourceLoader wrappedLoader) {
+    protected final SourceResolver resolver;
+
+    public SourceResourceLoader(ResourceLoader wrappedLoader, SourceResolver resolver) {
         this.wrappedLoader = wrappedLoader;
+        this.resolver = resolver;
     }
 
     /**
@@ -51,5 +61,77 @@
             }
         }
         return this.wrappedLoader.getResource(location);
+    }
+
+    public static class SourceResource implements Resource {
+
+        protected Source source;
+        protected SourceResolver resolver;
+        protected boolean open = false;
+
+        public SourceResource(Source s, SourceResolver r) {
+            this.source = s;
+            this.resolver =r;
+        }
+
+        /**
+         * @see org.springframework.core.io.InputStreamSource#getInputStream()
+         */
+        public InputStream getInputStream() throws IOException {
+            this.open = true;
+            return new SourceIOInputStream(this.resolver, this.source);
+        }
+
+        /**
+         * @see org.springframework.core.io.Resource#createRelative(java.lang.String)
+         */
+        public Resource createRelative(String uri) throws IOException {
+            int pos = this.source.getURI().lastIndexOf('/');
+            return new SourceResource(this.resolver.resolveURI(uri, this.source.getURI().substring(0, pos), null), this.resolver);
+        }
+
+        /**
+         * @see org.springframework.core.io.Resource#exists()
+         */
+        public boolean exists() {
+            return this.source.exists();
+        }
+
+        /**
+         * @see org.springframework.core.io.Resource#getDescription()
+         */
+        public String getDescription() {
+            return "Source: " + this.source;
+        }
+
+        /**
+         * @see org.springframework.core.io.Resource#getFile()
+         */
+        public File getFile() throws IOException {
+            return SourceUtil.getFile(this.source);
+        }
+
+        /**
+         * @see org.springframework.core.io.Resource#getFilename()
+         */
+        public String getFilename() {
+            int pos = this.source.getURI().lastIndexOf('/');
+            return this.source.getURI().substring(pos + 1);
+        }
+
+        /**
+         * @see org.springframework.core.io.Resource#getURL()
+         */
+        public URL getURL() throws IOException {
+            return new URL(this.source.getURI());
+        }
+
+        /**
+         * @see org.springframework.core.io.Resource#isOpen()
+         */
+        public boolean isOpen() {
+            return this.open;
+        }
+
     }
 }