/**
 *  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 javax.security.auth.message.config;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.Security;
import java.util.Map;

import javax.security.auth.AuthPermission;

public abstract class AuthConfigFactory {

    public static final String DEFAULT_FACTORY_SECURITY_PROPERTY = "authconfigprovider.factory";
    private static final String DEFAULT_JASPI_AUTH_CONFIG_FACTORY_IMPL = "org.apache.geronimo.components.jaspi.AuthConfigFactoryImpl";

    private static AuthConfigFactory factory;
    private static ClassLoader contextClassLoader;

    static {
        contextClassLoader = java.security.AccessController
                .doPrivileged(new java.security.PrivilegedAction<ClassLoader>() {
                    @Override
                    public ClassLoader run() {
                        return Thread.currentThread().getContextClassLoader();
                    }
                });
    }

    public static AuthConfigFactory getFactory() {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new AuthPermission("getAuthConfigFactory"));
        }
        if (factory == null) {
            final String className = getFactoryClassName();
            try {
                return AccessController
                        .doPrivileged(new PrivilegedExceptionAction<AuthConfigFactory>() {
                            @Override
                            public AuthConfigFactory run() throws ClassNotFoundException,
                                    InstantiationException, IllegalAccessException {
                                // TODO Review this
                                Class<?> clazz = Class.forName(className, true, contextClassLoader);
                                return (AuthConfigFactory) clazz.newInstance();
                            }
                        });
            } catch (PrivilegedActionException e) {
                Exception inner = e.getException();
                if (inner instanceof InstantiationException) {
                    throw (SecurityException) new SecurityException("AuthConfigFactory error:"
                            + inner.getCause().getMessage()).initCause(inner.getCause());
                }
                throw (SecurityException) new SecurityException("AuthConfigFactory error: " + inner)
                        .initCause(inner);
            }
        }
        return factory;
    }

    private static String getFactoryClassName() {
        String className = AccessController.doPrivileged(new PrivilegedAction<String>() {
            @Override
            public String run() {
                return Security.getProperty(DEFAULT_FACTORY_SECURITY_PROPERTY);
            }
        });
        if (className != null) {
            return className;
        }
        return DEFAULT_JASPI_AUTH_CONFIG_FACTORY_IMPL;
    }

    public static void setFactory(AuthConfigFactory factory) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new AuthPermission("setAuthConfigFactory"));
        }
        AuthConfigFactory.factory = factory;
    }


    public AuthConfigFactory() {
    }

    public abstract String[] detachListener(RegistrationListener listener, String layer, String appContext);

    public abstract AuthConfigProvider getConfigProvider(String layer, String appContext, RegistrationListener listener);

    public abstract RegistrationContext getRegistrationContext(String registrationID);

    public abstract String[] getRegistrationIDs(AuthConfigProvider provider);

    public abstract void refresh();

    public abstract String registerConfigProvider(AuthConfigProvider provider, String layer, String appContext, String description);

    @SuppressWarnings("rawtypes") // JASPIC API uses raw types
    public abstract String registerConfigProvider(String className, Map properties, String layer, String appContext, String description);

    public abstract boolean removeRegistration(String registrationID);

    public static interface RegistrationContext {

        String getAppContext();

        String getDescription();

        String getMessageLayer();

        boolean isPersistent();

    }

}

@@ -877,190 +877,33 @@
 ity.
-AccessController;%0Aimport java.security.PrivilegedAction;%0Aimport java.security.PrivilegedActionException;%0Aimport java.security.PrivilegedExceptionAction;%0Aimport java.security.Security
+PrivilegedActionException
 ;%0Aim
@@ -1157,32 +1157,29 @@
 SPI_AUTH
-_
 CONFIG
-_
 FACTORY
-_
 IMPL = %22
@@ -1962,22 +1962,16 @@
         
-final 
 String c
@@ -1985,72 +1985,587 @@
 e = 
-getFactoryClassName();%0A            try %7B%0A                return 
+java.security.AccessController%0A                    .doPrivileged(new java.security.PrivilegedAction%3CString%3E() %7B%0A                        @Override%0A                        public String run() %7B%0A                            return java.security.Security.getProperty(DEFAULT_FACTORY_SECURITY_PROPERTY);%0A                        %7D%0A                    %7D);%0A            if (className == null) %7B%0A                className = DEFAULT_JASPI_AUTHCONFIGFACTORYIMPL;%0A            %7D%0A            try %7B%0A                final String finalClassName = className;%0A                factory = java.security.
 Acce
@@ -2611,32 +2611,46 @@
 oPrivileged(new 
+java.security.
 PrivilegedExcept
@@ -2809,16 +2809,40 @@
 ception,
+ InstantiationException,
 %0A       
@@ -2873,32 +2873,8 @@
     
- InstantiationException,
  Ill
@@ -3010,17 +3010,22 @@
 forName(
-c
+finalC
 lassName
@@ -3552,33 +3552,44 @@
                %7D
-%0A
+ else %7B%0A    
                 
@@ -3676,33 +3676,8 @@
 ner)
-%0A                        
 .ini
@@ -3691,38 +3691,32 @@
 inner);%0A        
-    %7D%0A
         %7D%0A      
@@ -3721,459 +3721,46 @@
     
-return factory;%0A    %7D%0A%0A    private static String getFactoryClassName() %7B%0A        String className = AccessController.doPrivileged(new PrivilegedAction%3CString%3E() %7B%0A            @Override%0A            public String run() %7B%0A                return Security.getProperty(DEFAULT_FACTORY_SECURITY_PROPERTY);%0A            %7D%0A        %7D);%0A        if (className != null) %7B%0A            return className;%0A        %7D%0A        return DEFAULT_JASPI_AUTH_CONFIG_FACTORY_IMPL
+    %7D%0A        %7D%0A        return factory
 ;%0A  

Revert original line lengths patch - it included refactoring that did not pass review