You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by oz...@apache.org on 2004/05/04 16:23:51 UTC

cvs commit: jakarta-slide/src/stores/org/apache/slide/store/txfile/rm/impl FileResourceManager.java

ozeigermann    2004/05/04 07:23:51

  Modified:    src/stores/org/apache/slide/store/util FileHelper.java
               src/stores/org/apache/slide/store/txfile/rm/impl
                        FileResourceManager.java
  Log:
  Refactoring: Moved helpers to FileHelper to use it from over classes.
  
  Revision  Changes    Path
  1.3       +45 -3     jakarta-slide/src/stores/org/apache/slide/store/util/FileHelper.java
  
  Index: FileHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/util/FileHelper.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FileHelper.java	11 Feb 2004 11:30:22 -0000	1.2
  +++ FileHelper.java	4 May 2004 14:23:50 -0000	1.3
  @@ -42,6 +42,48 @@
       private static byte[] BUF = new byte[BUF_SIZE];
   
       /**
  +     * Deletes a file specified by a path.
  +     *  
  +     * @param path path of file to be deleted
  +     * @return <code>true</code> if file has been deleted, <code>false</code> otherwise
  +     */
  +    public static boolean deleteFile(String path) {
  +        File file = new File(path);
  +        return file.delete();
  +    }
  +
  +    /**
  +     * Checks if a file specified by a path exits.
  +     *  
  +     * @param path path of file to be checked
  +     * @return <code>true</code> if file exists, <code>false</code> otherwise
  +     */
  +    public static boolean fileExists(String path) {
  +        File file = new File(path);
  +        return file.exists();
  +    }
  +
  +    /**
  +     * Creates a file specified by a path. All necessary directories will be created.
  +     * 
  +     * @param path path of file to be created
  +     * @return <code>true</code> if file has been created, <code>false</code> if the file already exists
  +     * @throws  IOException
  +     *          If an I/O error occurred
  +     */
  +    public static boolean createFile(String path) throws IOException {
  +        File file = new File(path);
  +        if (file.isDirectory()) {
  +            return file.mkdirs();
  +        } else {
  +            File dir = file.getParentFile();
  +            // do not check if this worked, as it may also return false, when all neccessary dirs are present
  +            dir.mkdirs();
  +            return file.createNewFile();
  +        }
  +    }
  +
  +    /**
        * Removes a file. If the specified file is a directory all contained files will
        * be removed recursively as well. 
        * 
  
  
  
  1.9       +43 -77    jakarta-slide/src/stores/org/apache/slide/store/txfile/rm/impl/FileResourceManager.java
  
  Index: FileResourceManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/txfile/rm/impl/FileResourceManager.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- FileResourceManager.java	19 Feb 2004 17:05:39 -0000	1.8
  +++ FileResourceManager.java	4 May 2004 14:23:50 -0000	1.9
  @@ -122,8 +122,6 @@
    */
   public class FileResourceManager implements StreamableResourceManager, ResourceManagerErrorCodes {
   
  -    private static final String LOG_CHANNEL = FileResourceManager.class.getName();
  -
       // reflects the natural isolation level of this store
       protected static final int NATIVE_ISOLATION_LEVEL = ISOLATION_LEVEL_REPEATABLE_READ;
       protected static final int DEFAULT_ISOLATION_LEVEL = NATIVE_ISOLATION_LEVEL;
  @@ -156,28 +154,6 @@
        *  
        */
   
  -    protected static boolean deleteFile(String path) {
  -        File file = new File(path);
  -        return file.delete();
  -    }
  -
  -    protected static boolean createFile(String path) throws IOException {
  -        File file = new File(path);
  -        if (file.isDirectory()) {
  -            return file.mkdirs();
  -        } else {
  -            File dir = file.getParentFile();
  -            // do not check if this worked, as it may also return false, when all neccessary dirs are present
  -            dir.mkdirs();
  -            return file.createNewFile();
  -        }
  -    }
  -
  -    protected static boolean fileExists(String path) throws IOException {
  -        File file = new File(path);
  -        return file.exists();
  -    }
  -
       protected static void applyDeletes(File removeDir, File targetDir) throws IOException {
           if (removeDir.isDirectory() && targetDir.isDirectory()) {
               File[] files = removeDir.listFiles();
  @@ -697,8 +673,8 @@
   
               // if there still is a file in main store, we need to schedule
               // a delete additionally
  -            if (fileExists(mainPath)) {
  -                createFile(txDeletePath);
  +            if (FileHelper.fileExists(mainPath)) {
  +                FileHelper.createFile(txDeletePath);
               }
           } catch (IOException e) {
               throw new ResourceManagerSystemException(
  @@ -734,7 +710,7 @@
   
               // creation means either undoing a delete or actually scheduling a create
               if (!undoScheduledDelete(txId, resourceId)) {
  -                createFile(txChangePath);
  +                FileHelper.createFile(txChangePath);
               }
   
           } catch (IOException e) {
  @@ -1004,8 +980,8 @@
           try {
               // when we want to write, be sure to write to a local copy
               String txChangePath = getChangePath(txId, resourceId);
  -            if (!fileExists(txChangePath)) {
  -                createFile(txChangePath);
  +            if (!FileHelper.fileExists(txChangePath)) {
  +                FileHelper.createFile(txChangePath);
               }
               return txChangePath;
           } catch (IOException e) {
  @@ -1023,54 +999,44 @@
           String txChangePath = getChangePath(txId, resourceId);
           String txDeletePath = getDeletePath(txId, resourceId);
   
  -        try {
  -
  -            // now, this is gets bit complicated:
  -
  -            boolean changeExists = fileExists(txChangePath);
  -            boolean deleteExists = fileExists(txDeletePath);
  -            boolean mainExists = fileExists(mainPath);
  -            boolean resourceIsDir =
  -                ((mainExists && new File(mainPath).isDirectory())
  -                    || (changeExists && new File(txChangePath).isDirectory()));
  -            if (resourceIsDir) {
  -                logger.logWarning("Resource at '" + resourceId + "' maps to directory");
  -            }
  -
  -            // first do some sane checks
  -
  -            // this may never be, two cases are possible, both disallowing to have a delete together with a change
  -            // 1. first there was a change, than a delete -> at least delete file exists (when there is a file in main store)
  -            // 2. first there was a delete, than a change -> only change file exists
  -            if (!resourceIsDir && changeExists && deleteExists) {
  -                throw new ResourceManagerSystemException(
  -                    "Inconsistent delete and change combination for resource at '" + resourceId + "'",
  -                    ERR_TX_INCONSISTENT,
  -                    txId);
  -            }
  +        // now, this is gets bit complicated:
   
  -            // you should not have been allowed to delete a file that does not exist at all
  -            if (deleteExists && !mainExists) {
  -                throw new ResourceManagerSystemException(
  -                    "Inconsistent delete for resource at '" + resourceId + "'",
  -                    ERR_TX_INCONSISTENT,
  -                    txId);
  -            }
  -
  -            if (changeExists) {
  -                return txChangePath;
  -            } else if ((mainExists && !deleteExists)) {
  -                return mainPath;
  -            } else {
  -                return null;
  -            }
  +        boolean changeExists = FileHelper.fileExists(txChangePath);
  +        boolean deleteExists = FileHelper.fileExists(txDeletePath);
  +        boolean mainExists = FileHelper.fileExists(mainPath);
  +        boolean resourceIsDir =
  +            ((mainExists && new File(mainPath).isDirectory())
  +                || (changeExists && new File(txChangePath).isDirectory()));
  +        if (resourceIsDir) {
  +            logger.logWarning("Resource at '" + resourceId + "' maps to directory");
  +        }
  +
  +        // first do some sane checks
  +
  +        // this may never be, two cases are possible, both disallowing to have a delete together with a change
  +        // 1. first there was a change, than a delete -> at least delete file exists (when there is a file in main store)
  +        // 2. first there was a delete, than a change -> only change file exists
  +        if (!resourceIsDir && changeExists && deleteExists) {
  +            throw new ResourceManagerSystemException(
  +                "Inconsistent delete and change combination for resource at '" + resourceId + "'",
  +                ERR_TX_INCONSISTENT,
  +                txId);
  +        }
   
  -        } catch (IOException e) {
  +        // you should not have been allowed to delete a file that does not exist at all
  +        if (deleteExists && !mainExists) {
               throw new ResourceManagerSystemException(
  -                "Can not check for resource at '" + resourceId + "'",
  -                ERR_SYSTEM,
  -                txId,
  -                e);
  +                "Inconsistent delete for resource at '" + resourceId + "'",
  +                ERR_TX_INCONSISTENT,
  +                txId);
  +        }
  +
  +        if (changeExists) {
  +            return txChangePath;
  +        } else if ((mainExists && !deleteExists)) {
  +            return mainPath;
  +        } else {
  +            return null;
           }
       }
   
  
  
  

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