View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package org.slf4j.impl;
17  
18  import org.osgi.framework.BundleContext;
19  import org.osgi.framework.InvalidSyntaxException;
20  import org.osgi.framework.ServiceEvent;
21  import org.osgi.framework.ServiceListener;
22  import org.osgi.framework.ServiceReference;
23  import org.osgi.service.log.LogService;
24  import org.slf4j.ILoggerFactory;
25  import org.slf4j.Logger;
26  
27  public class OSGILogFactory implements ILoggerFactory
28  {
29      static private OSGiLogger s_logger = new OSGiLogger();
30  
31      private static BundleContext s_context = null;
32      private static ServiceReference s_serviceref = null;
33      private static LogService s_logservice = null;
34  
35      private static ServiceListener s_servlistener = new ServiceListener() {
36          public void serviceChanged(final ServiceEvent event)
37          {
38              LogService ls =
39                      (LogService) s_context.getService(event
40                              .getServiceReference());
41              if (ls != null)
42              {
43                  if (event.getType() == ServiceEvent.REGISTERED)
44                  {
45                      OSGILogFactory.setLogService(ls);
46  
47                  }
48                  else if (event.getType() == ServiceEvent.UNREGISTERING)
49                  {
50                      if (ls.equals(s_logservice))
51                      {
52                          OSGILogFactory.setLogService(null);
53  
54                          // Try to find another log service as a replacement for
55                          // our loss
56                          ServiceReference ref =
57                                  s_context.getServiceReference(LogService.class
58                                          .getName());
59                          if (ref != null)
60                          {
61                              s_logservice =
62                                      (LogService) s_context.getService(ref);
63                          }
64                      }
65                  }
66              }
67          }
68      };
69  
70      public static void initOSGI(final BundleContext context)
71      {
72          initOSGI(context, null);
73      }
74  
75      public static void
76      initOSGI(final BundleContext context, final ServiceReference servref)
77      {
78          s_context = context;
79          s_serviceref = servref;
80  
81          try {
82              String filter = "(objectclass=" + LogService.class.getName() + ")";
83              context.addServiceListener(s_servlistener, filter);
84          } catch (InvalidSyntaxException e) {
85              e.printStackTrace();
86          }
87  
88          ServiceReference ref =
89                  context.getServiceReference(LogService.class.getName());
90          if (ref != null)
91          {
92              s_logservice = (LogService) context.getService(ref);
93          }
94      }
95  
96      static public LogService getLogService()
97      {
98          return s_logservice;
99      }
100 
101     static public ServiceReference getServiceReference()
102     {
103         return s_serviceref;
104     }
105 
106     static public void setLogService(final LogService logservice)
107     {
108         s_logservice = logservice;
109     }
110 
111     static public void setServiceReference(final ServiceReference ref)
112     {
113         s_serviceref = ref;
114     }
115 
116     public Logger getLogger(final String arg0) {
117         return s_logger;
118     }
119 
120 }