incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r955916 - in /sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl: Activator.java OsgiInstallerImpl.java
Date Fri, 18 Jun 2010 09:49:29 GMT
Author: cziegeler
Date: Fri Jun 18 09:49:29 2010
New Revision: 955916

URL: http://svn.apache.org/viewvc?rev=955916&view=rev
Log:
SLING-1560 : Improve and clean up code
Move all tracker code into service to hide implementation details
Don't throw an exception on shutdown

Modified:
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java

Modified: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java?rev=955916&r1=955915&r2=955916&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
(original)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/Activator.java
Fri Jun 18 09:49:29 2010
@@ -30,21 +30,11 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.FrameworkListener;
 import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.log.LogService;
-import org.osgi.service.packageadmin.PackageAdmin;
-import org.osgi.service.startlevel.StartLevel;
-import org.osgi.util.tracker.ServiceTracker;
 
 public class Activator implements BundleActivator, FrameworkListener, BundleListener {
 
     private static final String VENDOR = "The Apache Software Foundation";
 
-    private static String PACKAGE_ADMIN_NAME = PackageAdmin.class.getName();
-    private static String START_LEVEL_NAME = StartLevel.class.getName();
-    private static String LOG_SERVICE_NAME = LogService.class.getName();
-
-    private ServiceTracker packageAdminTracker;
-    private ServiceTracker logServiceTracker;
     private OsgiInstallerImpl osgiControllerService;
     private ServiceRegistration osgiControllerServiceReg;
     private ServiceRegistration factoryServiceReg;
@@ -55,11 +45,6 @@ public class Activator implements Bundle
      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
      */
     public void start(BundleContext context) throws Exception {
-        this.packageAdminTracker = new ServiceTracker(context, PACKAGE_ADMIN_NAME, null);
-        this.logServiceTracker = new ServiceTracker(context, LOG_SERVICE_NAME, null);
-        this.packageAdminTracker.open();
-        this.logServiceTracker.open();
-
         // listen to framework and bundle events
         context.addFrameworkListener(this);
         context.addBundleListener(this);
@@ -70,11 +55,7 @@ public class Activator implements Bundle
             props.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Install Controller Service");
             props.put(Constants.SERVICE_VENDOR, VENDOR);
 
-            // Assume PackageAdmin is available before this bundle is started.
-            // That's the case when using Felix OSGi, not sure about other frameworks.
-            this.osgiControllerService = new OsgiInstallerImpl(context,
-                    (PackageAdmin)checkNotNull(this.packageAdminTracker.getService(), "PackageAdmin"),
-                    logServiceTracker);
+            this.osgiControllerService = new OsgiInstallerImpl(context);
             final String [] serviceInterfaces = {
                     OsgiInstaller.class.getName()
             };
@@ -99,14 +80,6 @@ public class Activator implements Bundle
         }
     }
 
-    /** Complain if value is null */
-    static Object checkNotNull(Object value, String what) {
-    	if(value == null) {
-    		throw new IllegalArgumentException(what + " is null, cannot activate");
-    	}
-    	return value;
-    }
-
     /**
      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
      */
@@ -126,14 +99,6 @@ public class Activator implements Bundle
             this.osgiControllerService.deactivate();
             this.osgiControllerService = null;
         }
-        if ( this.packageAdminTracker != null ) {
-            this.packageAdminTracker.close();
-            this.packageAdminTracker = null;
-        }
-        if ( this.logServiceTracker != null ) {
-            this.logServiceTracker.close();
-            this.logServiceTracker = null;
-        }
     }
 
     /** Used for tasks that wait for a framework or bundle event before retrying their operations
*/

Modified: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java?rev=955916&r1=955915&r2=955916&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
(original)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
Fri Jun 18 09:49:29 2010
@@ -26,7 +26,6 @@ import org.apache.sling.osgi.installer.I
 import org.apache.sling.osgi.installer.OsgiInstaller;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.log.LogService;
