<commit_before>/**
 * Copyright (c) 2001 The JA-SIG Collaborative.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the JA-SIG Collaborative
 *    (http://www.jasig.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

package org.jasig.portal.ldap;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.xml.transform.TransformerException;

import org.apache.xpath.XPathAPI;
import org.jasig.portal.ldap.ILdapServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.utils.ResourceLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * Provides LDAP access in a way similar to a relational DBMS. This class
 * was modified for the 2.4 release to function more like {@link org.jasig.portal.RDBMServices}.
 * The class should be used via the static {@link #getDefaultLdapServer()} and
 * {@link #getLdapServer(String name)} methods.
 * 
 * @author <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
 * @version $Revision$
 */
public class LdapServices
{  
    private static final Log log = LogFactory.getLog(LdapServices.class);
    
    private static final String PROPERTIES_PATH = "/properties/";
    private static final String LDAP_PROPERTIES_FILE = PROPERTIES_PATH + "ldap.properties";
    private static final String LDAP_XML_FILE = PROPERTIES_PATH + "ldap.xml";
    private static final String LDAP_XML_CONNECTION_XPATH = "ldapConnections/connection";
    
    private static final Map ldapConnections = new Hashtable();
    private static ILdapServer defaultConn = null;
    private static boolean initialized = false;
    
    /**
     * Get the default {@link ILdapServer}. A one-time initialization
     * is performed when this method or {@link #getLdapServer(String name)}
     * is called. If a default connection is not found during initialization
     * an <code>IllegalStateException</code> will be thrown.
     * 
     * @return The default {@link ILdapServer}. 
     */
    public static ILdapServer getDefaultLdapServer() {
        initConnections();
        
        return defaultConn;
    }
  
    /**
     * Get a named {@link ILdapServer}. A one-time initialization
     * is performed when this method or {@link #getDefaultLdapServer()}
     * is called. If a default connection is not found during initialization
     * an <code>IllegalStateException</code> will be thrown.
     * 
     * @param name The name of the connection to return.
     * @return An {@link ILdapServer} with the specified name, <code>null</code> if there is no connection with the specified name.
     */
    public static ILdapServer getLdapServer(String name) {
        initConnections();
        
        synchronized (ldapConnections) {<commit_msg>Fix for feature request 1725 	There is now a special name that LDAPServices will return the default LDAP server for.<commit_after>/**
 * Copyright (c) 2001 The JA-SIG Collaborative.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the JA-SIG Collaborative
 *    (http://www.jasig.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

package org.jasig.portal.ldap;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.xml.transform.TransformerException;

import org.apache.xpath.XPathAPI;
import org.jasig.portal.ldap.ILdapServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.utils.ResourceLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * Provides LDAP access in a way similar to a relational DBMS. This class
 * was modified for the 2.4 release to function more like {@link org.jasig.portal.RDBMServices}.
 * The class should be used via the static {@link #getDefaultLdapServer()} and
 * {@link #getLdapServer(String name)} methods.
 * 
 * @author <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
 * @version $Revision$
 */
public class LdapServices
{  
    public static final String DEFAULT_LDAP_SERVER = "DEFAULT_LDAP_SERVER";
    
    private static final Log log = LogFactory.getLog(LdapServices.class);
    
    private static final String PROPERTIES_PATH = "/properties/";
    private static final String LDAP_PROPERTIES_FILE = PROPERTIES_PATH + "ldap.properties";
    private static final String LDAP_XML_FILE = PROPERTIES_PATH + "ldap.xml";
    private static final String LDAP_XML_CONNECTION_XPATH = "ldapConnections/connection";
    
    private static final Map ldapConnections = new Hashtable();
    private static ILdapServer defaultConn = null;
    private static boolean initialized = false;
    
    /**
     * Get the default {@link ILdapServer}. A one-time initialization
     * is performed when this method or {@link #getLdapServer(String name)}
     * is called. If a default connection is not found during initialization
     * an <code>IllegalStateException</code> will be thrown.
     * 
     * @return The default {@link ILdapServer}. 
     */
    public static ILdapServer getDefaultLdapServer() {
        initConnections();
        
        return defaultConn;
    }
  
    /**
     * Get a named {@link ILdapServer}. A one-time initialization
     * is performed when this method or {@link #getDefaultLdapServer()}
     * is called. If a default connection is not found during initialization
     * an <code>IllegalStateException</code> will be thrown.
     * 
     * @param name The name of the connection to return.
     * @return An {@link ILdapServer} with the specified name, <code>null</code> if there is no connection with the specified name.
     */
    public static ILdapServer getLdapServer(String name) {
        if (DEFAULT_LDAP_SERVER.equals(name)) {
            return getDefaultLdapServer();
        }
        
        initConnections();
        
        synchronized (ldapConnections) {
{'fraction_non_alphanumeric': 0.08081357407004568, 'fraction_numerical': 0.0036980639547531, 'mean_word_length': 3.7445820433436534, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '4731497', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2535, 'n_words': 963}