You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by im...@apache.org on 2006/07/12 08:56:56 UTC

svn commit: r421169 - in /jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs: ./ impl/ provider/

Author: imario
Date: Tue Jul 11 23:56:56 2006
New Revision: 421169

URL: http://svn.apache.org/viewvc?rev=421169&view=rev
Log:
added a way to decorate all fileObjects. Implement stuff to decorate a fileObject with a SynchronizedFileObject guy which might be helpful in multi-threaded environments

Added:
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java   (with props)
Modified:
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/FileSystemManager.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/Resources.properties
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemManager.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileSystem.java

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/FileSystemManager.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/FileSystemManager.java?rev=421169&r1=421168&r2=421169&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/FileSystemManager.java (original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/FileSystemManager.java Tue Jul 11 23:56:56 2006
@@ -1,12 +1,12 @@
 /*
  * Copyright 2002-2005 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.
@@ -20,6 +20,7 @@
 import java.io.File;
 import java.net.URLStreamHandlerFactory;
 import java.util.Collection;
+import java.lang.reflect.Constructor;
 
 import org.apache.commons.vfs.operations.FileOperationProvider;
 
@@ -223,13 +224,24 @@
      * Get the cache used to cache fileobjects.
      */
 	public FilesCache getFilesCache();
-    
+
     /**
      * Get the cache strategy used
      */
     public CacheStrategy getCacheStrategy();
 
     /**
+     * Get the file object decorator used
+     */
+    public Class getFileObjectDecorator();
+
+    /**
+     * The constructor associated to the fileObjectDecorator.
+     * We cache it here for performance reasons.
+     */
+    public Constructor getFileObjectDecoratorConst();
+    
+    /**
      * The class to use to determine the content-type (mime-type)
      */
     public FileContentInfoFactory getFileContentInfoFactory();
@@ -296,5 +308,5 @@
      *
      * @throws FileSystemException
      */
-    public FileOperationProvider[] getOperationProviders(final String scheme) throws FileSystemException; 
+    public FileOperationProvider[] getOperationProviders(final String scheme) throws FileSystemException;
 }

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/Resources.properties
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/Resources.properties?rev=421169&r1=421168&r2=421169&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/Resources.properties (original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/Resources.properties Tue Jul 11 23:56:56 2006
@@ -131,6 +131,7 @@
 vfs.impl/delete-temp.warn=Could not clean up temporary file "{0}".
 vfs.impl/init-replicator.error=Could not initialise file replicator.
 vfs.impl/already-inited.error=Manager already inited, cant change the configuration now.
+vfs.impl/invalid-decorator.error="{0}" is not a valid decorator. It has to extend "DecoratedFileObject" and must provide a single argument constructor which takes a "FileObject"
 
 # StandardFileSystemManager
 vfs.impl/find-config-file.error=Could not find VFS configuration resource "{0}".

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemManager.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemManager.java?rev=421169&r1=421168&r2=421169&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemManager.java (original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemManager.java Tue Jul 11 23:56:56 2006
@@ -24,6 +24,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.lang.reflect.Constructor;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.vfs.CacheStrategy;
@@ -53,7 +54,7 @@
 
 /**
  * A default file system manager implementation.
- * 
+ *
  * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  * @version $Revision$ $Date: 2006-03-30 21:16:24 +0200 (Do, 30 Mrz
  *          2006) $
@@ -90,7 +91,7 @@
 	 */
 	private FileObject baseFile;
 
-	/**
+    /**
 	 * The files cache
 	 */
 	private FilesCache filesCache;
@@ -100,7 +101,13 @@
 	 */
 	private CacheStrategy fileCacheStrategy;
 
-	/**
+    /**
+     * Class which decorates all returned fileObjects
+     */
+    private Class fileObjectDecorator;
+    private Constructor fileObjectDecoratorConst;
+
+    /**
 	 * The class to use to determine the content-type (mime-type)
 	 */
 	private FileContentInfoFactory fileContentInfoFactory;
@@ -132,7 +139,7 @@
 	/**
 	 * Registers a file system provider. The manager takes care of all lifecycle
 	 * management. A provider may be registered multiple times.
-	 * 
+	 *
 	 * @param urlScheme
 	 *            The scheme the provider will handle.
 	 * @param provider
@@ -148,7 +155,7 @@
 	/**
 	 * Registers a file system provider. The manager takes care of all lifecycle
 	 * management. A provider may be registered multiple times.
-	 * 
+	 *
 	 * @param urlSchemes
 	 *            The schemes the provider will handle.
 	 * @param provider
@@ -194,7 +201,7 @@
 
 	/**
 	 * Adds an filename extension mapping.
-	 * 
+	 *
 	 * @param extension
 	 *            The file name extension.
 	 * @param scheme
@@ -207,7 +214,7 @@
 
 	/**
 	 * Adds a mime type mapping.
-	 * 
+	 *
 	 * @param mimeType
 	 *            The mime type.
 	 * @param scheme
@@ -259,7 +266,7 @@
 	 * <p>
 	 * The default is {@link CacheStrategy#ON_RESOLVE}
 	 * </p>
-	 * 
+	 *
 	 * @throws FileSystemException
 	 *             if this is not possible. e.g. it is already set.
 	 */
@@ -282,7 +289,53 @@
 		return fileCacheStrategy;
 	}
 
-	/**
+    /**
+     * Get the file object decorator used
+     */
+    public Class getFileObjectDecorator()
+    {
+        return fileObjectDecorator;
+    }
+
+    /**
+     * The constructor associated to the fileObjectDecorator.
+     * We cache it here for performance reasons.
+     */
+    public Constructor getFileObjectDecoratorConst()
+    {
+        return fileObjectDecoratorConst;
+    }
+
+    /**
+     * set a fileObject decorator to be used for ALL returned file objects
+     *
+     * @param fileObjectDecorator must be inherted from {@link DecoratedFileObject} a has to provide a
+     * constructor with a single {@link FileObject} as argument
+     */
+    public void setFileObjectDecorator(Class fileObjectDecorator) throws FileSystemException
+    {
+        if (init)
+        {
+            throw new FileSystemException("vfs.impl/already-inited.error");
+        }
+        if (!DecoratedFileObject.class.isAssignableFrom(fileObjectDecorator))
+        {
+            throw new FileSystemException("vfs.impl/invalid-decorator.error", fileObjectDecorator.getName());
+        }
+
+        try
+        {
+            fileObjectDecoratorConst = fileObjectDecorator.getConstructor(new Class[]{FileObject.class});
+        }
+        catch (NoSuchMethodException e)
+        {
+            throw new FileSystemException("vfs.impl/invalid-decorator.error", fileObjectDecorator.getName(), e);
+        }
+
+        this.fileObjectDecorator = fileObjectDecorator;
+    }
+
+    /**
 	 * get the fileContentInfoFactory used to determine the infos of a file
 	 * content.
 	 */
@@ -374,7 +427,7 @@
 
 	/**
 	 * Returns the file replicator.
-	 * 
+	 *
 	 * @return The file replicator. Never returns null.
 	 */
 	public FileReplicator getReplicator() throws FileSystemException
@@ -388,7 +441,7 @@
 
 	/**
 	 * Returns the temporary file store.
-	 * 
+	 *
 	 * @return The file store. Never returns null.
 	 */
 	public TemporaryFileStore getTemporaryFileStore()
@@ -632,7 +685,7 @@
 
 	/**
 	 * Resolves a name, relative to the root.
-	 * 
+	 *
 	 * @param base
 	 *            the base filename
 	 * @param name
@@ -715,7 +768,7 @@
 
 	/**
 	 * resolve the uri to a filename
-	 * 
+	 *
 	 * @throws FileSystemException
 	 */
 	public FileName resolveURI(String uri) throws FileSystemException
@@ -812,7 +865,7 @@
 
 	/**
 	 * Determines if a layered file system can be created for a given file.
-	 * 
+	 *
 	 * @param file
 	 *            The file to check for.
 	 */
@@ -895,14 +948,14 @@
 	 */
 	public String[] getSchemes()
 	{
-		String schemes[] = new String[providers.size()];
+		String[] schemes = new String[providers.size()];
 		providers.keySet().toArray(schemes);
 		return schemes;
 	}
 
 	/**
 	 * Get the capabilities for a given scheme.
-	 * 
+	 *
 	 * @throws FileSystemException
 	 *             if the given scheme is not konwn
 	 */
@@ -922,7 +975,7 @@
 
 	/**
 	 * Get the configuration builder for the given scheme
-	 * 
+	 *
 	 * @throws FileSystemException
 	 *             if the given scheme is not konwn
 	 */
@@ -949,7 +1002,7 @@
 	 * Several FileOperationProvider's might be registered for the same scheme.
 	 * For example, for "file" scheme we can register SvnWsOperationProvider and
 	 * CvsOperationProvider.
-	 * 
+	 *
 	 * @param scheme
 	 * @param operationProvider
 	 * @throws FileSystemException
@@ -965,7 +1018,7 @@
 	/**
 	 * @see FileSystemManager#addOperationProvider(String,
 	 *      org.apache.commons.vfs.operations.FileOperationProvider)
-	 * 
+	 *
 	 * @param schemes
 	 * @param operationProvider
 	 * @throws FileSystemException
@@ -1003,11 +1056,11 @@
 	 * @param scheme
 	 *            the scheme for wich we want to get the list af registered
 	 *            providers.
-	 * 
+	 *
 	 * @return the registered FileOperationProviders for the specified scheme.
 	 *         If there were no providers registered for the scheme, it returns
 	 *         null.
-	 * 
+	 *
 	 * @throws FileSystemException
 	 */
 	public FileOperationProvider[] getOperationProviders(final String scheme)

Added: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java?rev=421169&view=auto
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java (added)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java Tue Jul 11 23:56:56 2006
@@ -0,0 +1,176 @@
+package org.apache.commons.vfs.impl;
+
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSelector;
+import org.apache.commons.vfs.FileContent;
+import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.NameScope;
+
+import java.util.List;
+
+/**
+ * This decorator synchronize all access to the FileObject
+ *
+ * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
+ * @version $Revision$ $Date$
+ */
+public class SynchronizedFileObject extends DecoratedFileObject
+{
+	public SynchronizedFileObject(FileObject fileObject)
+	{
+		super(fileObject);
+	}
+
+	public void close() throws FileSystemException
+    {
+        synchronized(this)
+        {
+		    super.close();
+        }
+    }
+
+	public void copyFrom(FileObject srcFile, FileSelector selector) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		super.copyFrom(srcFile, selector);
+        }
+    }
+
+	public void createFile() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		super.createFile();
+        }
+    }
+
+	public void createFolder() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		super.createFolder();
+        }
+    }
+
+	public boolean delete() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.delete();
+        }
+    }
+
+	public int delete(FileSelector selector) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.delete(selector);
+        }
+    }
+
+	public boolean exists() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.exists();
+        }
+    }
+
+	public void findFiles(FileSelector selector, boolean depthwise, List selected) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		super.findFiles(selector, depthwise, selected);
+        }
+    }
+
+	public FileObject[] findFiles(FileSelector selector) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.findFiles(selector);
+        }
+    }
+
+	public FileObject getChild(String name) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.getChild(name);
+        }
+    }
+
+	public FileObject[] getChildren() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.getChildren();
+        }
+    }
+
+	public FileContent getContent() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.getContent();
+        }
+    }
+
+	public FileType getType() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.getType();
+        }
+    }
+
+	public boolean isHidden() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.isHidden();
+        }
+    }
+
+	public boolean isReadable() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.isReadable();
+        }
+    }
+
+	public boolean isWriteable() throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.isWriteable();
+        }
+    }
+
+	public void moveTo(FileObject destFile) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		super.moveTo(destFile);
+        }
+    }
+
+	public FileObject resolveFile(String name, NameScope scope) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.resolveFile(name, scope);
+        }
+    }
+
+	public FileObject resolveFile(String path) throws FileSystemException
+	{
+        synchronized(this)
+        {
+    		return super.resolveFile(path);
+        }
+    }
+}

