Author: [email protected] Date: Thu Oct 27 13:09:40 2011 New Revision: 1667
Log: AMDATU-433 split off the console logger Added: trunk/amdatu-core/log-console/ trunk/amdatu-core/log-console/pom.xml trunk/amdatu-core/log-console/src/ trunk/amdatu-core/log-console/src/main/ trunk/amdatu-core/log-console/src/main/java/ trunk/amdatu-core/log-console/src/main/java/org/ trunk/amdatu-core/log-console/src/main/java/org/amdatu/ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/osgi/ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/osgi/Activator.java trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/service/ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/service/ConsoleLogHandler.java trunk/amdatu-core/log-console/src/main/resources/ trunk/amdatu-core/log-console/src/main/resources/OSGI-INF/ trunk/amdatu-core/log-console/src/main/resources/OSGI-INF/metatype/ trunk/amdatu-core/log-console/src/main/resources/OSGI-INF/metatype/metatype.xml Added: trunk/amdatu-core/log-console/pom.xml ============================================================================== --- (empty file) +++ trunk/amdatu-core/log-console/pom.xml Thu Oct 27 13:09:40 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.console</artifactId> + <packaging>bundle</packaging> + <name>Amdatu Core - Log Console</name> + <description>The log console bundle sends log messages to the console.</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.console.osgi.Activator</Bundle-Activator> + <Bundle-SymbolicName>org.amdatu.core.log.console</Bundle-SymbolicName> + <Embed-Dependency>*;scope=compile</Embed-Dependency> + <Embed-Transitive>true</Embed-Transitive> + </instructions> + </configuration> + </plugin> + </plugins> + </build> +</project> Added: trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/osgi/Activator.java ============================================================================== --- (empty file) +++ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/osgi/Activator.java Thu Oct 27 13:09:40 2011 @@ -0,0 +1,50 @@ +/* + * 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.log.console.osgi; + +import java.util.Properties; + +import org.amdatu.core.log.console.service.ConsoleLogHandler; +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.cm.ManagedService; +import org.osgi.service.log.LogReaderService; + +/** + * + */ +public class Activator extends DependencyActivatorBase { + + @Override + public void init(BundleContext context, DependencyManager manager) throws Exception { + Properties props = new Properties(); + props.put(Constants.SERVICE_PID, ConsoleLogHandler.PID); + manager.add(createComponent() + .setInterface(ManagedService.class.getName(), props) + .setImplementation(ConsoleLogHandler.class) + .add(createServiceDependency() + .setService(LogReaderService.class) + .setRequired(false) + .setCallbacks("logReaderAdded", "logReaderRemoved")) + ); + } + + @Override + public void destroy(BundleContext context, DependencyManager manager) throws Exception { + } +} Added: trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/service/ConsoleLogHandler.java ============================================================================== --- (empty file) +++ trunk/amdatu-core/log-console/src/main/java/org/amdatu/core/log/console/service/ConsoleLogHandler.java Thu Oct 27 13:09:40 2011 @@ -0,0 +1,91 @@ +/* + * 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.log.console.service; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Map; + +import org.osgi.service.cm.ConfigurationException; +import org.osgi.service.cm.ManagedService; +import org.osgi.service.log.LogEntry; +import org.osgi.service.log.LogListener; +import org.osgi.service.log.LogReaderService; +import org.osgi.service.log.LogService; + +/** + * Handles log messages and prints them to the console. + */ +public class ConsoleLogHandler implements LogListener, ManagedService { + public static final String PID = "org.amdatu.core.log.console"; + private static int DEFAULT_LOG_LEVEL = LogService.LOG_DEBUG; + private static final Map<Integer, String> LOG_LEVELS; + private static final String MIN_LOGLEVEL_KEY = "console.mininum.loglevel"; + static { + LOG_LEVELS = new HashMap<Integer, String>(); + LOG_LEVELS.put(LogService.LOG_DEBUG, "DEBUG"); + LOG_LEVELS.put(LogService.LOG_INFO, "INFO"); + LOG_LEVELS.put(LogService.LOG_WARNING, "WARNING"); + LOG_LEVELS.put(LogService.LOG_ERROR, "ERROR"); + } + private final SimpleDateFormat m_formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private volatile int m_minLogLevel = DEFAULT_LOG_LEVEL; + + public void logReaderAdded(LogReaderService reader) { + reader.addLogListener(this); + } + + public void logReaderRemoved(LogReaderService reader) { + reader.removeLogListener(this); + } + + public void logged(LogEntry entry) { + int level = entry.getLevel(); + if (level <= m_minLogLevel) { + String levelName = LOG_LEVELS.get(level); + System.out.println("[" + m_formatter.format(new Date(entry.getTime())) + "] " + levelName + ": " + entry.getMessage() + " [" + entry.getBundle().getSymbolicName() + "]"); + if (entry.getException() != null) { + entry.getException().printStackTrace(System.out); + } + } + } + + public void updated(Dictionary properties) throws ConfigurationException { + m_minLogLevel = getLogLevelFromProperties(properties); + } + + private int getLogLevelFromProperties(Dictionary dictionary) { + if (dictionary == null) { + return DEFAULT_LOG_LEVEL; + } + + String minLogLevel = (String) dictionary.get(MIN_LOGLEVEL_KEY); + if (minLogLevel == null) { + return DEFAULT_LOG_LEVEL; + } + + for (Map.Entry<Integer, String> logLevelEntry : LOG_LEVELS.entrySet()) { + if (minLogLevel.equalsIgnoreCase(logLevelEntry.getValue())) { + return logLevelEntry.getKey(); + } + } + + // If we get here, there is no other way we can find the log level + return DEFAULT_LOG_LEVEL; + } +} Added: trunk/amdatu-core/log-console/src/main/resources/OSGI-INF/metatype/metatype.xml ============================================================================== --- (empty file) +++ trunk/amdatu-core/log-console/src/main/resources/OSGI-INF/metatype/metatype.xml Thu Oct 27 13:09:40 2011 @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0"> + <OCD id="org.amdatu.core.log.console" name="Amdatu Log Console"> + <AD id="console.mininum.loglevel" name="console.mininum.loglevel" type="String" description="The loglevel of messages that will be printed to the console. Can be DEBUG, INFO, WARNING or ERROR. For example if 'INFO' is defined, only log messages will be printed in the console which have a loglevel of INFO or higher (WARNING or ERROR)." /> + </OCD> + <Designate pid="org.amdatu.core.log.console"> + <Object ocdref="org.amdatu.core.log.console" /> + </Designate> +</metatype:MetaData> \ No newline at end of file _______________________________________________ Amdatu-commits mailing list [email protected] http://lists.amdatu.org/mailman/listinfo/amdatu-commits
