commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r159536 - in jakarta/commons/sandbox/vfs/trunk: src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java xdocs/api.xml xdocs/index.xml
Date Wed, 30 Mar 2005 23:11:42 GMT
Author: imario
Date: Wed Mar 30 15:11:40 2005
New Revision: 159536

URL: http://svn.apache.org/viewcvs?view=rev&rev=159536
Log:
process /META-INF/vfs-providers.xml found in the classpath

Modified:
    jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java
    jakarta/commons/sandbox/vfs/trunk/xdocs/api.xml
    jakarta/commons/sandbox/vfs/trunk/xdocs/index.xml

Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java?view=diff&r1=159535&r2=159536
==============================================================================
--- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java
(original)
+++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java
Wed Mar 30 15:11:40 2005
@@ -26,8 +26,15 @@
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.StringTokenizer;
+import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 
 /**
  * A {@link org.apache.commons.vfs.FileSystemManager} that configures itself
@@ -45,6 +52,7 @@
     private Log log = LogFactory.getLog(StandardFileSystemManager.class);
 
     private static final String CONFIG_RESOURCE = "providers.xml";
+    private static final String PLUGIN_CONFIG_RESOURCE = "META-INF/vfs-providers.xml";
 
     private String configUri;
     private ClassLoader classLoader;
@@ -95,10 +103,77 @@
         // Configure
         configure(configUri);
 
+        // Configure Plugins
+        configurePlugins();
+
         // Initialise super-class
         super.init();
     }
 
+    /**
+     * Scans the classpath to find any droped plugin.<br />
+     * The plugin-description has to be in /META-INF/vfs-providers.xml
+     */
+    protected void configurePlugins() throws FileSystemException
+    {
+        String classpath = System.getProperty("java.class.path");
+        if (classpath == null)
+        {
+            // huh? why should that be?
+            return;
+        }
+        
+        StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator, false);
+        while (st.hasMoreTokens())
+        {
+            String path = st.nextToken();
+
+            if (path.length() > 4 && path.substring(path.length()-4).toLowerCase().equals(".jar"))
+            {
+                try
+                {
+                    JarFile jarFile = new JarFile(path);
+                    JarEntry jarEntry = jarFile.getJarEntry(PLUGIN_CONFIG_RESOURCE);
+                    if (jarEntry != null)
+                    {
+                        InputStream configStream = null;
+                        try
+                        {
+                            configStream = jarFile.getInputStream(jarEntry);
+                            configure(jarEntry.getName(), configStream);
+                        }
+                        finally
+                        {
+                            if (configStream != null)
+                            {
+                                configStream.close();
+                            }
+                        }
+                    }
+                }
+                catch (FileSystemException e)
+                {
+                    // VFS exception - rethrow
+                    // Need to do this as FileSystemException extends IOException
+                    throw e;
+                }
+                catch (IOException e)
+                {
+                    // Maybe a damaged jar? Complain about but continue ...
+                    log.warn(e.getLocalizedMessage(), e);
+                }
+            }
+            else
+            {
+                File config = new File(path, PLUGIN_CONFIG_RESOURCE);
+                if (config.exists() && config.canRead())
+                {
+                    configure(config.getAbsolutePath());
+                }
+            }
+        }
+    }
+
     protected DefaultFileReplicator createDefaultFileReplicator()
     {
         return new DefaultFileReplicator();
@@ -113,49 +188,87 @@
         {
             // Load up the config
             // TODO - validate
-            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            factory.setIgnoringElementContentWhitespace(true);
-            factory.setIgnoringComments(true);
-            factory.setExpandEntityReferences(true);
-            final DocumentBuilder builder = factory.newDocumentBuilder();
+            final DocumentBuilder builder = createDocumentBuilder();
             final Element config = builder.parse(configUri).getDocumentElement();
 
-            // Add the providers
-            final NodeList providers = config.getElementsByTagName("provider");
-            final int count = providers.getLength();
-            for (int i = 0; i < count; i++)
-            {
-                final Element provider = (Element) providers.item(i);
-                addProvider(provider, false);
-            }
+            configure(config);
+        }
+        catch (final Exception e)
+        {
+            throw new FileSystemException("vfs.impl/load-config.error", configUri, e);
+        }
+    }
 
