1 package org.jahia.tools.migration; 2 3 import org.w3c.dom.Element ; 4 import org.w3c.dom.NodeList ; 5 6 import javax.naming.NamingException ; 7 import javax.naming.directory.InitialDirContext ; 8 9 import org.jahia.services.usermanager.ServerBean; 10 11 17 public class LDAPConnector { 18 19 private static org.apache.log4j.Logger logger = 20 org.apache.log4j.Logger.getLogger (LDAPConnector.class); 21 22 private static final String LDAP_SERVER_ROOT_TAG = "ldap-server"; 24 private static final String LDAP_SERVER_URL_TAG = "url"; 25 private static final String LDAP_SERVER_BASE_DN = "base-dn"; 26 private static final String LDAP_SERVER_USERNAME_TAG = "username"; 27 private static final String LDAP_SERVER_PASSWORD_TAG = "password"; 28 private static final String LDAP_SERVER_FACTORY_TAG = "factory"; 29 private static final String LDAP_SERVER_AUTH_MODE_TAG = "authentication-mode"; 30 private static final String LDAP_SERVER_DESTINATION_TAG = "provider-destination"; 31 private static final String LDAP_SERVER_PRIORITY_TAG = "priority"; 32 private static final String LDAP_SERVER_MAX_RECONN_TAG = "max-reconnection"; 33 private static final String LDAP_SERVER_REFERRAL = "referral"; 34 35 private ServerBean server; 37 private String baseDn; 38 39 private boolean initialized = false; 41 42 45 public LDAPConnector(Element rootElement) { 46 try { 47 initialized = init(rootElement); 48 } catch (Exception e) {} 49 } 50 51 56 public boolean isInitialized() { 57 return initialized; 58 } 59 60 private boolean init(Element rootElement) throws Exception { 61 logger.info("intialization started"); 62 boolean result = false; 63 64 NodeList serversList = rootElement.getElementsByTagName (LDAP_SERVER_ROOT_TAG); 65 66 if (serversList.getLength () != 1) 67 result = false; 68 else { 69 server = getServerFromDOM((Element )serversList.item(0)); 70 result = true; 71 } 72 73 logger.info("intialization ended"); 74 return result; 75 } 76 77 private ServerBean getServerFromDOM (Element element) throws Exception { 78 String provider = XMLUtility.getElementValue (LDAP_SERVER_DESTINATION_TAG, element); 79 String priority = XMLUtility.getElementValue (LDAP_SERVER_PRIORITY_TAG, element); 80 String maxreconn = XMLUtility.getElementValue (LDAP_SERVER_MAX_RECONN_TAG, element); 81 ServerBean result = new ServerBean(priority, provider, maxreconn); 82 83 result.setFactoryName(XMLUtility.getElementValue (LDAP_SERVER_FACTORY_TAG, element)); 84 result.setServerUrl(XMLUtility.getElementValue (LDAP_SERVER_URL_TAG, element)); 85 result.setUserName(XMLUtility.getElementValue (LDAP_SERVER_USERNAME_TAG, element)); 86 result.setAuthenticationMode(XMLUtility.getElementValue (LDAP_SERVER_AUTH_MODE_TAG, element)); 87 result.setUserPassword(XMLUtility.getElementValue (LDAP_SERVER_PASSWORD_TAG, element)); 88 result.setReferralBehavior(XMLUtility.getElementValue (LDAP_SERVER_REFERRAL, element)); 89 90 baseDn = XMLUtility.getElementValue (LDAP_SERVER_BASE_DN, element); 91 92 return result; 93 } 94 95 100 public String getBaseDN() { 101 return baseDn; 102 } 103 104 111 public InitialDirContext getContext() throws NamingException { 112 return new InitialDirContext (server.getPublicConnectionParameters()); 113 } 114 } | Popular Tags |