You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/05/18 14:03:21 UTC

[sling-org-apache-sling-installer-provider-file] branch feature/optionally-create-directory-to-monitor created (now 4750aa9)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a change to branch feature/optionally-create-directory-to-monitor
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-provider-file.git.


      at 4750aa9  SLING-7504 optionally create directories to monitor

This branch includes the following new commits:

     new 4750aa9  SLING-7504 optionally create directories to monitor

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[sling-org-apache-sling-installer-provider-file] 01/01: SLING-7504 optionally create directories to monitor

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/optionally-create-directory-to-monitor
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-provider-file.git

commit 4750aa9ae206da5c2abb45716546aafe0770be73
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue May 18 16:03:07 2021 +0200

    SLING-7504 optionally create directories to monitor
---
 .../installer/provider/file/impl/Activator.java    | 29 +++++++++++-----------
 .../provider/file/impl/FileInstaller.java          | 13 +++++++++-
 .../provider/file/impl/ServicesListener.java       | 11 ++++----
 3 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/sling/installer/provider/file/impl/Activator.java b/src/main/java/org/apache/sling/installer/provider/file/impl/Activator.java
index afeeb85..bd11c77 100644
--- a/src/main/java/org/apache/sling/installer/provider/file/impl/Activator.java
+++ b/src/main/java/org/apache/sling/installer/provider/file/impl/Activator.java
@@ -33,9 +33,14 @@ import org.osgi.framework.Constants;
 @Header(name = Constants.BUNDLE_ACTIVATOR, value = "${@class}")
 public class Activator implements BundleActivator {
 
+    /** Comma separated list of directories to monitor */
     public static final String KEY_DIR = "sling.fileinstall.dir";
+    /** The interval in milleseconds between two consecutive polls */
     public static final String KEY_DELAY = "sling.fileinstall.interval";
+    /**  If {@code true} OSGi configurations coming from the file system should be written back to the file system after modification */
     public static final String KEY_WRITEBACK = "sling.fileinstall.writeback";
+    /** If {@code true} the monitored directories are created if not yet existing during start */
+    public static final String KEY_AUTOCREATE_DIR = "sling.fileinstall.dir.autocreate";
 
     /** The services listener will activate the installer. */
     private ServicesListener servicesListener;
@@ -45,17 +50,13 @@ public class Activator implements BundleActivator {
      */
     public void start(final BundleContext context) {
         // read initial scan configurations
-        final List<ScanConfiguration> configs = new ArrayList<ScanConfiguration>();
+        final List<ScanConfiguration> configs = new ArrayList<>();
         final Object dir = getProp(context, KEY_DIR);
         if ( dir != null ) {
             Long delay = null;
-            final Object interval = getProp(context, KEY_DELAY);
+            final String interval = getProp(context, KEY_DELAY);
             if ( interval != null ) {
-                if ( interval instanceof Number ) {
-                    delay = ((Number)interval).longValue();
-                } else {
-                    delay = Long.valueOf(interval.toString());
-                }
+                delay = Long.valueOf(interval.toString());
             }
             final StringTokenizer st = new StringTokenizer(dir.toString(), ",");
             while ( st.hasMoreTokens() ) {
@@ -77,14 +78,12 @@ public class Activator implements BundleActivator {
         this.servicesListener = null;
     }
 
-    public static Object getProp(final BundleContext bundleContext, final String key) {
-        Object o = bundleContext.getProperty(key);
-        if (o == null) {
-            o = System.getProperty(key);
-            if ( o == null ) {
-                o = System.getProperty(key.toUpperCase().replace('.', '_'));
-            }
+    public static String getProp(final BundleContext bundleContext, final String key) {
+        // this already falls back to system properties
+        String value = bundleContext.getProperty(key);
+        if (value == null) {
+            value = System.getProperty(key.toUpperCase().replace('.', '_'));
         }
-        return o;
+        return value;
     }
 }
diff --git a/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java b/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java
index 44c02b1..6940dea 100644
--- a/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java
+++ b/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java
@@ -26,6 +26,8 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.math.BigInteger;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.security.MessageDigest;
 import java.util.ArrayList;
 import java.util.Dictionary;
@@ -65,9 +67,11 @@ public class FileInstaller
     private final List<FileMonitor> monitors = new ArrayList<FileMonitor>();
 
     private final boolean writeBack;
+    private final boolean autoCreateDirectories;
 
-    public FileInstaller(final List<ScanConfiguration> configs, final boolean writeBack) {
+    public FileInstaller(final List<ScanConfiguration> configs, final boolean writeBack, boolean autoCreateDirectories) {
         this.writeBack = writeBack;
+        this.autoCreateDirectories = autoCreateDirectories;
         if ( configs != null ) {
             scanConfigurations.addAll(configs);
         }
@@ -84,6 +88,13 @@ public class FileInstaller
                 key = "${sling.home}" + key.substring(settings.getSlingHomePath().length());
             }
             logger.debug("Starting monitor for {}", config.directory);
+            if (autoCreateDirectories) {
+                try {
+                    Files.createDirectory(Paths.get(config.directory));
+                } catch (IOException e) {
+                    logger.warn("Could not create directory to monitor at {}", config.directory, e);
+                }
+            }
             this.monitors.add(new FileMonitor(new File(config.directory),
                     config.scanInterval, new Installer(installer, settings, config.directory, hash(key))));
         }
diff --git a/src/main/java/org/apache/sling/installer/provider/file/impl/ServicesListener.java b/src/main/java/org/apache/sling/installer/provider/file/impl/ServicesListener.java
index 90d016a..5fbb675 100644
--- a/src/main/java/org/apache/sling/installer/provider/file/impl/ServicesListener.java
+++ b/src/main/java/org/apache/sling/installer/provider/file/impl/ServicesListener.java
@@ -64,7 +64,7 @@ public class ServicesListener {
     private final FileInstaller installer;
 
     /** Service registration. */
-    private ServiceRegistration registration;
+    private ServiceRegistration<UpdateHandler> registration;
 
     private boolean running = false;
 
@@ -76,7 +76,8 @@ public class ServicesListener {
         if ( writeBackObj != null && "false".equalsIgnoreCase(writeBackObj.toString())) {
             writeBack = false;
         }
-        this.installer = new FileInstaller(configs, writeBack);
+        boolean autoCreateDirectories = Boolean.parseBoolean(Activator.getProp(this.bundleContext, Activator.KEY_AUTOCREATE_DIR));
+        this.installer = new FileInstaller(configs, writeBack, autoCreateDirectories);
         this.installerListener = new Listener(INSTALLER_SERVICE_NAME);
         this.settingsListener = new Listener(SETTINGS_SERVICE_NAME);
         this.installerListener.start();
@@ -112,12 +113,12 @@ public class ServicesListener {
     private void startScanner(final OsgiInstaller installer, final SlingSettingsService settings) {
         if ( !running ) {
             this.installer.start(installer, settings);
-            final Dictionary<String, Object> props = new Hashtable<String, Object>();
+            final Dictionary<String, Object> props = new Hashtable<>();
             props.put(Constants.SERVICE_DESCRIPTION, "Apache Sling File Installer Controller Service");
             props.put(Constants.SERVICE_VENDOR, VENDOR);
             props.put(UpdateHandler.PROPERTY_SCHEMES, this.installer.getSchemes());
 
-            this.registration = this.bundleContext.registerService(UpdateHandler.class.getName(),
+            this.registration = this.bundleContext.registerService(UpdateHandler.class,
                     this.installer, props);
             running = true;
         }
@@ -138,7 +139,7 @@ public class ServicesListener {
 
         private final String serviceName;
 
-        private ServiceReference reference;
+        private ServiceReference<?> reference;
         private Object service;
 
         public Listener(final String serviceName) {