Better OSGi integration - Netty will use OSGi LogService when it's running on top of an OSGi framework

This commit is contained in:
Trustin Lee 2009-01-12 10:20:44 +00:00
parent d7256bc737
commit 5c02a13be0
4 changed files with 212 additions and 0 deletions

View File

@ -58,6 +58,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.guice</groupId>
<artifactId>guice</artifactId>

View File

@ -35,10 +35,15 @@ import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.logging.OsgiLoggerFactory;
import org.jboss.netty.util.ExecutorShutdownUtil;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.log.LogService;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -53,6 +58,7 @@ public class NettyBundleActivator implements BundleActivator {
private Executor executor;
public void start(BundleContext ctx) throws Exception {
initLoggerFactory(ctx);
executor = Executors.newCachedThreadPool();
// The default transport is NIO.
@ -73,6 +79,32 @@ public class NettyBundleActivator implements BundleActivator {
executor = null;
}
private void initLoggerFactory(BundleContext ctx) {
ServiceReference logServiceRef = ctx.getServiceReference(LogService.class.getName());
if (logServiceRef == null) {
// LogService is not available.
return;
}
LogService logService;
try {
logService = (LogService) ctx.getService(logServiceRef);
} catch (Exception e) {
// Same name, different service
return;
}
Properties props = new Properties();
props.setProperty("category", "netty");
ServiceRegistration reg = ctx.registerService(
InternalLogger.class.getName(), new Object(), props);
registrations.add(reg);
InternalLoggerFactory.setDefaultFactory(
new OsgiLoggerFactory(logService, reg.getReference()));
}
private void register(BundleContext ctx, ChannelFactory factory, Class<?>... factoryTypes) {
Properties props = new Properties();
props.setProperty("category", "netty");

View File

@ -0,0 +1,112 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.logging;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
/**
* <a href="http://www.osgi.org/">OSGi</a> {@link LogService} logger.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*
*/
class OsgiLogger implements InternalLogger {
private final LogService logService;
private final ServiceReference serviceRef;
private final String name;
private final String prefix;
OsgiLogger(LogService logService, ServiceReference serviceRef, String name) {
if (logService == null) {
throw new NullPointerException("logService");
}
if (serviceRef == null) {
throw new NullPointerException("serviceRef");
}
if (name == null) {
throw new NullPointerException("name");
}
this.logService = logService;
this.serviceRef = serviceRef;
this.name = name;
prefix = "[" + name + "] ";
}
public void debug(String msg) {
logService.log(serviceRef, LogService.LOG_DEBUG, prefix + msg);
}
public void debug(String msg, Throwable cause) {
logService.log(serviceRef, LogService.LOG_DEBUG, prefix + msg, cause);
}
public void error(String msg) {
logService.log(serviceRef, LogService.LOG_ERROR, prefix + msg);
}
public void error(String msg, Throwable cause) {
logService.log(serviceRef, LogService.LOG_ERROR, prefix + msg, cause);
}
public void info(String msg) {
logService.log(serviceRef, LogService.LOG_INFO, prefix + msg);
}
public void info(String msg, Throwable cause) {
logService.log(serviceRef, LogService.LOG_INFO, prefix + msg, cause);
}
public boolean isDebugEnabled() {
return true;
}
public boolean isErrorEnabled() {
return true;
}
public boolean isInfoEnabled() {
return true;
}
public boolean isWarnEnabled() {
return true;
}
public void warn(String msg) {
logService.log(serviceRef, LogService.LOG_WARNING, prefix + msg);
}
public void warn(String msg, Throwable cause) {
logService.log(serviceRef, LogService.LOG_WARNING, prefix + msg, cause);
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.logging;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
/**
* Logger factory which creates an <a href="http://www.osgi.org/">OSGi</a>
* {@link LogService} logger.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*
*/
public class OsgiLoggerFactory extends InternalLoggerFactory {
private final LogService logService;
private final ServiceReference serviceRef;
public OsgiLoggerFactory(LogService logService, ServiceReference serviceRef) {
if (logService == null) {
throw new NullPointerException("logService");
}
if (serviceRef == null) {
throw new NullPointerException("serviceRef");
}
this.logService = logService;
this.serviceRef = serviceRef;
}
@Override
public InternalLogger newInstance(String name) {
return new OsgiLogger(logService, serviceRef, name);
}
}