<commit_before>/*
 * Licensed 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.karaf.cellar.core.utils;

import org.osgi.framework.Bundle;

import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * A class loader which combines multiple bundle class loaders.
 * A bundle can add itself for to this class loader, so that the class loader can load classes from the bundle.
 * It is meant to be used together with the extender pattern in order to extends cellars class space.
 */
public class CombinedClassLoader extends ClassLoader {

    private final Map<Long, Bundle> bundles = new LinkedHashMap<Long, Bundle>();

    public void init() {
        bundles.clear();
    }

    public void destroy() {
        bundles.clear();
    }

    public void addBundle(Bundle bundle) {
        bundles.put(bundle.getBundleId(), bundle);
    }

    public void removeBundle(Bundle bundle) {
        bundles.remove(bundle.getBundleId());
    }


    public Class findClass(String name) throws ClassNotFoundException {
        for (Map.Entry<Long, Bundle> entry : bundles.entrySet()) {
            try {
                Bundle bundle = entry.getValue();
                if (bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STARTING) {
                    return bundle.loadClass(name);
                }
            } catch (ClassNotFoundException cnfe) {
                // Try next
            }
        }
        throw new ClassNotFoundException(name);
    }



    public URL getResource(String name) {
        for (Map.Entry<Long, Bundle> entry : bundles.entrySet()) {
            Bundle bundle = entry.getValue();
            if (bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STARTING) {
                URL url = bundle.getResource(name);
                if (url != null) {
                    return url;
                }
            }
        }
        return null;
    }
}<commit_msg>[KARAF-842] Converted LinkedHashMap to ConcurrentHashMap in Cellars combined class loader.<commit_after>/*
 * Licensed 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.karaf.cellar.core.utils;

import org.osgi.framework.Bundle;

import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * A class loader which combines multiple bundle class loaders.
 * A bundle can add itself for to this class loader, so that the class loader can load classes from the bundle.
 * It is meant to be used together with the extender pattern in order to extends cellars class space.
 */
public class CombinedClassLoader extends ClassLoader {

    private final ConcurrentMap<Long, Bundle> bundles = new ConcurrentHashMap<Long, Bundle>();

    public void init() {
        bundles.clear();
    }

    public void destroy() {
        bundles.clear();
    }

    public void addBundle(Bundle bundle) {
        bundles.put(bundle.getBundleId(), bundle);
    }

    public void removeBundle(Bundle bundle) {
        bundles.remove(bundle.getBundleId());
    }


    public Class findClass(String name) throws ClassNotFoundException {
        for (Map.Entry<Long, Bundle> entry : bundles.entrySet()) {
            try {
                Bundle bundle = entry.getValue();
                if (bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STARTING) {
                    return bundle.loadClass(name);
                }
            } catch (ClassNotFoundException cnfe) {
                // Try next
            }
        }
        throw new ClassNotFoundException(name);
    }



    public URL getResource(String name) {
        for (Map.Entry<Long, Bundle> entry : bundles.entrySet()) {
            Bundle bundle = entry.getValue();
            if (bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STARTING) {
                URL url = bundle.getResource(name);
                if (url != null) {
                    return url;
                }
            }
        }
        return null;
    }
}
{'fraction_non_alphanumeric': 0.09245837414299707, 'fraction_numerical': 0.0021547502448579824, 'mean_word_length': 2.43606998654105, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3439434', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1288, 'n_words': 465}