View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Logger factory which creates an <a href="http://www.osgi.org/">OSGi</a>
25   * {@link LogService} logger.
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  }