This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.installer.provider.file-1.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-provider-file.git
commit 6b38c95de9fd045d8609fe76a392a9ee1e381658 Author: Carsten Ziegeler <[email protected]> AuthorDate: Tue Sep 14 15:36:26 2010 +0000 Collect changes before submitting them to the installer git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/installer/fileinstall@996957 13f79535-47bb-0310-9956-ffa450edef68 --- .../installer/file/impl/FileChangesListener.java | 6 +- .../sling/installer/file/impl/FileMonitor.java | 22 ++++-- .../sling/installer/file/impl/Installer.java | 84 ++++++++++++++-------- 3 files changed, 70 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java b/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java index b9d7485..ded285c 100644 --- a/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java +++ b/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java @@ -25,9 +25,5 @@ public interface FileChangesListener { void initialSet(List<File> files); - void removed(File file); - - void added(File file); - - void changed(File file); + void updated(List<File> added, List<File> changed, List<File> removed); } diff --git a/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java b/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java index a80a0ba..875a256 100644 --- a/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java +++ b/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java @@ -81,6 +81,12 @@ public class FileMonitor extends TimerTask { } } + private final static class Collector { + public final List<File> added = new ArrayList<File>(); + public final List<File> removed = new ArrayList<File>(); + public final List<File> changed = new ArrayList<File>(); + } + /** * Stop periodically executing this task. If the task is currently executing it * will never be run again after the current execution, otherwise it will simply @@ -124,7 +130,9 @@ public class FileMonitor extends TimerTask { } synchronized ( this ) { try { - this.check(this.root); + final Collector c = new Collector(); + this.check(this.root, c); + this.listener.updated(c.added, c.changed, c.removed); } catch (Exception e) { // ignore this } @@ -140,7 +148,7 @@ public class FileMonitor extends TimerTask { * @param monitorable The monitorable to check * @param localEA The event admin */ - private void check(final Monitorable monitorable) { + private void check(final Monitorable monitorable, final Collector collector) { logger.debug("Checking {}", monitorable.file); // if the file is non existing, check if it has been readded if ( monitorable.status instanceof NonExistingStatus ) { @@ -150,7 +158,7 @@ public class FileMonitor extends TimerTask { final List<File> files = new ArrayList<File>(); collect(monitorable.file, files); for(final File file : files ) { - this.listener.added(file); + collector.added.add(file); } } } else { @@ -160,7 +168,7 @@ public class FileMonitor extends TimerTask { final List<File> files = new ArrayList<File>(); collectDeleted(monitorable, files); for(final File file : files ) { - this.listener.removed(file); + collector.removed.add(file); } monitorable.status = NonExistingStatus.SINGLETON; } else { @@ -171,7 +179,7 @@ public class FileMonitor extends TimerTask { fs.lastModified = monitorable.file.lastModified(); // changed if ( monitorable.file.isFile() ) { - this.listener.changed(monitorable.file); + collector.changed.add(monitorable.file); } changed = true; } @@ -179,7 +187,7 @@ public class FileMonitor extends TimerTask { // directory final DirStatus ds = (DirStatus)fs; for(int i=0; i<ds.children.length; i++) { - check(ds.children[i]); + check(ds.children[i], collector); } // if the dir changed we have to update if ( changed ) { @@ -198,7 +206,7 @@ public class FileMonitor extends TimerTask { if (children[i] == null) { children[i] = new Monitorable(files[i]); children[i].status = NonExistingStatus.SINGLETON; - check(children[i]); + check(children[i], collector); } } ds.children = children; diff --git a/src/main/java/org/apache/sling/installer/file/impl/Installer.java b/src/main/java/org/apache/sling/installer/file/impl/Installer.java index 6798152..7bc3e98 100644 --- a/src/main/java/org/apache/sling/installer/file/impl/Installer.java +++ b/src/main/java/org/apache/sling/installer/file/impl/Installer.java @@ -29,19 +29,22 @@ import java.util.List; import org.apache.sling.osgi.installer.InstallableResource; import org.apache.sling.osgi.installer.OsgiInstaller; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The <code>Installer</code> is the service calling the * OSGi installer * - * TODO - We should collect all changes from a scan and send - * them to the installer in a batch */ public class Installer implements FileChangesListener { + /** The scheme we use to register our resources. */ private static final String SCHEME_PREFIX = "fileinstall"; + /** Logger. */ + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + /** The OSGi installer service. */ private final OsgiInstaller installer; @@ -55,31 +58,13 @@ public class Installer implements FileChangesListener { } /** - * @see org.apache.sling.installer.file.impl.FileChangesListener#added(java.io.File) - */ - public void added(final File file) { - LoggerFactory.getLogger(this.getClass()).info("Added file {}", file); - final InstallableResource resource = this.createResource(file); - this.installer.updateResources(this.scheme, new InstallableResource[] {resource}, null); - } - - /** - * @see org.apache.sling.installer.file.impl.FileChangesListener#changed(java.io.File) - */ - public void changed(final File file) { - LoggerFactory.getLogger(this.getClass()).info("Changed file {}", file); - final InstallableResource resource = this.createResource(file); - this.installer.updateResources(this.scheme, new InstallableResource[] {resource}, null); - } - - /** * @see org.apache.sling.installer.file.impl.FileChangesListener#initialSet(java.util.List) */ public void initialSet(final List<File> files) { - LoggerFactory.getLogger(this.getClass()).info("Initial set for {}", this.scheme); + logger.debug("Initial set for {}", this.scheme); final List<InstallableResource> resources = new ArrayList<InstallableResource>(); for(final File f : files) { - LoggerFactory.getLogger(this.getClass()).info("File {}", f); + logger.debug("Initial file {}", f); final InstallableResource resource = this.createResource(f); if ( resource != null ) { resources.add(resource); @@ -88,6 +73,52 @@ public class Installer implements FileChangesListener { this.installer.registerResources(this.scheme, resources.toArray(new InstallableResource[resources.size()])); } + /** + * @see org.apache.sling.installer.file.impl.FileChangesListener#updated(java.util.List, java.util.List, java.util.List) + */ + public void updated(List<File> added, List<File> changed, List<File> removed) { + final List<InstallableResource> updated; + if ( (added != null && added.size() > 0) || (changed != null && changed.size() > 0) ) { + updated = new ArrayList<InstallableResource>(); + if ( added != null ) { + for(final File f : added) { + logger.debug("Added file {}", f); + final InstallableResource resource = this.createResource(f); + if ( resource != null ) { + updated.add(resource); + } + } + } + if ( changed != null ) { + for(final File f : changed) { + logger.debug("Changed file {}", f); + final InstallableResource resource = this.createResource(f); + if ( resource != null ) { + updated.add(resource); + } + } + } + } else { + updated = null; + } + final String[] removedUrls; + if ( removed != null && removed.size() > 0 ) { + removedUrls = new String[removed.size()]; + int index = 0; + for(final File f : removed) { + removedUrls[index] = f.getAbsolutePath(); + logger.debug("Removed file {}", removedUrls[index]); + index++; + } + } else { + removedUrls = null; + } + if ( updated != null || removedUrls != null ) { + this.installer.updateResources(this.scheme, + updated == null ? null : updated.toArray(new InstallableResource[updated.size()]), removedUrls); + } + } + private InstallableResource createResource(final File file) { try { final InputStream is = new FileInputStream(file); @@ -109,15 +140,8 @@ public class Installer implements FileChangesListener { return new InstallableResource(file.getAbsolutePath(), is, dict, digest, null, null); } catch (IOException io) { - // ignore this for now (TODO) + logger.error("Unable to read file " + file, io); } return null; } - /** - * @see org.apache.sling.installer.file.impl.FileChangesListener#removed(java.io.File) - */ - public void removed(final File file) { - LoggerFactory.getLogger(this.getClass()).info("Removed file {}", file); - this.installer.updateResources(this.scheme, null, new String[] {file.getAbsolutePath()}); - } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
