ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1477108 - /ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
Date Mon, 29 Apr 2013 14:42:59 GMT
Author: bramk
Date: Mon Apr 29 14:42:58 2013
New Revision: 1477108

URL: http://svn.apache.org/r1477108
Log:
ACE-346 Consider file changes in all sub directories


Modified:
    ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java

Modified: ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java?rev=1477108&r1=1477107&r2=1477108&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
(original)
+++ ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
Mon Apr 29 14:42:58 2013
@@ -26,8 +26,12 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.channels.FileChannel;
+import java.util.ArrayList;
 import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Stack;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.jar.Attributes;
 import java.util.jar.JarInputStream;
@@ -55,28 +59,24 @@ public class BundleFileStore implements 
     private static int BUFFER_SIZE = 8 * 1024;
     private static final String REPOSITORY_XML = "repository.xml";
 
-    private final Map<String, Long> m_foundFiles = new ConcurrentHashMap<String,
Long>();
     private final Object m_dirLock = new Object();
 
     private volatile MetadataGenerator m_metadata; /* will be injected by dependencymanager
*/
     private volatile LogService m_log; /* will be injected by dependencymanager */
 
+    private volatile Map<String, Long> m_foundFiles;;
+
     /** protected by m_dirLock. */
     private File m_dir;
 
     public void generateMetadata() throws IOException {
         File dir = getWorkingDir();
-        File[] files = dir.listFiles();
-
         m_metadata.generateMetadata(dir);
-
-        for (File current : files) {
-            m_foundFiles.put(current.getAbsolutePath(), current.lastModified() ^ current.length());
-        }
+        m_foundFiles = mapDirectory(dir);
     }
 
     public InputStream get(String fileName) throws IOException {
-        if (REPOSITORY_XML.equals(fileName) && directoryChanged(getWorkingDir()))
{
+        if (REPOSITORY_XML.equals(fileName) && directoryChanged(getWorkingDir(),
m_foundFiles)) {
             generateMetadata(); // might be called too often
         }
         FileInputStream result = null;
@@ -153,13 +153,12 @@ public class BundleFileStore implements 
                 synchronized (m_dirLock) {
                     m_dir = newDir;
                 }
-
-                m_foundFiles.clear();
+                m_foundFiles = null;
             }
         }
         else {
             // clean up after getting a null as dictionary, as the service is going to be
pulled afterwards
-            m_foundFiles.clear();
+            m_foundFiles = null;
         }
     }
 
@@ -175,28 +174,47 @@ public class BundleFileStore implements 
         }
     }
 
-    @SuppressWarnings("boxing")
-    private boolean directoryChanged(File dir) {
-        File[] files = dir.listFiles();
+    private Map<String, Long> mapDirectory(File dir) {
+        Map<String, Long> foundFiles = new HashMap<String, Long>();
+        Stack<File> dirs = new Stack<File>();
+        dirs.push(dir);
+        while(!dirs.isEmpty()){
+            File pwd = dirs.pop();
+            for(File file : pwd.listFiles()){
+                if(file.isDirectory()){
+                    dirs.push(file);
+                } else {
+                    foundFiles.put(file.getAbsolutePath(), file.lastModified() ^ file.length());
+                }
+            }
+        }
+        return foundFiles;
+    }
 
-        // if number of files changed, create new metadata
-        if (files.length != m_foundFiles.size()) {
+    private boolean directoryChanged(File dir, Map<String, Long> foundFiles) {
+        if(foundFiles == null){
             return true;
         }
-
-        // iterate over the current files
-        for (File current : files) {
-            Long modifiedDateAndLengthXOR = m_foundFiles.get(current.getAbsolutePath());
-            // if one of the current files is not in the old set of files, create new metadata
-            if (modifiedDateAndLengthXOR == null) {
-                return true;
-            }
-            // else if of one of the files the size or the date has been changed, create
new metadata
-            if ((current.lastModified() ^ current.length()) != modifiedDateAndLengthXOR)
{
-                return true;
+        Stack<File> dirs = new Stack<File>();
+        dirs.push(dir);
+        int fileCount = 0;
+        while(!dirs.isEmpty()){
+            File pwd = dirs.pop();
+            for(File file : pwd.listFiles()){
+                if(file.isDirectory()){
+                    dirs.push(file);
+                } else {
+                    fileCount++;
+                    Long modifiedDateAndLengthXOR = foundFiles.get(file.getAbsolutePath());
+                    if(modifiedDateAndLengthXOR == null || (file.lastModified() ^ file.length())
!= modifiedDateAndLengthXOR){
+                        return true;
+                    }
+                }
             }
         }
-
+        if(fileCount != foundFiles.size()){
+            return true;
+        }
         return false;
     }
 



Mime
View raw message