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      // until we have at least one Provider, we'll lock ProviderUtil which locks LogManager.<clinit> by extension.
49      // this variable needs to be reset once the lock has been released
50      private boolean lockingProviderUtil;
51  
52      private static void checkPermission(final Permission permission) {
53          if (SECURITY_MANAGER != null) {
54              SECURITY_MANAGER.checkPermission(permission);
55          }
56      }
57  
58      private void loadProvider(final Bundle bundle) {
59          if (bundle.getState() == Bundle.UNINSTALLED) {
60              return;
61          }
62          try {
63              checkPermission(new AdminPermission(bundle, AdminPermission.RESOURCE));
64              checkPermission(new AdaptPermission(BundleWiring.class.getName(), bundle, AdaptPermission.ADAPT));
65              loadProvider(bundle.adapt(BundleWiring.class));
66          } catch (final SecurityException e) {
67              LOGGER.debug("Cannot access bundle [{}] contents. Ignoring.", bundle.getSymbolicName(), e);
68          } catch (final Exception e) {
69              LOGGER.warn("Problem checking bundle {} for Log4j 2 provider.", bundle.getSymbolicName(), e);
70          }
71      }
72  
73      private void loadProvider(final BundleWiring provider) {
74          final List<URL> urls = provider.findEntries("META-INF", "log4j-provider.properties", 0);
75          for (final URL url : urls) {
76              ProviderUtil.loadProvider(url, provider.getClassLoader());
77          }
78      }
79  
80      @Override
81      public void start(final BundleContext context) throws Exception {
82          ProviderUtil.STARTUP_LOCK.lock();
83          lockingProviderUtil = true;
84          final BundleWiring self = context.getBundle().adapt(BundleWiring.class);
85          final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
86          for (final BundleWire wire : required) {
87              loadProvider(wire.getProviderWiring());
88          }
89          context.addBundleListener(this);
90          final Bundle[] bundles = context.getBundles();
91          for (final Bundle bundle : bundles) {
92              loadProvider(bundle);
93          }
94          unlockIfReady();
95      }
96  
97      private void unlockIfReady() {
98          if (lockingProviderUtil && !ProviderUtil.PROVIDERS.isEmpty()) {
99              ProviderUtil.STARTUP_LOCK.unlock();
100             lockingProviderUtil = false;
101         }
102     }
103 
104     @Override
105     public void stop(final BundleContext context) throws Exception {
106         context.removeBundleListener(this);
107         unlockIfReady();
108     }
109 
110     @Override
111     public void bundleChanged(final BundleEvent event) {
112         switch (event.getType()) {
113             case BundleEvent.STARTED:
114                 loadProvider(event.getBundle());
115                 unlockIfReady();
116                 break;
117 
118             default:
119                 break;
120         }
121     }
122 
123 }