<commit_before>/*
 *   Copyright 2006 The Apache Software Foundation
 *
 *   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.directory.server.core.sp;


import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import org.apache.directory.server.core.jndi.ServerLdapContext;
import org.apache.directory.shared.ldap.filter.BranchNode;
import org.apache.directory.shared.ldap.filter.LeafNode;
import org.apache.directory.shared.ldap.filter.SimpleNode;
import org.apache.directory.shared.ldap.name.LdapDN;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * A class loader that loads classes from an LDAP DIT.
 * 
 * <p>
 * This loader looks for an configuration entry whose DN is
 * determined by defaultSearchContextsConfig variable. If there is such
 * an entry it gets the search contexts from the entry and searches the 
 * class to be loaded in those contexts.
 * If there is no default search context configuration entry it searches
 * the class in the whole DIT. 
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$ $Date$
 */
public class LdapClassLoader extends ClassLoader
{
    private static final Logger log = LoggerFactory.getLogger( LdapClassLoader.class );
    public static String defaultSearchContextsConfig = "cn=classLoaderDefaultSearchContext,ou=configuration,ou=system";
    private ServerLdapContext RootDSE;

    public LdapClassLoader( ServerLdapContext RootDSE ) throws NamingException
    {
        this.RootDSE = ( ( ServerLdapContext ) RootDSE.lookup( "" ) );
    }

    private byte[] findClassInDIT( NamingEnumeration searchContexts, String name ) throws ClassNotFoundException
    {
        String currentSearchContextName = null;
        ServerLdapContext currentSearchContext = null;
        NamingEnumeration javaClassEntries = null;
        byte[] classBytes = null;
        
        BranchNode filter = new BranchNode( BranchNode.AND );
        filter.addNode( new SimpleNode( "fullyQualifiedClassName", name, LeafNode.EQUALITY ) );
        filter.addNode( new SimpleNode( "objectClass", "javaClass", LeafNode.EQUALITY ) );
        
        SearchControls controls = new SearchControls();
        controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
        
        try
        {
            while( searchContexts.hasMore() )
            {
                currentSearchContextName = ( String ) searchContexts.next();
                currentSearchContext = ( ServerLdapContext ) RootDSE.lookup( currentSearchContextName );
                
                javaClassEntries = currentSearchContext.search( LdapDN.EMPTY_LDAPDN, filter, controls );
                if ( javaClassEntries.hasMore() ) // there should be only one!
                {
                    SearchResult javaClassEntry = ( SearchResult ) javaClassEntries.next();
                    Attribute byteCode = javaClassEntry.getAttributes().get( "byteCode" );
                    classBytes = ( byte[] ) byteCode.get();
                    continue;
                }
            }
        }
        catch ( NamingException e )<commit_msg>Made the class loader exit on first hit of the searched class.<commit_after>/*
 *   Copyright 2006 The Apache Software Foundation
 *
 *   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.directory.server.core.sp;


import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import org.apache.directory.server.core.jndi.ServerLdapContext;
import org.apache.directory.shared.ldap.filter.BranchNode;
import org.apache.directory.shared.ldap.filter.LeafNode;
import org.apache.directory.shared.ldap.filter.SimpleNode;
import org.apache.directory.shared.ldap.name.LdapDN;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * A class loader that loads classes from an LDAP DIT.
 * 
 * <p>
 * This loader looks for an configuration entry whose DN is
 * determined by defaultSearchContextsConfig variable. If there is such
 * an entry it gets the search contexts from the entry and searches the 
 * class to be loaded in those contexts.
 * If there is no default search context configuration entry it searches
 * the class in the whole DIT. 
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$ $Date$
 */
public class LdapClassLoader extends ClassLoader
{
    private static final Logger log = LoggerFactory.getLogger( LdapClassLoader.class );
    public static String defaultSearchContextsConfig = "cn=classLoaderDefaultSearchContext,ou=configuration,ou=system";
    private ServerLdapContext RootDSE;

    public LdapClassLoader( ServerLdapContext RootDSE ) throws NamingException
    {
        this.RootDSE = ( ( ServerLdapContext ) RootDSE.lookup( "" ) );
    }

    private byte[] findClassInDIT( NamingEnumeration searchContexts, String name ) throws ClassNotFoundException
    {
        String currentSearchContextName = null;
        ServerLdapContext currentSearchContext = null;
        NamingEnumeration javaClassEntries = null;
        byte[] classBytes = null;
        
        BranchNode filter = new BranchNode( BranchNode.AND );
        filter.addNode( new SimpleNode( "fullyQualifiedClassName", name, LeafNode.EQUALITY ) );
        filter.addNode( new SimpleNode( "objectClass", "javaClass", LeafNode.EQUALITY ) );
        
        SearchControls controls = new SearchControls();
        controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
        
        try
        {
            while( searchContexts.hasMore() )
            {
                currentSearchContextName = ( String ) searchContexts.next();
                currentSearchContext = ( ServerLdapContext ) RootDSE.lookup( currentSearchContextName );
                
                javaClassEntries = currentSearchContext.search( LdapDN.EMPTY_LDAPDN, filter, controls );
                if ( javaClassEntries.hasMore() ) // there should be only one!
                {
                    SearchResult javaClassEntry = ( SearchResult ) javaClassEntries.next();
                    Attribute byteCode = javaClassEntry.getAttributes().get( "byteCode" );
                    classBytes = ( byte[] ) byteCode.get();
                    break; // exit on first hit!
                }
            }
        }
        catch ( NamingException e )
{'fraction_non_alphanumeric': 0.08078431372549019, 'fraction_numerical': 0.00261437908496732, 'mean_word_length': 3.0183823529411766, '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': '5567068', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1993, 'n_words': 605}