<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.classloader.xwiki.internal;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;

import org.xwiki.classloader.ClassLoaderManager;
import org.xwiki.classloader.NamespaceURLClassLoader;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReferenceValueProvider;

/**
 * Dynamic classloader which select the current class loader based on context wiki each time it's called.
 * 
 * @version $Id$
 * @since 6.4M2
 */
public class ContextNamespaceURLClassLoader extends NamespaceURLClassLoader
{
    private final EntityReferenceValueProvider currentWikiProvider;

    private final ClassLoaderManager classLoaderManager;

    private String cachedCurrentWiki;

    private NamespaceURLClassLoader currentClassLoader;

    /**
     * @param currentWikiProvider the component used to access current wiki
     * @param classLoaderManager the component used to get the ClassLoader associated to current wiki
     */
    public ContextNamespaceURLClassLoader(EntityReferenceValueProvider currentWikiProvider,
        ClassLoaderManager classLoaderManager)
    {
        super(new URI[] {}, null, null);

        this.currentWikiProvider = currentWikiProvider;
        this.classLoaderManager = classLoaderManager;
    }

    // TODO: Add support for context user ?
    private NamespaceURLClassLoader getCurrentClassLoader()
    {
        String currentWiki = this.currentWikiProvider.getDefaultValue(EntityType.WIKI);

        if (!Objects.equals(currentWiki, this.cachedCurrentWiki)) {
            this.currentClassLoader =
                this.classLoaderManager.getURLClassLoader(currentWiki != null ? "wiki:" + currentWiki : null, false);

            if (this.currentClassLoader == null) {
                // Fallback on system classloader in the very weird edge case where ClassLoaderManager does not return
                // any (which is already supposed to fallback on system classloader)
                this.currentClassLoader = new NamespaceURLClassLoader(new URI[] {}, getSystemClassLoader(), null);
            }

            this.cachedCurrentWiki = currentWiki;
        }
<commit_msg>XWIKI-11706: Using a JNDI DataSource doesn't work anymore<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.classloader.xwiki.internal;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;

import org.xwiki.classloader.ClassLoaderManager;
import org.xwiki.classloader.NamespaceURLClassLoader;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReferenceValueProvider;

/**
 * Dynamic classloader which select the current class loader based on context wiki each time it's called.
 * 
 * @version $Id$
 * @since 6.4M2
 */
public class ContextNamespaceURLClassLoader extends NamespaceURLClassLoader
{
    private final EntityReferenceValueProvider currentWikiProvider;

    private final ClassLoaderManager classLoaderManager;

    private String cachedCurrentWiki;

    private NamespaceURLClassLoader currentClassLoader;

    /**
     * @param currentWikiProvider the component used to access current wiki
     * @param classLoaderManager the component used to get the ClassLoader associated to current wiki
     */
    public ContextNamespaceURLClassLoader(EntityReferenceValueProvider currentWikiProvider,
        ClassLoaderManager classLoaderManager)
    {
        // Note: it's important to set the parent CL to be the Context CL since some third party frameworks can use
        // that information. For example the Apache Naming (JNDI) implementation will get the context CL to check if
        // there's any NamingContext instance bound to it and at Tomcat's init (for example), Tomcat binds the defined
        // DataSource to the Tomcat CL (WebappClassLoader). Thus if we loose the path from our CL to the Tomcat CL, all
        // the DataSources defined in Tomcat will fail to be usable from our Hibernate code.
        super(new URI[] {}, Thread.currentThread().getContextClassLoader(), null);

        this.currentWikiProvider = currentWikiProvider;
        this.classLoaderManager = classLoaderManager;
    }

    // TODO: Add support for context user ?
    private NamespaceURLClassLoader getCurrentClassLoader()
    {
        String currentWiki = this.currentWikiProvider.getDefaultValue(EntityType.WIKI);

        if (!Objects.equals(currentWiki, this.cachedCurrentWiki)) {
            this.currentClassLoader =
                this.classLoaderManager.getURLClassLoader(currentWiki != null ? "wiki:" + currentWiki : null, false);

            if (this.currentClassLoader == null) {
                // Fallback on system classloader in the very weird edge case where ClassLoaderManager does not return
                // any (which is already supposed to fallback on system classloader)
                this.currentClassLoader = new NamespaceURLClassLoader(new URI[] {}, getSystemClassLoader(), null);
            }

            this.cachedCurrentWiki = currentWiki;
        }

{'fraction_non_alphanumeric': 0.06671438797423049, 'fraction_numerical': 0.005297065139584825, 'mean_word_length': 3.611221122112211, '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': '1471012', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1706, 'n_words': 679}