You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by cr...@apache.org on 2004/12/14 00:27:17 UTC

svn commit: r111762 - in excalibur/trunk/components/sourceresolve: . src/java/org/apache/excalibur/source/impl

Author: crafterm
Date: Mon Dec 13 15:27:16 2004
New Revision: 111762

URL: http://svn.apache.org/viewcvs?view=rev&rev=111762
Log:
Closes Excalibur JIRA #18.

This patch adds support for using Commons VFS as the protocol engine
inside of the source resolver.

This opens up several new protocols, including CIFS, SFTP, amongst others,
(essentially anything supported by Commons VFS) to users of the source
resolver library.

Added:
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSourceFactory.java
Modified:
   excalibur/trunk/components/sourceresolve/project.xml

Modified: excalibur/trunk/components/sourceresolve/project.xml
Url: http://svn.apache.org/viewcvs/excalibur/trunk/components/sourceresolve/project.xml?view=diff&rev=111762&p1=excalibur/trunk/components/sourceresolve/project.xml&r1=111761&p2=excalibur/trunk/components/sourceresolve/project.xml&r2=111762
==============================================================================
--- excalibur/trunk/components/sourceresolve/project.xml	(original)
+++ excalibur/trunk/components/sourceresolve/project.xml	Mon Dec 13 15:27:16 2004
@@ -47,6 +47,11 @@
             <version>4.1.5</version>
         </dependency>
         <dependency>
+            <groupId>commons-vfs</groupId>
+            <artifactId>commons-vfs</artifactId>
+            <version>20030518103800</version>
+        </dependency>
+        <dependency>
             <id>commons-httpclient</id>
             <version>2.0</version>
         </dependency>

Added: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSource.java
Url: http://svn.apache.org/viewcvs/excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSource.java?view=auto&rev=111762
==============================================================================
--- (empty file)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSource.java	Mon Dec 13 15:27:16 2004
@@ -0,0 +1,157 @@
+/* 
+ * Copyright 2002-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.excalibur.source.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+
+import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.commons.vfs.FileContent;
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemManager;
+import org.apache.commons.vfs.VFS;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceUtil;
+
+/**
+ * Source implementation that provides resolver access to all protocols
+ * supported by <a href="http://jakarta.apache.org/commons/sandbox/vfs">Commons VFS</a>. 
+ * 
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version $Revision:$
+ * @since Nov 19, 2004 10:54:02 AM
+ */
+public class CommonsVFSSource extends AbstractSource
+    implements LogEnabled {
+    
+    /**
+     * Constructor, creates instance of class.
+     *
+     * @param location location to resolve
+     * @param parameters protocol specific parameters
+     * @throws FileSystemException if an error occurs
+     */
+    public CommonsVFSSource(final String location, final Map parameters)
+        throws FileSystemException {
+        m_location = location;
+        m_manager = VFS.getManager();
+        m_fileObject = m_manager.resolveFile(location); // REVISIT: parameters
+        m_fileContent = m_fileObject.getContent();
+    }
+    
+    /**
+     * Sets content information for this source.
+     * 
+     * @see org.apache.excalibur.source.impl.AbstractSource#getInfos()
+     */
+    protected void getInfos() {
+        try {
+            setContentLength(m_fileContent.getSize());
+        } catch (final FileSystemException e) {
+            
+            if (getLogger().isWarnEnabled()) {
+                getLogger().warn(
+                    "Unable to determine content length for " + m_location, e
+                );
+            }
+            setContentLength(-1); // Source API says return -1 if unknown
+        }
+        
+        try {
+            setLastModified(m_fileContent.getLastModifiedTime());
+        } catch (final FileSystemException e) {
+
+            if (getLogger().isWarnEnabled()) {
+                getLogger().warn(
+                    "Unable to determine last modified date for " + m_location, e
+                );
+            }
+            setLastModified(0); // Source API says return 0 if unknown
+        }
+
+        setSystemId(m_location);
+        setScheme(SourceUtil.getScheme(m_location));
+    }
+    
+    /**
+     * Obtain an {@link InputStream} for this source.
+     * 
+     * @throws IOException if an IO error occurs
+     * @throws SourceException if a source exception occurs
+     * @return {@link InputStream}
+     * @see org.apache.excalibur.source.Source#getInputStream()
+     */
+    public InputStream getInputStream() throws IOException, SourceException {
+        return m_fileContent.getInputStream();
+    }
+
+    /**
+     * Whether this resource exists or not
+     * 
+     * @return true if it does exist, false otherwise, false on error
+     * @see org.apache.excalibur.source.Source#exists()
+     */
+    public boolean exists() {
+        try {
+            return m_fileObject.exists();
+        } catch (final FileSystemException e) {
+
+            if (getLogger().isWarnEnabled()) {
+                getLogger().warn("Unable to determine existence for " + m_location, e);
+            }
+            return false;
+        }
+    }
+
+    /**
+     * Enables logging for this source.
+     * 
+     * @param logger {@link Logger} instance to use
+     * @see org.apache.avalon.framework.logger.LogEnabled
+     *      #enableLogging(org.apache.avalon.framework.logger.Logger)
+     */
+    public void enableLogging(final Logger logger) {
+        m_logger = logger;
+    }
+    
+    /**
+     * Obtain access to this components logger.
+     * 
+     * @return the logger
+     */
+    private Logger getLogger() {
+        return m_logger;
+    }
+
+    /** Resource location */
+    private final String m_location;
+    
+    /** {@link FileSystemManager} reference */
+    private final FileSystemManager m_manager;
+    
+    /** The resource itself */
+    private final FileObject m_fileObject;
+    
+    /** The content of the resource */
+    private final FileContent m_fileContent;
+    
+    /** Our logging target */
+    private Logger m_logger;
+}

Added: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSourceFactory.java
Url: http://svn.apache.org/viewcvs/excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSourceFactory.java?view=auto&rev=111762
==============================================================================
--- (empty file)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/CommonsVFSSourceFactory.java	Mon Dec 13 15:27:16 2004
@@ -0,0 +1,70 @@
+/* 
+ * Copyright 2002-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.excalibur.source.impl;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceFactory;
+
+/**
+ * A factory for source types supported by 
+ * <a href="http://jakarta.apache.org/commons/sandbox/vfs">Commons VFS</a>.
+ * 
+ * @avalon.component
+ * @avalon.service type=SourceFactory
+ * @x-avalon.info name=file-source
+ * @x-avalon.lifestyle type=singleton
+ * 
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version $Id: $
+ */
+public class CommonsVFSSourceFactory extends AbstractLogEnabled 
+    implements SourceFactory, ThreadSafe
+{
+    /**
+     * Returns a {@link CommonsVFSSource} instance primed with the specified location
+     * 
+     * @param location source location
+     * @param parameters source parameters
+     * @throws IOException if an IO error occurs
+     * @throws MalformedURLException if a URL is malformed
+     * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
+     */
+    public Source getSource( String location, Map parameters ) throws IOException, MalformedURLException
+    {
+        final Source source = new CommonsVFSSource( location, parameters );
+        ContainerUtil.enableLogging(source, getLogger());
+        return source;
+    }
+
+    /**
+     * Releases the given source.
+     * 
+     * @param source source to release 
+     * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
+     */
+    public void release( Source source )
+    {
+        // Nothing to do here
+    }
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org