You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2014/03/24 17:32:10 UTC

[12/24] git commit: [KARAF-2833] Make kar/core independent of blueprint

[KARAF-2833] Make kar/core independent of blueprint


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/04593bdc
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/04593bdc
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/04593bdc

Branch: refs/heads/master
Commit: 04593bdc393094c6da5321d0a0ca64e86e5feb79
Parents: 2a1815d
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Sun Mar 23 14:45:11 2014 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Mon Mar 24 17:30:13 2014 +0100

----------------------------------------------------------------------
 .../standard/src/main/feature/feature.xml       |   1 -
 kar/core/pom.xml                                |   5 +
 .../karaf/kar/internal/osgi/Activator.java      | 190 +++++++++++++++++++
 .../resources/OSGI-INF/blueprint/kar-core.xml   |  53 ------
 4 files changed, 195 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/04593bdc/assemblies/features/standard/src/main/feature/feature.xml
----------------------------------------------------------------------
diff --git a/assemblies/features/standard/src/main/feature/feature.xml b/assemblies/features/standard/src/main/feature/feature.xml
index 9064d05..dff6bb2 100644
--- a/assemblies/features/standard/src/main/feature/feature.xml
+++ b/assemblies/features/standard/src/main/feature/feature.xml
@@ -204,7 +204,6 @@
     </feature>
 
     <feature name="kar" description="Provide KAR (KARaf archive) support" version="${project.version}" resolver="(obr)">
-        <feature version="${project.version}">aries-blueprint</feature>
         <bundle start-level="30">mvn:org.apache.karaf.kar/org.apache.karaf.kar.core/${project.version}</bundle>
         <bundle start-level="30">mvn:org.apache.karaf.kar/org.apache.karaf.kar.command/${project.version}</bundle>
         <conditional>

http://git-wip-us.apache.org/repos/asf/karaf/blob/04593bdc/kar/core/pom.xml
----------------------------------------------------------------------
diff --git a/kar/core/pom.xml b/kar/core/pom.xml
index 2c43d38..c9d6e4e 100644
--- a/kar/core/pom.xml
+++ b/kar/core/pom.xml
@@ -101,9 +101,14 @@
                         </Export-Package>
                         <Private-Package>
                             org.apache.karaf.kar.internal,
+                            org.apache.karaf.kar.internal.osgi,
                             org.apache.karaf.util.maven,
+                            org.apache.karaf.util.tracker,
                             org.apache.felix.utils.properties
                         </Private-Package>
+                        <Bundle-Activator>
+                            org.apache.karaf.kar.internal.osgi.Activator
+                        </Bundle-Activator>
                     </instructions>
                 </configuration>
             </plugin>

http://git-wip-us.apache.org/repos/asf/karaf/blob/04593bdc/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
new file mode 100644
index 0000000..f0418cf
--- /dev/null
+++ b/kar/core/src/main/java/org/apache/karaf/kar/internal/osgi/Activator.java
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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.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<String, ?> configuration;
+    private ServiceRegistration<KarService> registration;
+    private ServiceRegistration mbeanRegistration;
+    private ServiceRegistration<ManagedService> managedServiceRegistration;
+    private SingleServiceTracker<FeaturesService> featuresServiceTracker;
+
+    @Override
+    public void start(BundleContext context) throws Exception {
+        bundleContext = context;
+        scheduled.set(true);
+
+        Hashtable<String, Object> props = new Hashtable<String, Object>();
+        props.put(Constants.SERVICE_PID, "org.apache.karaf.kar");
+        managedServiceRegistration = bundleContext.registerService(ManagedService.class, this, props);
+
+        featuresServiceTracker = new SingleServiceTracker<FeaturesService>(
+                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<String, ?> 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 doStart() throws Exception {
+        FeaturesService featuresService = featuresServiceTracker.getService();
+        Dictionary<String, ?> config = configuration;
+        if (featuresService == null) {
+            return;
+        }
+
+        boolean noAutoRefreshBundles = getBoolean(config, "noAutoRefreshBundles", false);
+
+        KarServiceImpl karService = new KarServiceImpl(
+                System.getProperty("karaf.base"),
+                featuresService
+        );
+        karService.setNoAutoRefreshBundles(noAutoRefreshBundles);
+        registration = bundleContext.registerService(KarService.class, karService, null);
+
+        try {
+            KarsMBeanImpl mbean = new KarsMBeanImpl();
+            mbean.setKarService(karService);
+            Hashtable<String, Object> props = new Hashtable<String, Object>();
+            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<String> names = new ArrayList<String>();
+        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<String> names, Class clazz) {
+        for (Class cl : clazz.getInterfaces()) {
+            names.add(cl.getName());
+            addSuperInterfaces(names, cl);
+        }
+    }
+
+    private boolean getBoolean(Dictionary<String, ?> 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;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/04593bdc/kar/core/src/main/resources/OSGI-INF/blueprint/kar-core.xml
----------------------------------------------------------------------
diff --git a/kar/core/src/main/resources/OSGI-INF/blueprint/kar-core.xml b/kar/core/src/main/resources/OSGI-INF/blueprint/kar-core.xml
deleted file mode 100644
index f3f824e..0000000
--- a/kar/core/src/main/resources/OSGI-INF/blueprint/kar-core.xml
+++ /dev/null
@@ -1,53 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-
-        Licensed to the Apache Software Foundation (ASF) under one or more
-        contributor license agreements. See the NOTICE file distributed with
-        this work for additional information regarding copyright ownership.
-        The ASF licenses this file to You under the Apache License, Version
-        2.0 (the "License"); you may not use this file except in compliance
-        with the License. You may obtain a copy of the License at
-
-        http://www.apache.org/licenses/LICENSE-2.0 Unless required by
-        applicable law or agreed to in writing, software distributed under the
-        License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-        CONDITIONS OF ANY KIND, either express or implied. See the License for
-        the specific language governing permissions and limitations under the
-        License.
-    -->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
-           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
-           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
-           default-activation="lazy">
-
-    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
-
-    <!-- AdminConfig property place holder for the org.apache.karaf.kar  -->
-    <cm:property-placeholder persistent-id="org.apache.karaf.kar" update-strategy="reload">
-        <cm:default-properties>
-            <cm:property name="noAutoRefreshBundles" value="false"/>
-        </cm:default-properties>
-    </cm:property-placeholder>
-    
-    <reference id="featuresService" interface="org.apache.karaf.features.FeaturesService"/>
-    <reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin" />
-
-    <bean id="karService" class="org.apache.karaf.kar.internal.KarServiceImpl">
-        <argument value="$[karaf.base]" />
-        <argument ref="featuresService" />
-        <property name="noAutoRefreshBundles" value="${noAutoRefreshBundles}"/>
-    </bean>
-    
-    <service ref="karService" interface="org.apache.karaf.kar.KarService"/>
-    
-    <bean id="mbeanImpl" class="org.apache.karaf.kar.internal.KarsMBeanImpl">
-        <property name="karService" ref="karService"/>
-    </bean>
-    
-    <service ref="mbeanImpl" auto-export="interfaces">
-        <service-properties>
-            <entry key="jmx.objectname" value="org.apache.karaf:type=kar,name=$[karaf.name]"/>
-        </service-properties>
-    </service>
-
-</blueprint>