📄 ldapservices.java
字号:
/** * 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 Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a> * @version $Revision: 1.4.2.5 $ */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) { return (ILdapServer)ldapConnections.get(name); } } private static void initConnections() { //synchronize on the connections map, ensures only one thread //will be initializing the connections and nothing will be //trying to read from the map while it is being initialized. synchronized (ldapConnections) { //If already initialized just return if (initialized) return; //This try/catch/finaly block reads the default LDAP connection //from a properties file. InputStream ins = null; try { //Read properties file ins = LdapServices.class.getResourceAsStream(LDAP_PROPERTIES_FILE); //If the properties file was found if (ins != null) { Properties ldapProps = new Properties(); ldapProps.load(ins); try { //Create the default connection object defaultConn = new LdapConnectionImpl( "ldap.properties configured connection", ldapProps.getProperty("ldap.host"), ldapProps.getProperty("ldap.port"), ldapProps.getProperty("ldap.baseDN"), ldapProps.getProperty("ldap.uidAttribute"), ldapProps.getProperty("ldap.managerDN"), ldapProps.getProperty("ldap.managerPW"), ldapProps.getProperty("ldap.protocol"), ldapProps.getProperty("ldap.factory")); } catch (IllegalArgumentException iae) { log.info( "Invalid data in " + LDAP_PROPERTIES_FILE, iae); } } else { log.info( "LdapServices::initConnections(): " + LDAP_PROPERTIES_FILE + " was not found, all ldap connections will be loaded from " + LDAP_XML_FILE); } } catch(Exception e) { log.error( "LdapServices::initConnections(): Error while loading default ldap connection from " + LDAP_PROPERTIES_FILE, e); } finally { try { if(ins != null) ins.close(); } catch(IOException ioe) { log.error( "LdapServices::initConnections(): Unable to close " + LDAP_PROPERTIES_FILE + " InputStream " + ioe); } } //Read extra connections from ldap.xml Document config = null; try { config = ResourceLoader.getResourceAsDocument(LdapServices.class, LDAP_XML_FILE); } catch (Exception e) { log.error( "LdapServices::initConnections(): Could not create Document from " + LDAP_XML_FILE, e); } if (config != null){ config.normalize(); try { NodeList connElements = XPathAPI.selectNodeList(config, LDAP_XML_CONNECTION_XPATH); //Loop through each <connection> element for (int connIndex = 0; connIndex < connElements.getLength(); connIndex++) { Node connElement = connElements.item(connIndex); try { if (connElement instanceof Element) { //See if this connection is flagged as default NamedNodeMap connAtts = connElement.getAttributes(); Node defaultFlagAtt = connAtts.getNamedItem("default"); boolean isDefaultConn; if (defaultFlagAtt != null) isDefaultConn = (new Boolean(defaultFlagAtt.getNodeValue())).booleanValue(); else isDefaultConn = false; String name = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -