<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.sling.commons.classloader.impl;

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


/**
 * The <code>ClassLoaderFacade</code> is a facade
 * for the dynamic class loading.
 * This class loader is returned to the clients of the
 * dynamic class loader manager.
 * This class loader delegates to other class loaders
 * but caches its result for performance.
 */
public class ClassLoaderFacade extends ClassLoader {

    private final DynamicClassLoaderManagerImpl manager;

    public ClassLoaderFacade(final DynamicClassLoaderManagerImpl manager) {
        this.manager = manager;
    }

    /**
     * @see java.lang.ClassLoader#getResource(java.lang.String)
     */
    public URL getResource(String name) {
        if ( !this.manager.isActive() ) {
            throw new RuntimeException("Dynamic class loader has already been deactivated.");
        }
        final ClassLoader[] loaders = manager.getDynamicClassLoaders();
        for(final ClassLoader cl : loaders) {
            if ( cl != null ) {
                final URL u = cl.getResource(name);
                if ( u != null ) {
                    return u;
                }
            }
        }
        return null;
    }

    /**
     * @see java.lang.ClassLoader#getResources(java.lang.String)
     */
    public Enumeration<URL> getResources(String name) throws IOException {
        if ( !this.manager.isActive() ) {
            throw new RuntimeException("Dynamic class loader has already been deactivated.");
        }
        final ClassLoader[] loaders = manager.getDynamicClassLoaders();
        for(final ClassLoader cl : loaders) {
            if ( cl != null ) {
                final Enumeration<URL> e = cl.getResources(name);
                if ( e != null && e.hasMoreElements() ) {
                    return e;
                }
            }
        }
        return null;
    }

    /**
     * @see java.lang.ClassLoader#loadClass(java.lang.String)
     */
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        if ( !this.manager.isActive() ) {
            throw new RuntimeException("Dynamic class loader has already been deactivated.");
        }
        final ClassLoader[] loaders = manager.getDynamicClassLoaders();
        for(final ClassLoader cl : loaders) {
            if ( cl != null ) {
                try {
                    final Class<?> c = cl.loadClass(name);
                    return c;
                } catch (Exception cnfe) {
                    // we just ignore this and try the next class loader
                }
            }
        }
        throw new ClassNotFoundException("Class not found: " + name);
    }
}<commit_msg>SLING-1135 need to overwrite the loadClass(String, boolean) method and not the loadClass(String) method because only the former is called from "child" class loaders in class loader hierarchies.<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.sling.commons.classloader.impl;

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


/**
 * The <code>ClassLoaderFacade</code> is a facade
 * for the dynamic class loading.
 * This class loader is returned to the clients of the
 * dynamic class loader manager.
 * This class loader delegates to other class loaders
 * but caches its result for performance.
 */
public class ClassLoaderFacade extends ClassLoader {

    private final DynamicClassLoaderManagerImpl manager;

    public ClassLoaderFacade(final DynamicClassLoaderManagerImpl manager) {
        this.manager = manager;
    }

    /**
     * @see java.lang.ClassLoader#getResource(java.lang.String)
     */
    public URL getResource(String name) {
        if ( !this.manager.isActive() ) {
            throw new RuntimeException("Dynamic class loader has already been deactivated.");
        }
        final ClassLoader[] loaders = manager.getDynamicClassLoaders();
        for(final ClassLoader cl : loaders) {
            if ( cl != null ) {
                final URL u = cl.getResource(name);
                if ( u != null ) {
                    return u;
                }
            }
        }
        return null;
    }

    /**
     * @see java.lang.ClassLoader#getResources(java.lang.String)
     */
    public Enumeration<URL> getResources(String name) throws IOException {
        if ( !this.manager.isActive() ) {
            throw new RuntimeException("Dynamic class loader has already been deactivated.");
        }
        final ClassLoader[] loaders = manager.getDynamicClassLoaders();
        for(final ClassLoader cl : loaders) {
            if ( cl != null ) {
                final Enumeration<URL> e = cl.getResources(name);
                if ( e != null && e.hasMoreElements() ) {
                    return e;
                }
            }
        }
        return null;
    }

    @Override
    protected synchronized Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException {

        if ( !this.manager.isActive() ) {
            throw new RuntimeException("Dynamic class loader has already been deactivated.");
        }
        final ClassLoader[] loaders = manager.getDynamicClassLoaders();
        for(final ClassLoader cl : loaders) {
            if ( cl != null ) {
                try {
                    final Class<?> c = cl.loadClass(name);
                    return c;
                } catch (Exception cnfe) {
                    // we just ignore this and try the next class loader
                }
            }
        }
        throw new ClassNotFoundException("Class not found: " + name);
    }
}
{'fraction_non_alphanumeric': 0.08503307607497243, 'fraction_numerical': 0.0016538037486218302, 'mean_word_length': 2.246979865771812, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '6894899', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1775, 'n_words': 650}