/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2014, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.webservices.service;

import java.security.Provider;
import java.security.Security;
import java.util.List;

import javax.management.MBeanServer;

import org.jboss.as.server.ServerEnvironment;
import org.jboss.as.server.ServerEnvironmentService;
import org.jboss.as.webservices.config.ServerConfigImpl;
import org.jboss.as.webservices.logging.WSLogger;
import org.jboss.as.webservices.util.WSServices;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceBuilder.DependencyType;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.ServiceTarget;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.ImmediateValue;
import org.jboss.wsf.spi.management.ServerConfig;
import org.wildfly.extension.undertow.UndertowService;

/**
 * WS server config service.
 *
 * @author <a href="alessio.soldano@jboss.com">Alessio Soldano</a>
 * @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
 * @author <a href="mailto:ema@redhat.com">Jim Ma</a>
 */
public final class ServerConfigService implements Service<ServerConfig> {

    private static final ServiceName MBEAN_SERVER_NAME = ServiceName.JBOSS.append("mbean", "server");
    private final ServerConfigImpl serverConfig;

    private ServerConfigService(final ServerConfigImpl serverConfig) {
        this.serverConfig = serverConfig;
    }

    @Override
    public ServerConfig getValue() {
        return serverConfig;
    }

    @Override
    public void start(final StartContext context) throws StartException {
        try {
            serverConfig.create();
            //we fix newly added BouncyCastle security provider to remove DH KeyPairGenerator
            fixSecurityProvider();
        } catch (final Exception e) {
            WSLogger.ROOT_LOGGER.configServiceCreationFailed();
            throw new StartException(e);
        }
    }

    @Override
    public void stop(final StopContext context) {
        try {
            serverConfig.destroy();
        } catch (final Exception e) {
            WSLogger.ROOT_LOGGER.configServiceDestroyFailed();
        }
    }

    public static ServiceController<?> install(final ServiceTarget serviceTarget, final ServerConfigImpl serverConfig,
            final List<ServiceName> dependencies, final boolean jmxSubsystemAvailable) {
        final ServiceBuilder<ServerConfig> builder = serviceTarget.addService(WSServices.CONFIG_SERVICE, new ServerConfigService(serverConfig));
        if (jmxSubsystemAvailable) {
            builder.addDependency(DependencyType.REQUIRED, MBEAN_SERVER_NAME, MBeanServer.class, serverConfig.getMBeanServerInjector());
        } else {
            serverConfig.getMBeanServerInjector().setValue(new ImmediateValue<MBeanServer>(null));
        }
        builder.addDependency(ServerEnvironmentService.SERVICE_NAME, ServerEnvironment.class, serverConfig.getServerEnvironmentInjector());
        builder.addDependency(DependencyType.REQUIRED, UndertowService.UNDERTOW, UndertowService.class, serverConfig.getUndertowServiceInjector());
        for (ServiceName dep : dependencies) {
            builder.addDependency(dep);
        }
        builder.setInitialMode(Mode.ACTIVE);
        return builder.install();
    }

    /*
    Fixes WFLY-3252 First HTTPS / SSL request after startup of Wildfly 8.0.0.Final is blocked for many seconds
    This is a big hack, which removes BouncyCastle DH KeyPairGenerator algorithm as it can hang / eat lots of CPU
    see https://issues.apache.org/jira/browse/HARMONY-3789 & http://bouncycastle.org/jira/browse/BJA-19 for more
    */
    private static void fixSecurityProvider(){
        reorderProviders();
    }

    /*
    We reorder providers so BC provider comes after JCE ones from the jvm, if we cannot find index of JCE provider we put it to last place in list
     */
    private static void reorderProviders() {
        Provider bc = Security.getProvider("BC");
        if (bc != null) { //reorder needed
            int index = -1;
            Provider[] providers = Security.getProviders();
            for (int i = 0; i < providers.length; i++) {
                Provider provider = providers[i];
                if (provider.getName().equals("SunJCE") || provider.getName().equals("IBMJCE")) {
                    index = i;
                    break;
                }
            }
            Security.removeProvider(bc.getName());
            if (index > -1) {
                Security.insertProviderAt(bc, index + 1);
            } else {
                Security.insertProviderAt(bc, providers.length); //add it to last place
            }
        }
    }
}

@@ -1063,70 +1063,8 @@
 e;%0A%0A
-import java.security.Provider;%0Aimport java.security.Security;%0A
 impo
@@ -1082,17 +1082,16 @@
 l.List;%0A
-%0A
 import j
@@ -2786,137 +2786,8 @@
 ();%0A
-            //we fix newly added BouncyCastle security provider to remove DH KeyPairGenerator%0A            fixSecurityProvider();%0A
     
@@ -4257,16 +4257,16 @@
 CTIVE);%0A
+
         
@@ -4300,1403 +4300,7 @@
    %7D
-%0A%0A    /*%0A    Fixes WFLY-3252 First HTTPS / SSL request after startup of Wildfly 8.0.0.Final is blocked for many seconds%0A    This is a big hack, which removes BouncyCastle DH KeyPairGenerator algorithm as it can hang / eat lots of CPU%0A    see https://issues.apache.org/jira/browse/HARMONY-3789 & http://bouncycastle.org/jira/browse/BJA-19 for more%0A    */%0A    private static void fixSecurityProvider()%7B%0A        reorderProviders();%0A    %7D%0A%0A    /*%0A    We reorder providers so BC provider comes after JCE ones from the jvm, if we cannot find index of JCE provider we put it to last place in list%0A     */%0A    private static void reorderProviders() %7B%0A        Provider bc = Security.getProvider(%22BC%22);%0A        if (bc != null) %7B //reorder needed%0A            int index = -1;%0A            Provider%5B%5D providers = Security.getProviders();%0A            for (int i = 0; i %3C providers.length; i++) %7B%0A                Provider provider = providers%5Bi%5D;%0A                if (provider.getName().equals(%22SunJCE%22) %7C%7C provider.getName().equals(%22IBMJCE%22)) %7B%0A                    index = i;%0A                    break;%0A                %7D%0A            %7D%0A            Security.removeProvider(bc.getName());%0A            if (index %3E -1) %7B%0A                Security.insertProviderAt(bc, index + 1);%0A            %7D else %7B%0A                Security.insertProviderAt(bc, providers.length); //add it to last place%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Remove reordering workaround as JBWS 5.0+ doesn't install bouncy castle anymore.