further fixes to the change reporting framework
authorzapata <zapata>
Sat, 11 Nov 2006 19:21:28 +0000 (19:21 +0000)
committerzapata <zapata>
Sat, 11 Nov 2006 19:21:28 +0000 (19:21 +0000)
source/mir/changetracker/ChangeReporter.java [deleted file]
source/mir/changetracker/ChangeTracker.java
source/mircoders/global/ChangeEngine.java
source/mircoders/producer/ChangeReportingProducerNode.java
source/mircoders/producer/MediaGeneratingProducerNode.java
source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java

diff --git a/source/mir/changetracker/ChangeReporter.java b/source/mir/changetracker/ChangeReporter.java
deleted file mode 100644 (file)
index 401721b..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (C) 2001-2006 The Mir-coders group
- *
- * This file is part of Mir.
- *
- * Mir is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Mir is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Mir; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with  any library licensed under the Apache Software License,
- * and distribute linked combinations including the two.  You must obey the
- * GNU General Public License in all respects for all of the code used other than
- * the above mentioned libraries.  If you modify this file, you may extend this
- * exception to your version of the file, but you are not obligated to do so.
- * If you do not wish to do so, delete this exception statement from your version.
- */package mir.changetracker;
-
-import mir.util.LockFile;
-
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * Class to report changes by writing to a file and using a lockfile.
- *
- * This class is NOT thread-safe, the perform() method should not be called
- * concurrently.
- */
-public class ChangeReporter {
-  public static void reportChanges(
-      ChangeTracker aTracker, String aBasePath,
-      List anExcludedPaths, File aReportFilename,
-      File aReportLockfile, long aMaxTimeOut, boolean aFlush) throws IOException {
-
-    reportChanges(aTracker, aBasePath,
-        (String[]) anExcludedPaths.toArray(new String[0]),
-        aReportFilename, aReportLockfile, aMaxTimeOut, aFlush);
-  }
-
-  /**
-   * @param aBasePath        The base path that should be tracked
-   * @param anExcludedPaths  The paths that are excluded from tracking
-   * @param aTracker         The actual tracker to query for changes
-   * @param aReportFilename  The file to write changed filenames to
-   * @param aReportLockfile  The lockfile for the report file
-   */
-  public static void reportChanges(ChangeTracker aTracker, String aBasePath,
-                                   String[] anExcludedPaths, File aReportFilename, File aReportLockfile,
-                                   long aMaxTimeOut, boolean aFlush) throws IOException {
-    List changesToReport = aTracker.getChanges(aBasePath, anExcludedPaths);
-    LockFile lockFile = null;
-
-    if (aReportLockfile!=null)
-      lockFile = new LockFile(aReportLockfile);
-
-    if (changesToReport.size()>0) {
-      if (lockFile == null || lockFile.lock()) {
-        try {
-          FileWriter writer = new FileWriter(aReportFilename.getAbsolutePath(), true);
-          try {
-            Iterator i = changesToReport.iterator();
-            while (i.hasNext()) {
-              writer.write((String) i.next());
-              writer.write("\n");
-            }
-          }
-          finally {
-            try {
-              writer.close();
-            }
-            catch (Throwable t) {
-            }
-          }
-
-          if (aFlush) {
-            aTracker.removeChanges(changesToReport);
-          }
-        }
-        finally {
-          try {
-            if (lockFile!=null)
-              lockFile.unlock();
-          }
-          catch (Throwable t) {
-          }
-        }
-      }
-    }
-  }
-}
index 3474b26..3df943e 100644 (file)
@@ -33,6 +33,9 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Calendar;
 
 /**
  * Change tracker, tracks changes to a path based repository.
@@ -41,6 +44,11 @@ import java.util.Collection;
 
 public class ChangeTracker {
   private final List changes = new ArrayList();
+  private Calendar calendar = Calendar.getInstance();
+
+  // to prevent memory issues with the number of recorded changes,
+  //   we'll adhere to this maximum
+  private static final int MAX_CHANGES = 10000;
 
   /**
    * Add a single change. A change is represented by the full path
@@ -48,7 +56,7 @@ public class ChangeTracker {
    */
   public void addChange(String aPath, ChangeType aChangeType) {
     synchronized (changes) {
-      changes.add(new Change(aPath, aChangeType));
+      changes.add(new Change(aPath, aChangeType, calendar.getTime()));
     }
   }
 