-            // Add the default provider
-            final NodeList defProviders = config.getElementsByTagName("default-provider");
-            if (defProviders.getLength() > 0)
-            {
-                final Element provider = (Element) defProviders.item(0);
-                addProvider(provider, true);
-            }
+    /**
+     * Configures this manager from an XML configuration file.
+     */
+    private void configure(final String configUri, final InputStream configStream) throws
FileSystemException
+    {
+        try
+        {
+            // Load up the config
+            // TODO - validate
+            final DocumentBuilder builder = createDocumentBuilder();
+            final Element config = builder.parse(configStream).getDocumentElement();
 
-            // Add the mime-type maps
-            final NodeList mimeTypes = config.getElementsByTagName("mime-type-map");
-            for (int i = 0; i < mimeTypes.getLength(); i++)
-            {
-                final Element map = (Element) mimeTypes.item(i);
-                addMimeTypeMap(map);
-            }
+            configure(config);
 
-            // Add the extension maps
-            final NodeList extensions = config.getElementsByTagName("extension-map");
-            for (int i = 0; i < extensions.getLength(); i++)
-            {
-                final Element map = (Element) extensions.item(i);
-                addExtensionMap(map);
-            }
         }
         catch (final Exception e)
         {
             throw new FileSystemException("vfs.impl/load-config.error", configUri, e);
+        }
+    }
+
+    /**
+     * Configure and create a DocumentBuilder
+     */
+    private DocumentBuilder createDocumentBuilder() throws ParserConfigurationException
+    {
+        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setIgnoringElementContentWhitespace(true);
+        factory.setIgnoringComments(true);
+        factory.setExpandEntityReferences(true);
+        final DocumentBuilder builder = factory.newDocumentBuilder();
+        return builder;
+    }
+
+    /**
+     * Configures this manager from an parsed XML configuration file
+     */
+    private void configure(final Element config) throws FileSystemException
+    {
+        // Add the providers
+        final NodeList providers = config.getElementsByTagName("provider");
+        final int count = providers.getLength();
+        for (int i = 0; i < count; i++)
+        {
+            final Element provider = (Element) providers.item(i);
+            addProvider(provider, false);
+        }
+
+        // Add the default provider
+        final NodeList defProviders = config.getElementsByTagName("default-provider");
+        if (defProviders.getLength() > 0)
+        {
+            final Element provider = (Element) defProviders.item(0);
+            addProvider(provider, true);
+        }
+
+        // Add the mime-type maps
+        final NodeList mimeTypes = config.getElementsByTagName("mime-type-map");
+        for (int i = 0; i < mimeTypes.getLength(); i++)
+        {
+            final Element map = (Element) mimeTypes.item(i);
+            addMimeTypeMap(map);
+        }
+
+        // Add the extension maps
+        final NodeList extensions = config.getElementsByTagName("extension-map");
+        for (int i = 0; i < extensions.getLength(); i++)
+        {
+            final Element map = (Element) extensions.item(i);
+            addExtensionMap(map);
         }
     }
 

Modified: jakarta/commons/sandbox/vfs/trunk/xdocs/api.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/xdocs/api.xml?view=diff&r1=159535&r2=159536
==============================================================================
--- jakarta/commons/sandbox/vfs/trunk/xdocs/api.xml (original)
+++ jakarta/commons/sandbox/vfs/trunk/xdocs/api.xml Wed Mar 30 15:11:40 2005
@@ -179,10 +179,21 @@
                 location of the configuration file to use.  The configuration
                 file format is described below.
             </p>
+
+            <p>
+                The fourth method is in addition to the default providers.xml.
+                This method automatically scans the classpath for a /META-INF/vfs-providers.xml
file (even if it is within an jar).
+                If such a file is found Commons VFS uses it in addition to the default.
+                This allows you to start using a new filesystem by simply drop its implementation
into the classpath.
+                The configuration file format is described below.<br />
+                <b>Notice:</b> Currently it is not allowed to override a already
configured filesystem. Commons VFS throws
+                an exception if there is already a filesystem for a scheme. 
+            </p>
+
             <p>
                 <code>StandardFileSystemManager</code> is a subclass of
                 <code>DefaultFileSystemManager</code>, so you can also
-                also configure it programmatically, as described above.
+                configure it programmatically, as described above.
             </p>
             <subsection name="Configuration File">
                 <p>

Modified: jakarta/commons/sandbox/vfs/trunk/xdocs/index.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/xdocs/index.xml?view=diff&r1=159535&r2=159536
==============================================================================
--- jakarta/commons/sandbox/vfs/trunk/xdocs/index.xml (original)
+++ jakarta/commons/sandbox/vfs/trunk/xdocs/index.xml Wed Mar 30 15:11:40 2005
@@ -47,6 +47,8 @@
         </section>
 
         <section name="News">
+            <p>2005-03 - imario@apache.org: easier configuration for plugins</p>
+
             <p>2005-03 - imario@apache.org: The filename parsing and api has changed.
             <ul>
                 <li>Now it is needet to encode the '%' sign using '%25'</li>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message