View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.util;
18  
19  import java.net.URL;
20  import java.security.Permission;
21  import java.util.List;
22  
23  import org.apache.logging.log4j.Logger;
24  import org.apache.logging.log4j.spi.LoggerContextFactory;
25  import org.apache.logging.log4j.status.StatusLogger;
26  import org.osgi.framework.AdaptPermission;
27  import org.osgi.framework.AdminPermission;
28  import org.osgi.framework.Bundle;
29  import org.osgi.framework.BundleActivator;
30  import org.osgi.framework.BundleContext;
31  import org.osgi.framework.BundleEvent;
32  import org.osgi.framework.SynchronousBundleListener;
33  import org.osgi.framework.wiring.BundleWire;
34  import org.osgi.framework.wiring.BundleWiring;
35  
36  /**
37   * OSGi bundle activator. Used for locating an implementation of
38   * {@link org.apache.logging.log4j.spi.LoggerContextFactory} et al. that have corresponding
39   * {@code META-INF/log4j-provider.properties} files. As with all OSGi BundleActivator classes, this class is not for
40   * public use and is only useful in an OSGi framework environment.
41   */
42  public class Activator implements BundleActivator, SynchronousBundleListener {
43  
44      private static final SecurityManager SECURITY_MANAGER = System.getSecurityManager();
45  
46      private static final Logger LOGGER = StatusLogger.getLogger();
47  
48      private static void checkPermission(final Permission permission) {
49          if (SECURITY_MANAGER != null) {
50              SECURITY_MANAGER.checkPermission(permission);
51          }
52      }
53  
54      private void loadProvider(final Bundle bundle) {
55          if (bundle.getState() == Bundle.UNINSTALLED) {
56              return;
57          }
58          try {
59              checkPermission(new AdminPermission(bundle, AdminPermission.RESOURCE));
60              checkPermission(new AdaptPermission(BundleWiring.class.getName(), bundle, AdaptPermission.ADAPT));
61              loadProvider(bundle.adapt(BundleWiring.class));
62          } catch (final SecurityException e) {
63              LOGGER.debug("Cannot access bundle [{}] contents. Ignoring.", bundle.getSymbolicName(), e);
64          } catch (final Exception e) {
65              LOGGER.warn("Problem checking bundle {} for Log4j 2 provider.", bundle.getSymbolicName(), e);
66          }
67      }
68  
69      private void loadProvider(BundleWiring provider) {
70          final List<URL> urls = provider.findEntries("META-INF", "log4j-provider.properties", 0);
71          for (final URL url : urls) {
72              ProviderUtil.loadProvider(url, provider.getClassLoader());
73          }
74      }
75  
76      @Override
77      public void start(final BundleContext context) throws Exception {
78          final BundleWiring self = context.getBundle().adapt(BundleWiring.class);
79          final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
80          for (BundleWire wire : required) {
81              loadProvider(wire.getProviderWiring());
82          }
83          context.addBundleListener(this);
84          final Bundle[] bundles = context.getBundles();
85          for (final Bundle bundle : bundles) {
86              loadProvider(bundle);
87          }
88      }
89  
90      @Override
91      public void stop(final BundleContext context) throws Exception {
92          context.removeBundleListener(this);
93      }
94  
95      @Override
96      public void bundleChanged(final BundleEvent event) {
97          switch (event.getType()) {
98              case BundleEvent.STARTED:
99                  // FIXME: LogManager won't see this update if it happens after LogManager is loaded
100                 loadProvider(event.getBundle());
101                 break;
102 
103             default:
104                 break;
105         }
106     }
107 
108 }