* POM cleanup
* Made sure OSGi logger looks up the LogService dynamically
This commit is contained in:
parent
93cec1c6d5
commit
6bd8933532
4
pom.xml
4
pom.xml
@ -530,12 +530,10 @@
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
<checksumPolicy>ignore</checksumPolicy>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>interval:10080</updatePolicy>
|
||||
<checksumPolicy>ignore</checksumPolicy>
|
||||
</releases>
|
||||
<id>jboss.release</id>
|
||||
<name>JBoss releases</name>
|
||||
@ -548,12 +546,10 @@
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
<checksumPolicy>ignore</checksumPolicy>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>interval:10080</updatePolicy>
|
||||
<checksumPolicy>ignore</checksumPolicy>
|
||||
</releases>
|
||||
<id>jboss.release</id>
|
||||
<name>JBoss releases</name>
|
||||
|
@ -35,15 +35,12 @@ 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)
|
||||
@ -56,10 +53,14 @@ public class NettyBundleActivator implements BundleActivator {
|
||||
new ArrayList<ServiceRegistration>();
|
||||
|
||||
private Executor executor;
|
||||
private OsgiLoggerFactory loggerFactory;
|
||||
|
||||
public void start(BundleContext ctx) throws Exception {
|
||||
initLoggerFactory(ctx);
|
||||
// Switch the internal logger to the OSGi LogService.
|
||||
loggerFactory = new OsgiLoggerFactory(ctx);
|
||||
InternalLoggerFactory.setDefaultFactory(loggerFactory);
|
||||
|
||||
// Prepare the resources required for creating ChannelFactories.
|
||||
Executor executor = this.executor = Executors.newCachedThreadPool();
|
||||
|
||||
// The default transport is NIO.
|
||||
@ -80,33 +81,12 @@ public class NettyBundleActivator implements BundleActivator {
|
||||
ExecutorShutdownUtil.shutdown(executor);
|
||||
executor = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void initLoggerFactory(BundleContext ctx) {
|
||||
ServiceReference logServiceRef =
|
||||
ctx.getServiceReference("org.osgi.service.log.LogService");
|
||||
if (logServiceRef == null) {
|
||||
// LogService is not available.
|
||||
return;
|
||||
if (loggerFactory != null) {
|
||||
InternalLoggerFactory.setDefaultFactory(loggerFactory.getFallback());
|
||||
loggerFactory.destroy();
|
||||
loggerFactory = null;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -22,7 +22,6 @@
|
||||
*/
|
||||
package org.jboss.netty.logging;
|
||||
|
||||
import org.osgi.framework.ServiceReference;
|
||||
import org.osgi.service.log.LogService;
|
||||
|
||||
/**
|
||||
@ -36,49 +35,70 @@ import org.osgi.service.log.LogService;
|
||||
*/
|
||||
class OsgiLogger implements InternalLogger {
|
||||
|
||||
private final LogService logService;
|
||||
private final ServiceReference serviceRef;
|
||||
private final OsgiLoggerFactory parent;
|
||||
private final InternalLogger fallback;
|
||||
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;
|
||||
OsgiLogger(OsgiLoggerFactory parent, String name, InternalLogger fallback) {
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
this.fallback = fallback;
|
||||
prefix = "[" + name + "] ";
|
||||
}
|
||||
|
||||
public void debug(String msg) {
|
||||
logService.log(serviceRef, LogService.LOG_DEBUG, prefix + msg);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_DEBUG, prefix + msg);
|
||||
} else {
|
||||
fallback.debug(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void debug(String msg, Throwable cause) {
|
||||
logService.log(serviceRef, LogService.LOG_DEBUG, prefix + msg, cause);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_DEBUG, prefix + msg, cause);
|
||||
} else {
|
||||
fallback.debug(msg, cause);
|
||||
}
|
||||
}
|
||||
|
||||
public void error(String msg) {
|
||||
logService.log(serviceRef, LogService.LOG_ERROR, prefix + msg);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_ERROR, prefix + msg);
|
||||
} else {
|
||||
fallback.error(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void error(String msg, Throwable cause) {
|
||||
logService.log(serviceRef, LogService.LOG_ERROR, prefix + msg, cause);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_ERROR, prefix + msg, cause);
|
||||
} else {
|
||||
fallback.error(msg, cause);
|
||||
}
|
||||
}
|
||||
|
||||
public void info(String msg) {
|
||||
logService.log(serviceRef, LogService.LOG_INFO, prefix + msg);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_INFO, prefix + msg);
|
||||
} else {
|
||||
fallback.info(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void info(String msg, Throwable cause) {
|
||||
logService.log(serviceRef, LogService.LOG_INFO, prefix + msg, cause);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_INFO, prefix + msg, cause);
|
||||
} else {
|
||||
fallback.info(msg, cause);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
@ -98,11 +118,21 @@ class OsgiLogger implements InternalLogger {
|
||||
}
|
||||
|
||||
public void warn(String msg) {
|
||||
logService.log(serviceRef, LogService.LOG_WARNING, prefix + msg);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_WARNING, prefix + msg);
|
||||
} else {
|
||||
fallback.warn(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void warn(String msg, Throwable cause) {
|
||||
logService.log(serviceRef, LogService.LOG_WARNING, prefix + msg, cause);
|
||||
LogService logService = parent.getLogService();
|
||||
if (logService != null) {
|
||||
logService.log(LogService.LOG_WARNING, prefix + msg, cause);
|
||||
} else {
|
||||
fallback.warn(msg, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,10 +22,10 @@
|
||||
*/
|
||||
package org.jboss.netty.logging;
|
||||
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
import org.osgi.service.log.LogService;
|
||||
|
||||
|
||||
import org.osgi.util.tracker.ServiceTracker;
|
||||
|
||||
/**
|
||||
* Logger factory which creates an <a href="http://www.osgi.org/">OSGi</a>
|
||||
@ -39,23 +39,59 @@ import org.osgi.service.log.LogService;
|
||||
*/
|
||||
public class OsgiLoggerFactory extends InternalLoggerFactory {
|
||||
|
||||
private final LogService logService;
|
||||
private final ServiceReference serviceRef;
|
||||
private final ServiceTracker logServiceTracker;
|
||||
private final InternalLoggerFactory fallback;
|
||||
volatile LogService logService;
|
||||
|
||||
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;
|
||||
public OsgiLoggerFactory(BundleContext ctx) {
|
||||
this(ctx, null);
|
||||
}
|
||||
|
||||
public OsgiLoggerFactory(BundleContext ctx, InternalLoggerFactory fallback) {
|
||||
if (ctx == null) {
|
||||
throw new NullPointerException("ctx");
|
||||
}
|
||||
if (fallback == null) {
|
||||
fallback = InternalLoggerFactory.getDefaultFactory();
|
||||
if (fallback instanceof OsgiLoggerFactory) {
|
||||
fallback = new JdkLoggerFactory();
|
||||
}
|
||||
}
|
||||
|
||||
this.fallback = fallback;
|
||||
logServiceTracker = new ServiceTracker(
|
||||
ctx, "org.osgi.service.log.LogService", null) {
|
||||
@Override
|
||||
public Object addingService(ServiceReference reference) {
|
||||
LogService service = (LogService) super.addingService(reference);
|
||||
logService = service;
|
||||
return service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removedService(ServiceReference reference,
|
||||
Object service) {
|
||||
logService = null;
|
||||
}
|
||||
};
|
||||
logServiceTracker.open();
|
||||
}
|
||||
|
||||
public InternalLoggerFactory getFallback() {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
public LogService getLogService() {
|
||||
return logService;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
logService = null;
|
||||
logServiceTracker.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InternalLogger newInstance(String name) {
|
||||
return new OsgiLogger(logService, serviceRef, name);
|
||||
return new OsgiLogger(this, name, fallback.newInstance(name));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user