View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    28   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    29   *
    30   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    31   *
    32   */
    33  public class OsgiLoggerFactory extends InternalLoggerFactory {
    34
    35      private final ServiceTracker logServiceTracker;
    36      private final InternalLoggerFactory fallback;
    37      volatile LogService logService;
    38
    39      public OsgiLoggerFactory(BundleContext ctx) {
    40          this(ctx, null);
    41      }
    42
    43      public OsgiLoggerFactory(BundleContext ctx, InternalLoggerFactory fallback) {
    44          if (ctx == null) {
    45              throw new NullPointerException("ctx");
    46          }
    47          if (fallback == null) {
    48              fallback = InternalLoggerFactory.getDefaultFactory();
    49              if (fallback instanceof OsgiLoggerFactory) {
    50                  fallback = new JdkLoggerFactory();
    51              }
    52          }
    53
    54          this.fallback = fallback;
    55          logServiceTracker = new ServiceTracker(
    56                  ctx, "org.osgi.service.log.LogService", null) {
    57                      @Override
    58                      public Object addingService(ServiceReference reference) {
    59                          LogService service = (LogService) super.addingService(reference);
    60                          logService = service;
    61                          return service;
    62                      }
    63
    64                      @Override
    65                      public void removedService(ServiceReference reference,
    66                              Object service) {
    67                          logService = null;
    68                      }
    69          };
    70          logServiceTracker.open();
    71      }
    72
    73      public InternalLoggerFactory getFallback() {
    74          return fallback;
    75      }
    76
    77      public LogService getLogService() {
    78          return logService;
    79      }
    80
    81      public void destroy() {
    82          logService = null;
    83          logServiceTracker.close();
    84      }
    85
    86      @Override
    87      public InternalLogger newInstance(String name) {
    88          return new OsgiLogger(this, name, fallback.newInstance(name));
    89      }
    90  }