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:00 UTC

[02/24] git commit: [KARAF-2833] Make config/core independent of blueprint

[KARAF-2833] Make config/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/5db77ebe
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/5db77ebe
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/5db77ebe

Branch: refs/heads/master
Commit: 5db77ebe8980f0b54dc5ec15b44b9ffd2c0c7867
Parents: 766e7dd
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Fri Mar 21 18:18:32 2014 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Mon Mar 24 17:30:12 2014 +0100

----------------------------------------------------------------------
 .../standard/src/main/feature/feature.xml       |   1 -
 config/core/pom.xml                             |  12 +-
 .../karaf/config/core/impl/osgi/Activator.java  | 148 +++++++++++++++++++
 .../resources/OSGI-INF/blueprint/blueprint.xml  |  44 ------
 4 files changed, 159 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/5db77ebe/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 0bfb302..8656d87 100644
--- a/assemblies/features/standard/src/main/feature/feature.xml
+++ b/assemblies/features/standard/src/main/feature/feature.xml
@@ -130,7 +130,6 @@
     </feature>
 
     <feature name="config" description="Provide OSGi ConfigAdmin support" version="${project.version}">
-        <feature version="${project.version}">aries-blueprint</feature>
         <bundle start-level="30" start="true">mvn:org.apache.karaf.config/org.apache.karaf.config.core/${project.version}</bundle>
         <bundle start-level="30" start="true">mvn:org.apache.karaf.config/org.apache.karaf.config.command/${project.version}</bundle>
     </feature>

http://git-wip-us.apache.org/repos/asf/karaf/blob/5db77ebe/config/core/pom.xml
----------------------------------------------------------------------
diff --git a/config/core/pom.xml b/config/core/pom.xml
index 8fd5cc3..3fc5286 100644
--- a/config/core/pom.xml
+++ b/config/core/pom.xml
@@ -53,6 +53,11 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.karaf</groupId>
+            <artifactId>org.apache.karaf.util</artifactId>
+        </dependency>
         
         <dependency>
             <groupId>org.slf4j</groupId>
@@ -90,8 +95,13 @@
                             *
                         </Import-Package>
                         <Private-Package>
-                            org.apache.karaf.config.core.impl
+                            org.apache.karaf.config.core.impl,
+                            org.apache.karaf.config.core.impl.osgi,
+                            org.apache.karaf.util.tracker
                         </Private-Package>
+                        <Bundle-Activator>
+                            org.apache.karaf.config.core.impl.osgi.Activator
+                        </Bundle-Activator>
                     </instructions>
                 </configuration>
             </plugin>

http://git-wip-us.apache.org/repos/asf/karaf/blob/5db77ebe/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
new file mode 100644
index 0000000..e8d3dd8
--- /dev/null
+++ b/config/core/src/main/java/org/apache/karaf/config/core/impl/osgi/Activator.java
@@ -0,0 +1,148 @@
+/*
+ * 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.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.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<ConfigurationAdmin> configurationAdminTracker;
+
+    private ServiceRegistration<ConfigRepository> configRepositoryRegistration;
+    private ServiceRegistration configRepositoryMBeanRegistration;
+
+    @Override
+    public void start(BundleContext context) throws Exception {
+        bundleContext = context;
+        configurationAdminTracker = new SingleServiceTracker<ConfigurationAdmin>(
+                bundleContext, ConfigurationAdmin.class, this
+        );
+        configurationAdminTracker.open();
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+        configurationAdminTracker.close();
+        executor.shutdown();
+        executor.awaitTermination(30, TimeUnit.SECONDS);
+    }
+
+    protected void doStart() {
+        ConfigurationAdmin configurationAdmin = configurationAdminTracker.getService();
+
+        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<String, Object> props = new Hashtable<String, Object>();
+            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<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 String getString(Properties configuration, String key, String value) {
+        return configuration.getProperty(key, value);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/5db77ebe/config/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/config/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/config/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
deleted file mode 100644
index 6433ed8..0000000
--- a/config/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ /dev/null
@@ -1,44 +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"
-           default-activation="lazy">
-
-    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
-
-    <bean id="configRepo" class="org.apache.karaf.config.core.impl.ConfigRepositoryImpl">
-        <argument ref="configAdmin"/>
-    </bean>
-    
-    <reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"  />
-
-    <service interface="org.apache.karaf.config.core.ConfigRepository" ref="configRepo"/>
-
-    <bean id="configMBean" class="org.apache.karaf.config.core.impl.ConfigMBeanImpl">
-        <property name="configRepo" ref="configRepo"/>
-    </bean>
-
-    <service ref="configMBean" auto-export="interfaces">
-        <service-properties>
-            <entry key="jmx.objectname" value="org.apache.karaf:type=config,name=$[karaf.name]"/>
-        </service-properties>
-    </service>
-
-</blueprint>