Propchange: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/SynchronizedFileObject.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java?rev=421169&r1=421168&r2=421169&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java (original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java Tue Jul 11 23:56:56 2006
@@ -79,7 +79,7 @@
     // private FileObject[] children;
     private FileName[] children;
     private List objects;
-    
+
 	/**
 	 * FileServices instance.
 	 */
@@ -397,8 +397,7 @@
      */
     public boolean exists() throws FileSystemException
     {
-        attach();
-        return (type != FileType.IMAGINARY);
+        return (getType() != FileType.IMAGINARY);
     }
 
     /**
@@ -406,8 +405,11 @@
      */
     public FileType getType() throws FileSystemException
     {
-        attach();
-        return type;
+        synchronized (fs)
+        {
+            attach();
+            return type;
+        }
     }
 
     /**
@@ -417,7 +419,6 @@
     {
         try
         {
-            attach();
             if (exists())
             {
                 return doIsHidden();
@@ -440,7 +441,6 @@
     {
         try
         {
-            attach();
             if (exists())
             {
                 return doIsReadable();
@@ -463,7 +463,6 @@
     {
         try
         {
-            attach();
             if (exists())
             {
                 return doIsWriteable();
@@ -521,8 +520,7 @@
     {
         synchronized (fs)
         {
-            attach();
-            if (!type.hasChildren())
+            if (!getType().hasChildren())
             {
                 throw new FileSystemException("vfs.provider/list-children-not-folder.error", name);
             }
@@ -845,8 +843,6 @@
     public void copyFrom(final FileObject file, final FileSelector selector)
         throws FileSystemException
     {
-    	attach();
-    	
         if (!file.exists())
         {
             throw new FileSystemException("vfs.provider/copy-missing-file.error", file);
@@ -903,8 +899,6 @@
      */
     public void moveTo(FileObject destFile) throws FileSystemException
     {
-    	attach();
-    	
         if (canRenameTo(destFile))
         {
 	        if (!getParent().isWriteable())
@@ -1009,16 +1003,19 @@
      */
     public FileContent getContent() throws FileSystemException
     {
-        attach();
-        if (content == null)
+        synchronized(fs)
         {
-            content = new DefaultFileContent(this, getFileContentInfoFactory());
+            attach();
+            if (content == null)
+            {
+                content = new DefaultFileContent(this, getFileContentInfoFactory());
+            }
+            return content;
         }
-        return content;
     }
 
     /**
-     * This will prepare the fileObject to get resynchronized with the underlaying filesystem if required 
+     * This will prepare the fileObject to get resynchronized with the underlaying filesystem if required
      */
     public void refresh() throws FileSystemException
     {
@@ -1032,7 +1029,7 @@
             throw new FileSystemException("vfs.provider/resync.error", name, e);
         }
     }
-    
+
     /**
      * Closes this file, and its content.
      */
@@ -1074,8 +1071,7 @@
      */
     public InputStream getInputStream() throws FileSystemException
     {
-        attach();
-        if (!type.hasContent())
+        if (!getType().hasContent())
         {
             throw new FileSystemException("vfs.provider/read-not-file.error", name);
         }
@@ -1101,8 +1097,7 @@
      */
     public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException
     {
-        attach();
-        if (!type.hasContent())
+        if (!getType().hasContent())
         {
             throw new FileSystemException("vfs.provider/read-not-file.error", name);
         }

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileSystem.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileSystem.java?rev=421169&r1=421168&r2=421169&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileSystem.java (original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractFileSystem.java Tue Jul 11 23:56:56 2006
@@ -42,6 +42,7 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
+import java.lang.reflect.InvocationTargetException;
 
 /**
  * A partial {@link org.apache.commons.vfs.FileSystem} implementation.
@@ -297,7 +298,7 @@
             {
                 throw new FileSystemException("vfs.provider/resolve-file.error", name, e);
             }
-            
+
             file = decorateFileObject(file);
 
             // imario@apache.org ==> use putFileToCache
@@ -318,13 +319,33 @@
         return file;
     }
 
-    protected FileObject decorateFileObject(FileObject file)
+    protected FileObject decorateFileObject(FileObject file)  throws FileSystemException
 	{
         if (getFileSystemManager().getCacheStrategy().equals(CacheStrategy.ON_CALL))
         {
-        	return new OnCallRefreshFileObject(file);
+        	file = new OnCallRefreshFileObject(file);
+        }
+
+        if (getFileSystemManager().getFileObjectDecoratorConst() != null)
+        {
+            try
+            {
+                file = (FileObject) getFileSystemManager().getFileObjectDecoratorConst().newInstance(new Object[]{file});
+            }
+            catch (InstantiationException e)
+            {
+                throw new FileSystemException("vfs.impl/invalid-decorator.error", getFileSystemManager().getFileObjectDecorator().getName(), e);
+            }
+            catch (IllegalAccessException e)
+            {
+                throw new FileSystemException("vfs.impl/invalid-decorator.error", getFileSystemManager().getFileObjectDecorator().getName(), e);
+            }
+            catch (InvocationTargetException e)
+            {
+                throw new FileSystemException("vfs.impl/invalid-decorator.error", getFileSystemManager().getFileObjectDecorator().getName(), e);
+            }
         }
-        
+
         return file;
 	}
 



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