@@ -76,10 +84,12 @@ public class ChangeTracker {
   public class Change {
     private ChangeType type;
     private String path;
+    private Date date;
 
-    Change(String anAbsolutePath, ChangeType aType) {
+    Change(String anAbsolutePath, ChangeType aType, Date aDate) {
       type = aType;
       path = anAbsolutePath;
+      date = aDate;
     }
 
     public String getPath() {
@@ -90,14 +100,18 @@ public class ChangeTracker {
       return type;
     }
 
+    public Date getDate() {
+      return date;
+    }
+
   }
 
   /**
    * Returns a <code>Collection</code> of {@link Change}s within a base
    *     path, and removes them from the tracker.
    */
-  public List flushChanges(String aBasePath) {
-    return flushChanges(aBasePath, new String[0]);
+  public Collection flushChanges(String aBasePath) {
+    return flushChanges(aBasePath, Collections.EMPTY_LIST);
   }
 
   /**
@@ -105,9 +119,9 @@ public class ChangeTracker {
    *     path, exluding a list of excluded paths, and removes them from
    *     the tracker.
    */
-  public List flushChanges(String aBasePath, String[] anExcludedPaths) {
+  public Collection flushChanges(String aBasePath, Collection anExcludedPaths) {
     synchronized (changes) {
-      List result = getChanges(aBasePath, anExcludedPaths);
+      Collection result = getChanges(aBasePath, anExcludedPaths);
 
       removeChanges(result);
 
@@ -147,22 +161,34 @@ public class ChangeTracker {
   }
 
   /**
-   * gets all changes within a base path, but excluding some other paths
+   * Gets all changes within a base path, but excluding some other paths
+   *
+   * @param aBasePath
+   * @param anExcludedPaths a collection of paths to exclude. may be <code>null</code>
+   *       to not exclude anything
    */
-  List getChanges(String aBasePath, String[] anExcludingPaths) {
+  Collection getChanges(String aBasePath, Collection anExcludedPaths) {
     synchronized (changes) {
       List result = getChanges(aBasePath);
 
-      for (int i=0; i<anExcludingPaths.length && result.size()>0; i++) {
-        List remove = new ArrayList();
-        Iterator j = result.iterator();
-        while (j.hasNext()) {
-          Change change = (Change) j.next();
-          if (change.getPath().startsWith(anExcludingPaths[i])) {
-            remove.add(change);
+      if (anExcludedPaths != null) {
+        Iterator i = anExcludedPaths.iterator();
+        while (i.hasNext()) {
+          String excludedPath = (String) i.next();
+          List remove = new ArrayList();
+          Iterator j = result.iterator();
+          while (j.hasNext()) {
+            Change change = (Change) j.next();
+            if (change.getPath().startsWith(excludedPath)) {
+              remove.add(change);
+            }
           }
+          result.removeAll(remove);
         }
-        result.removeAll(remove);
+      }
+
+      if (changes.size()>MAX_CHANGES) {
+        changes.clear();
       }
 
       return result;
index e1f44cc..e3d08ea 100644 (file)
  */\r
 package mircoders.global;\r
 \r
-import mir.changetracker.ChangeReporter;\r
 import mir.changetracker.ChangeTracker;\r
-import mir.log.LoggerWrapper;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.util.List;\r
 \r
 public class ChangeEngine {\r
   private ChangeTracker tracker;\r
-  private LoggerWrapper logger;\r
 \r
   public ChangeEngine() {\r
     tracker = new ChangeTracker();\r
-    logger = new LoggerWrapper("Global.Changes");\r
   }\r
 \r
   /**\r
@@ -52,25 +44,4 @@ public class ChangeEngine {
   public ChangeTracker getTracker() {\r
     return tracker;\r
   }\r
-\r
-  /**\r
-   *\r
-   * @param aReportFileName\r
-   * @param aLockFileName\r
-   * @param aBasePath\r
-   * @param anExcludedPaths\r
-   * @param aFlush\r
-   */\r
-  public void report(String aReportFileName, String aLockFileName, String aBasePath,\r
-                     List anExcludedPaths, boolean aFlush) throws IOException {\r
-\r
-    File reportFile = new File(MirGlobal.config().getHome(), aReportFileName);\r
-    File lockFile = null;\r
-\r
-    if (aLockFileName!=null) {\r
-      lockFile = new File(MirGlobal.config().getHome(), aLockFileName);\r
-    }\r
-\r
-    ChangeReporter.reportChanges(tracker, aBasePath, anExcludedPaths, reportFile, lockFile, 30*1000, aFlush);\r
-  }\r
 }\r
index 623bf89..c271a13 100644 (file)
@@ -4,32 +4,31 @@ import mir.changetracker.ChangeTracker;
 import mir.log.LoggerWrapper;\r
 import mir.producer.AbstractProducerNode;\r
 import mir.util.ParameterExpander;\r
+import mir.util.StringRoutines;\r
 import mircoders.global.MirGlobal;\r
 \r
 import java.io.File;\r
 import java.io.FileNotFoundException;\r
 import java.io.FileOutputStream;\r
-import java.io.IOException;\r
 import java.io.OutputStream;\r
 import java.io.OutputStreamWriter;\r
-import java.io.Writer;\r
 import java.io.PrintWriter;\r
 import java.util.Collection;\r
+import java.util.HashMap;\r
 import java.util.Iterator;\r
+import java.util.List;\r
 import java.util.Map;\r
 \r
 public class ChangeReportingProducerNode extends AbstractProducerNode {\r
   private String format;\r
   private String outputFile;\r
   private String basePath;\r
-  private String baseUrl;\r
   private String exclusionList;\r
 \r
-  public ChangeReportingProducerNode(String anOutputFile, String aFormat, String aBasePath, String aBaseUrl, String anExclusionList) {\r
+  public ChangeReportingProducerNode(String anOutputFile, String aFormat, String aBasePath, String anExclusionList) {\r
     format = aFormat;\r
     outputFile = anOutputFile;\r
     basePath = aBasePath;\r
-    baseUrl = aBaseUrl;\r
     exclusionList = anExclusionList;\r
   }\r
 \r
@@ -50,13 +49,28 @@ public class ChangeReportingProducerNode extends AbstractProducerNode {
         PrintWriter writer = new PrintWriter(new OutputStreamWriter(stream));\r
         try {\r
           String expandedBasePath = ParameterExpander.expandExpression(aValueMap, basePath);\r
-          Collection changes = MirGlobal.getChangeEngine().getTracker().flushChanges(expandedBasePath);\r
+\r
+          List excludedPaths = null;\r
+          if (exclusionList != null) {\r
+            String expandedExclusionList = ParameterExpander.expandExpression(aValueMap, exclusionList);\r
+            excludedPaths = StringRoutines.separateString(expandedExclusionList, ",");\r
+          }\r
+\r
+          Collection changes = MirGlobal.getChangeEngine().getTracker().flushChanges(expandedBasePath, excludedPaths);\r
 \r
           Iterator i = changes.iterator();\r
           while (i.hasNext()) {\r
             ChangeTracker.Change change = (ChangeTracker.Change) i.next();\r
 \r
-            aValueMap.put("change", change);\r
+            Map changeMap = new HashMap();\r
+\r
+            // the absolute path of the change needs to be transformed into a path relative\r
+            //    to base\r
+            changeMap.put("path", change.getPath().substring(expandedBasePath.length()));\r
+            changeMap.put("type", change.getType().getName());\r
+            changeMap.put("date", change.getDate());\r
+\r
+            aValueMap.put("change", changeMap);\r
             String line = ParameterExpander.expandExpression(aValueMap, format);\r
             writer.println(line);\r
           }\r
index b3bb6ff..d9f8664 100755 (executable)
@@ -89,9 +89,9 @@ public class MediaGeneratingProducerNode extends AbstractProducerNode {
       // Inform the Change Reporter of the change
       String publishpath = entity.getFieldValue("publish_path");
       ChangeTracker tracker = MirGlobal.getChangeEngine().getTracker();
-      
+
       tracker.addChange(publishpath, ChangeType.MODIFICATION);
-      
+
       aLogger.info("media with id " + uploadedMediaEntity.getFieldValue("id") +
           ", mediaType " + mediaType.getFieldValue("name") + " successfully produced");
     }
index 2038f46..12d01ee 100755 (executable)
@@ -255,13 +255,15 @@ public class SupplementalProducerNodeBuilders {
     private final static String CHANGEREPORTING_FILE_ATTRIBUTE = "file";
     private final static String CHANGEREPORTING_FORMAT_ATTRIBUTE = "format";
     private final static String CHANGEREPORTING_BASEPATH_ATTRIBUTE = "basepath";
+    private final static String CHANGEREPORTING_EXCLUDEDPATHS_ATTRIBUTE = "excludedpaths";
     private final static String[] CHANGEREPORTING_REQUIRED_ATTRIBUTES = {CHANGEREPORTING_FILE_ATTRIBUTE,
         CHANGEREPORTING_FORMAT_ATTRIBUTE, CHANGEREPORTING_BASEPATH_ATTRIBUTE};
-    private static final String[] CHANGEREPORTING_OPTIONAL_ATTRIBUTES = {};
+    private static final String[] CHANGEREPORTING_OPTIONAL_ATTRIBUTES = {CHANGEREPORTING_EXCLUDEDPATHS_ATTRIBUTE};
 
     private String file;
     private String format;
     private String basepath;
+    private String excludedpaths;
 
     public ChangeReportingProducerNodeBuilder() {
       super(new String[0]);
@@ -273,10 +275,11 @@ public class SupplementalProducerNodeBuilders {
       file = (String) anAttributes.get(CHANGEREPORTING_FILE_ATTRIBUTE);
       format = (String) anAttributes.get(CHANGEREPORTING_FORMAT_ATTRIBUTE);
       basepath = (String) anAttributes.get(CHANGEREPORTING_BASEPATH_ATTRIBUTE);
+      excludedpaths = (String) anAttributes.get(CHANGEREPORTING_EXCLUDEDPATHS_ATTRIBUTE);
     }
 
     public ProducerNode constructNode() {
-      return new ChangeReportingProducerNode(file, format, basepath, null, null);
+      return new ChangeReportingProducerNode(file, format, basepath, excludedpaths);
     }
   }
 }