<commit_before>/*
 * 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);
    }
}<commit_msg>Make aware of the bean container's classloader to find the resource<commit_after>/*
 * 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.util.ClassUtils;

import javax.annotation.PostConstruct;
import java.net.URL;

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

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

    private ClassLoader classLoader;

    private String resourceName;

    public void setBeanClassLoader(final ClassLoader classLoader) {
        // classLoader could be null

        if (classLoader == null) {
            this.classLoader = ClassUtils.getDefaultClassLoader();
        }
        else {
            this.classLoader = classLoader;
        }
    }

    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) {
            URL resource = classLoader.getResource(resourceName);

            //
            // FIXME: This is not very friendly for threaded environments
            //
            
            if (resource != null) {
                path = resource.toExternalForm();
                System.setProperty(KEY, path);
            }
        }

        if (path == null) {
            log.warn("Unable to locate JAAS login config");
        }
        else {
            log.info("Using JAAS login config: {}", path);
        }
    }
}
{'fraction_non_alphanumeric': 0.08287961282516637, 'fraction_numerical': 0.0024198427102238356, 'mean_word_length': 2.7293233082706765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776286', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1230, 'n_words': 452}