Added org.simantics.filesystem.services 21/1221/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 14 Nov 2017 23:14:55 +0000 (01:14 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 14 Nov 2017 23:28:57 +0000 (02:28 +0300)
Contains DirectorySizeService for tracking the size of filesystem
directories.

refs #7617

Change-Id: I08e7c9297046c66c6d2b9bbbcb6122e370103c83

15 files changed:
bundles/org.simantics.filesystem.services/.classpath [new file with mode: 0644]
bundles/org.simantics.filesystem.services/.project [new file with mode: 0644]
bundles/org.simantics.filesystem.services/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
bundles/org.simantics.filesystem.services/META-INF/MANIFEST.MF [new file with mode: 0644]
bundles/org.simantics.filesystem.services/OSGI-INF/org.simantics.filesystem.services.internal.sizetracker.DirectorySizeServiceImpl.xml [new file with mode: 0644]
bundles/org.simantics.filesystem.services/build.properties [new file with mode: 0644]
bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/Activator.java [new file with mode: 0644]
bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/DirectorySizeServiceImpl.java [new file with mode: 0644]
bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeChangeEventImpl.java [new file with mode: 0644]
bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeTrackerImpl.java [new file with mode: 0644]
bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/DirectorySizeService.java [new file with mode: 0644]
bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeChangeEvent.java [new file with mode: 0644]
bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeTracker.java [new file with mode: 0644]
bundles/pom.xml
features/org.simantics.sdk.feature/feature.xml

diff --git a/bundles/org.simantics.filesystem.services/.classpath b/bundles/org.simantics.filesystem.services/.classpath
new file mode 100644 (file)
index 0000000..b862a29
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<classpath>\r
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>\r
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>\r
+       <classpathentry kind="src" path="src"/>\r
+       <classpathentry kind="output" path="bin"/>\r
+</classpath>\r
diff --git a/bundles/org.simantics.filesystem.services/.project b/bundles/org.simantics.filesystem.services/.project
new file mode 100644 (file)
index 0000000..0fac112
--- /dev/null
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<projectDescription>\r
+       <name>org.simantics.filesystem.services</name>\r
+       <comment></comment>\r
+       <projects>\r
+       </projects>\r
+       <buildSpec>\r
+               <buildCommand>\r
+                       <name>org.eclipse.jdt.core.javabuilder</name>\r
+                       <arguments>\r
+                       </arguments>\r
+               </buildCommand>\r
+               <buildCommand>\r
+                       <name>org.eclipse.pde.ManifestBuilder</name>\r
+                       <arguments>\r
+                       </arguments>\r
+               </buildCommand>\r
+               <buildCommand>\r
+                       <name>org.eclipse.pde.SchemaBuilder</name>\r
+                       <arguments>\r
+                       </arguments>\r
+               </buildCommand>\r
+               <buildCommand>\r
+                       <name>org.eclipse.pde.ds.core.builder</name>\r
+                       <arguments>\r
+                       </arguments>\r
+               </buildCommand>\r
+       </buildSpec>\r
+       <natures>\r
+               <nature>org.eclipse.pde.PluginNature</nature>\r
+               <nature>org.eclipse.jdt.core.javanature</nature>\r
+       </natures>\r
+</projectDescription>\r
diff --git a/bundles/org.simantics.filesystem.services/.settings/org.eclipse.jdt.core.prefs b/bundles/org.simantics.filesystem.services/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..295926d
--- /dev/null
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1\r
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled\r
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8\r
+org.eclipse.jdt.core.compiler.compliance=1.8\r
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error\r
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error\r
+org.eclipse.jdt.core.compiler.source=1.8\r
diff --git a/bundles/org.simantics.filesystem.services/META-INF/MANIFEST.MF b/bundles/org.simantics.filesystem.services/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..2347ee2
--- /dev/null
@@ -0,0 +1,15 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Workspace Size Monitoring Service
+Bundle-SymbolicName: org.simantics.filesystem.services
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.simantics.filesystem.services.internal.Activator
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.osgi.services,
+ org.simantics.utils.datastructures,
+ org.slf4j.api
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Bundle-ActivationPolicy: lazy
+Bundle-Vendor: Semantum Oy
+Export-Package: org.simantics.filesystem.services.sizetracker
+Service-Component: OSGI-INF/org.simantics.filesystem.services.internal.sizetracker.DirectorySizeServiceImpl.xml
diff --git a/bundles/org.simantics.filesystem.services/OSGI-INF/org.simantics.filesystem.services.internal.sizetracker.DirectorySizeServiceImpl.xml b/bundles/org.simantics.filesystem.services/OSGI-INF/org.simantics.filesystem.services.internal.sizetracker.DirectorySizeServiceImpl.xml
new file mode 100644 (file)
index 0000000..eeb59b3
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="org.simantics.filesystem.services.internal.sizetracker.DirectorySizeServiceImpl">
+   <service>
+      <provide interface="org.simantics.filesystem.services.sizetracker.DirectorySizeService"/>
+   </service>
+   <implementation class="org.simantics.filesystem.services.internal.sizetracker.DirectorySizeServiceImpl"/>
+</scr:component>
\ No newline at end of file
diff --git a/bundles/org.simantics.filesystem.services/build.properties b/bundles/org.simantics.filesystem.services/build.properties
new file mode 100644 (file)
index 0000000..a65755c
--- /dev/null
@@ -0,0 +1,5 @@
+source.. = src/\r
+output.. = bin/\r
+bin.includes = META-INF/,\\r
+               .,\\r
+               OSGI-INF/\r
diff --git a/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/Activator.java b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/Activator.java
new file mode 100644 (file)
index 0000000..e84fdbc
--- /dev/null
@@ -0,0 +1,34 @@
+package org.simantics.filesystem.services.internal;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ */
+public class Activator implements BundleActivator {
+
+       private static BundleContext context;
+
+       static BundleContext getContext() {
+               return context;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+        */
+       public void start(BundleContext bundleContext) throws Exception {
+               Activator.context = bundleContext;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+        */
+       public void stop(BundleContext bundleContext) throws Exception {
+               Activator.context = null;
+       }
+
+}
\ No newline at end of file
diff --git a/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/DirectorySizeServiceImpl.java b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/DirectorySizeServiceImpl.java
new file mode 100644 (file)
index 0000000..db8ec3d
--- /dev/null
@@ -0,0 +1,77 @@
+package org.simantics.filesystem.services.internal.sizetracker;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.simantics.filesystem.services.sizetracker.DirectorySizeService;
+import org.simantics.filesystem.services.sizetracker.SizeTracker;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ */
+@Component
+public class DirectorySizeServiceImpl implements DirectorySizeService {
+
+       private Logger logger = LoggerFactory.getLogger(DirectorySizeServiceImpl.class);
+
+       private final Map<Path, SizeTrackerImpl> trackers = new HashMap<>();
+
+       @Override
+       public SizeTracker track(Path directory) throws IOException {
+               synchronized (trackers) {
+                       SizeTrackerImpl tracker = trackers.get(directory);
+                       if (tracker != null)
+                               return tracker;
+                       tracker = new SizeTrackerImpl(this, directory);
+                       try {
+                               tracker.start();
+                               trackers.put(directory, tracker);
+                               return tracker;
+                       } catch (IOException e) {
+                               safeCloseTracker(tracker);
+                               throw e;
+                       }
+               }
+       }
+
+       void removeTracker(Path path) {
+               synchronized (trackers) {
+                       trackers.remove(path);
+               }
+       }
+
+       public void shutdown() {
+               synchronized (trackers) {
+                       trackers.forEach((p,t) -> safeCloseTracker(t));
+                       trackers.clear();
+               }
+       }
+
+       private void safeCloseTracker(SizeTrackerImpl t) {
+               try {
+                       t.close();
+               } catch (IOException e) {
+                       logger.error("Error closing size tracker {}", t, e);
+               }
+       }
+
+       @Activate
+       public void activate() {
+               //System.out.println("Activated DirectorySizeServiceImpl");
+       }
+
+       @Deactivate
+       public void deactivate() {
+               //System.out.println("Deactivated DirectorySizeServiceImpl");
+               shutdown();
+       }
+
+}
diff --git a/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeChangeEventImpl.java b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeChangeEventImpl.java
new file mode 100644 (file)
index 0000000..9a197d7
--- /dev/null
@@ -0,0 +1,38 @@
+package org.simantics.filesystem.services.internal.sizetracker;
+
+import java.nio.file.Path;
+
+import org.simantics.filesystem.services.sizetracker.SizeChangeEvent;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ */
+public class SizeChangeEventImpl implements SizeChangeEvent {
+
+       private final Path path;
+       private final long oldSize;
+       private final long newSize;
+
+       public SizeChangeEventImpl(Path path, long oldSize, long newSize) {
+               this.path = path;
+               this.oldSize = oldSize;
+               this.newSize = newSize;
+       }
+
+       @Override
+       public Path path() {
+               return path;
+       }
+
+       @Override
+       public long newSize() {
+               return newSize;
+       }
+
+       @Override
+       public long oldSize() {
+               return oldSize;
+       }
+
+}
\ No newline at end of file
diff --git a/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeTrackerImpl.java b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeTrackerImpl.java
new file mode 100644 (file)
index 0000000..b1ca0aa
--- /dev/null
@@ -0,0 +1,92 @@
+package org.simantics.filesystem.services.internal.sizetracker;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Consumer;
+import java.util.function.LongConsumer;
+
+import org.simantics.filesystem.services.sizetracker.SizeChangeEvent;
+import org.simantics.filesystem.services.sizetracker.SizeTracker;
+import org.simantics.utils.datastructures.file.DirectorySizeTracker;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ * 
+ * TODO: change to use quiet time and "post notification" to throttle updates
+ */
+public class SizeTrackerImpl implements SizeTracker {
+
+       private Logger logger = LoggerFactory.getLogger(SizeTrackerImpl.class);
+
+       private Path path;
+       private DirectorySizeServiceImpl service;
+       private DirectorySizeTracker tracker;
+       private CopyOnWriteArrayList<Consumer<SizeChangeEvent>> listeners = new CopyOnWriteArrayList<>();
+       private volatile long oldSize = 0L;
+
+       private LongConsumer theListener = newSize -> {
+               long os = oldSize;
+               oldSize = newSize;
+               //logger.info(path + ": size changed: " + ((double) os / (1024.0*1024.0)) + " MB -> " + ((double) newSize / (1024.0*1024.0)) + " MB");
+               fireSizeChange(os, newSize);
+       };
+
+       public SizeTrackerImpl(DirectorySizeServiceImpl service, Path path) throws IOException {
+               this.service = service;
+               this.path = path;
+               this.tracker = DirectorySizeTracker.startTracker(theListener);
+       }
+
+       public SizeTrackerImpl start() throws IOException {
+               new Thread(() -> {
+                       try {
+                               synchronized (SizeTrackerImpl.this) {
+                                       if (tracker != null)
+                                               tracker.track(path);
+                               }
+                       } catch (IOException e) {
+                               logger.error("Failed to start tracking size of directory " + path, e);
+                       }
+               }, "SizeTrackerStarter").start();
+               return this;
+       }
+
+       @Override
+       public synchronized void close() throws IOException {
+               if (tracker == null)
+                       return;
+               tracker.close();
+               tracker = null;
+               service.removeTracker(path);
+       }
+
+       @Override
+       public Path path() {
+               return path;
+       }
+
+       @Override
+       public long size() {
+               return oldSize;
+       }
+
+       @Override
+       public void addListener(Consumer<SizeChangeEvent> listener) {
+               listeners.add(listener);
+       }
+
+       @Override
+       public void removeListener(Consumer<SizeChangeEvent> listener) {
+               listeners.remove(listener);
+       }
+
+       private void fireSizeChange(long oldSize, long newSize) {
+               SizeChangeEvent e = new SizeChangeEventImpl(path, oldSize, newSize);
+               listeners.forEach(c -> c.accept(e));
+       }
+
+}
diff --git a/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/DirectorySizeService.java b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/DirectorySizeService.java
new file mode 100644 (file)
index 0000000..f329b69
--- /dev/null
@@ -0,0 +1,14 @@
+package org.simantics.filesystem.services.sizetracker;
+
+import java.io.IOException;
+import java.nio.file.Path;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ */
+public interface DirectorySizeService {
+
+       SizeTracker track(Path directory) throws IOException;
+
+}
diff --git a/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeChangeEvent.java b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeChangeEvent.java
new file mode 100644 (file)
index 0000000..847bc49
--- /dev/null
@@ -0,0 +1,17 @@
+package org.simantics.filesystem.services.sizetracker;
+
+import java.nio.file.Path;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ */
+public interface SizeChangeEvent {
+
+       Path path();
+
+       long newSize();
+
+       long oldSize();
+
+}
diff --git a/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeTracker.java b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeTracker.java
new file mode 100644 (file)
index 0000000..f6039f4
--- /dev/null
@@ -0,0 +1,19 @@
+package org.simantics.filesystem.services.sizetracker;
+
+import java.io.Closeable;
+import java.nio.file.Path;
+import java.util.function.Consumer;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ */
+public interface SizeTracker extends Closeable {
+
+       Path path();
+       long size();
+
+       void addListener(Consumer<SizeChangeEvent> listener);
+       void removeListener(Consumer<SizeChangeEvent> listener);
+
+}
index 502f0f41df45cc2f1a3d7de031378fc5481b6643..04328c95be799d40b9c0645b65942ef21b5d78d3 100644 (file)
                <module>org.simantics.export.core</module>
                <module>org.simantics.export.ui</module>
                <module>org.simantics.fastlz</module>
+               <module>org.simantics.filesystem.services</module>
                <module>org.simantics.fileimport</module>
                <module>org.simantics.fileimport.ui</module>
                <module>org.simantics.g2d</module>
index 61bb1e5b53d83ddfcafb7852110f6d1bfc00e6fd..7a6d287ff219ed5b4a0257dd8e94535226228f99 100644 (file)
          version="0.0.0"
          unpack="false"/>
 
+   <plugin
+         id="org.simantics.filesystem.services"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
    <plugin
          id="org.simantics.backup.ontology"
          download-size="0"