karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [07/11] git commit: [KARAF-2833] Refactor activators with a single base class
Date Tue, 01 Apr 2014 11:43:40 GMT
[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/75eb098e
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/75eb098e
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/75eb098e

Branch: refs/heads/master
Commit: 75eb098e56f390bb1a5327e9fba3232824a53cd0
Parents: cc94e84
Author: Guillaume Nodet <gnodet@gmail.com>
Authored: Mon Mar 31 23:01:48 2014 +0200
Committer: Guillaume Nodet <gnodet@gmail.com>
Committed: Tue Apr 1 10:14:57 2014 +0200

----------------------------------------------------------------------
 deployer/blueprint/pom.xml                      |   3 +-
 .../deployer/blueprint/osgi/Activator.java      |  31 +---
 .../karaf/deployer/features/osgi/Activator.java |  99 ++----------
 deployer/spring/pom.xml                         |   3 +-
 .../karaf/deployer/spring/osgi/Activator.java   |  31 +---
 .../karaf/deployer/wrap/osgi/Activator.java     |  42 +----
 .../org/apache/karaf/shell/ssh/Activator.java   | 158 +++++--------------
 7 files changed, 72 insertions(+), 295 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/75eb098e/deployer/blueprint/pom.xml
----------------------------------------------------------------------
diff --git a/deployer/blueprint/pom.xml b/deployer/blueprint/pom.xml
index 011ecda..008f533 100644
--- a/deployer/blueprint/pom.xml
+++ b/deployer/blueprint/pom.xml
@@ -92,7 +92,8 @@
                         <Private-Package>
                             org.apache.karaf.deployer.blueprint,
                             org.apache.karaf.deployer.blueprint.osgi,
-                            org.apache.karaf.util
+                            org.apache.karaf.util,
+                            org.apache.karaf.util.tracker
                         </Private-Package>
                         <Bundle-Activator>
                             org.apache.karaf.deployer.blueprint.osgi.Activator

http://git-wip-us.apache.org/repos/asf/karaf/blob/75eb098e/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/osgi/Activator.java
b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/osgi/Activator.java
index 96057bb..5251150 100644
--- a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/osgi/Activator.java
+++ b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/osgi/Activator.java
@@ -23,37 +23,18 @@ import org.apache.felix.fileinstall.ArtifactListener;
 import org.apache.felix.fileinstall.ArtifactUrlTransformer;
 import org.apache.karaf.deployer.blueprint.BlueprintDeploymentListener;
 import org.apache.karaf.deployer.blueprint.BlueprintURLHandler;
-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.url.URLStreamHandlerService;
 
-public class Activator implements BundleActivator {
-
-    private ServiceRegistration urlHandlerRegistration;
-    private ServiceRegistration urlTransformerRegistration;
+public class Activator extends BaseActivator {
 
     @Override
-    public void start(BundleContext context) throws Exception {
+    protected void doStart() throws Exception {
         Hashtable<String, Object> props = new Hashtable<String, Object>();
         props.put("url.handler.protocol", "blueprint");
-        urlHandlerRegistration = context.registerService(
-                URLStreamHandlerService.class,
-                new BlueprintURLHandler(),
-                props);
-
-        urlTransformerRegistration = context.registerService(
-                new String[] {
-                        ArtifactUrlTransformer.class.getName(),
-                        ArtifactListener.class.getName()
-                },
-                new BlueprintDeploymentListener(),
-                null);
+        register(URLStreamHandlerService.class, new BlueprintURLHandler(), props);
+        register(new String[]{ArtifactUrlTransformer.class.getName(), ArtifactListener.class.getName()},
+                new BlueprintDeploymentListener());
     }
 
-    @Override
-    public void stop(BundleContext context) throws Exception {
-        urlTransformerRegistration.unregister();
-        urlHandlerRegistration.unregister();
-    }
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/75eb098e/deployer/features/src/main/java/org/apache/karaf/deployer/features/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/deployer/features/src/main/java/org/apache/karaf/deployer/features/osgi/Activator.java
b/deployer/features/src/main/java/org/apache/karaf/deployer/features/osgi/Activator.java
index 51f9725..cffb688 100644
--- a/deployer/features/src/main/java/org/apache/karaf/deployer/features/osgi/Activator.java
+++ b/deployer/features/src/main/java/org/apache/karaf/deployer/features/osgi/Activator.java
@@ -18,91 +18,27 @@
 package org.apache.karaf.deployer.features.osgi;
 
 import java.util.Hashtable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.felix.fileinstall.ArtifactListener;
 import org.apache.felix.fileinstall.ArtifactUrlTransformer;
 import org.apache.karaf.deployer.features.FeatureDeploymentListener;
 import org.apache.karaf.deployer.features.FeatureURLHandler;
 import org.apache.karaf.features.FeaturesService;
-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.url.URLStreamHandlerService;
-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 AtomicBoolean scheduled = new AtomicBoolean();
-    private BundleContext bundleContext;
-    private ServiceRegistration urlHandlerRegistration;
-    private ServiceRegistration urlTransformerRegistration;
-    private SingleServiceTracker<FeaturesService> featuresServiceTracker;
     private FeatureDeploymentListener listener;
 
     @Override
-    public void start(BundleContext context) throws Exception {
-        bundleContext = context;
-        scheduled.set(true);
-
-        featuresServiceTracker = new SingleServiceTracker<FeaturesService>(
-                context, FeaturesService.class, this);
-        featuresServiceTracker.open();
-
-        scheduled.set(false);
-        reconfigure();
-    }
-
-    @Override
-    public void stop(BundleContext context) throws Exception {
-        featuresServiceTracker.close();
-        executor.shutdown();
-        executor.awaitTermination(30, TimeUnit.SECONDS);
-    }
-
-    @Override
-    public void serviceFound() {
-        reconfigure();
-    }
-
-    @Override
-    public void serviceLost() {
-        reconfigure();
+    protected void doOpen() throws Exception {
+        trackService(FeaturesService.class);
     }
 
     @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 features deployer", e);
-                        doStop();
-                    }
-                }
-            });
-        }
-    }
-
     protected void doStart() throws Exception {
-        FeaturesService service = featuresServiceTracker.getService();
+        FeaturesService service = getTrackedService(FeaturesService.class);
         if (service == null) {
             return;
         }
@@ -110,34 +46,19 @@ public class Activator implements BundleActivator, SingleServiceTracker.SingleSe
         Hashtable<String, Object> props = new Hashtable<String, Object>();
         props.put("url.handler.protocol", "feature");
         FeatureURLHandler handler = new FeatureURLHandler();
-        urlHandlerRegistration = bundleContext.registerService(
-                URLStreamHandlerService.class,
-                handler,
-                props);
+        register(URLStreamHandlerService.class, handler, props);
 
         listener = new FeatureDeploymentListener();
         listener.setFeaturesService(service);
         listener.setBundleContext(bundleContext);
         listener.init();
-
-        urlTransformerRegistration = bundleContext.registerService(
-                new String[] {
-                        ArtifactUrlTransformer.class.getName(),
-                        ArtifactListener.class.getName()
-                },
-                listener,
-                null);
+        register(new String[]{
+                ArtifactUrlTransformer.class.getName(), ArtifactListener.class.getName()
+        }, listener);
     }
 
     protected void doStop() {
-        if (urlTransformerRegistration != null) {
-            urlTransformerRegistration.unregister();
-            urlTransformerRegistration = null;
-        }
-        if (urlHandlerRegistration != null) {
-            urlHandlerRegistration.unregister();
-            urlHandlerRegistration = null;
-        }
+        super.doStop();
         if (listener != null) {
             listener.destroy();
             listener = null;

http://git-wip-us.apache.org/repos/asf/karaf/blob/75eb098e/deployer/spring/pom.xml
----------------------------------------------------------------------
diff --git a/deployer/spring/pom.xml b/deployer/spring/pom.xml
index 13db7d0..a812842 100644
--- a/deployer/spring/pom.xml
+++ b/deployer/spring/pom.xml
@@ -96,7 +96,8 @@
                         <Private-Package>
                             org.apache.karaf.deployer.spring,
                             org.apache.karaf.deployer.spring.osgi,
-                            org.apache.karaf.util
+                            org.apache.karaf.util,
+                            org.apache.karaf.util.tracker
                         </Private-Package>
                         <Bundle-Activator>
                             org.apache.karaf.deployer.spring.osgi.Activator

http://git-wip-us.apache.org/repos/asf/karaf/blob/75eb098e/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/osgi/Activator.java
b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/osgi/Activator.java
index 146928b..116524c 100644
--- a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/osgi/Activator.java
+++ b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/osgi/Activator.java
@@ -23,37 +23,18 @@ import org.apache.felix.fileinstall.ArtifactListener;
 import org.apache.felix.fileinstall.ArtifactUrlTransformer;
 import org.apache.karaf.deployer.spring.SpringDeploymentListener;
 import org.apache.karaf.deployer.spring.SpringURLHandler;
-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.url.URLStreamHandlerService;
 
-public class Activator implements BundleActivator {
-
-    private ServiceRegistration urlHandlerRegistration;
-    private ServiceRegistration urlTransformerRegistration;
+public class Activator extends BaseActivator {
 
     @Override
-    public void start(BundleContext context) throws Exception {
+    protected void doStart() throws Exception {
         Hashtable<String, Object> props = new Hashtable<String, Object>();
         props.put("url.handler.protocol", "spring");
-        urlHandlerRegistration = context.registerService(
-                URLStreamHandlerService.class,
-                new SpringURLHandler(),
-                props);
-
-        urlTransformerRegistration = context.registerService(
-                new String[] {
-                        ArtifactUrlTransformer.class.getName(),
-                        ArtifactListener.class.getName()
-                },
-                new SpringDeploymentListener(),
-                null);
+        register(URLStreamHandlerService.class, new SpringURLHandler(), props);
+        register(new String[] { ArtifactUrlTransformer.class.getName(), ArtifactListener.class.getName()
},
+                 new SpringDeploymentListener());
     }
 
-    @Override
-    public void stop(BundleContext context) throws Exception {
-        urlTransformerRegistration.unregister();
-        urlHandlerRegistration.unregister();
-    }
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/75eb098e/deployer/wrap/src/main/java/org/apache/karaf/deployer/wrap/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/deployer/wrap/src/main/java/org/apache/karaf/deployer/wrap/osgi/Activator.java
b/deployer/wrap/src/main/java/org/apache/karaf/deployer/wrap/osgi/Activator.java
index 4f4001b..e992f91 100644
--- a/deployer/wrap/src/main/java/org/apache/karaf/deployer/wrap/osgi/Activator.java
+++ b/deployer/wrap/src/main/java/org/apache/karaf/deployer/wrap/osgi/Activator.java
@@ -21,54 +21,28 @@ import java.util.Hashtable;
 
 import org.apache.felix.fileinstall.ArtifactUrlTransformer;
 import org.apache.karaf.deployer.wrap.WrapDeploymentListener;
+import org.apache.karaf.util.tracker.BaseActivator;
 import org.apache.karaf.util.tracker.SingleServiceTracker;
-import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.url.URLStreamHandlerService;
 
-public class Activator implements BundleActivator, SingleServiceTracker.SingleServiceListener
{
+public class Activator extends BaseActivator {
 
     private BundleContext bundleContext;
     private ServiceRegistration<ArtifactUrlTransformer> urlTransformerRegistration;
     private SingleServiceTracker<URLStreamHandlerService> urlHandlerTracker;
 
     @Override
-    public void start(BundleContext context) throws Exception {
-        bundleContext = context;
-        urlHandlerTracker = new SingleServiceTracker<URLStreamHandlerService>(
-                context, URLStreamHandlerService.class,
-                "(url.handler.protocol=wrap)", this);
-        urlHandlerTracker.open();
+    protected void doOpen() throws Exception {
+        trackService(URLStreamHandlerService.class, "(url.handler.protocol=wrap)");
     }
 
     @Override
-    public void stop(BundleContext context) throws Exception {
-        urlHandlerTracker.close();
-    }
-
-    @Override
-    public void serviceFound() {
-        if (urlTransformerRegistration == null) {
-            Hashtable<String, Object> props = new Hashtable<String, Object>();
-            props.put("service.ranking", -1);
-            urlTransformerRegistration = bundleContext.registerService(
-                    ArtifactUrlTransformer.class,
-                    new WrapDeploymentListener(),
-                    props);
-        }
-    }
-
-    @Override
-    public void serviceLost() {
-        if (urlTransformerRegistration != null) {
-            urlTransformerRegistration.unregister();
-            urlTransformerRegistration = null;
-        }
-    }
-
-    @Override
-    public void serviceReplaced() {
+    protected void doStart() throws Exception {
+        Hashtable<String, Object> props = new Hashtable<String, Object>();
+        props.put("service.ranking", -1);
+        register(ArtifactUrlTransformer.class, new WrapDeploymentListener(), props);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/75eb098e/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/Activator.java
----------------------------------------------------------------------
diff --git a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/Activator.java b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/Activator.java
index af5fd54..16f08ab 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/Activator.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/Activator.java
@@ -27,10 +27,13 @@ import java.util.Hashtable;
 import java.util.List;
 import java.util.concurrent.Callable;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.karaf.shell.api.action.lifecycle.Manager;
 import org.apache.karaf.shell.api.console.Session;
 import org.apache.karaf.shell.api.console.SessionFactory;
+import org.apache.karaf.util.tracker.BaseActivator;
 import org.apache.karaf.util.tracker.SingleServiceTracker;
 import org.apache.sshd.SshServer;
 import org.apache.sshd.common.NamedFactory;
@@ -51,58 +54,22 @@ import org.slf4j.LoggerFactory;
 /**
  * Activate this bundle
  */
-public class Activator implements BundleActivator, ManagedService {
+public class Activator extends BaseActivator implements ManagedService {
 
     static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
 
-    ServiceRegistration registration;
-
-    List<Session> sessions = new CopyOnWriteArrayList<Session>();
-
-    BundleContext bundleContext;
-    SingleServiceTracker<SessionFactory> sessionFactoryTracker;
     ServiceTracker<Session, Session> sessionTracker;
-    Dictionary<String, ?> configuration;
-
-    final KarafAgentFactory agentFactory = new KarafAgentFactory();
-
+    KarafAgentFactory agentFactory;
     SessionFactory sessionFactory;
     SshClientFactory sshClientFactory;
-    final Callable<SshServer> sshServerFactory = new Callable<SshServer>() {
-        @Override
-        public SshServer call() throws Exception {
-            return createSshServer(sessionFactory);
-        }
-    };
     SshServer server;
-    final List<SshServer> servers = new ArrayList<SshServer>();
-
 
-    public void start(BundleContext context) throws Exception {
-        bundleContext = context;
-
-        Hashtable<String, Object> props = new Hashtable<String, Object>();
-        props.put(Constants.SERVICE_PID, "org.apache.karaf.shell");
-        registration = bundleContext.registerService(ManagedService.class, this, props);
-
-        sshClientFactory = new SshClientFactory(agentFactory, new File(context.getProperty("user.home"),
".sshkaraf/known_hosts"));
-
-        sessionFactoryTracker = new SingleServiceTracker<SessionFactory>(bundleContext,
SessionFactory.class, new SingleServiceTracker.SingleServiceListener() {
-            @Override
-            public void serviceFound() {
-                bindSessionFactory(sessionFactoryTracker.getService());
-            }
-            @Override
-            public void serviceLost() {
-                unbindSessionFactory();
-            }
-            @Override
-            public void serviceReplaced() {
-                serviceLost();
-                serviceFound();
-            }
-        });
-        sessionFactoryTracker.open();
+    @Override
+    protected void doOpen() throws Exception {
+        agentFactory = new KarafAgentFactory();
+        sshClientFactory = new SshClientFactory(agentFactory, new File(bundleContext.getProperty("user.home"),
".sshkaraf/known_hosts"));
+        manage("org.apache.karaf.shell");
+        trackService(SessionFactory.class);
 
         sessionTracker = new ServiceTracker<Session, Session>(bundleContext, Session.class,
null) {
             @Override
@@ -118,16 +85,25 @@ public class Activator implements BundleActivator, ManagedService {
             }
         };
         sessionTracker.open();
+    }
 
+    @Override
+    protected void doClose() {
+        sessionTracker.close();
+        super.doClose();
     }
 
-    private void bindSessionFactory(final SessionFactory sessionFactory) {
-        this.sessionFactory = sessionFactory;
-        this.sessionFactory.getRegistry().register(sshServerFactory, SshServer.class);
-        this.sessionFactory.getRegistry().register(sshClientFactory);
-        this.sessionFactory.getRegistry().getService(Manager.class).register(SshServerAction.class);
-        this.sessionFactory.getRegistry().getService(Manager.class).register(SshAction.class);
-        if (Boolean.parseBoolean(Activator.this.bundleContext.getProperty("karaf.startRemoteShell")))
{
+    @Override
+    protected void doStart() throws Exception {
+        SessionFactory sf = getTrackedService(SessionFactory.class);
+        if (sf == null) {
+            return;
+        }
+
+        sessionFactory = sf;
+        sessionFactory.getRegistry().register(sshClientFactory);
+        sessionFactory.getRegistry().getService(Manager.class).register(SshAction.class);
+        if (Boolean.parseBoolean(bundleContext.getProperty("karaf.startRemoteShell"))) {
             server = createSshServer(sessionFactory);
             try {
                 server.start();
@@ -137,77 +113,22 @@ public class Activator implements BundleActivator, ManagedService {
         }
     }
 
-    private void unbindSessionFactory() {
-        this.sessionFactory.getRegistry().getService(Manager.class).unregister(SshAction.class);
-        this.sessionFactory.getRegistry().getService(Manager.class).unregister(SshServerAction.class);
-        this.sessionFactory.getRegistry().unregister(sshClientFactory);
-        this.sessionFactory.getRegistry().unregister(sshServerFactory);
-        SshServer srv = server;
-        server = null;
-        if (srv != null) {
+    @Override
+    protected void doStop() {
+        if (sessionFactory != null) {
+            sessionFactory.getRegistry().getService(Manager.class).unregister(SshAction.class);
+            sessionFactory.getRegistry().unregister(sshClientFactory);
+            sessionFactory = null;
+        }
+        if (server != null) {
             try {
-                srv.stop();
+                server.stop();
             } catch (InterruptedException e) {
                 LOGGER.warn("Exception caught while stopping SSH server", e);
             }
+            server = null;
         }
-    }
-
-    public void stop(BundleContext context) {
-        registration.unregister();
-        sessionTracker.close();
-        sessionFactoryTracker.close();
-        synchronized (servers) {
-            for (SshServer server : servers) {
-                try {
-                    server.stop();
-                } catch (InterruptedException e) {
-                    LOGGER.warn("Exception caught while stopping SSH server", e);
-                }
-            }
-        }
-    }
-
-    @Override
-    public void updated(Dictionary<String, ?> configuration) throws ConfigurationException
{
-        this.configuration = configuration;
-    }
-
-    private int getInt(String key, int def) {
-        Dictionary<String, ?> config = this.configuration;
-        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(String key, long def) {
-        Dictionary<String, ?> config = this.configuration;
-        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 String getString(String key, String def) {
-        Dictionary<String, ?> config = this.configuration;
-        if (config != null) {
-            Object val = config.get(key);
-            if (val != null) {
-                return val.toString();
-            }
-        }
-        return def;
+        super.doStop();
     }
 
     protected SshServer createSshServer(SessionFactory sessionFactory) {
@@ -248,9 +169,6 @@ public class Activator implements BundleActivator, ManagedService {
         server.setAgentFactory(agentFactory);
         server.getProperties().put(SshServer.IDLE_TIMEOUT, Long.toString(sshIdleTimeout));
 
-        synchronized (servers) {
-            servers.add(server);
-        }
         return server;
     }
 


Mime
View raw message