diff --git a/pom.xml b/pom.xml index 4c673c547a..4d63f9e9df 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,13 @@ compile true + + org.apache.felix + org.osgi.compendium + 1.2.0 + compile + true + com.google.code.guice guice diff --git a/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java b/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java index d88b74ff9c..5bd95a8540 100644 --- a/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java +++ b/src/main/java/org/jboss/netty/container/osgi/NettyBundleActivator.java @@ -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"); diff --git a/src/main/java/org/jboss/netty/logging/OsgiLogger.java b/src/main/java/org/jboss/netty/logging/OsgiLogger.java new file mode 100644 index 0000000000..ed2e6ef178 --- /dev/null +++ b/src/main/java/org/jboss/netty/logging/OsgiLogger.java @@ -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; + +/** + * OSGi {@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; + } +} \ No newline at end of file diff --git a/src/main/java/org/jboss/netty/logging/OsgiLoggerFactory.java b/src/main/java/org/jboss/netty/logging/OsgiLoggerFactory.java new file mode 100644 index 0000000000..367abc3f1f --- /dev/null +++ b/src/main/java/org/jboss/netty/logging/OsgiLoggerFactory.java @@ -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 OSGi + * {@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); + } +}