/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.util;

import java.io.IOException;
import java.net.URL;
import java.security.Permission;
import java.util.Enumeration;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;
import org.osgi.framework.AdminPermission;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.SynchronousBundleListener;

/**
 * OSGi bundle activator. Used for locating an implementation of
 * {@link org.apache.logging.log4j.spi.LoggerContextFactory} et al. that have corresponding
 * {@code META-INF/log4j-provider.properties} files. As with all OSGi BundleActivator classes, this class is not for
 * public use and is only useful in an OSGi framework environment.
 */
public class Activator implements BundleActivator, SynchronousBundleListener {

    private static final SecurityManager SECURITY_MANAGER = System.getSecurityManager();

    private static final Logger LOGGER = StatusLogger.getLogger();

    static {
        checkPermission(new RuntimePermission("createClassLoader"));
    }

    private static void checkPermission(final Permission permission) {
        if (SECURITY_MANAGER != null) {
            SECURITY_MANAGER.checkPermission(permission);
        }
    }

    private void loadProvider(final Bundle bundle) {
        try {
            checkPermission(new AdminPermission(bundle, AdminPermission.RESOURCE));
            final URL url = bundle.getEntry(ProviderUtil.PROVIDER_RESOURCE);
            if (url != null) {
                checkPermission(new AdminPermission(bundle, AdminPermission.CLASS));
                ProviderUtil.loadProvider(url, new BundleDelegatingClassLoader(bundle));
            }
        } catch (final Exception e) {
            LOGGER.warn("Problem checking bundle {} for Log4j 2 provider.", bundle.getSymbolicName(), e);
        }
    }

    @Override
    public void start(final BundleContext context) throws Exception {
        context.addBundleListener(this);
        final Bundle[] bundles = context.getBundles();
        for (final Bundle bundle : bundles) {
            loadProvider(bundle);
        }
    }

    @Override
    public void stop(final BundleContext context) throws Exception {

    }

    @Override
    public void bundleChanged(final BundleEvent event) {
        switch (event.getType()) {
            case BundleEvent.STARTED:
                // FIXME: LogManager won't see this update if it happens after LogManager is loaded
                loadProvider(event.getBundle());
                break;

            default:
                break;
        }
    }

    /**
     * ClassLoader that delegates to an OSGi Bundle.
     */
    public static class BundleDelegatingClassLoader extends ClassLoader {

        private final Bundle bundle;

        public BundleDelegatingClassLoader(final Bundle bundle) {
            this.bundle = bundle;
        }

        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            return bundle.loadClass(name);
        }

        @Override
        protected URL findResource(String name) {
            return bundle.getResource(name);
        }

        @Override
        protected Enumeration<URL> findResources(String name) throws IOException {
            return bundle.getResources(name);
        }
    }
}

@@ -838,36 +838,8 @@
 l;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -855,16 +855,16 @@
 et.URL;%0A
+
 import j
@@ -891,38 +891,8 @@
 ion;
-%0Aimport java.util.Enumeration;
 %0A%0Aim
@@ -1234,16 +1234,63 @@
 istener;
+%0Aimport org.osgi.framework.wiring.BundleWiring;
 %0A%0A/**%0A *
@@ -1874,97 +1874,8 @@
 );%0A%0A
-    static %7B%0A        checkPermission(new RuntimePermission(%22createClassLoader%22));%0A    %7D%0A%0A
     
@@ -2101,32 +2101,117 @@
 undle bundle) %7B%0A
+        if (bundle.getState() == Bundle.UNINSTALLED) %7B%0A            return;%0A        %7D%0A
         try %7B%0A  
@@ -2536,28 +2536,17 @@
 rl, 
-new 
+get
 Bundle
-Delegating
 Clas
@@ -2730,32 +2730,218 @@
        %7D%0A    %7D%0A%0A
+    private static ClassLoader getBundleClassLoader(Bundle bundle) %7B%0A        final BundleWiring wiring = bundle.adapt(BundleWiring.class);%0A        return wiring.getClassLoader();%0A    %7D%0A%0A
     @Override%0A  
@@ -3284,17 +3284,60 @@
 ption %7B%0A
+        context.removeBundleListener(this);
 %0A
-
     %7D%0A%0A 
@@ -3717,740 +3717,6 @@
  %7D%0A%0A
-    /**%0A     * ClassLoader that delegates to an OSGi Bundle.%0A     */%0A    public static class BundleDelegatingClassLoader extends ClassLoader %7B%0A%0A        private final Bundle bundle;%0A%0A        public BundleDelegatingClassLoader(final Bundle bundle) %7B%0A            this.bundle = bundle;%0A        %7D%0A%0A        @Override%0A        protected Class%3C?%3E findClass(String name) throws ClassNotFoundException %7B%0A            return bundle.loadClass(name);%0A        %7D%0A%0A        @Override%0A        protected URL findResource(String name) %7B%0A            return bundle.getResource(name);%0A        %7D%0A%0A        @Override%0A        protected Enumeration%3CURL%3E findResources(String name) throws IOException %7B%0A            return bundle.getResources(name);%0A        %7D%0A    %7D%0A
 %7D%0A

Remove BundleDelegatingClassLoader.