1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.logging;
17
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20 import org.osgi.service.log.LogService;
21 import org.osgi.util.tracker.ServiceTracker;
22
23
24
25
26
27 public class OsgiLoggerFactory extends InternalLoggerFactory {
28
29 private final ServiceTracker logServiceTracker;
30 private final InternalLoggerFactory fallback;
31 volatile LogService logService;
32
33 public OsgiLoggerFactory(BundleContext ctx) {
34 this(ctx, null);
35 }
36
37 public OsgiLoggerFactory(BundleContext ctx, InternalLoggerFactory fallback) {
38 if (ctx == null) {
39 throw new NullPointerException("ctx");
40 }
41 if (fallback == null) {
42 fallback = InternalLoggerFactory.getDefaultFactory();
43 if (fallback instanceof OsgiLoggerFactory) {
44 fallback = new JdkLoggerFactory();
45 }
46 }
47
48 this.fallback = fallback;
49 logServiceTracker = new ServiceTracker(
50 ctx, "org.osgi.service.log.LogService", null) {
51 @Override
52 public Object addingService(ServiceReference reference) {
53 LogService service = (LogService) super.addingService(reference);
54 logService = service;
55 return service;
56 }
57
58 @Override
59 public void removedService(ServiceReference reference,
60 Object service) {
61 logService = null;
62 }
63 };
64 logServiceTracker.open();
65 }
66
67 public InternalLoggerFactory getFallback() {
68 return fallback;
69 }
70
71 public LogService getLogService() {
72 return logService;
73 }
74
75 public void destroy() {
76 logService = null;
77 logServiceTracker.close();
78 }
79
80 @Override
81 public InternalLogger newInstance(String name) {
82 return new OsgiLogger(this, name, fallback.newInstance(name));
83 }
84 }