From: zapata Date: Sat, 11 Nov 2006 19:21:28 +0000 (+0000) Subject: further fixes to the change reporting framework X-Git-Tag: LATEST_MERGED_1_1~23 X-Git-Url: http://erislabs.net/gitweb/?p=mir.git;a=commitdiff_plain;h=b97f8b5eb26357c7c4eb1933ff77d2f0b0e625ef further fixes to the change reporting framework --- diff --git a/source/mir/changetracker/ChangeReporter.java b/source/mir/changetracker/ChangeReporter.java deleted file mode 100644 index 401721b1..00000000 --- a/source/mir/changetracker/ChangeReporter.java +++ /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) { - } - } - } - } - } -} diff --git a/source/mir/changetracker/ChangeTracker.java b/source/mir/changetracker/ChangeTracker.java index 3474b269..3df943e7 100644 --- a/source/mir/changetracker/ChangeTracker.java +++ b/source/mir/changetracker/ChangeTracker.java @@ -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 Collection 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 null + * 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; i0; 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; diff --git a/source/mircoders/global/ChangeEngine.java b/source/mircoders/global/ChangeEngine.java index e1f44ccb..e3d08ea2 100644 --- a/source/mircoders/global/ChangeEngine.java +++ b/source/mircoders/global/ChangeEngine.java @@ -29,21 +29,13 @@ */ package mircoders.global; -import mir.changetracker.ChangeReporter; import mir.changetracker.ChangeTracker; -import mir.log.LoggerWrapper; - -import java.io.File; -import java.io.IOException; -import java.util.List; public class ChangeEngine { private ChangeTracker tracker; - private LoggerWrapper logger; public ChangeEngine() { tracker = new ChangeTracker(); - logger = new LoggerWrapper("Global.Changes"); } /** @@ -52,25 +44,4 @@ public class ChangeEngine { public ChangeTracker getTracker() { return tracker; } - - /** - * - * @param aReportFileName - * @param aLockFileName - * @param aBasePath - * @param anExcludedPaths - * @param aFlush - */ - public void report(String aReportFileName, String aLockFileName, String aBasePath, - List anExcludedPaths, boolean aFlush) throws IOException { - - File reportFile = new File(MirGlobal.config().getHome(), aReportFileName); - File lockFile = null; - - if (aLockFileName!=null) { - lockFile = new File(MirGlobal.config().getHome(), aLockFileName); - } - - ChangeReporter.reportChanges(tracker, aBasePath, anExcludedPaths, reportFile, lockFile, 30*1000, aFlush); - } } diff --git a/source/mircoders/producer/ChangeReportingProducerNode.java b/source/mircoders/producer/ChangeReportingProducerNode.java index 623bf89c..c271a133 100644 --- a/source/mircoders/producer/ChangeReportingProducerNode.java +++ b/source/mircoders/producer/ChangeReportingProducerNode.java @@ -4,32 +4,31 @@ import mir.changetracker.ChangeTracker; import mir.log.LoggerWrapper; import mir.producer.AbstractProducerNode; import mir.util.ParameterExpander; +import mir.util.StringRoutines; import mircoders.global.MirGlobal; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.Writer; import java.io.PrintWriter; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; public class ChangeReportingProducerNode extends AbstractProducerNode { private String format; private String outputFile; private String basePath; - private String baseUrl; private String exclusionList; - public ChangeReportingProducerNode(String anOutputFile, String aFormat, String aBasePath, String aBaseUrl, String anExclusionList) { + public ChangeReportingProducerNode(String anOutputFile, String aFormat, String aBasePath, String anExclusionList) { format = aFormat; outputFile = anOutputFile; basePath = aBasePath; - baseUrl = aBaseUrl; exclusionList = anExclusionList; } @@ -50,13 +49,28 @@ public class ChangeReportingProducerNode extends AbstractProducerNode { PrintWriter writer = new PrintWriter(new OutputStreamWriter(stream)); try { String expandedBasePath = ParameterExpander.expandExpression(aValueMap, basePath); - Collection changes = MirGlobal.getChangeEngine().getTracker().flushChanges(expandedBasePath); + + List excludedPaths = null; + if (exclusionList != null) { + String expandedExclusionList = ParameterExpander.expandExpression(aValueMap, exclusionList); + excludedPaths = StringRoutines.separateString(expandedExclusionList, ","); + } + + Collection changes = MirGlobal.getChangeEngine().getTracker().flushChanges(expandedBasePath, excludedPaths); Iterator i = changes.iterator(); while (i.hasNext()) { ChangeTracker.Change change = (ChangeTracker.Change) i.next(); - aValueMap.put("change", change); + Map changeMap = new HashMap(); + + // the absolute path of the change needs to be transformed into a path relative + // to base + changeMap.put("path", change.getPath().substring(expandedBasePath.length())); + changeMap.put("type", change.getType().getName()); + changeMap.put("date", change.getDate()); + + aValueMap.put("change", changeMap); String line = ParameterExpander.expandExpression(aValueMap, format); writer.println(line); } diff --git a/source/mircoders/producer/MediaGeneratingProducerNode.java b/source/mircoders/producer/MediaGeneratingProducerNode.java index b3bb6fff..d9f86648 100755 --- a/source/mircoders/producer/MediaGeneratingProducerNode.java +++ b/source/mircoders/producer/MediaGeneratingProducerNode.java @@ -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"); } diff --git a/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java b/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java index 2038f469..12d01eea 100755 --- a/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java +++ b/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java @@ -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); } } }