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

[20/24] git commit: [KARAF-2833] Make log/core independent of blueprint

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

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

----------------------------------------------------------------------
 .../standard/src/main/feature/feature.xml       |   1 -
 log/core/pom.xml                                |  12 +-
 .../karaf/log/core/internal/osgi/Activator.java | 235 +++++++++++++++++++
 .../resources/OSGI-INF/blueprint/blueprint.xml  |  75 ------
 4 files changed, 246 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/60c0f820/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 dff6bb2..8979b32 100644
--- a/assemblies/features/standard/src/main/feature/feature.xml
+++ b/assemblies/features/standard/src/main/feature/feature.xml
@@ -155,7 +155,6 @@
     </feature>
 
     <feature name="log" description="Provide Log support" version="${project.version}">
-        <feature version="${project.version}">aries-blueprint</feature>
         <bundle start-level="30" start="true">mvn:org.apache.karaf.log/org.apache.karaf.log.core/${project.version}</bundle>
         <bundle start-level="30" start="true">mvn:org.apache.karaf.log/org.apache.karaf.log.command/${project.version}</bundle>
     </feature>

http://git-wip-us.apache.org/repos/asf/karaf/blob/60c0f820/log/core/pom.xml
----------------------------------------------------------------------
diff --git a/log/core/pom.xml b/log/core/pom.xml
index 2bb59e0..261e896 100644
--- a/log/core/pom.xml
+++ b/log/core/pom.xml
@@ -59,6 +59,11 @@
             <artifactId>pax-logging-api</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf</groupId>
+            <artifactId>org.apache.karaf.util</artifactId>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 
     <build>
@@ -91,8 +96,13 @@
                         </Import-Package>
                         <Private-Package>
                             org.apache.karaf.log.core.internal,
-                            org.apache.karaf.log.core.internal.layout
+                            org.apache.karaf.log.core.internal.layout,
+                            org.apache.karaf.log.core.internal.osgi,
+                            org.apache.karaf.util.tracker
                         </Private-Package>
+                        <Bundle-Activator>
+                            org.apache.karaf.log.core.internal.osgi.Activator
+                        </Bundle-Activator>
                     </instructions>
                 </configuration>
             </plugin>

http://git-wip-us.apache.org/repos/asf/karaf/blob/60c0f820/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
new file mode 100644
index 0000000..d9085fc
--- /dev/null
+++ b/log/core/src/main/java/org/apache/karaf/log/core/internal/osgi/Activator.java
@@ -0,0 +1,235 @@
+/*
+ * 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.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;
+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.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<String, ?> configuration;
+    private ServiceRegistration managedServiceRegistration;
+    private SingleServiceTracker<ConfigurationAdmin> configAdminTracker;
+    private ServiceRegistration<LogService> serviceRegistration;
+    private ServiceRegistration<LogEventFormatter> formatterRegistration;
+    private ServiceRegistration<PaxAppender> appenderRegistration;
+    private ServiceRegistration mbeanRegistration;
+
+    @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.log");
+        managedServiceRegistration = bundleContext.registerService(ManagedService.class, this, props);
+
+        configAdminTracker = new SingleServiceTracker<ConfigurationAdmin>(
+                bundleContext, ConfigurationAdmin.class, this);
+        configAdminTracker.open();
+
+        scheduled.set(false);
+        reconfigure();
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+        configAdminTracker.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 {
+        ConfigurationAdmin configurationAdmin = configAdminTracker != null ? configAdminTracker.getService() : null;
+        Dictionary<String, ?> config = configuration;
+        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");
+
+        LruList events = new LruList(size);
+        Hashtable<String, Object> props = new Hashtable<String, Object>();
+        props.put("org.ops4j.pax.logging.appender.name", "VmLogAppender");
+        appenderRegistration = bundleContext.registerService(
+                PaxAppender.class, events, props);
+
+        LogEventFormatterImpl formatter = new LogEventFormatterImpl();
+        formatter.setPattern(pattern);
+        formatter.setFatalColor(fatalColor);
+        formatter.setErrorColor(errorColor);
+        formatter.setWarnColor(warnColor);
+        formatter.setInfoColor(infoColor);
+        formatter.setDebugColor(debugColor);
+        formatter.setTraceColor(traceColor);
+        formatterRegistration = bundleContext.registerService(
+                LogEventFormatter.class, formatter, null);
+
+        LogServiceImpl logService = new LogServiceImpl(configurationAdmin, events);
+        serviceRegistration = bundleContext.registerService(
+                LogService.class, logService, null);
+
+
+        try {
+            LogMBeanImpl securityMBean = new LogMBeanImpl(logService);
+            props = new Hashtable<String, Object>();
+            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<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 int getInt(Dictionary<String, ?> 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<String, ?> 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/60c0f820/log/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/log/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/log/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
deleted file mode 100644
index f9b0ee4..0000000
--- a/log/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ /dev/null
@@ -1,75 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-   Licensed 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:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
-    xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0">
-
-    <ext:property-placeholder placeholder-prefix="$(" placeholder-suffix=")"/>
-
-    <cm:property-placeholder persistent-id="org.apache.karaf.log" update-strategy="reload">
-        <cm:default-properties>
-            <cm:property name="size" value="500"/>
-            <cm:property name="pattern" value="%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n"/>
-            <cm:property name="fatalColor" value="31"/>
-            <cm:property name="errorColor" value="31"/>
-            <cm:property name="warnColor" value="35"/>
-            <cm:property name="infoColor" value="36"/>
-            <cm:property name="debugColor" value="39"/>
-            <cm:property name="traceColor" value="39"/>
-        </cm:default-properties>
-    </cm:property-placeholder>
-    
-    <reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"/>
-    
-    <bean id="events" class="org.apache.karaf.log.core.internal.LruList">
-        <argument value="${size}"/>
-    </bean>
-    
-    <service ref="events" interface="org.ops4j.pax.logging.spi.PaxAppender">
-        <service-properties>
-                <entry key="org.ops4j.pax.logging.appender.name" value="VmLogAppender"/>
-        </service-properties>
-    </service>
-    
-    <bean id="logService" class="org.apache.karaf.log.core.internal.LogServiceImpl">
-        <argument ref="configAdmin"/>
-        <argument ref="events" />
-    </bean>
-    
-    <service ref="logService" interface="org.apache.karaf.log.core.LogService"/>
-
-    <bean id="logMBean" class="org.apache.karaf.log.core.internal.LogMBeanImpl">
-        <argument ref="logService"/>
-    </bean>
-
-    <service ref="logMBean" auto-export="interfaces">
-        <service-properties>
-            <entry key="jmx.objectname" value="org.apache.karaf:type=log,name=$(karaf.name)"/>
-        </service-properties>
-    </service>
-    
-    <bean id="formatter" class="org.apache.karaf.log.core.internal.LogEventFormatterImpl">
-        <property name="pattern" value="${pattern}"/>
-        <property name="fatalColor" value="${fatalColor}"/>
-        <property name="errorColor" value="${errorColor}"/>
-        <property name="warnColor" value="${warnColor}"/>
-        <property name="infoColor" value="${infoColor}"/>
-        <property name="debugColor" value="${debugColor}"/>
-        <property name="traceColor" value="${traceColor}"/>
-    </bean>
-    
-    <service ref="formatter" interface="org.apache.karaf.log.core.LogEventFormatter"/>
-
-</blueprint>
\ No newline at end of file