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.Collection;
22  import java.util.List;
23  
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.spi.LoggerContextFactory;
26  import org.apache.logging.log4j.spi.Provider;
27  import org.apache.logging.log4j.status.StatusLogger;
28  import org.osgi.framework.AdaptPermission;
29  import org.osgi.framework.AdminPermission;
30  import org.osgi.framework.Bundle;
31  import org.osgi.framework.BundleActivator;
32  import org.osgi.framework.BundleContext;
33  import org.osgi.framework.BundleEvent;
34  import org.osgi.framework.InvalidSyntaxException;
35  import org.osgi.framework.ServiceReference;
36  import org.osgi.framework.SynchronousBundleListener;
37  import org.osgi.framework.wiring.BundleWire;
38  import org.osgi.framework.wiring.BundleWiring;
39  
40  /**
41   * <em>Consider this class private.</em>
42   * OSGi bundle activator. Used for locating an implementation of
43   * {@link org.apache.logging.log4j.spi.LoggerContextFactory} et al. that have corresponding
44   * {@code META-INF/log4j-provider.properties} files. As with all OSGi BundleActivator classes, this class is not for
45   * public use and is only useful in an OSGi framework environment.
46   */
47  public class Activator implements BundleActivator, SynchronousBundleListener {
48  
49      private static final SecurityManager SECURITY_MANAGER = System.getSecurityManager();
50  
51      private static final Logger LOGGER = StatusLogger.getLogger();
52  
53      // until we have at least one Provider, we'll lock ProviderUtil which locks LogManager.<clinit> by extension.
54      // this variable needs to be reset once the lock has been released
55      private boolean lockingProviderUtil;
56  
57      private static void checkPermission(final Permission permission) {
58          if (SECURITY_MANAGER != null) {
59              SECURITY_MANAGER.checkPermission(permission);
60          }
61      }
62  
63      private void loadProvider(final Bundle bundle) {
64          if (bundle.getState() == Bundle.UNINSTALLED) {
65              return;
66          }
67          try {
68              checkPermission(new AdminPermission(bundle, AdminPermission.RESOURCE));
69              checkPermission(new AdaptPermission(BundleWiring.class.getName(), bundle, AdaptPermission.ADAPT));
70              loadProvider(bundle.getBundleContext(), bundle.adapt(BundleWiring.class));
71          } catch (final SecurityException e) {
72              LOGGER.debug("Cannot access bundle [{}] contents. Ignoring.", bundle.getSymbolicName(), e);
73          } catch (final Exception e) {
74              LOGGER.warn("Problem checking bundle {} for Log4j 2 provider.", bundle.getSymbolicName(), e);
75          }
76      }
77  
78      private void loadProvider(final BundleContext context, final BundleWiring bundleWiring) {
79          final String filter = "(APIVersion>=2.60)";
80          try {
81              final Collection<ServiceReference<Provider>> serviceReferences = context.getServiceReferences(Provider.class, filter);
82              Provider maxProvider = null;
83              for (final ServiceReference<Provider> serviceReference : serviceReferences) {
84                  final Provider provider = context.getService(serviceReference);
85                  if (maxProvider == null || provider.getPriority() > maxProvider.getPriority()) {
86                      maxProvider = provider;
87                  }
88              }
89              if (maxProvider != null) {
90                  ProviderUtil.addProvider(maxProvider);
91              }
92          } catch (final InvalidSyntaxException ex) {
93              LOGGER.error("Invalid service filter: " + filter, ex);
94          }
95          final List<URL> urls = bundleWiring.findEntries("META-INF", "log4j-provider.properties", 0);
96          for (final URL url : urls) {
97              ProviderUtil.loadProvider(url, bundleWiring.getClassLoader());
98          }
99      }
100 
101     @Override
102     public void start(final BundleContext context) throws Exception {
103         ProviderUtil.STARTUP_LOCK.lock();
104         lockingProviderUtil = true;
105         final BundleWiring self = context.getBundle().adapt(BundleWiring.class);
106         final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
107         for (final BundleWire wire : required) {
108             loadProvider(context, wire.getProviderWiring());
109         }
110         context.addBundleListener(this);
111         final Bundle[] bundles = context.getBundles();
112         for (final Bundle bundle : bundles) {
113             loadProvider(bundle);
114         }
115         unlockIfReady();
116     }
117 
118     private void unlockIfReady() {
119         if (lockingProviderUtil && !ProviderUtil.PROVIDERS.isEmpty()) {
120             ProviderUtil.STARTUP_LOCK.unlock();
121             lockingProviderUtil = false;
122         }
123     }
124 
125     @Override
126     public void stop(final BundleContext context) throws Exception {
127         context.removeBundleListener(this);
128         unlockIfReady();
129     }
130 
131     @Override
132     public void bundleChanged(final BundleEvent event) {
133         switch (event.getType()) {
134             case BundleEvent.STARTED:
135                 loadProvider(event.getBundle());
136                 unlockIfReady();
137                 break;
138 
139             default:
140                 break;
141         }
142     }
143 
144 }