You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2010/09/14 17:36:27 UTC
svn commit: r996957 - in
/sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl:
FileChangesListener.java FileMonitor.java Installer.java
Author: cziegeler
Date: Tue Sep 14 15:36:26 2010
New Revision: 996957
URL: http://svn.apache.org/viewvc?rev=996957&view=rev
Log:
Collect changes before submitting them to the installer
Modified:
sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java
sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java
sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/Installer.java
Modified: sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java?rev=996957&r1=996956&r2=996957&view=diff
==============================================================================
--- sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java (original)
+++ sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileChangesListener.java Tue Sep 14 15:36:26 2010
@@ -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);
}
Modified: sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java?rev=996957&r1=996956&r2=996957&view=diff
==============================================================================
--- sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java (original)
+++ sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/FileMonitor.java Tue Sep 14 15:36:26 2010
@@ -81,6 +81,12 @@ public class FileMonitor extends TimerTa
}
}
+ 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 TimerTa
}
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 TimerTa
* @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 TimerTa
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 TimerTa
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 TimerTa
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 TimerTa
// 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 TimerTa
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;
Modified: sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/Installer.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/Installer.java?rev=996957&r1=996956&r2=996957&view=diff
==============================================================================
--- sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/Installer.java (original)
+++ sling/trunk/installer/fileinstall/src/main/java/org/apache/sling/installer/file/impl/Installer.java Tue Sep 14 15:36:26 2010
@@ -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 FileCh
}
/**
- * @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 FileCh
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 FileCh
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()});
- }
}