incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r791818 - in /sling/trunk/contrib/extensions/jcrinstall/osgi/src: main/java/org/apache/sling/osgi/installer/ main/java/org/apache/sling/osgi/installer/impl/ test/java/org/apache/sling/osgi/installer/impl/
Date Tue, 07 Jul 2009 12:50:21 GMT
Author: bdelacretaz
Date: Tue Jul  7 12:50:20 2009
New Revision: 791818

URL: http://svn.apache.org/viewvc?rev=791818&view=rev
Log:
SLING-905 - more defensive use of external services

Modified:
    sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/OsgiControllerServices.java
    sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
    sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java
    sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigResourceProcessorTest.java

Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/OsgiControllerServices.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/OsgiControllerServices.java?rev=791818&r1=791817&r2=791818&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/OsgiControllerServices.java
(original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/OsgiControllerServices.java
Tue Jul  7 12:50:20 2009
@@ -19,6 +19,7 @@
 package org.apache.sling.osgi.installer;
 
 import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.log.LogService;
 
 /** Proxy for services that might not be always available, allows
  * 	classes which are not OSGi services to access such services easily.
@@ -27,4 +28,5 @@
  */
 public interface OsgiControllerServices {
 	ConfigurationAdmin getConfigurationAdmin();
+	LogService getLogService();
 }

Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java?rev=791818&r1=791817&r2=791818&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
(original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
Tue Jul  7 12:50:20 2009
@@ -20,6 +20,7 @@
 
 import java.util.Hashtable;
 
+import org.apache.sling.osgi.installer.JcrInstallException;
 import org.apache.sling.osgi.installer.OsgiController;
 import org.apache.sling.osgi.installer.OsgiControllerServices;
 import org.osgi.framework.BundleActivator;
@@ -62,15 +63,26 @@
         final Hashtable<String, String> props = new Hashtable<String, String>();
         props.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Install Controller Service");
         props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        
+        // Assume PackageAdmin is available before this bundle is started.
+        // That's the case when using Felix OSGi, not sure about other frameworks.
         this.service = new OsgiControllerImpl(context,
-                (PackageAdmin)this.packageAdminTracker.getService(),
-                (LogService)this.logServiceTracker.getService());
+                (PackageAdmin)checkNotNull(this.packageAdminTracker.getService(), "PackageAdmin"),
+                logServiceTracker);
         final String [] serviceInterfaces = {
         		OsgiController.class.getName(),
         		OsgiControllerServices.class.getName()
         };
         serviceReg = context.registerService(serviceInterfaces, service, props);
     }
+    
+    /** Complain if value is null */
+    static Object checkNotNull(Object value, String what) throws JcrInstallException {
+    	if(value == null) {
+    		throw new JcrInstallException(what + " is null, cannot activate");
+    	}
+    	return value;
+    }
 
     /**
      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)

Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java?rev=791818&r1=791817&r2=791818&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java
(original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java
Tue Jul  7 12:50:20 2009
@@ -35,6 +35,7 @@
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.log.LogService;
 import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.util.tracker.ServiceTracker;
 
 /**
  * OsgiController service
@@ -51,6 +52,7 @@
     private ResourceOverrideRules roRules;
     private final List<OsgiControllerTask> tasks = new LinkedList<OsgiControllerTask>();
     private final OsgiControllerTaskExecutor executor = new OsgiControllerTaskExecutor();
+    private final ServiceTracker logServiceTracker;
 
     public static final String STORAGE_FILENAME = "controller.storage";
 
@@ -59,18 +61,16 @@
 
     private final PackageAdmin packageAdmin;
 
-    protected final LogService logService;
-
     /** Default value for getLastModified() */
     public static final long LAST_MODIFIED_NOT_FOUND = -1;
 
     public OsgiControllerImpl(final BundleContext bc,
                               final PackageAdmin pa,
-                              final LogService ls)
+                              final ServiceTracker logServiceTracker)
     throws IOException {
         this.bundleContext = bc;
         this.packageAdmin = pa;
-        this.logService = ls;
+        this.logServiceTracker = logServiceTracker;
         processors = new OsgiResourceProcessorList(bc, packageAdmin, this);
         storage = new Storage(bc.getDataFile(STORAGE_FILENAME));
     }
@@ -79,8 +79,8 @@
         try {
             storage.saveToFile();
         } catch(IOException ioe) {
-        	if (logService != null) {
-        		logService.log(LogService.LOG_WARNING, "IOException in Storage.saveToFile()", ioe);
+        	if (getLogService() != null) {
+        		getLogService().log(LogService.LOG_WARNING, "IOException in Storage.saveToFile()",
ioe);
         	}
         }
 
@@ -88,8 +88,8 @@
             processor.dispose();
         }
 
-        if(logService != null) {
-            logService.log(LogService.LOG_WARNING,
+        if(getLogService() != null) {
+            getLogService().log(LogService.LOG_WARNING,
                     OsgiController.class.getName()
                     + " service deactivated - this warning can be ignored if system is shutting
down");
         }
@@ -134,8 +134,8 @@
 
     	// Ready to work?
         if(processors == null) {
-        	if(logService != null) {
-                logService.log(LogService.LOG_INFO, "Not activated yet, cannot executeScheduledOperations");
+        	if(getLogService() != null) {
+                getLogService().log(LogService.LOG_INFO, "Not activated yet, cannot executeScheduledOperations");
         	}
             return;
         }
@@ -153,8 +153,8 @@
             }
 
             // Now execute all our tasks in a separate thread
-        	if(logService != null) {
-                logService.log(LogService.LOG_INFO, "Executing " + tasks.size() + " queued
tasks");
+        	if(getLogService() != null) {
+                getLogService().log(LogService.LOG_INFO, "Executing " + tasks.size() + "
queued tasks");
         	}
             final long start = System.currentTimeMillis();
 
@@ -164,8 +164,8 @@
             tasks.clear();
             tasks.addAll(remainingTasks);
 
-        	if(logService != null) {
-                logService.log(LogService.LOG_INFO,
+        	if(getLogService() != null) {
+                getLogService().log(LogService.LOG_INFO,
                 		"Done executing queued tasks (" + (System.currentTimeMillis() - start)
+ " msec)");
         	}
 		}
@@ -198,4 +198,8 @@
 	public Storage getStorage() {
 		return storage;
 	}
+	
+	public LogService getLogService() {
+		return (LogService)logServiceTracker.getService();
+	}
 }
\ No newline at end of file

Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigResourceProcessorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigResourceProcessorTest.java?rev=791818&r1=791817&r2=791818&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigResourceProcessorTest.java
(original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigResourceProcessorTest.java
Tue Jul  7 12:50:20 2009
@@ -28,6 +28,7 @@
 import org.jmock.Mockery;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.log.LogService;
 
 public class ConfigResourceProcessorTest {
     
@@ -46,6 +47,9 @@
 			public ConfigurationAdmin getConfigurationAdmin() {
 				return ca;
 			}
+			public LogService getLogService() {
+				return null;
+			}
         };
         final ConfigResourceProcessor p = new ConfigResourceProcessor(sp);
         final Configuration c = mockery.mock(Configuration.class);



Mime
View raw message