KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > migration > LDAPConnector


1 package org.jahia.tools.migration;
2
3 import org.w3c.dom.Element JavaDoc;
4 import org.w3c.dom.NodeList JavaDoc;
5
6 import javax.naming.NamingException JavaDoc;
7 import javax.naming.directory.InitialDirContext JavaDoc;
8
9 import org.jahia.services.usermanager.ServerBean;
10
11 /**
12  * This class will handle the connection to the LDAP provider to migrate
13  *
14  * @author PELTIER Olivier
15  * @version 1.0
16  */

17 public class LDAPConnector {
18
19     private static org.apache.log4j.Logger logger =
20             org.apache.log4j.Logger.getLogger (LDAPConnector.class);
21             
22     // Strings to get parameters from xml Element
23
private static final String JavaDoc LDAP_SERVER_ROOT_TAG = "ldap-server";
24     private static final String JavaDoc LDAP_SERVER_URL_TAG = "url";
25     private static final String JavaDoc LDAP_SERVER_BASE_DN = "base-dn";
26     private static final String JavaDoc LDAP_SERVER_USERNAME_TAG = "username";
27     private static final String JavaDoc LDAP_SERVER_PASSWORD_TAG = "password";
28     private static final String JavaDoc LDAP_SERVER_FACTORY_TAG = "factory";
29     private static final String JavaDoc LDAP_SERVER_AUTH_MODE_TAG = "authentication-mode";
30     private static final String JavaDoc LDAP_SERVER_DESTINATION_TAG = "provider-destination";
31     private static final String JavaDoc LDAP_SERVER_PRIORITY_TAG = "priority";
32     private static final String JavaDoc LDAP_SERVER_MAX_RECONN_TAG = "max-reconnection";
33     private static final String JavaDoc LDAP_SERVER_REFERRAL = "referral";
34     
35     // the internal server to acces LDAP
36
private ServerBean server;
37     private String JavaDoc baseDn;
38     
39     // initilization status
40
private boolean initialized = false;
41     
42     /**
43      * Default Constructor.
44      */

45     public LDAPConnector(Element JavaDoc rootElement) {
46         try {
47             initialized = init(rootElement);
48         } catch (Exception JavaDoc e) {}
49     }
50     
51     /**
52      * Return the initalization status.
53      *
54      * @return boolean.
55      */

56     public boolean isInitialized() {
57         return initialized;
58     }
59     
60     private boolean init(Element JavaDoc rootElement) throws Exception JavaDoc {
61         logger.info("intialization started");
62         boolean result = false;
63         
64         NodeList JavaDoc serversList = rootElement.getElementsByTagName (LDAP_SERVER_ROOT_TAG);
65
66         if (serversList.getLength () != 1)
67             result = false;
68         else {
69             server = getServerFromDOM((Element JavaDoc)serversList.item(0));
70             result = true;
71         }
72         
73         logger.info("intialization ended");
74         return result;
75     }
76     
77     private ServerBean getServerFromDOM (Element JavaDoc element) throws Exception JavaDoc {
78         String JavaDoc provider = XMLUtility.getElementValue (LDAP_SERVER_DESTINATION_TAG, element);
79         String JavaDoc priority = XMLUtility.getElementValue (LDAP_SERVER_PRIORITY_TAG, element);
80         String JavaDoc 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     /**
96      * Return a the base DN for the server.
97      *
98      * @return String.
99      */

100     public String JavaDoc getBaseDN() {
101         return baseDn;
102     }
103     
104     /**
105      * Return a context on the LDAP server.
106      *
107      * @return InitialDirContext.
108      *
109      * @throws NamingException.
110      */

111     public InitialDirContext JavaDoc getContext() throws NamingException JavaDoc {
112         return new InitialDirContext JavaDoc(server.getPublicConnectionParameters());
113     }
114 }
Popular Tags