@@ -36,22 +35,45 @@ import org.osgi.util.tracker.ServiceTrac
 /** OsgiInstaller service implementation */
 public class OsgiInstallerImpl implements OsgiInstaller, OsgiInstallerContext {
 
+    /** Interface of the package admin */
+    private static String PACKAGE_ADMIN_NAME = PackageAdmin.class.getName();
+    /** Interface of the log service */
+    private static String LOG_SERVICE_NAME = LogService.class.getName();
+    /** Interface of the config admin */
+    private static String CONFIG_ADMIN_SERVICE_NAME = ConfigurationAdmin.class.getName();
+
     public static final String MAVEN_SNAPSHOT_MARKER = "SNAPSHOT";
 
+    /** The bundle context. */
 	private final BundleContext bundleContext;
-    private final PackageAdmin packageAdmin;
-    private final ServiceTracker logServiceTracker;
+
+	/** The actual worker thread. */
     private final OsgiInstallerThread installerThread;
+
     private long [] counters = new long[COUNTERS_SIZE];
     private PersistentBundleInfo bundleDigestsStorage;
 
-    public OsgiInstallerImpl(final BundleContext bc,
-                              final PackageAdmin pa,
-                              final ServiceTracker logServiceTracker)
+    /** Tracker for the package admin. */
+    private final ServiceTracker packageAdminTracker;
+    /** Tracker for the log service. */
+    private final ServiceTracker logServiceTracker;
+    /** Tracker for the configuration admin. */
+    private final ServiceTracker configAdminServiceTracker;
+
+    /**
+     * Construct a new service
+     */
+    public OsgiInstallerImpl(final BundleContext bc)
     throws IOException {
         this.bundleContext = bc;
-        this.packageAdmin = pa;
-        this.logServiceTracker = logServiceTracker;
+        // create and start tracker
+        this.packageAdminTracker = new ServiceTracker(bc, PACKAGE_ADMIN_NAME, null);
+        this.logServiceTracker = new ServiceTracker(bc, LOG_SERVICE_NAME, null);
+        this.configAdminServiceTracker = new ServiceTracker(bc, CONFIG_ADMIN_SERVICE_NAME,
null);
+        this.packageAdminTracker.open();
+        this.logServiceTracker.open();
+        this.configAdminServiceTracker.open();
+
         bundleDigestsStorage = new PersistentBundleInfo(this, bc.getDataFile("bundle-digests.properties"));
 
         installerThread = new OsgiInstallerThread(this);
@@ -59,30 +81,42 @@ public class OsgiInstallerImpl implement
         installerThread.start();
     }
 
-    public void deactivate() throws InterruptedException, IOException {
+    /**
+     * Deactivate this service.
+     */
+    public void deactivate() {
         installerThread.deactivate();
 
         final TreeSet<String> installedBundlesSymbolicNames = new TreeSet<String>();
         for(Bundle b : bundleContext.getBundles()) {
             installedBundlesSymbolicNames.add(b.getSymbolicName());
         }
-        bundleDigestsStorage.purgeAndSave(installedBundlesSymbolicNames);
+        try {
+            bundleDigestsStorage.purgeAndSave(installedBundlesSymbolicNames);
+        } catch (IOException e) {
+            logWarn(OsgiInstaller.class.getName() + " service failed to save state.", e);
+        }
 
         this.logInfo("Waiting for installer thread to stop");
-        installerThread.join();
+        try {
+            installerThread.join();
+        } catch (InterruptedException e) {
+            // we simply ignore this
+        }
+
+        this.packageAdminTracker.close();
+        this.logServiceTracker.close();
+        this.configAdminServiceTracker.close();
 
         this.logWarn(OsgiInstaller.class.getName()
                     + " service deactivated - this warning can be ignored if system is shutting
down");
     }
 
+	/**
+	 * @see org.apache.sling.osgi.installer.impl.OsgiInstallerContext#getConfigurationAdmin()
+	 */
 	public ConfigurationAdmin getConfigurationAdmin() {
-		if(bundleContext != null) {
-		   	final ServiceReference ref = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
-		    if(ref != null) {
-		    	return (ConfigurationAdmin)bundleContext.getService(ref);
-		    }
-		}
-		return null;
+	    return (ConfigurationAdmin)this.configAdminServiceTracker.getService();
 	}
 
 	public void addTaskToCurrentCycle(OsgiInstallerTask t) {
@@ -94,12 +128,18 @@ public class OsgiInstallerImpl implement
 		installerThread.addTaskToNextCycle(t);
 	}
 
+	/**
+	 * @see org.apache.sling.osgi.installer.impl.OsgiInstallerContext#getBundleContext()
+	 */
 	public BundleContext getBundleContext() {
 		return bundleContext;
 	}
 
+	/**
+	 * @see org.apache.sling.osgi.installer.impl.OsgiInstallerContext#getPackageAdmin()
+	 */
 	public PackageAdmin getPackageAdmin() {
-		return packageAdmin;
+		return (PackageAdmin)this.packageAdminTracker.getService();
 	}
 
 	public long [] getCounters() {



Mime
View raw message