[Yanel-dev] Patch for splited filepath

Balz Schreier balz.schreier at gmail.com
Thu Oct 28 21:38:11 CEST 2010


Skipped content of type multipart/alternative-------------- next part -----=
---------
Index: src/impl/java/org/wyona/yarep/impl/VFileSystemMapImpl.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- src/impl/java/org/wyona/yarep/impl/VFileSystemMapImpl.java	(revision 54=
229)
+++ src/impl/java/org/wyona/yarep/impl/VFileSystemMapImpl.java	(working cop=
y)
@@ -1,23 +1,22 @@
 package org.wyona.yarep.impl;
 =

+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.apache.avalon.framework.configuration.Configuration;
-
+import org.apache.log4j.Category;
 import org.wyona.commons.io.FileUtil;
 import org.wyona.yarep.core.Map;
 import org.wyona.yarep.core.Path;
 import org.wyona.yarep.core.RepositoryException;
 import org.wyona.yarep.core.UID;
+import org.wyona.yarep.core.impl.vfs.SplitPathConfig;
 =

-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.BufferedReader;
-import java.io.FilenameFilter;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.log4j.Category;
-
 /**
  *
  */
@@ -29,7 +28,10 @@
     protected Pattern[] ignorePatterns;
     protected ChildrenFilter childrenFilter =3D new ChildrenFilter();
     =

+    // Configuration parameters of the <splitpath ...> element
+    private SplitPathConfig splitPathConfig =3D new SplitPathConfig();
 =

+
     /**
      *
      */
@@ -41,6 +43,26 @@
                 Configuration[] ignoreElements =3D mapConfig.getChildren("=
ignore");
                 setIgnorePatterns(ignoreElements);
             }
