<commit_before>/**
 *
 * Copyright 2007-2009 (C) The original author or authors
 *
 * 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.papoose.core;

import java.io.IOException;
import java.net.URL;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.apache.xbean.classloader.NamedClassLoader;
import org.apache.xbean.classloader.ResourceHandle;
import org.apache.xbean.classloader.ResourceLocation;
import org.osgi.framework.BundleException;

import org.papoose.core.descriptions.DynamicDescription;
import org.papoose.core.descriptions.ImportDescription;
import org.papoose.core.spi.ArchiveStore;


/**
 * @version $Revision$ $Date$
 */
public class BundleClassLoader extends NamedClassLoader
{
    private final static URL[] EMPTY_URLS = new URL[0];
    private final Papoose framework;
    private final BundleGeneration bundle;
    private final Set<Wire> wires;
    private final List<Wire> requiredBundles;
    private final String[] exportedPackages;
    private final Set<DynamicDescription> dynamicImports;
    private final List<ResourceLocation> boundClassPath;
    private final Set<ArchiveStore> archiveStores;


    BundleClassLoader(String name, ClassLoader parent,
                      Papoose framework,
                      BundleGeneration bundle,
                      Set<Wire> wires,
                      List<Wire> requiredBundles,
                      String[] bootDelegates,
                      String[] exportedPackages,
                      Set<DynamicDescription> dynamicImports,
                      List<ResourceLocation> boundClassPath,
                      Set<ArchiveStore> archiveStores) throws BundleException
    {
        super(name, EMPTY_URLS, parent);

        assert name != null;
        assert framework != null;
        assert bundle != null;
        assert wires != null;
        assert requiredBundles != null;
        assert boundClassPath != null;

        this.framework = framework;
        this.bundle = bundle;
        this.wires = new HashSet<Wire>(wires);
        this.requiredBundles = requiredBundles;
        this.exportedPackages = exportedPackages;
        this.dynamicImports = Collections.synchronizedSet(dynamicImports);
        this.boundClassPath = boundClassPath;
        this.archiveStores = archiveStores;
    }


    BundleGeneration getBundle()
    {
        return bundle;
    }

    public Set<Wire> getWires()
    {
        return Collections.unmodifiableSet(wires);
    }

    public String[] getExportedPackages()
    {
        return exportedPackages;
    }

    @SuppressWarnings({ "EmptyCatchBlock" })
    public Class<?> loadClass(String className) throws ClassNotFoundException
    {
        if (className.startsWith("java.")) return getParent().loadClass(className);

        String packageName = className.substring(0, Math.max(0, className.lastIndexOf('.')));

        for (String delegate : framework.getBootDelegates())
        {
            if ((delegate.endsWith(".") && packageName.regionMatches(0, delegate, 0, delegate.length() - 1)) || packageName.equals(delegate))
            {
                try
                {
                    return getParent().loadClass(className);
                }
                catch (ClassNotFoundException doNothing)
                {
                }
            }
        }

        return delegateLoadClass(className);
    }

    public URL getResource(String resourceName)
    {
        ResourceHandle handle;

        if (resourceName.startsWith("/")) resourceName = resourceName.substring(1, resourceName.length());

        for (ResourceLocation location : boundClassPath)
        {
            if ((handle = location.getResourceHandle(resourceName)) != null) return handle.getUrl();
        }

        return null;
    }

    public Enumeration<URL> findResources(String resourceName) throws IOException
    {
        List<URL> urls = new ArrayList<URL>();
        ResourceHandle handle;

        for (ResourceLocation location : boundClassPath)
        {<commit_msg>Scrub root character<commit_after>/**
 *
 * Copyright 2007-2009 (C) The original author or authors
 *
 * 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.papoose.core;

import java.io.IOException;
import java.net.URL;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.apache.xbean.classloader.NamedClassLoader;
import org.apache.xbean.classloader.ResourceHandle;
import org.apache.xbean.classloader.ResourceLocation;
import org.osgi.framework.BundleException;

import org.papoose.core.descriptions.DynamicDescription;
import org.papoose.core.descriptions.ImportDescription;
import org.papoose.core.spi.ArchiveStore;


/**
 * @version $Revision$ $Date$
 */
public class BundleClassLoader extends NamedClassLoader
{
    private final static URL[] EMPTY_URLS = new URL[0];
    private final Papoose framework;
    private final BundleGeneration bundle;
    private final Set<Wire> wires;
    private final List<Wire> requiredBundles;
    private final String[] exportedPackages;
    private final Set<DynamicDescription> dynamicImports;
    private final List<ResourceLocation> boundClassPath;
    private final Set<ArchiveStore> archiveStores;


    BundleClassLoader(String name, ClassLoader parent,
                      Papoose framework,
                      BundleGeneration bundle,
                      Set<Wire> wires,
                      List<Wire> requiredBundles,
                      String[] bootDelegates,
                      String[] exportedPackages,
                      Set<DynamicDescription> dynamicImports,
                      List<ResourceLocation> boundClassPath,
                      Set<ArchiveStore> archiveStores) throws BundleException
    {
        super(name, EMPTY_URLS, parent);

        assert name != null;
        assert framework != null;
        assert bundle != null;
        assert wires != null;
        assert requiredBundles != null;
        assert boundClassPath != null;

        this.framework = framework;
        this.bundle = bundle;
        this.wires = new HashSet<Wire>(wires);
        this.requiredBundles = requiredBundles;
        this.exportedPackages = exportedPackages;
        this.dynamicImports = Collections.synchronizedSet(dynamicImports);
        this.boundClassPath = boundClassPath;
        this.archiveStores = archiveStores;
    }


    BundleGeneration getBundle()
    {
        return bundle;
    }

    public Set<Wire> getWires()
    {
        return Collections.unmodifiableSet(wires);
    }

    public String[] getExportedPackages()
    {
        return exportedPackages;
    }

    @SuppressWarnings({ "EmptyCatchBlock" })
    public Class<?> loadClass(String className) throws ClassNotFoundException
    {
        if (className.startsWith("java.")) return getParent().loadClass(className);

        String packageName = className.substring(0, Math.max(0, className.lastIndexOf('.')));

        for (String delegate : framework.getBootDelegates())
        {
            if ((delegate.endsWith(".") && packageName.regionMatches(0, delegate, 0, delegate.length() - 1)) || packageName.equals(delegate))
            {
                try
                {
                    return getParent().loadClass(className);
                }
                catch (ClassNotFoundException doNothing)
                {
                }
            }
        }

        return delegateLoadClass(className);
    }

    public URL getResource(String resourceName)
    {
        if (resourceName.startsWith("/")) resourceName = resourceName.substring(1, resourceName.length());

        ResourceHandle handle;

        for (ResourceLocation location : boundClassPath)
        {
            if ((handle = location.getResourceHandle(resourceName)) != null) return handle.getUrl();
        }

        return null;
    }

    public Enumeration<URL> findResources(String resourceName) throws IOException
    {
        if (resourceName.startsWith("/")) resourceName = resourceName.substring(1, resourceName.length());

        List<URL> urls = new ArrayList<URL>();
        ResourceHandle handle;

        for (ResourceLocation location : boundClassPath)
        {
{'fraction_non_alphanumeric': 0.08848046600381641, 'fraction_numerical': 0.00391684242241639, 'mean_word_length': 2.7748294162244123, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '827736', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2508, 'n_words': 671}