📄 ldapservices.java
字号:
String host = null; String port = null; String baseDN = null; String managerDN = null; String managerPW = null; String uidAttribute = null; String protocol = null; String factory = null; //Loop through all the child nodes of the connection NodeList connParams = connElement.getChildNodes(); for (int connParamIndex = 0; connParamIndex < connParams.getLength(); connParamIndex++) { Node connParam = connParams.item(connParamIndex); if (connParam instanceof Element) { String tagName = ((Element)connParam).getTagName(); String tagValue = null; if (connParam.getFirstChild() instanceof Text) { tagValue = ((Text)connParam.getFirstChild()).getData(); } if (tagName.equals("name")) { name = tagValue; } else if (tagName.equals("host")) { host = tagValue; } else if (tagName.equals("port")) { port = tagValue; } else if (tagName.equals("baseDN")) { baseDN = tagValue; } else if (tagName.equals("managerDN")) { managerDN = tagValue; } else if (tagName.equals("managerPW")) { managerPW = tagValue; } else if (tagName.equals("uidAttribute")) { uidAttribute = tagValue; } else if (tagName.equals("protocol")) { protocol = tagValue; } else if (tagName.equals("factory")) { factory = tagValue; } } } //Create a new ILdapServer if (name != null) { try { ILdapServer newConn = new LdapConnectionImpl(name, host, port, baseDN, uidAttribute, managerDN, managerPW, protocol, factory); ldapConnections.put(name, newConn); if (isDefaultConn) { defaultConn = newConn; if (log.isInfoEnabled()) log.info("Replaced '" + LDAP_PROPERTIES_FILE + "' connection with default connection '" + name + "' from '" + LDAP_XML_FILE + "'"); } } catch (IllegalArgumentException iae) { log.info( "Invalid data for server " + name + " in " + LDAP_XML_FILE, iae); } } else { log.error( "LdapServices::initConnections(): Error creating ILdapServer, no name specified."); } } } catch (Exception e) { log.error( "LdapServices::initConnections(): Error creating ILdapServer from node: " + connElement.getNodeName(), e); } } } catch (TransformerException te) { log.error( "LdapServices::initConnections(): Error applying XPath query (" + LDAP_XML_CONNECTION_XPATH + ") on " + LDAP_XML_FILE, te); } } else { log.error( "LdapServices::initConnections(): No document was loaded from " + LDAP_XML_FILE); } //Make sure a default connection was created. if (defaultConn == null) { RuntimeException re = new IllegalStateException("No default connection was created during initialization."); log.error( "LdapServices::initConnections():", re); throw re; } initialized = true; } } /** * This class only provides static methods. */ private LdapServices() { } /** * Internal implementation of the {@link ILdapServer} interface. * * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a> */ private static class LdapConnectionImpl implements ILdapServer { private final String ldapName; private final String ldapHost; private final String ldapPort; private final String ldapBaseDn; private final String ldapUidAttribute; private final String ldapManagerDn; private final String ldapManagerPw; private final String ldapManagerProtocol; private final String ldapInitCtxFactory; public LdapConnectionImpl( String name, String host, String port, String baseDn, String uidAttribute, String managerDn, String managerPw, String managerProtocol, String initialContextFactory) { if (name == null) throw new IllegalArgumentException("name cannot be null."); if (host == null) throw new IllegalArgumentException("host cannot be null."); this.ldapName = name; this.ldapHost = checkNull(host, ""); this.ldapPort = checkNull(port, "389"); this.ldapBaseDn = checkNull(baseDn, ""); this.ldapUidAttribute = checkNull(uidAttribute, ""); this.ldapManagerDn = checkNull(managerDn, ""); this.ldapManagerPw = checkNull(managerPw, ""); this.ldapManagerProtocol = checkNull(managerProtocol, ""); this.ldapInitCtxFactory = checkNull(initialContextFactory, "com.sun.jndi.ldap.LdapCtxFactory"); log.debug("LdapServices: Creating LDAP Connection: " + this.ldapName); log.debug("\thost = " + this.ldapHost); log.debug("\tport = " + this.ldapPort); log.debug("\tbaseDN = " + this.ldapBaseDn); log.debug("\tuidAttribute = " + this.ldapUidAttribute); log.debug("\tmanagerDN = " + this.ldapManagerDn); log.debug("\tmanagerPW = " + this.ldapManagerPw); log.debug("\tprotocol = " + this.ldapManagerProtocol); } private String checkNull(String chkStr, String defStr) { if (chkStr == null) return defStr; else return chkStr; } public DirContext getConnection() { DirContext conn = null; try { Hashtable env = new Hashtable(5, 0.75f); env.put(Context.INITIAL_CONTEXT_FACTORY, this.ldapInitCtxFactory); StringBuffer urlBuffer = new StringBuffer("ldap://"); urlBuffer.append(ldapHost).append(":").append(ldapPort); env.put(Context.PROVIDER_URL, urlBuffer.toString()); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, ldapManagerDn); env.put(Context.SECURITY_CREDENTIALS, ldapManagerPw); if(ldapManagerProtocol.equals("ssl")) env.put(Context.SECURITY_PROTOCOL,"ssl"); conn = new InitialDirContext(env); } catch ( Exception e ) { log.error( "LdapServices::LdapConnectionImpl::getConnection(): Error creating the LDAP Connection.", e); } return conn; } public String getBaseDN() { return ldapBaseDn; } public String getUidAttribute() { return ldapUidAttribute; } public void releaseConnection (DirContext conn) { if (conn == null) return; try { conn.close(); } catch (Exception e) { log.debug("LdapServices::LdapConnectionImpl::getConnection(): Error closing the LDAP Connection.", e); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -