You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:43:20 UTC

[sling-org-apache-sling-installer-provider-file] 06/19: Fixstartup problems and potential NPEs

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 8233905e85de5940e61d16b856b1925c85dc7ab6
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Sep 14 16:43:38 2010 +0000

    Fixstartup problems and potential NPEs
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/installer/fileinstall@996983 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/installer/file/impl/Activator.java       |  3 +--
 .../sling/installer/file/impl/FileMonitor.java     |  7 ++++--
 .../installer/file/impl/ServicesListener.java      | 28 +++++++++++++++++-----
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/sling/installer/file/impl/Activator.java b/src/main/java/org/apache/sling/installer/file/impl/Activator.java
index 9702cbe..155b492 100644
--- a/src/main/java/org/apache/sling/installer/file/impl/Activator.java
+++ b/src/main/java/org/apache/sling/installer/file/impl/Activator.java
@@ -24,7 +24,6 @@ import java.util.StringTokenizer;
 
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
 
 /**
  * The <code>Activator</code>
@@ -40,7 +39,7 @@ public class Activator implements BundleActivator {
     /**
      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
      */
-    public void start(final BundleContext context) throws InvalidSyntaxException {
+    public void start(final BundleContext context) {
         // read initial scan configurations
         final List<ScanConfiguration> configs = new ArrayList<ScanConfiguration>();
         final Object dir = this.getProp(context, KEY_DIR);
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 875a256..e5ee425 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
@@ -62,8 +62,11 @@ public class FileMonitor extends TimerTask {
     private void collect(final File file, final List<File> files) {
         if ( file.exists() ) {
             if ( file.isDirectory() ) {
-                for(final File child : file.listFiles() ) {
-                    collect(child, files);
+                final File[] children = file.listFiles();
+                if ( children != null ) {
+                    for(final File child : children ) {
+                        collect(child, files);
+                    }
                 }
             } else {
                 files.add(file);
diff --git a/src/main/java/org/apache/sling/installer/file/impl/ServicesListener.java b/src/main/java/org/apache/sling/installer/file/impl/ServicesListener.java
index 72dd79c..abcd5ec 100644
--- a/src/main/java/org/apache/sling/installer/file/impl/ServicesListener.java
+++ b/src/main/java/org/apache/sling/installer/file/impl/ServicesListener.java
@@ -31,6 +31,8 @@ import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The <code>ServicesListener</code> listens for the required services
@@ -39,6 +41,9 @@ import org.osgi.framework.ServiceReference;
  */
 public class ServicesListener {
 
+    /** The logger. */
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
     /** The name of the installer service. */
     private static final String INSTALLER_SERVICE_NAME = OsgiInstaller.class.getName();
 
@@ -57,13 +62,13 @@ public class ServicesListener {
     private boolean running = false;
 
     public ServicesListener(final BundleContext bundleContext,
-            final List<ScanConfiguration> configs)
-    throws InvalidSyntaxException{
+            final List<ScanConfiguration> configs) {
         if ( configs != null ) {
             scanConfigurations.addAll(configs);
         }
         this.bundleContext = bundleContext;
         this.installerListener = new Listener(INSTALLER_SERVICE_NAME);
+        this.installerListener.start();
     }
 
     public synchronized void notifyChange() {
@@ -72,8 +77,10 @@ public class ServicesListener {
             final OsgiInstaller installer = (OsgiInstaller)this.installerListener.getService();
 
             if ( installer != null&& !running ) {
+                logger.debug("Starting scanner");
                 this.startScanner(installer);
             } else if ( running && installer == null ) {
+                logger.debug("Stopping scanner");
                 this.stopScanner();
             }
         }
@@ -90,6 +97,7 @@ public class ServicesListener {
     private void startScanner(final OsgiInstaller installer) {
         if ( !running ) {
             for(final ScanConfiguration config : this.scanConfigurations) {
+                logger.debug("Starting monitor for {}", config.directory);
                 this.monitors.add(new FileMonitor(new File(config.directory),
                         config.scanInterval, new Installer(installer, hash(config.directory))));
             }
@@ -114,11 +122,19 @@ public class ServicesListener {
         private ServiceReference reference;
         private Object service;
 
-        public Listener(final String serviceName) throws InvalidSyntaxException {
+        public Listener(final String serviceName) {
             this.serviceName = serviceName;
-            bundleContext.addServiceListener(this, "("
-                    + Constants.OBJECTCLASS + "=" + serviceName + ")");
-            this.getService();
+        }
+
+        public void start() {
+            this.retainService();
+            try {
+                bundleContext.addServiceListener(this, "("
+                        + Constants.OBJECTCLASS + "=" + serviceName + ")");
+            } catch (final InvalidSyntaxException ise) {
+                // this should really never happen
+                throw new RuntimeException("Unexpected exception occured.", ise);
+            }
         }
 
         public void deactivate() {

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.