/*
 * 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.geronimo.gshell.remote.jaas;

import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;

/**
 * Component to configure JAAS.
 *
 * @version $Rev$ $Date$
 */
public class JaasConfigurationLoader
{
    private static final String KEY = "java.security.auth.login.config";

    private final Logger log = LoggerFactory.getLogger(getClass());

    private String resourceName;

    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(final String resourceName) {
        this.resourceName = resourceName;
    }

    @PostConstruct
    public void init() {
        if (resourceName == null) {
            throw new IllegalStateException("Missing required property: resourceName");
        }

        // Initialize the JAAS configuration
        String path = System.getProperty(KEY);

        if (path == null) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            URL resource = cl.getResource(resourceName);

            if (resource != null) {
                path = resource.toExternalForm();
                System.setProperty(KEY, path);
            }
        }

        log.debug("Using JAAS login config: {}", path);
    }
}
@@ -854,30 +854,8 @@
 s;%0A%0A
-import java.net.URL;%0A%0A
 impo
@@ -906,16 +906,123 @@
 Factory;
+%0Aimport org.springframework.beans.factory.BeanClassLoaderAware;%0Aimport org.springframework.util.ClassUtils;
 %0A%0Aimport
@@ -1053,16 +1053,37 @@
 nstruct;
+%0Aimport java.net.URL;
 %0A%0A/**%0A *
@@ -1181,16 +1181,52 @@
 nLoader%0A
+    implements BeanClassLoaderAware%0A
 %7B%0A    pr
@@ -1378,28 +1378,360 @@
 ate 
-String resourceName;
+ClassLoader classLoader;%0A%0A    private String resourceName;%0A%0A    public void setBeanClassLoader(final ClassLoader classLoader) %7B%0A        // classLoader could be null%0A%0A        if (classLoader == null) %7B%0A            this.classLoader = ClassUtils.getDefaultClassLoader();%0A        %7D%0A        else %7B%0A            this.classLoader = classLoader;%0A        %7D%0A    %7D
 %0A%0A  
@@ -2228,73 +2228,63 @@
     
-ClassLoader cl = Thread.currentThread().getContextClassLoader(
+URL resource = classLoader.getResource(resourceName
 );%0A
+%0A
     
@@ -2295,53 +2295,112 @@
     
-URL resource = cl.getResource(resourceName);%0A
+//%0A            // FIXME: This is not very friendly for threaded environments%0A            //%0A            
 %0A   
@@ -2566,17 +2566,133 @@
     
-log.debug
+if (path == null) %7B%0A            log.warn(%22Unable to locate JAAS login config%22);%0A        %7D%0A        else %7B%0A            log.info
 (%22Us
@@ -2722,19 +2722,29 @@
 %7B%7D%22, path);%0A
+        %7D%0A
     %7D%0A%7D

Make aware of the bean container's classloader to find the resource