+            // Read the <splitpath> configuration
+            log.debug("Reading Split Path Configuation");
+            Configuration splitConfig =3D mapConfig.getChild("splitpath", =
false);
+            if (splitConfig !=3D null) {
+                splitPathConfig.setSplitparts(Integer.parseInt(splitConfig=
.getAttribute("depth", "0")));
+                splitPathConfig.setSplitlength(Integer.parseInt(splitConfi=
g.getAttribute("length", "0")));
+                splitPathConfig.setSeparator(splitConfig.getAttribute("esc=
ape", "-"));
+
+                int numberOfIncludePaths =3D splitConfig.getChildren("incl=
ude").length;
+                int i =3D 0;
+                if (numberOfIncludePaths > 0) {
+                    String[] includepaths =3D new String[numberOfIncludePa=
ths];
+                    for (Configuration include : splitConfig.getChildren("=
include")) {
+                        includepaths[i++] =3D include.getAttribute("path");
+                    }
+                    splitPathConfig.setIncludepaths(includepaths);
+                }
+                log.debug("Split Path Configuration DONE");
+            } =

+            =

         } catch(Exception e) {
             log.error(e);
             throw new RepositoryException("Could not read map configuratio=
n: " =

@@ -89,7 +111,11 @@
      *
      */
     public boolean isResource(Path path) throws RepositoryException {
-        File file =3D new File(pathsDir + path.toString());
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(pa=
th.toString(), this.splitPathConfig);
+        File file =3D new File(pathsDir + maybeSplitedPath);
+        if (!file.exists()) {
+            file =3D new File(pathsDir + path.toString());
+        }
         return file.isFile();
     }
 =

@@ -97,14 +123,18 @@
      *
      */
     public boolean exists(Path path) throws RepositoryException {
-        File file =3D new File(pathsDir + path.toString());
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(pa=
th.toString(), this.splitPathConfig);
+        File file =3D new File(pathsDir + maybeSplitedPath);
+        if (!file.exists()) {
+            file =3D new File(pathsDir + path.toString());
+        }
         // TODO: Get name of repository for debugging ...
         //log.debug("File: " + file);
         return file.exists() && !ignorePath(file.getPath());
     }
 =

     /**
-     *
+     * Calling this method has no effect anymore because delete is done in=
 the storage impl!
      */
     public boolean delete(Path path) throws RepositoryException {
         // don't do anything because if we delete the file here, the delete
@@ -118,41 +148,75 @@
      *
      */
     public boolean isCollection(Path path) throws RepositoryException {
-        File file =3D new File(pathsDir + path.toString());
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(pa=
th.toString(), this.splitPathConfig);
+        File file =3D new File(pathsDir + maybeSplitedPath);
+        if (!file.exists()) {
+            file =3D new File(pathsDir + path.toString());
+        }
         return file.isDirectory();
     }
 =

     /**
-     * Get children
+     * Get children, the path of the children includes the path of the par=
ent!
      */
     public Path[] getChildren(Path path) throws RepositoryException {
-        File file =3D new File(pathsDir + path.toString());
-        if (!file.exists()) {
-            log.warn("No such file or directory: " + file);
+        // Note: path is always NOT splited, because the caller of this me=
thod does not anything about it
+        File startingDirectory =3D new File(pathsDir + path.toString());
+        if (!startingDirectory.exists()) {
+            log.warn("No such file or directory: " + startingDirectory);
             return new Path[0];
         }
-
-        String[] filenames =3D file.list(this.childrenFilter);
-
-	// NOTE: This situation should only occur if one is trying to get childre=
n for a file than a directory! One might want to consider to test first wit=
h isResource() or isCollection().
-        if (filenames =3D=3D null) {
-            log.warn("No children: " + path + " (" + file + ")");
+        if (startingDirectory.isFile()) {
+            log.warn("Can not get children form a file! : " + startingDire=
ctory);
             return new Path[0];
         }
-
-        log.debug("Number of children: " + filenames.length + " (" + file =
+ ")");
-        Path[] children =3D new Path[filenames.length];
-        for (int i =3D 0;i < children.length; i++) {
-            if (path.toString().endsWith(File.separator)) {
-                children[i] =3D new Path(path + filenames[i]);
+        =

+        List<File> allChildren =3D getAllFiles(startingDirectory);
+        log.debug("Number of children: " + allChildren.size() + " (" + sta=
rtingDirectory + ")");
+        Path[] children =3D new Path[allChildren.size()];
+        String fileSepForRegEx =3D File.separator;
+        if (fileSepForRegEx.equals("\\")) {
+            fileSepForRegEx =3D "\\\\"; // this is a double backslash, use=
d for the regex later
+        }
+        =

+        int i =3D 0;
+        for (File child: allChildren) {
+            String unsplitPath =3D child.getAbsolutePath().replaceAll(file=
SepForRegEx, "/"); // whatever the file separator was, yarep uses "/"
+            log.debug("1. unsplitPath =3D "+unsplitPath);
+            if (unsplitPath.startsWith(startingDirectory.getAbsolutePath()=
)) {
+                unsplitPath =3D unsplitPath.substring(startingDirectory.ge=
tAbsolutePath().length());
+                log.debug("2. unsplitPath =3D "+unsplitPath);
+                unsplitPath =3D unsplitPath.replaceAll("/", "");
+                log.debug("3. unsplitPath =3D "+unsplitPath);
+                if (path.toString().endsWith(File.separator)) {
+                    children[i++] =3D new Path(path + unsplitPath);
+                } else {
+                    // NOTE: Do not use File.separator here, because it's =
the repository path and not the Operating System's File System path
+                    children[i++] =3D new Path(path + "/" + unsplitPath);
+                }
             } else {
-                // NOTE: Do not use File.separator here, because it's the =
repository path and not the Operating System File System path
-                children[i] =3D new Path(path + "/" + filenames[i]);
+                log.error("Something is wrong: children are not within par=
ents!");
             }
-            log.debug("Child: " + children[i]);
         }
+
         return children;
     }
+    =

+    private List<File> getAllFiles(File dir) {
+        List<File> result =3D new ArrayList<File>();
+        if (dir.isDirectory()) {
+            File[] filesAndDirsArray =3D dir.listFiles();
+            List<File> filesAndDirs =3D Arrays.asList(filesAndDirsArray);
+            for (File file : filesAndDirs) {
+              result.add(file);
+              if (!file.isFile()) {
+                List<File> deeperList =3D getAllFiles(file);
+                result.addAll(deeperList);
+              }
+            }
+        }
+        return result;
+    }
 =

     /**
      * Get UID
@@ -163,11 +227,11 @@
     }
 =

     /**
-     * Create UID
+     * Create UID:
      */
     public synchronized UID create(Path path, int type) throws RepositoryE=
xception {
         // TODO: Check if leading slash should be removed ...
-        File parent =3D new File(pathsDir + File.separator + path.getParen=
t().toString());
+        File parent =3D new File(pathsDir + File.separator + path.getParen=
t().toString()); // e.g. access-control/users
         if (!parent.exists()) {
             log.warn("Directory will be created: " + parent);
             parent.mkdirs();
@@ -176,9 +240,19 @@
             new File(parent, path.getName()).mkdir();
         } else {
             try {
-                if(!new File(parent, path.getName()).createNewFile()) log.=
warn("File has not been created: " + new File(parent, path.getName()));
+                String maybeSplitedPath =3D SplitPathConfig.splitPathIfReq=
uired(path.toString(), this.splitPathConfig);
+                String newParent =3D pathsDir.getAbsolutePath();
+                if (maybeSplitedPath.contains("/")) {
+                    newParent =3D newParent + maybeSplitedPath.lastIndexOf=
("/");
+                }
+                File newFilePath =3D new File(newParent , new File(maybeSp=
litedPath).getName());
+                newFilePath.mkdirs();
+                boolean created =3D newFilePath.createNewFile();
+                if (!created)  {
+                    log.warn("Maybe file has not been created: " + new Fil=
e(parent, maybeSplitedPath)); // On Mac OSX 10.6, the file gets created eve=
n in this case
+                }
             } catch (Exception e) {
-                log.error(e.getMessage(), e);
+                log.error("Could not create new file!! Exception: "+e.getM=
essage(), e);
             }
         }
         =

@@ -186,7 +260,7 @@
     }
 =

     /**
-     *
+     * An exception gets thrown if you call this method because symbolic l=
inks are not implemented for virtual file systems!
      */
     public void addSymbolicLink(Path path, UID uid) throws RepositoryExcep=
tion {
         throw new RepositoryException("Symbolic links not implemented for =
virtual file system!");
@@ -199,6 +273,9 @@
         public ChildrenFilter() {
         }
         =

+        /**
+         * @param dir is ignored in this implementation
+         */
         public boolean accept(File dir, String name) {
             =

             if (VFileSystemMapImpl.this.ignorePath(name)) {
Index: src/impl/java/org/wyona/yarep/core/impl/vfs/VFileSystemStorage.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- src/impl/java/org/wyona/yarep/core/impl/vfs/VFileSystemStorage.java	(re=
vision 54229)
+++ src/impl/java/org/wyona/yarep/core/impl/vfs/VFileSystemStorage.java	(wo=
rking copy)
@@ -1,12 +1,5 @@
 package org.wyona.yarep.core.impl.vfs;
 =

-import org.wyona.commons.io.FileUtil;
-import org.wyona.yarep.core.NoSuchNodeException;
-import org.wyona.yarep.core.Path;
-import org.wyona.yarep.core.RepositoryException;
-import org.wyona.yarep.core.Storage;
-import org.wyona.yarep.core.UID;
-
 import java.io.File;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -15,6 +8,12 @@
 =

 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.log4j.Category;
+import org.wyona.commons.io.FileUtil;
+import org.wyona.yarep.core.NoSuchNodeException;
+import org.wyona.yarep.core.Path;
+import org.wyona.yarep.core.RepositoryException;
+import org.wyona.yarep.core.Storage;
+import org.wyona.yarep.core.UID;
 =

 /**
  *
@@ -27,6 +26,12 @@
     private String alternative =3D null;
     private String dirListingMimeType =3D "application/xml";
 =

+    // Configuration parameters of the <splitpath ...> element
+    private SplitPathConfig splitPathConfig =3D new SplitPathConfig();
+    private boolean splitPathEnabled =3D false;
+    =

+    =

+
     /**
      * Read VFS Storage configuration
      */
@@ -50,6 +55,28 @@
                 dirListingMimeType =3D directoryConfig.getAttribute("mime-=
type", "application/xml");
                 log.debug("Mime type of directory listing: " + dirListingM=
imeType);
             }
+
+            // Read the <splitpath> configuration
+            log.debug("Reading Split Path Configuation");
+            Configuration splitConfig =3D storageConfig.getChild("splitpat=
h", false);
+            if (splitConfig !=3D null) {
+                splitPathEnabled =3D true;
+                splitPathConfig.setSplitparts(Integer.parseInt(splitConfig=
.getAttribute("depth", "0")));
+                splitPathConfig.setSplitlength(Integer.parseInt(splitConfi=
g.getAttribute("length", "0")));
+                splitPathConfig.setSeparator(splitConfig.getAttribute("esc=
ape", "-"));
+
+                int numberOfIncludePaths =3D splitConfig.getChildren("incl=
ude").length;
+                int i =3D 0;
+                if (numberOfIncludePaths > 0) {
+                    String[] includepaths =3D new String[numberOfIncludePa=
ths];
+                    for (Configuration include : splitConfig.getChildren("=
include")) {
+                        includepaths[i++] =3D include.getAttribute("path");
+                    }
+                    splitPathConfig.setIncludepaths(includepaths);
+                }
+                log.debug("Split Path Configuration DONE");
+            } =

+            =

         } catch (Exception e) {
             log.error(e);
             throw new RepositoryException(e.getMessage(), e);
@@ -60,57 +87,117 @@
      *
      */
     public Writer getWriter(UID uid, Path path) {
-        return new VFileSystemRepositoryWriter(uid, path, contentDir);
+        // TODO We pass null as uid argument because the class anyway does=
 not need it at this moment. =

+        // If in future this class is going to process the uid argument to=
o, the splitpathConfig object can be passed to it
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(pa=
th.toString(), this.splitPathConfig);
+        Path newPath =3D new Path(maybeSplitedPath);
+        Writer writer =3D null;
+        try {
+            writer =3D new VFileSystemRepositoryWriter(null, newPath, cont=
entDir);
+        } catch (Exception e) {
+        }
+        if (writer =3D=3D null) {
+            writer =3D new VFileSystemRepositoryWriter(null, path, content=
Dir);
+        }
+        return writer;
     }
 =

     /**
-     *
+     * @param uid is ignored in this implementation!
      */
     public OutputStream getOutputStream(UID uid, Path path) throws Reposit=
oryException {
-        return new VFileSystemRepositoryOutputStream(uid, path, contentDir=
);
+        // TODO We pass null as uid argument because the class anyway does=
 not need it at this moment. =

+        // If in future this class is going to process the uid argument to=
o, the splitpathConfig object can be passed to it
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(pa=
th.toString(), this.splitPathConfig);
+        Path newPath =3D new Path(maybeSplitedPath);
+        OutputStream out =3D null;
+        try {
+            out =3D new VFileSystemRepositoryOutputStream(null, newPath, c=
ontentDir);
+        } catch (Exception e) {
+        }
+        if (out =3D=3D null) {
+            out =3D new VFileSystemRepositoryOutputStream(null, path, cont=
entDir);
+        }
+        return out;
     }
 =

     /**
      *
      */
     public Reader getReader(UID uid, Path path) throws NoSuchNodeException=
 {
-        return new VFileSystemRepositoryReader(uid, path, contentDir);
+        // TODO We pass null as uid argument because the class anyway does=
 not need it at this moment. =

+        // If in future this class is going to process the uid argument to=
o, the splitpathConfig object can be passed to it
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(pa=
th.toString(), this.splitPathConfig);
+        Path newPath =3D new Path(maybeSplitedPath);
+        Reader reader =3D null;
+        try {
+            reader =3D new VFileSystemRepositoryReader(null, newPath, cont=
entDir);
+        } catch (Exception e) {
+        }
+        if (reader =3D=3D null) {
+            reader =3D new VFileSystemRepositoryReader(null, path, content=
Dir);
+        }
+        return reader;
     }
 =

     /**
-     *
+     * @param uid is ignored in this implementation!
      */
     public InputStream getInputStream(UID uid, Path path) throws Repositor=
yException {
-        return new VFileSystemRepositoryInputStream(uid, path, contentDir,=
 alternative, dirListingMimeType);
+        // TODO: if uid is processed by VFileSystemRepositoryInputStream, =
the splitPathConfig must be passed to it too. =

+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(pa=
th.toString(), this.splitPathConfig);
+        Path newPath =3D new Path(maybeSplitedPath);
+        InputStream in =3D null;
+        try {
+            in =3D new VFileSystemRepositoryInputStream(null, newPath, con=
tentDir, alternative, dirListingMimeType);
+        } catch (Exception e) {
+        }
+        if (in =3D=3D null) {
+            in =3D new VFileSystemRepositoryInputStream(null, path, conten=
tDir, alternative, dirListingMimeType);
+        }
+        return in;
     }
 =

     /**
-     *
+     * @param path is currently ignored!!
      */
     public long getLastModified(UID uid, Path path) throws RepositoryExcep=
tion {
-        File file =3D new File(contentDir.getAbsolutePath() + File.separat=
or + uid.toString());
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(ui=
d.toString(), this.splitPathConfig);
+        File file =3D new File(contentDir.getAbsolutePath() + File.separat=
or + maybeSplitedPath);
+        if (!file.exists()) {
+            file =3D new File(contentDir.getAbsolutePath() + File.separato=
r + uid.toString());
+        }
         return file.lastModified(); =

     }
     =

     /**
+     * @param path is currently not used!
      * @return Size of file in bytes
      */
     public long getSize(UID uid, Path path) throws RepositoryException {
-    	File file =3D new File(contentDir.getAbsolutePath() + File.separator =
+ uid.toString());
-    	return file.length(); =

+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(ui=
d.toString(), this.splitPathConfig);
+        File file =3D new File(contentDir.getAbsolutePath() + File.separat=
or + maybeSplitedPath);
+        if (!file.exists()) {
+            file =3D new File(contentDir.getAbsolutePath() + File.separato=
r + uid.toString());
+        }
+        return file.length(); =

     }
 =

     /**
-     *
+     * @param path is currently not used!
      */
     public boolean delete(UID uid, Path path) throws RepositoryException {
-        File file =3D new File(contentDir.getAbsolutePath() + File.separat=
or + uid.toString());
+        String maybeSplitedPath =3D SplitPathConfig.splitPathIfRequired(ui=
d.toString(), this.splitPathConfig);
+        File file =3D new File(contentDir.getAbsolutePath() + File.separat=
or + maybeSplitedPath.toString());
+        if (!file.exists()) {
+            file =3D new File(contentDir.getAbsolutePath() + File.separato=
r + uid.toString());
+        }
         log.debug("Try to delete: " + file);
         return file.delete();
     }
     =

     /**
-     * =

+     * Not implemented at this moment
      */
     public String[] getRevisions(UID uid, Path path) throws RepositoryExce=
ption {
         log.warn("Versioning not implemented yet");
@@ -118,16 +205,32 @@
     }
 =

     /**
-     *
+     * Checks the existence via uid first and then via path parameter
      */
     public boolean exists(UID uid, Path path) {
+        boolean exists =3D false;
         if (uid !=3D null) {
-            return new File(contentDir.getAbsolutePath() + File.separator =
+ uid.toString()).exists();
+            if (splitPathEnabled) {
+                File normalFile =3D new File(contentDir.getAbsolutePath() =
+ File.separator + uid.toString());
+                File splitFile =3D new File(contentDir.getAbsolutePath() +=
 File.separator + SplitPathConfig.splitPathIfRequired(uid.toString(), this.=
splitPathConfig));
+                exists =3D normalFile.exists() || splitFile.exists();
+            } else {
+                exists =3D new File(contentDir.getAbsolutePath() + File.se=
parator + uid.toString()).exists();
+            }
+
         } else if (path !=3D null) {
             log.warn("No UUID specified, hence check path: " + path + " (C=
ontent dir: " + contentDir + ")");
-            return new File(contentDir.getAbsolutePath() + File.separator =
+ path.toString()).exists();
-        } else {
-            return false;
-        }
+            if (splitPathEnabled) {
+                File normalFile =3D new File(contentDir.getAbsolutePath() =
+ File.separator + path.toString());
+                File splitFile =3D new File(contentDir.getAbsolutePath() +=
 File.separator + SplitPathConfig.splitPathIfRequired(path.toString(), this=
.splitPathConfig));
+                exists =3D normalFile.exists() || splitFile.exists();
+            } else {
+                exists =3D new File(contentDir.getAbsolutePath() + File.se=
parator + path.toString()).exists();
+            }
+
+        } =

+        return exists;
     }
+    =

+
 }


More information about the Yanel-development mailing list