Author: [email protected]
Date: Thu Oct 27 13:11:01 2011
New Revision: 1668

Log:
AMDATU-433 split off the jdk log forwarder

Added:
   trunk/amdatu-core/log-forwarder-jdk/
   trunk/amdatu-core/log-forwarder-jdk/pom.xml
   trunk/amdatu-core/log-forwarder-jdk/src/
   trunk/amdatu-core/log-forwarder-jdk/src/main/
   trunk/amdatu-core/log-forwarder-jdk/src/main/java/
   trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/
   trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/
   trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/
   trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/
   
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/osgi/
   
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/osgi/Activator.java
   
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/service/
   
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/service/JdkLogForwarder.java

Added: trunk/amdatu-core/log-forwarder-jdk/pom.xml
==============================================================================
--- (empty file)
+++ trunk/amdatu-core/log-forwarder-jdk/pom.xml Thu Oct 27 13:11:01 2011
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright (c) 2010, 2011 The Amdatu Foundation
+
+  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.verning permissions and limitations
+  under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu.core</groupId>
+    <artifactId>org.amdatu.core</artifactId>
+    <version>0.2.1-SNAPSHOT</version>
+  </parent>
+  <artifactId>org.amdatu.core.log.forwarder.jdk</artifactId>
+  <packaging>bundle</packaging>
+  <name>Amdatu Core - Log Forwarder JDK</name>
+  <description>The log forwarder for JDK forwards standard JDK logging to the 
OSGi LogService</description>
+  
+  <dependencies> 
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.1.1</version>
+      <scope>compile</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <configuration>
+          <instructions>
+            
<Bundle-Activator>org.amdatu.core.log.forwarder.jdk.osgi.Activator</Bundle-Activator>
+            
<Bundle-SymbolicName>org.amdatu.core.log.forwarder.jdk</Bundle-SymbolicName>
+            <Embed-Dependency>*;scope=compile</Embed-Dependency>
+            <Embed-Transitive>true</Embed-Transitive>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build> 
+</project>

Added: 
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/osgi/Activator.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/osgi/Activator.java
    Thu Oct 27 13:11:01 2011
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ * 
+ * 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.
+ */
+package org.amdatu.core.loghandler.osgi;
+
+import org.amdatu.core.loghandler.service.JdkLogForwarder;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.log.LogService;
+
+/**
+ * This is the OSGi activator for this log handler bundle.
+ * @author ivol
+ */
+public class Activator extends DependencyActivatorBase {
+
+    @Override
+    public void init(BundleContext context, DependencyManager manager) throws 
Exception {
+        // Register the JDKLog bridge
+        manager.add(createComponent()
+            .setImplementation(JdkLogForwarder.class)
+            .add(createServiceDependency()
+                       .setService(LogService.class)
+                       .setRequired(true))
+            .add(createServiceDependency()
+                       .setService("(" + Constants.OBJECTCLASS + "=*)")
+                       .setRequired(false)
+                       .setCallbacks("onAdded", "onRemoved"))
+               );
+    }
+
+    @Override
+    public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
+    }
+}

Added: 
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/service/JdkLogForwarder.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-core/log-forwarder-jdk/src/main/java/org/amdatu/core/loghandler/service/JdkLogForwarder.java
   Thu Oct 27 13:11:01 2011
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ * 
+ * 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.
+ */
+package org.amdatu.core.loghandler.service;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogService;
+
+/**
+ * This class forwards JDK log messages to the OSGi log service.
+ * 
+ * @author ivol
+ */
+public class JdkLogForwarder extends Handler {
+
+       private static final Map<Level, Integer> LOG_LEVEL_MAPPING = new 
HashMap<Level, Integer>();
+       static {
+               LOG_LEVEL_MAPPING.put(Level.SEVERE, LogService.LOG_ERROR);
+               LOG_LEVEL_MAPPING.put(Level.WARNING, LogService.LOG_WARNING);
+               LOG_LEVEL_MAPPING.put(Level.INFO, LogService.LOG_INFO);
+               LOG_LEVEL_MAPPING.put(Level.CONFIG, LogService.LOG_INFO);
+       }
+
+       private volatile LogService m_log;
+       private final List<String> m_loggerNames;
+
+       public JdkLogForwarder() {
+               m_loggerNames = new ArrayList<String>();
+       }
+
+       public synchronized void start() {
+               for (String loggerName : m_loggerNames) {
+                       Logger logger = Logger.getLogger(loggerName);
+                       logger.setUseParentHandlers(false);
+                       logger.addHandler(this);
+               }
+       }
+
+       public synchronized void stop() {
+               for (String loggerName : m_loggerNames) {
+                       Logger.getLogger(loggerName).removeHandler(this);
+               }
+       }
+
+       public synchronized void onAdded(ServiceReference ref, Object service) {
+               // We need to get all Java util logging loggers for this service
+               LogManager logManager = LogManager.getLogManager();
+               Enumeration<String> loggerNames = logManager.getLoggerNames();
+               while (loggerNames.hasMoreElements()) {
+                       String loggerName = loggerNames.nextElement();
+                       if (!m_loggerNames.contains(loggerName)) {
+                               m_loggerNames.add(loggerName);
+                               Logger logger = 
logManager.getLogger(loggerName);
+                               if (logger != null) {
+                                       logger.setUseParentHandlers(false);
+                                       
+                                       // Remove all current log handlers, it 
should be only me!
+                                       if (logger.getHandlers() != null) {
+                                               for (Handler logHandler : 
logger.getHandlers()) {
+                                                       
logger.removeHandler(logHandler);
+                                               }
+                                       }
+                                       
+                                       // Add me
+                                       logger.addHandler(this);
+                               }
+                       }
+               }
+       }
+
+       public void onRemoved(ServiceReference ref, Object service) {
+       }
+
+       @Override
+       public void publish(LogRecord record) {
+               if (record.getLevel() == Level.OFF) {
+                       return;
+               }
+
+               if (record.getThrown() != null) {
+                       m_log.log(getToOsgiLogLevel(record.getLevel()),
+                                       record.getMessage(), 
record.getThrown());
+               } else {
+                       m_log
+                                       
.log(getToOsgiLogLevel(record.getLevel()), record
+                                                       .getMessage());
+               }
+       }
+
+       @Override
+       public void close() throws SecurityException {
+       }
+
+       @Override
+       public void flush() {
+       }
+
+       private int getToOsgiLogLevel(Level level) {
+               if (LOG_LEVEL_MAPPING.containsKey(level)) {
+                       return LOG_LEVEL_MAPPING.get(level);
+               } else {
+                       return LogService.LOG_DEBUG;
+               }
+       }
+
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to