From: Tuukka Lehtonen Date: Tue, 14 Nov 2017 23:14:55 +0000 (+0200) Subject: Added org.simantics.filesystem.services X-Git-Tag: v1.31.0~39 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F21%2F1221%2F2;p=simantics%2Fplatform.git Added org.simantics.filesystem.services Contains DirectorySizeService for tracking the size of filesystem directories. refs #7617 Change-Id: I08e7c9297046c66c6d2b9bbbcb6122e370103c83 --- diff --git a/bundles/org.simantics.filesystem.services/.classpath b/bundles/org.simantics.filesystem.services/.classpath new file mode 100644 index 000000000..b862a296d --- /dev/null +++ b/bundles/org.simantics.filesystem.services/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/bundles/org.simantics.filesystem.services/.project b/bundles/org.simantics.filesystem.services/.project new file mode 100644 index 000000000..0fac112c7 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/.project @@ -0,0 +1,33 @@ + + + org.simantics.filesystem.services + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.pde.ds.core.builder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + 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 index 000000000..295926d96 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 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 index 000000000..2347ee275 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/META-INF/MANIFEST.MF @@ -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 index 000000000..eeb59b380 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/OSGI-INF/org.simantics.filesystem.services.internal.sizetracker.DirectorySizeServiceImpl.xml @@ -0,0 +1,7 @@ + + + + + + + \ 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 index 000000000..a65755cb2 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/ 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 index 000000000..e84fdbcab --- /dev/null +++ b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/Activator.java @@ -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 index 000000000..db8ec3da1 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/DirectorySizeServiceImpl.java @@ -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 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 index 000000000..9a197d76c --- /dev/null +++ b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeChangeEventImpl.java @@ -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 index 000000000..b1ca0aaa2 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/internal/sizetracker/SizeTrackerImpl.java @@ -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> 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 listener) { + listeners.add(listener); + } + + @Override + public void removeListener(Consumer 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 index 000000000..f329b6994 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/DirectorySizeService.java @@ -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 index 000000000..847bc49f7 --- /dev/null +++ b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeChangeEvent.java @@ -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 index 000000000..f6039f4cb --- /dev/null +++ b/bundles/org.simantics.filesystem.services/src/org/simantics/filesystem/services/sizetracker/SizeTracker.java @@ -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 listener); + void removeListener(Consumer listener); + +} diff --git a/bundles/pom.xml b/bundles/pom.xml index 502f0f41d..04328c95b 100644 --- a/bundles/pom.xml +++ b/bundles/pom.xml @@ -123,6 +123,7 @@ org.simantics.export.core org.simantics.export.ui org.simantics.fastlz + org.simantics.filesystem.services org.simantics.fileimport org.simantics.fileimport.ui org.simantics.g2d diff --git a/features/org.simantics.sdk.feature/feature.xml b/features/org.simantics.sdk.feature/feature.xml index 61bb1e5b5..7a6d287ff 100644 --- a/features/org.simantics.sdk.feature/feature.xml +++ b/features/org.simantics.sdk.feature/feature.xml @@ -214,6 +214,13 @@ version="0.0.0" unpack="false"/> + +