Return-Path: X-Original-To: apmail-karaf-commits-archive@minotaur.apache.org Delivered-To: apmail-karaf-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C808C10C47 for ; Tue, 1 Apr 2014 11:44:06 +0000 (UTC) Received: (qmail 54006 invoked by uid 500); 1 Apr 2014 11:44:06 -0000 Delivered-To: apmail-karaf-commits-archive@karaf.apache.org Received: (qmail 53928 invoked by uid 500); 1 Apr 2014 11:44:00 -0000 Mailing-List: contact commits-help@karaf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@karaf.apache.org Delivered-To: mailing list commits@karaf.apache.org Received: (qmail 53315 invoked by uid 99); 1 Apr 2014 11:43:36 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Apr 2014 11:43:36 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 6E41791B8C5; Tue, 1 Apr 2014 11:43:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: gnodet@apache.org To: commits@karaf.apache.org Date: Tue, 01 Apr 2014 11:43:37 -0000 Message-Id: <1e7790ac33ba4da18831037f0876348c@git.apache.org> In-Reply-To: <9402365d797e4b5da251b415b3fc6ef3@git.apache.org> References: <9402365d797e4b5da251b415b3fc6ef3@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [04/11] git commit: [KARAF-2833] Refactor activators with a single base class [KARAF-2833] Refactor activators with a single base class Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/c57532de Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/c57532de Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/c57532de Branch: refs/heads/master Commit: c57532de5a248d1cbc9b42bc604a30df0b219277 Parents: 166b9c6 Author: Guillaume Nodet Authored: Sat Mar 29 00:38:34 2014 +0100 Committer: Guillaume Nodet Committed: Tue Apr 1 10:14:56 2014 +0200 ---------------------------------------------------------------------- .../bundle/core/internal/osgi/Activator.java | 124 +------ .../karaf/config/core/impl/osgi/Activator.java | 122 +------ .../karaf/deployer/kar/osgi/Activator.java | 55 +-- .../karaf/features/internal/osgi/Activator.java | 179 ++-------- instance/core/pom.xml | 3 +- .../instance/core/internal/osgi/Activator.java | 57 +--- .../karaf/kar/internal/osgi/Activator.java | 158 +-------- .../karaf/log/core/internal/osgi/Activator.java | 196 ++--------- .../karaf/management/internal/Activator.java | 238 ++----------- package/core/pom.xml | 7 + .../packages/core/internal/osgi/Activator.java | 60 +--- service/core/pom.xml | 9 +- .../service/core/internal/osgi/Activator.java | 44 +-- system/core/pom.xml | 5 + .../karaf/system/internal/osgi/Activator.java | 63 +--- util/pom.xml | 9 + .../karaf/util/tracker/BaseActivator.java | 337 +++++++++++++++++++ wrapper/core/pom.xml | 3 +- .../karaf/wrapper/internal/osgi/Activator.java | 60 +--- 19 files changed, 531 insertions(+), 1198 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/osgi/Activator.java b/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/osgi/Activator.java index df3899e..6999485 100644 --- a/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/osgi/Activator.java +++ b/bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/osgi/Activator.java @@ -16,16 +16,6 @@ */ package org.apache.karaf.bundle.core.internal.osgi; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; -import java.util.Properties; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import javax.management.NotCompliantMBeanException; - import org.apache.karaf.bundle.core.BundleService; import org.apache.karaf.bundle.core.BundleStateService; import org.apache.karaf.bundle.core.BundleWatcher; @@ -33,56 +23,31 @@ import org.apache.karaf.bundle.core.internal.BundleServiceImpl; import org.apache.karaf.bundle.core.internal.BundleWatcherImpl; import org.apache.karaf.bundle.core.internal.BundlesMBeanImpl; import org.apache.karaf.bundle.core.internal.MavenConfigService; -import org.apache.karaf.util.tracker.SingleServiceTracker; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; +import org.apache.karaf.util.tracker.BaseActivator; import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.util.tracker.ServiceTracker; import org.osgi.util.tracker.ServiceTrackerCustomizer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Activator implements BundleActivator, SingleServiceTracker.SingleServiceListener { - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); +public class Activator extends BaseActivator { - private ExecutorService executor = Executors.newSingleThreadExecutor(); - private BundleContext bundleContext; - private SingleServiceTracker configurationAdminTracker; private ServiceTracker bundleStateServicesTracker; - private BundleWatcherImpl bundleWatcher; - private ServiceRegistration bundleWatcherRegistration; - private ServiceRegistration bundleServiceRegistration; - private ServiceRegistration bundleServiceMBeanRegistration; @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - configurationAdminTracker = new SingleServiceTracker( - bundleContext, ConfigurationAdmin.class, this - ); - configurationAdminTracker.open(); + protected void doOpen() throws Exception { + trackService(ConfigurationAdmin.class); } @Override - public void stop(BundleContext context) throws Exception { - configurationAdminTracker.close(); - executor.shutdown(); - executor.awaitTermination(30, TimeUnit.SECONDS); - } - - protected void doStart() { - ConfigurationAdmin configurationAdmin = configurationAdminTracker.getService(); - + protected void doStart() throws Exception { + ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class); if (configurationAdmin == null) { return; } final BundleServiceImpl bundleService = new BundleServiceImpl(bundleContext); - bundleServiceRegistration = bundleContext.registerService(BundleService.class, bundleService, null); + register(BundleService.class, bundleService); bundleStateServicesTracker = new ServiceTracker( bundleContext, BundleStateService.class, new ServiceTrackerCustomizer() { @Override @@ -105,88 +70,23 @@ public class Activator implements BundleActivator, SingleServiceTracker.SingleSe bundleWatcher = new BundleWatcherImpl(bundleContext, new MavenConfigService(configurationAdmin), bundleService); bundleWatcher.start(); - bundleWatcherRegistration = bundleContext.registerService(BundleWatcher.class, bundleWatcher, null); + register(BundleWatcher.class, bundleWatcher); - try { - BundlesMBeanImpl bundlesMBeanImpl = new BundlesMBeanImpl(bundleContext, bundleService); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=bundle,name=" + System.getProperty("karaf.name")); - bundleServiceMBeanRegistration = bundleContext.registerService( - getInterfaceNames(bundlesMBeanImpl), - bundlesMBeanImpl, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating FeaturesService mbean", e); - } + BundlesMBeanImpl bundlesMBeanImpl = new BundlesMBeanImpl(bundleContext, bundleService); + registerMBean(bundlesMBeanImpl, "type=bundle"); } + @Override protected void doStop() { if (bundleStateServicesTracker != null) { bundleStateServicesTracker.close(); bundleStateServicesTracker = null; } + super.doStop(); if (bundleWatcher != null) { bundleWatcher.stop(); bundleWatcher = null; } - if (bundleServiceMBeanRegistration != null) { - bundleServiceMBeanRegistration.unregister(); - bundleServiceMBeanRegistration = null; - } - if (bundleServiceRegistration != null) { - bundleServiceRegistration.unregister(); - bundleServiceRegistration = null; - } - if (bundleWatcherRegistration != null) { - bundleWatcherRegistration.unregister(); - bundleWatcherRegistration = null; - } - if (bundleWatcher != null) { - bundleWatcher.stop(); - bundleWatcher = null; - } - } - - @Override - public void serviceFound() { - executor.submit(new Runnable() { - @Override - public void run() { - doStop(); - try { - doStart(); - } catch (Exception e) { - LOGGER.warn("Error starting FeaturesService", e); - doStop(); - } - } - }); - } - - @Override - public void serviceLost() { - serviceFound(); - } - - @Override - public void serviceReplaced() { - serviceFound(); - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } - - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } } } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/config/core/src/main/java/org/apache/karaf/config/core/impl/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/config/core/src/main/java/org/apache/karaf/config/core/impl/osgi/Activator.java b/config/core/src/main/java/org/apache/karaf/config/core/impl/osgi/Activator.java index e8d3dd8..bdfab09 100644 --- a/config/core/src/main/java/org/apache/karaf/config/core/impl/osgi/Activator.java +++ b/config/core/src/main/java/org/apache/karaf/config/core/impl/osgi/Activator.java @@ -16,133 +16,31 @@ */ package org.apache.karaf.config.core.impl.osgi; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; -import java.util.Properties; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import javax.management.NotCompliantMBeanException; - import org.apache.karaf.config.core.ConfigRepository; import org.apache.karaf.config.core.impl.ConfigMBeanImpl; import org.apache.karaf.config.core.impl.ConfigRepositoryImpl; -import org.apache.karaf.util.tracker.SingleServiceTracker; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; +import org.apache.karaf.util.tracker.BaseActivator; import org.osgi.service.cm.ConfigurationAdmin; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Activator implements BundleActivator, SingleServiceTracker.SingleServiceListener { - - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - private ExecutorService executor = Executors.newSingleThreadExecutor(); - private BundleContext bundleContext; - private SingleServiceTracker configurationAdminTracker; - - private ServiceRegistration configRepositoryRegistration; - private ServiceRegistration configRepositoryMBeanRegistration; - - @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - configurationAdminTracker = new SingleServiceTracker( - bundleContext, ConfigurationAdmin.class, this - ); - configurationAdminTracker.open(); - } +public class Activator extends BaseActivator { @Override - public void stop(BundleContext context) throws Exception { - configurationAdminTracker.close(); - executor.shutdown(); - executor.awaitTermination(30, TimeUnit.SECONDS); + protected void doOpen() throws Exception { + trackService(ConfigurationAdmin.class); } - protected void doStart() { - ConfigurationAdmin configurationAdmin = configurationAdminTracker.getService(); - + protected void doStart() throws Exception { + ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class); if (configurationAdmin == null) { return; } ConfigRepository configRepository = new ConfigRepositoryImpl(configurationAdmin); - configRepositoryRegistration = bundleContext.registerService(ConfigRepository.class, configRepository, null); - - try { - ConfigMBeanImpl configMBean = new ConfigMBeanImpl(); - configMBean.setConfigRepo(configRepository); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=config,name=" + System.getProperty("karaf.name")); - configRepositoryMBeanRegistration = bundleContext.registerService( - getInterfaceNames(configMBean), - configMBean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating ConfigRepository mbean", e); - } - } - - protected void doStop() { - if (configRepositoryRegistration != null) { - configRepositoryRegistration.unregister(); - configRepositoryRegistration = null; - } - if (configRepositoryMBeanRegistration != null) { - configRepositoryMBeanRegistration.unregister(); - configRepositoryMBeanRegistration = null; - } - } - - @Override - public void serviceFound() { - executor.submit(new Runnable() { - @Override - public void run() { - doStop(); - try { - doStart(); - } catch (Exception e) { - LOGGER.warn("Error starting FeaturesService", e); - doStop(); - } - } - }); - } - - @Override - public void serviceLost() { - serviceFound(); - } - - @Override - public void serviceReplaced() { - serviceFound(); - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } - - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } - } + register(ConfigRepository.class, configRepository); - private String getString(Properties configuration, String key, String value) { - return configuration.getProperty(key, value); + ConfigMBeanImpl configMBean = new ConfigMBeanImpl(); + configMBean.setConfigRepo(configRepository); + registerMBean(configMBean, "type=config"); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/deployer/kar/src/main/java/org/apache/karaf/deployer/kar/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/deployer/kar/src/main/java/org/apache/karaf/deployer/kar/osgi/Activator.java b/deployer/kar/src/main/java/org/apache/karaf/deployer/kar/osgi/Activator.java index 1b5038f..ac3b520 100644 --- a/deployer/kar/src/main/java/org/apache/karaf/deployer/kar/osgi/Activator.java +++ b/deployer/kar/src/main/java/org/apache/karaf/deployer/kar/osgi/Activator.java @@ -17,65 +17,28 @@ */ package org.apache.karaf.deployer.kar.osgi; -import java.util.Hashtable; - import org.apache.felix.fileinstall.ArtifactInstaller; import org.apache.felix.fileinstall.ArtifactListener; import org.apache.karaf.deployer.kar.KarArtifactInstaller; import org.apache.karaf.kar.KarService; -import org.apache.karaf.util.tracker.SingleServiceTracker; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; - -public class Activator implements BundleActivator, SingleServiceTracker.SingleServiceListener { - - private BundleContext bundleContext; - private ServiceRegistration urlTransformerRegistration; - private SingleServiceTracker karServiceTracker; +import org.apache.karaf.util.tracker.BaseActivator; - @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - karServiceTracker = new SingleServiceTracker( - context, KarService.class, this); - karServiceTracker.open(); - } +public class Activator extends BaseActivator { @Override - public void stop(BundleContext context) throws Exception { - karServiceTracker.close(); + protected void doOpen() throws Exception { + trackService(KarService.class); } @Override - public void serviceFound() { - KarService service = karServiceTracker.getService(); - if (urlTransformerRegistration == null && service != null) { + protected void doStart() throws Exception { + KarService service = getTrackedService(KarService.class); + if (service != null) { KarArtifactInstaller installer = new KarArtifactInstaller(); installer.setKarService(service); - Hashtable props = new Hashtable(); - urlTransformerRegistration = bundleContext.registerService( - new String[] { - ArtifactInstaller.class.getName(), - ArtifactListener.class.getName() - }, - installer, - null); - } - } - - @Override - public void serviceLost() { - if (urlTransformerRegistration != null) { - urlTransformerRegistration.unregister(); - urlTransformerRegistration = null; + register(new String[] { ArtifactInstaller.class.getName(), ArtifactListener.class.getName() }, + installer); } } - @Override - public void serviceReplaced() { - serviceLost(); - serviceFound(); - } - } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java index 4e6129d..6c59387 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java @@ -19,14 +19,10 @@ package org.apache.karaf.features.internal.osgi; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.util.ArrayList; +import java.util.Dictionary; import java.util.Hashtable; -import java.util.List; import java.util.Properties; import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import javax.management.NotCompliantMBeanException; @@ -40,71 +36,25 @@ import org.apache.karaf.features.internal.FeaturesServiceImpl; import org.apache.karaf.features.internal.PersistentBundleManager; import org.apache.karaf.features.management.internal.FeaturesServiceMBeanImpl; import org.apache.karaf.region.persist.RegionsPersistence; -import org.apache.karaf.util.locks.FileLockUtils; -import org.apache.karaf.util.tracker.SingleServiceTracker; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; +import org.apache.karaf.util.tracker.BaseActivator; import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.cm.ManagedService; import org.osgi.service.url.URLStreamHandlerService; import org.osgi.util.tracker.ServiceTracker; import org.osgi.util.tracker.ServiceTrackerCustomizer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -public class Activator implements BundleActivator, SingleServiceTracker.SingleServiceListener { +public class Activator extends BaseActivator { - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - - private ExecutorService executor = Executors.newSingleThreadExecutor(); - private BundleContext bundleContext; - private SingleServiceTracker regionsPersistenceTracker; - private SingleServiceTracker mvnUrlHandlerTracker; - private SingleServiceTracker configurationAdminTracker; private ServiceTracker featuresListenerTracker; - private FeaturesServiceImpl featuresService; - private ServiceRegistration featureFinderRegistration; - private ServiceRegistration featuresServiceRegistration; - private ServiceRegistration featuresServiceMBeanRegistration; - - @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - regionsPersistenceTracker = new SingleServiceTracker( - bundleContext, "org.apache.karaf.region.persist.RegionsPersistence", this - ); - mvnUrlHandlerTracker = new SingleServiceTracker( - bundleContext, URLStreamHandlerService.class, "(url.handler.protocol=mvn)", this - ); - configurationAdminTracker = new SingleServiceTracker( - bundleContext, ConfigurationAdmin.class, this - ); - regionsPersistenceTracker.open(); - mvnUrlHandlerTracker.open(); - configurationAdminTracker.open(); - } @Override - public void stop(BundleContext context) throws Exception { - configurationAdminTracker.close(); - mvnUrlHandlerTracker.close(); - regionsPersistenceTracker.close(); - executor.shutdown(); - executor.awaitTermination(30, TimeUnit.SECONDS); - } - - protected void doStart() { - ConfigurationAdmin configurationAdmin = configurationAdminTracker.getService(); - Object regionsPersistence = regionsPersistenceTracker.getService(); - URLStreamHandlerService mvnUrlHandler = mvnUrlHandlerTracker.getService(); - - if (configurationAdmin == null || mvnUrlHandler == null) { - return; - } + protected void doOpen() throws Exception { + trackService("org.apache.karaf.region.persist.RegionsPersistence"); + trackService(URLStreamHandlerService.class, "(url.handler.protocol=mvn)"); + trackService(ConfigurationAdmin.class); Properties configuration = new Properties(); File configFile = new File(System.getProperty("karaf.etc"), "org.apache.karaf.features.cfg"); @@ -112,14 +62,25 @@ public class Activator implements BundleActivator, SingleServiceTracker.SingleSe try { configuration.load(new FileReader(configFile)); } catch (IOException e) { - LOGGER.warn("Error reading configuration file " + configFile.toString(), e); + logger.warn("Error reading configuration file " + configFile.toString(), e); } } + updated((Dictionary) configuration); + } + + protected void doStart() throws NotCompliantMBeanException { + ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class); + Object regionsPersistence = getTrackedService("org.apache.karaf.region.persist.RegionsPersistence"); + URLStreamHandlerService mvnUrlHandler = getTrackedService(URLStreamHandlerService.class); + + if (configurationAdmin == null || mvnUrlHandler == null) { + return; + } FeatureFinder featureFinder = new FeatureFinder(); Hashtable props = new Hashtable(); props.put(Constants.SERVICE_PID, "org.apache.karaf.features.repos"); - featureFinderRegistration = bundleContext.registerService(ManagedService.class, featureFinder, props); + register(ManagedService.class, featureFinder, props); BundleManager bundleManager; if (regionsPersistence != null) { @@ -135,11 +96,11 @@ public class Activator implements BundleActivator, SingleServiceTracker.SingleSe bundleManager = new BundleManager(bundleContext); } FeatureConfigInstaller configInstaller = new FeatureConfigInstaller(configurationAdmin); - String featuresRepositories = getString(configuration, "featuresRepositories", ""); - boolean respectStartLvlDuringFeatureStartup = getBoolean(configuration, "respectStartLvlDuringFeatureStartup", true); - boolean respectStartLvlDuringFeatureUninstall = getBoolean(configuration, "respectStartLvlDuringFeatureUninstall", true); - long resolverTimeout = getLong(configuration, "resolverTimeout", 5000); - String overrides = getString(configuration, "overrides", new File(System.getProperty("karaf.etc"), "overrides.properties").toString()); + String featuresRepositories = getString("featuresRepositories", ""); + boolean respectStartLvlDuringFeatureStartup = getBoolean("respectStartLvlDuringFeatureStartup", true); + boolean respectStartLvlDuringFeatureUninstall = getBoolean("respectStartLvlDuringFeatureUninstall", true); + long resolverTimeout = getLong("resolverTimeout", 5000); + String overrides = getString("overrides", new File(System.getProperty("karaf.etc"), "overrides.properties").toString()); featuresService = new FeaturesServiceImpl(bundleManager, configInstaller); featuresService.setUrls(featuresRepositories); featuresService.setRespectStartLvlDuringFeatureStartup(respectStartLvlDuringFeatureStartup); @@ -148,7 +109,7 @@ public class Activator implements BundleActivator, SingleServiceTracker.SingleSe featuresService.setOverrides(overrides); featuresService.setFeatureFinder(featureFinder); featuresService.start(); - featuresServiceRegistration = bundleContext.registerService(FeaturesService.class, featuresService, null); + register(FeaturesService.class, featuresService); featuresListenerTracker = new ServiceTracker( bundleContext, FeaturesListener.class, new ServiceTrackerCustomizer() { @@ -170,25 +131,15 @@ public class Activator implements BundleActivator, SingleServiceTracker.SingleSe ); featuresListenerTracker.open(); - String featuresBoot = getString(configuration, "featuresBoot", ""); - boolean featuresBootAsynchronous = getBoolean(configuration, "featuresBootAsynchronous", false); + String featuresBoot = getString("featuresBoot", ""); + boolean featuresBootAsynchronous = getBoolean("featuresBootAsynchronous", false); BootFeaturesInstaller bootFeaturesInstaller = new BootFeaturesInstaller(bundleContext, featuresService, featuresBoot, featuresBootAsynchronous); bootFeaturesInstaller.start(); - try { - FeaturesServiceMBeanImpl featuresServiceMBean = new FeaturesServiceMBeanImpl(); - featuresServiceMBean.setBundleContext(bundleContext); - featuresServiceMBean.setFeaturesService(featuresService); - props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=feature,name=" + System.getProperty("karaf.name")); - featuresServiceMBeanRegistration = bundleContext.registerService( - getInterfaceNames(featuresServiceMBean), - featuresServiceMBean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating FeaturesService mbean", e); - } + FeaturesServiceMBeanImpl featuresServiceMBean = new FeaturesServiceMBeanImpl(); + featuresServiceMBean.setBundleContext(bundleContext); + featuresServiceMBean.setFeaturesService(featuresService); + registerMBean(featuresServiceMBean, "type=feature"); } protected void doStop() { @@ -196,75 +147,11 @@ public class Activator implements BundleActivator, SingleServiceTracker.SingleSe featuresListenerTracker.close(); featuresListenerTracker = null; } - if (featureFinderRegistration != null) { - featureFinderRegistration.unregister(); - featureFinderRegistration = null; - } - if (featuresServiceRegistration != null) { - featuresServiceRegistration.unregister(); - featuresServiceRegistration = null; - } - if (featuresServiceMBeanRegistration != null) { - featuresServiceMBeanRegistration.unregister(); - featuresServiceMBeanRegistration = null; - } + super.doStop(); if (featuresService != null) { featuresService.stop(); featuresService = null; } } - @Override - public void serviceFound() { - executor.submit(new Runnable() { - @Override - public void run() { - doStop(); - try { - doStart(); - } catch (Throwable t) { - LOGGER.warn("Error starting FeaturesService", t); - doStop(); - } - } - }); - } - - @Override - public void serviceLost() { - serviceFound(); - } - - @Override - public void serviceReplaced() { - serviceFound(); - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } - - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } - } - - private String getString(Properties configuration, String key, String value) { - return configuration.getProperty(key, value); - } - - private boolean getBoolean(Properties configuration, String key, boolean value) { - return Boolean.parseBoolean(getString(configuration, key, Boolean.toString(value))); - } - - private long getLong(Properties configuration, String key, long value) { - return Long.parseLong(getString(configuration, key, Long.toString(value))); - } - } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/instance/core/pom.xml ---------------------------------------------------------------------- diff --git a/instance/core/pom.xml b/instance/core/pom.xml index a7e69eb..4c745ec 100644 --- a/instance/core/pom.xml +++ b/instance/core/pom.xml @@ -154,7 +154,8 @@ org.apache.karaf.instance.core.internal, org.apache.karaf.instance.core.internal.osgi, org.apache.felix.utils.properties;-split-package:=merge-first, - org.apache.karaf.util.locks + org.apache.karaf.util.locks, + org.apache.karaf.util.tracker org.apache.karaf.instance.core.internal.osgi.Activator http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/instance/core/src/main/java/org/apache/karaf/instance/core/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/instance/core/src/main/java/org/apache/karaf/instance/core/internal/osgi/Activator.java b/instance/core/src/main/java/org/apache/karaf/instance/core/internal/osgi/Activator.java index 6e8344a..a50db46 100644 --- a/instance/core/src/main/java/org/apache/karaf/instance/core/internal/osgi/Activator.java +++ b/instance/core/src/main/java/org/apache/karaf/instance/core/internal/osgi/Activator.java @@ -16,65 +16,20 @@ */ package org.apache.karaf.instance.core.internal.osgi; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; - -import javax.management.NotCompliantMBeanException; - import org.apache.karaf.instance.core.InstanceService; import org.apache.karaf.instance.core.internal.InstanceServiceImpl; import org.apache.karaf.instance.core.internal.InstancesMBeanImpl; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Activator implements BundleActivator { +import org.apache.karaf.util.tracker.BaseActivator; - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - - private ServiceRegistration serviceRegistration; - private ServiceRegistration mbeanRegistration; +public class Activator extends BaseActivator { @Override - public void start(BundleContext context) throws Exception { + protected void doStart() throws Exception { InstanceService instanceService = new InstanceServiceImpl(); - serviceRegistration = context.registerService(InstanceService.class, instanceService, null); - try { - InstancesMBeanImpl mbean = new InstancesMBeanImpl(instanceService); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=instance,name=" + System.getProperty("karaf.name")); - mbeanRegistration = context.registerService( - getInterfaceNames(mbean), - mbean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating ConfigRepository mbean", e); - } - } - - @Override - public void stop(BundleContext context) throws Exception { - mbeanRegistration.unregister(); - serviceRegistration.unregister(); - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } + register(InstanceService.class, instanceService); - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } + InstancesMBeanImpl mbean = new InstancesMBeanImpl(instanceService); + registerMBean(mbean, "type=instance"); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/kar/core/src/main/java/org/apache/karaf/kar/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/kar/core/src/main/java/org/apache/karaf/kar/internal/osgi/Activator.java b/kar/core/src/main/java/org/apache/karaf/kar/internal/osgi/Activator.java index f0418cf..05a1b59 100644 --- a/kar/core/src/main/java/org/apache/karaf/kar/internal/osgi/Activator.java +++ b/kar/core/src/main/java/org/apache/karaf/kar/internal/osgi/Activator.java @@ -16,175 +16,39 @@ */ package org.apache.karaf.kar.internal.osgi; -import java.util.ArrayList; -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.management.NotCompliantMBeanException; - import org.apache.karaf.features.FeaturesService; import org.apache.karaf.kar.KarService; import org.apache.karaf.kar.internal.KarServiceImpl; import org.apache.karaf.kar.internal.KarsMBeanImpl; -import org.apache.karaf.util.tracker.SingleServiceTracker; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.cm.ConfigurationException; +import org.apache.karaf.util.tracker.BaseActivator; import org.osgi.service.cm.ManagedService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Activator implements BundleActivator, ManagedService, SingleServiceTracker.SingleServiceListener { - - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - - private ExecutorService executor = Executors.newSingleThreadExecutor(); - private AtomicBoolean scheduled = new AtomicBoolean(); - private BundleContext bundleContext; - private Dictionary configuration; - private ServiceRegistration registration; - private ServiceRegistration mbeanRegistration; - private ServiceRegistration managedServiceRegistration; - private SingleServiceTracker featuresServiceTracker; - - @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - scheduled.set(true); - - Hashtable props = new Hashtable(); - props.put(Constants.SERVICE_PID, "org.apache.karaf.kar"); - managedServiceRegistration = bundleContext.registerService(ManagedService.class, this, props); - - featuresServiceTracker = new SingleServiceTracker( - bundleContext, FeaturesService.class, this); - featuresServiceTracker.open(); - - scheduled.set(false); - reconfigure(); - } - - @Override - public void stop(BundleContext context) throws Exception { - featuresServiceTracker.close(); - managedServiceRegistration.unregister(); - executor.shutdown(); - executor.awaitTermination(30, TimeUnit.SECONDS); - } - - @Override - public void updated(Dictionary properties) throws ConfigurationException { - this.configuration = properties; - reconfigure(); - } - @Override - public void serviceFound() { - reconfigure(); - } +public class Activator extends BaseActivator implements ManagedService { @Override - public void serviceLost() { - reconfigure(); - } - - @Override - public void serviceReplaced() { - reconfigure(); - } - - protected void reconfigure() { - if (scheduled.compareAndSet(false, true)) { - executor.submit(new Runnable() { - @Override - public void run() { - scheduled.set(false); - doStop(); - try { - doStart(); - } catch (Exception e) { - LOGGER.warn("Error starting management layer", e); - doStop(); - } - } - }); - } + protected void doOpen() throws Exception { + manage("org.apache.karaf.kar"); + trackService(FeaturesService.class); } protected void doStart() throws Exception { - FeaturesService featuresService = featuresServiceTracker.getService(); - Dictionary config = configuration; + FeaturesService featuresService = getTrackedService(FeaturesService.class); if (featuresService == null) { return; } - boolean noAutoRefreshBundles = getBoolean(config, "noAutoRefreshBundles", false); + boolean noAutoRefreshBundles = getBoolean("noAutoRefreshBundles", false); KarServiceImpl karService = new KarServiceImpl( System.getProperty("karaf.base"), featuresService ); karService.setNoAutoRefreshBundles(noAutoRefreshBundles); - registration = bundleContext.registerService(KarService.class, karService, null); + register(KarService.class, karService); - try { - KarsMBeanImpl mbean = new KarsMBeanImpl(); - mbean.setKarService(karService); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=kar,name=" + System.getProperty("karaf.name")); - mbeanRegistration = bundleContext.registerService( - getInterfaceNames(mbean), - mbean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating Kars mbean", e); - } - } - - protected void doStop() { - if (mbeanRegistration != null) { - mbeanRegistration.unregister(); - mbeanRegistration = null; - } - if (registration != null) { - registration.unregister(); - registration = null; - } - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } - - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } - } - - private boolean getBoolean(Dictionary config, String key, boolean def) { - if (config != null) { - Object val = config.get(key); - if (val instanceof Boolean) { - return (Boolean) val; - } else if (val != null) { - return Boolean.parseBoolean(val.toString()); - } - } - return def; + KarsMBeanImpl mbean = new KarsMBeanImpl(); + mbean.setKarService(karService); + registerMBean(mbean, "type=kar"); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/log/core/src/main/java/org/apache/karaf/log/core/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/log/core/src/main/java/org/apache/karaf/log/core/internal/osgi/Activator.java b/log/core/src/main/java/org/apache/karaf/log/core/internal/osgi/Activator.java index d9085fc..2e8c4d8 100644 --- a/log/core/src/main/java/org/apache/karaf/log/core/internal/osgi/Activator.java +++ b/log/core/src/main/java/org/apache/karaf/log/core/internal/osgi/Activator.java @@ -16,16 +16,7 @@ */ package org.apache.karaf.log.core.internal.osgi; -import java.util.ArrayList; -import java.util.Dictionary; import java.util.Hashtable; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.management.NotCompliantMBeanException; import org.apache.karaf.log.core.LogEventFormatter; import org.apache.karaf.log.core.LogService; @@ -33,118 +24,38 @@ import org.apache.karaf.log.core.internal.LogEventFormatterImpl; import org.apache.karaf.log.core.internal.LogMBeanImpl; import org.apache.karaf.log.core.internal.LogServiceImpl; import org.apache.karaf.log.core.internal.LruList; -import org.apache.karaf.util.tracker.SingleServiceTracker; +import org.apache.karaf.util.tracker.BaseActivator; import org.ops4j.pax.logging.spi.PaxAppender; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; import org.osgi.service.cm.ConfigurationAdmin; -import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Activator implements BundleActivator, ManagedService, SingleServiceTracker.SingleServiceListener { - - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - - private ExecutorService executor = Executors.newSingleThreadExecutor(); - private AtomicBoolean scheduled = new AtomicBoolean(); - private BundleContext bundleContext; - private Dictionary configuration; - private ServiceRegistration managedServiceRegistration; - private SingleServiceTracker configAdminTracker; - private ServiceRegistration serviceRegistration; - private ServiceRegistration formatterRegistration; - private ServiceRegistration appenderRegistration; - private ServiceRegistration mbeanRegistration; - - @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - scheduled.set(true); - Hashtable props = new Hashtable(); - props.put(Constants.SERVICE_PID, "org.apache.karaf.log"); - managedServiceRegistration = bundleContext.registerService(ManagedService.class, this, props); - - configAdminTracker = new SingleServiceTracker( - bundleContext, ConfigurationAdmin.class, this); - configAdminTracker.open(); - - scheduled.set(false); - reconfigure(); - } +public class Activator extends BaseActivator implements ManagedService { @Override - public void stop(BundleContext context) throws Exception { - configAdminTracker.close(); - managedServiceRegistration.unregister(); - executor.shutdown(); - executor.awaitTermination(30, TimeUnit.SECONDS); - } - - @Override - public void updated(Dictionary properties) throws ConfigurationException { - this.configuration = properties; - reconfigure(); - } - - @Override - public void serviceFound() { - reconfigure(); - } - - @Override - public void serviceLost() { - reconfigure(); - } - - @Override - public void serviceReplaced() { - reconfigure(); - } - - protected void reconfigure() { - if (scheduled.compareAndSet(false, true)) { - executor.submit(new Runnable() { - @Override - public void run() { - scheduled.set(false); - doStop(); - try { - doStart(); - } catch (Exception e) { - LOGGER.warn("Error starting management layer", e); - doStop(); - } - } - }); - } + protected void doOpen() throws Exception { + manage("org.apache.karaf.log"); + trackService(ConfigurationAdmin.class); } protected void doStart() throws Exception { - ConfigurationAdmin configurationAdmin = configAdminTracker != null ? configAdminTracker.getService() : null; - Dictionary config = configuration; + ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class); if (configurationAdmin == null) { return; } - int size = getInt(config, "size", 500); - String pattern = getString(config, "pattern", "%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n"); - String fatalColor = getString(config, "fatalColor", "31"); - String errorColor = getString(config, "errorColor", "31"); - String warnColor = getString(config, "warnColor", "35"); - String infoColor = getString(config, "infoColor", "36"); - String debugColor = getString(config, "debugColor", "39"); - String traceColor = getString(config, "traceColor", "39"); + int size = getInt("size", 500); + String pattern = getString("pattern", "%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n"); + String fatalColor = getString("fatalColor", "31"); + String errorColor = getString("errorColor", "31"); + String warnColor = getString("warnColor", "35"); + String infoColor = getString("infoColor", "36"); + String debugColor = getString("debugColor", "39"); + String traceColor = getString("traceColor", "39"); LruList events = new LruList(size); Hashtable props = new Hashtable(); props.put("org.ops4j.pax.logging.appender.name", "VmLogAppender"); - appenderRegistration = bundleContext.registerService( - PaxAppender.class, events, props); + register(PaxAppender.class, events, props); LogEventFormatterImpl formatter = new LogEventFormatterImpl(); formatter.setPattern(pattern); @@ -154,82 +65,13 @@ public class Activator implements BundleActivator, ManagedService, SingleService formatter.setInfoColor(infoColor); formatter.setDebugColor(debugColor); formatter.setTraceColor(traceColor); - formatterRegistration = bundleContext.registerService( - LogEventFormatter.class, formatter, null); + register(LogEventFormatter.class, formatter); LogServiceImpl logService = new LogServiceImpl(configurationAdmin, events); - serviceRegistration = bundleContext.registerService( - LogService.class, logService, null); - - - try { - LogMBeanImpl securityMBean = new LogMBeanImpl(logService); - props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=log,name=" + System.getProperty("karaf.name")); - mbeanRegistration = bundleContext.registerService( - getInterfaceNames(securityMBean), - securityMBean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating Log mbean", e); - } - } - - protected void doStop() { - if (mbeanRegistration != null) { - mbeanRegistration.unregister(); - mbeanRegistration = null; - } - if (serviceRegistration != null) { - serviceRegistration.unregister(); - serviceRegistration = null; - } - if (formatterRegistration != null) { - formatterRegistration.unregister(); - formatterRegistration = null; - } - if (appenderRegistration != null) { - appenderRegistration.unregister(); - appenderRegistration = null; - } - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } + register(LogService.class, logService); - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } - } - - private int getInt(Dictionary config, String key, int def) { - if (config != null) { - Object val = config.get(key); - if (val instanceof Number) { - return ((Number) val).intValue(); - } else if (val != null) { - return Integer.parseInt(val.toString()); - } - } - return def; - } - - private String getString(Dictionary config, String key, String def) { - if (config != null) { - Object val = config.get(key); - if (val != null) { - return val.toString(); - } - } - return def; + LogMBeanImpl securityMBean = new LogMBeanImpl(logService); + registerMBean(securityMBean, "type=log"); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/management/server/src/main/java/org/apache/karaf/management/internal/Activator.java ---------------------------------------------------------------------- diff --git a/management/server/src/main/java/org/apache/karaf/management/internal/Activator.java b/management/server/src/main/java/org/apache/karaf/management/internal/Activator.java index b9a6917..8745d82 100644 --- a/management/server/src/main/java/org/apache/karaf/management/internal/Activator.java +++ b/management/server/src/main/java/org/apache/karaf/management/internal/Activator.java @@ -16,19 +16,10 @@ */ package org.apache.karaf.management.internal; -import java.util.ArrayList; -import java.util.Dictionary; import java.util.HashMap; -import java.util.Hashtable; -import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; import javax.management.MBeanServer; -import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; import org.apache.karaf.jaas.config.KeystoreManager; @@ -37,136 +28,51 @@ import org.apache.karaf.management.JaasAuthenticator; import org.apache.karaf.management.KarafMBeanServerGuard; import org.apache.karaf.management.MBeanServerFactory; import org.apache.karaf.management.RmiRegistryFactory; -import org.apache.karaf.util.tracker.SingleServiceTracker; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; +import org.apache.karaf.util.tracker.BaseActivator; import org.osgi.service.cm.ConfigurationAdmin; -import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -public class Activator implements BundleActivator, ManagedService, SingleServiceTracker.SingleServiceListener { +public class Activator extends BaseActivator implements ManagedService { - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - - private ExecutorService executor = Executors.newSingleThreadExecutor(); - private AtomicBoolean scheduled = new AtomicBoolean(); - private BundleContext bundleContext; - private Dictionary configuration; - private ServiceRegistration registration; - private SingleServiceTracker configAdminTracker; - private SingleServiceTracker keystoreManagerTracker; - private ServiceRegistration serverRegistration; - private ServiceRegistration securityRegistration; private ConnectorServerFactory connectorServerFactory; private RmiRegistryFactory rmiRegistryFactory; private MBeanServerFactory mbeanServerFactory; @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - scheduled.set(true); - - Hashtable props = new Hashtable(); - props.put(Constants.SERVICE_PID, "org.apache.karaf.management"); - registration = bundleContext.registerService(ManagedService.class, this, props); - - configAdminTracker = new SingleServiceTracker( - bundleContext, ConfigurationAdmin.class, this); - keystoreManagerTracker = new SingleServiceTracker( - bundleContext, KeystoreManager.class, this); - configAdminTracker.open(); - keystoreManagerTracker.open(); - - scheduled.set(false); - reconfigure(); - } - - @Override - public void stop(BundleContext context) throws Exception { - keystoreManagerTracker.close(); - configAdminTracker.close(); - registration.unregister(); - executor.shutdown(); - executor.awaitTermination(30, TimeUnit.SECONDS); - } - - @Override - public void updated(Dictionary properties) throws ConfigurationException { - this.configuration = properties; - reconfigure(); - } - - @Override - public void serviceFound() { - reconfigure(); - } - - @Override - public void serviceLost() { - reconfigure(); - } - - @Override - public void serviceReplaced() { - reconfigure(); - } - - protected void reconfigure() { - if (scheduled.compareAndSet(false, true)) { - executor.submit(new Runnable() { - @Override - public void run() { - scheduled.set(false); - doStop(); - try { - doStart(); - } catch (Exception e) { - LOGGER.warn("Error starting management layer", e); - doStop(); - } - } - }); - } + protected void doOpen() throws Exception { + manage("org.apache.karaf.management"); + trackService(ConfigurationAdmin.class); + trackService(KeystoreManager.class); } protected void doStart() throws Exception { - // This can happen while the bundle is starting as we register - // the ManagedService before creating the service trackers - if (configAdminTracker == null || keystoreManagerTracker == null) { - return; - } // Verify dependencies - ConfigurationAdmin configurationAdmin = configAdminTracker.getService(); - KeystoreManager keystoreManager = keystoreManagerTracker.getService(); - Dictionary config = configuration; + ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class); + KeystoreManager keystoreManager = getTrackedService(KeystoreManager.class); if (configurationAdmin == null || keystoreManager == null) { return; } - String rmiRegistryHost = getString(config, "rmiRegistryHost", ""); - int rmiRegistryPort = getInt(config, "rmiRegistryPort", 1099); - String rmiServerHost = getString(config, "rmiServerHost", "0.0.0.0"); - int rmiServerPort = getInt(config, "rmiServerPort", 44444); + String rmiRegistryHost = getString("rmiRegistryHost", ""); + int rmiRegistryPort = getInt("rmiRegistryPort", 1099); + String rmiServerHost = getString("rmiServerHost", "0.0.0.0"); + int rmiServerPort = getInt("rmiServerPort", 44444); - String jmxRealm = getString(config, "jmxRealm", "karaf"); - String serviceUrl = getString(config, "serviceUrl", + String jmxRealm = getString("jmxRealm", "karaf"); + String serviceUrl = getString("serviceUrl", "service:jmx:rmi://0.0.0.0:" + rmiServerPort + "/jndi/rmi://0.0.0.0:" + rmiRegistryPort + "/karaf-" + System.getProperty("karaf.name")); - boolean daemon = getBoolean(config, "daemon", true); - boolean threaded = getBoolean(config, "threaded", true); - ObjectName objectName = new ObjectName(getString(config, "objectName", "connector:name=rmi")); - long keyStoreAvailabilityTimeout = getLong(config, "keyStoreAvailabilityTimeout", 5000); - String authenticatorType = getString(config, "authenticatorType", "password"); - boolean secured = getBoolean(config, "secured", false); - String secureAlgorithm = getString(config, "secureAlgorithm", "default"); - String secureProtocol = getString(config, "secureProtocol", "TLS"); - String keyStore = getString(config, "keyStore", "karaf.ks"); - String keyAlias = getString(config, "keyAlias", "karaf"); - String trustStore = getString(config, "trustStore", "karaf.ts"); + boolean daemon = getBoolean("daemon", true); + boolean threaded = getBoolean("threaded", true); + ObjectName objectName = new ObjectName(getString("objectName", "connector:name=rmi")); + long keyStoreAvailabilityTimeout = getLong("keyStoreAvailabilityTimeout", 5000); + String authenticatorType = getString("authenticatorType", "password"); + boolean secured = getBoolean("secured", false); + String secureAlgorithm = getString("secureAlgorithm", "default"); + String secureProtocol = getString("secureProtocol", "TLS"); + String keyStore = getString("keyStore", "karaf.ks"); + String keyAlias = getString("keyAlias", "karaf"); + String trustStore = getString("trustStore", "karaf.ts"); KarafMBeanServerGuard guard = new KarafMBeanServerGuard(); guard.setConfigAdmin(configurationAdmin); @@ -210,37 +116,20 @@ public class Activator implements BundleActivator, ManagedService, SingleService connectorServerFactory.setKeystoreManager(keystoreManager); connectorServerFactory.init(); - try { - JMXSecurityMBeanImpl securityMBean = new JMXSecurityMBeanImpl(); - securityMBean.setMBeanServer(mbeanServer); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=security,area=jmx,name=" + System.getProperty("karaf.name")); - securityRegistration = bundleContext.registerService( - getInterfaceNames(securityMBean), - securityMBean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating JMX security mbean", e); - } + JMXSecurityMBeanImpl securityMBean = new JMXSecurityMBeanImpl(); + securityMBean.setMBeanServer(mbeanServer); + registerMBean(securityMBean, "type=security,area=jmx"); - serverRegistration = bundleContext.registerService(MBeanServer.class, mbeanServer, null); + register(MBeanServer.class, mbeanServer); } protected void doStop() { - if (securityRegistration != null) { - securityRegistration.unregister(); - securityRegistration = null; - } - if (serverRegistration != null) { - serverRegistration.unregister(); - serverRegistration = null; - } + super.doStop(); if (connectorServerFactory != null) { try { connectorServerFactory.destroy(); } catch (Exception e) { - LOGGER.warn("Error destroying ConnectorServerFactory", e); + logger.warn("Error destroying ConnectorServerFactory", e); } connectorServerFactory = null; } @@ -248,7 +137,7 @@ public class Activator implements BundleActivator, ManagedService, SingleService try { mbeanServerFactory.destroy(); } catch (Exception e) { - LOGGER.warn("Error destroying MBeanServerFactory", e); + logger.warn("Error destroying MBeanServerFactory", e); } mbeanServerFactory = null; } @@ -256,71 +145,10 @@ public class Activator implements BundleActivator, ManagedService, SingleService try { rmiRegistryFactory.destroy(); } catch (Exception e) { - LOGGER.warn("Error destroying RMIRegistryFactory", e); + logger.warn("Error destroying RMIRegistryFactory", e); } rmiRegistryFactory = null; } } - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } - - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } - } - - private int getInt(Dictionary config, String key, int def) { - if (config != null) { - Object val = config.get(key); - if (val instanceof Number) { - return ((Number) val).intValue(); - } else if (val != null) { - return Integer.parseInt(val.toString()); - } - } - return def; - } - - private long getLong(Dictionary config, String key, long def) { - if (config != null) { - Object val = config.get(key); - if (val instanceof Number) { - return ((Number) val).longValue(); - } else if (val != null) { - return Long.parseLong(val.toString()); - } - } - return def; - } - - private boolean getBoolean(Dictionary config, String key, boolean def) { - if (config != null) { - Object val = config.get(key); - if (val instanceof Boolean) { - return (Boolean) val; - } else if (val != null) { - return Boolean.parseBoolean(val.toString()); - } - } - return def; - } - - private String getString(Dictionary config, String key, String def) { - if (config != null) { - Object val = config.get(key); - if (val != null) { - return val.toString(); - } - } - return def; - } - } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/package/core/pom.xml ---------------------------------------------------------------------- diff --git a/package/core/pom.xml b/package/core/pom.xml index 72e043a..bc5238f 100644 --- a/package/core/pom.xml +++ b/package/core/pom.xml @@ -61,6 +61,12 @@ org.apache.felix.utils provided + + + org.apache.karaf + org.apache.karaf.util + provided + org.slf4j @@ -97,6 +103,7 @@ org.apache.karaf.packages.core.internal, org.apache.karaf.packages.core.internal.osgi, + org.apache.karaf.util.tracker, org.apache.felix.utils.version, org.apache.felix.utils.manifest http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/package/core/src/main/java/org/apache/karaf/packages/core/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/package/core/src/main/java/org/apache/karaf/packages/core/internal/osgi/Activator.java b/package/core/src/main/java/org/apache/karaf/packages/core/internal/osgi/Activator.java index bf906cc..ea8ce1d 100644 --- a/package/core/src/main/java/org/apache/karaf/packages/core/internal/osgi/Activator.java +++ b/package/core/src/main/java/org/apache/karaf/packages/core/internal/osgi/Activator.java @@ -16,65 +16,19 @@ */ package org.apache.karaf.packages.core.internal.osgi; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; - -import javax.management.NotCompliantMBeanException; - import org.apache.karaf.packages.core.PackageService; import org.apache.karaf.packages.core.internal.PackageServiceImpl; import org.apache.karaf.packages.core.internal.PackagesMBeanImpl; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Activator implements BundleActivator { +import org.apache.karaf.util.tracker.BaseActivator; - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - - private ServiceRegistration serviceRegistration; - private ServiceRegistration mbeanRegistration; +public class Activator extends BaseActivator { @Override - public void start(BundleContext context) throws Exception { - PackageService packageService = new PackageServiceImpl(context); - serviceRegistration = context.registerService(PackageService.class, packageService, null); - try { - PackagesMBeanImpl mbean = new PackagesMBeanImpl(packageService); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=package,name=" + System.getProperty("karaf.name")); - mbeanRegistration = context.registerService( - getInterfaceNames(mbean), - mbean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating Packages mbean", e); - } - } + protected void doStart() throws Exception { + PackageService packageService = new PackageServiceImpl(bundleContext); + register(PackageService.class, packageService); - @Override - public void stop(BundleContext context) throws Exception { - mbeanRegistration.unregister(); - serviceRegistration.unregister(); + PackagesMBeanImpl mbean = new PackagesMBeanImpl(packageService); + registerMBean(mbean, "type=package"); } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } - - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } - } - } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/service/core/pom.xml ---------------------------------------------------------------------- diff --git a/service/core/pom.xml b/service/core/pom.xml index 2f5671e..e617fc3 100644 --- a/service/core/pom.xml +++ b/service/core/pom.xml @@ -43,6 +43,12 @@ org.osgi.core provided + + org.apache.karaf + org.apache.karaf.util + provided + + @@ -72,7 +78,8 @@ org.apache.karaf.service.core.internal, - org.apache.karaf.service.core.internal.osgi + org.apache.karaf.service.core.internal.osgi, + org.apache.karaf.util.tracker org.apache.karaf.service.core.internal.osgi.Activator http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/service/core/src/main/java/org/apache/karaf/service/core/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/service/core/src/main/java/org/apache/karaf/service/core/internal/osgi/Activator.java b/service/core/src/main/java/org/apache/karaf/service/core/internal/osgi/Activator.java index 25b9e30..dcdb406 100644 --- a/service/core/src/main/java/org/apache/karaf/service/core/internal/osgi/Activator.java +++ b/service/core/src/main/java/org/apache/karaf/service/core/internal/osgi/Activator.java @@ -16,50 +16,16 @@ */ package org.apache.karaf.service.core.internal.osgi; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; - import org.apache.karaf.service.core.internal.ServicesMBeanImpl; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; - -public class Activator implements BundleActivator { +import org.apache.karaf.util.tracker.BaseActivator; - private ServiceRegistration mbeanRegistration; +public class Activator extends BaseActivator { @Override - public void start(BundleContext context) throws Exception { + protected void doStart() throws Exception { ServicesMBeanImpl mbean = new ServicesMBeanImpl(); - mbean.setBundleContext(context); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=service,name=" + System.getProperty("karaf.name")); - mbeanRegistration = context.registerService( - getInterfaceNames(mbean), - mbean, - props - ); - } - - @Override - public void stop(BundleContext context) throws Exception { - mbeanRegistration.unregister(); - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } - - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } + mbean.setBundleContext(bundleContext); + registerMBean(mbean, "type=service"); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/system/core/pom.xml ---------------------------------------------------------------------- diff --git a/system/core/pom.xml b/system/core/pom.xml index 4a90207..61c68e0 100644 --- a/system/core/pom.xml +++ b/system/core/pom.xml @@ -51,6 +51,10 @@ provided + org.apache.karaf + org.apache.karaf.util + + org.apache.felix org.apache.felix.utils provided @@ -87,6 +91,7 @@ org.apache.karaf.system.internal, org.apache.karaf.system.internal.osgi, org.apache.karaf.system.management.internal, + org.apache.karaf.util.tracker, org.apache.felix.utils.properties http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/system/core/src/main/java/org/apache/karaf/system/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/system/core/src/main/java/org/apache/karaf/system/internal/osgi/Activator.java b/system/core/src/main/java/org/apache/karaf/system/internal/osgi/Activator.java index b734253..b7f8eab 100644 --- a/system/core/src/main/java/org/apache/karaf/system/internal/osgi/Activator.java +++ b/system/core/src/main/java/org/apache/karaf/system/internal/osgi/Activator.java @@ -16,68 +16,23 @@ */ package org.apache.karaf.system.internal.osgi; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; - -import javax.management.NotCompliantMBeanException; - import org.apache.karaf.system.SystemService; import org.apache.karaf.system.internal.SystemServiceImpl; import org.apache.karaf.system.management.internal.SystemMBeanImpl; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Activator implements BundleActivator { +import org.apache.karaf.util.tracker.BaseActivator; - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - - private ServiceRegistration serviceRegistration; - private ServiceRegistration mbeanRegistration; +public class Activator extends BaseActivator { @Override - public void start(BundleContext context) throws Exception { + protected void doStart() throws Exception { SystemServiceImpl systemService = new SystemServiceImpl(); - systemService.setBundleContext(context); - serviceRegistration = context.registerService(SystemService.class, systemService, null); - try { - SystemMBeanImpl mbean = new SystemMBeanImpl(); - mbean.setBundleContext(context); - mbean.setSystemService(systemService); - Hashtable props = new Hashtable(); - props.put("jmx.objectname", "org.apache.karaf:type=system,name=" + System.getProperty("karaf.name")); - mbeanRegistration = context.registerService( - getInterfaceNames(mbean), - mbean, - props - ); - } catch (NotCompliantMBeanException e) { - LOGGER.warn("Error creating System mbean", e); - } - } - - @Override - public void stop(BundleContext context) throws Exception { - mbeanRegistration.unregister(); - serviceRegistration.unregister(); - } - - private String[] getInterfaceNames(Object object) { - List names = new ArrayList(); - for (Class cl = object.getClass(); cl != Object.class; cl = cl.getSuperclass()) { - addSuperInterfaces(names, cl); - } - return names.toArray(new String[names.size()]); - } + systemService.setBundleContext(bundleContext); + register(SystemService.class, systemService); - private void addSuperInterfaces(List names, Class clazz) { - for (Class cl : clazz.getInterfaces()) { - names.add(cl.getName()); - addSuperInterfaces(names, cl); - } + SystemMBeanImpl mbean = new SystemMBeanImpl(); + mbean.setBundleContext(bundleContext); + mbean.setSystemService(systemService); + registerMBean(mbean, "type=system"); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/c57532de/util/pom.xml ---------------------------------------------------------------------- diff --git a/util/pom.xml b/util/pom.xml index 7a789a5..b5a7768 100644 --- a/util/pom.xml +++ b/util/pom.xml @@ -40,9 +40,18 @@ provided + org.osgi + org.osgi.compendium + provided + + org.apache.felix org.apache.felix.utils + + org.slf4j + slf4j-api +