KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > store > ldap > LdapConfig


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.store.ldap;
20
21 import java.util.HashMap JavaDoc;
22
23 import javax.xml.parsers.*;
24
25 import org.lucane.common.Logging;
26 import org.w3c.dom.*;
27
28
29 /**
30  * Server configuration
31  */

32 public class LdapConfig
33 {
34     public static final String JavaDoc CONFIG_FILE = "etc/ldap-store.xml";
35     
36     private String JavaDoc ldapUrl;
37     
38     private String JavaDoc authType;
39     private String JavaDoc authBindDn;
40     private String JavaDoc authPassword;
41     
42     private String JavaDoc pluginsDn;
43     private HashMap JavaDoc pluginsAttributes = new HashMap JavaDoc();
44     private HashMap JavaDoc pluginsMapping = new HashMap JavaDoc();
45     
46     private String JavaDoc servicesDn;
47     private HashMap JavaDoc servicesAttributes = new HashMap JavaDoc();
48     private HashMap JavaDoc servicesMapping = new HashMap JavaDoc();
49     
50     private String JavaDoc usersDn;
51     private HashMap JavaDoc usersAttributes = new HashMap JavaDoc();
52     private HashMap JavaDoc usersMapping = new HashMap JavaDoc();
53     
54     private String JavaDoc groupsDn;
55     private HashMap JavaDoc groupsAttributes = new HashMap JavaDoc();
56     private HashMap JavaDoc groupsMapping = new HashMap JavaDoc();
57     
58     
59     /**
60      * Constructor
61      *
62      * @param filename the XML config file
63      */

64     public LdapConfig(String JavaDoc filename)
65     throws Exception JavaDoc
66     {
67           DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
68           Document document = builder.parse(filename);
69                                                                   
70           //-- root element
71
Node node = document.getFirstChild();
72           while(node != null && node.getNodeType() != Node.ELEMENT_NODE)
73             node = node.getNextSibling();
74           
75           if(node == null || !node.getNodeName().equals("ldap"))
76             throw new Exception JavaDoc("root element is different from 'ldap'");
77           
78           this.ldapUrl = node.getAttributes().getNamedItem("url").getNodeValue();
79           
80           node = node.getFirstChild();
81           while(node != null)
82           {
83             if(node.getNodeType() == Node.ELEMENT_NODE)
84             {
85                 if(node.getNodeName().equals("authentication"))
86                     handleAuthentication(node);
87                 else if(node.getNodeName().equals("plugins"))
88                     handlePlugins(node);
89                 else if(node.getNodeName().equals("services"))
90                     handleServices(node);
91                 else if(node.getNodeName().equals("users"))
92                     handleUsers(node);
93                 else if(node.getNodeName().equals("groups"))
94                     handleGroups(node);
95                 else
96                   Logging.getLogger().warning("unexepected node : " + node.getNodeName());
97             }
98             node = node.getNextSibling();
99           }
100     }
101     
102     /**
103      * Parse authentication node
104      *
105      * @param node the authentication node
106      */

107     private void handleAuthentication(Node node)
108     {
109         this.authType = node.getAttributes().getNamedItem("type").getNodeValue();
110         this.authBindDn = node.getAttributes().getNamedItem("bind-dn").getNodeValue();
111         this.authPassword = node.getAttributes().getNamedItem("password").getNodeValue();
112     }
113     
114     /**
115      * Parse plugins node
116      *
117      * @param node the plugins node
118      */

119     private void handlePlugins(Node node)
120     {
121         this.pluginsDn = node.getAttributes().getNamedItem("dn").getNodeValue();
122         
123         node = node.getFirstChild();
124         while(node != null)
125         {
126           if(node.getNodeType() == Node.ELEMENT_NODE)
127           {
128               if(node.getNodeName().equals("attribute"))
129                 handleMapping(this.pluginsAttributes, node);
130               else if(node.getNodeName().equals("mapping"))
131                 handleMapping(this.pluginsMapping, node);
132               else
133                 Logging.getLogger().warning("unexepected node : " + node.getNodeName());
134           }
135           node = node.getNextSibling();
136         }
137     }
138     
139     /**
140      * Parse services node
141      *
142      * @param node the services node
143      */

144     private void handleServices(Node node)
145     {
146         this.servicesDn = node.getAttributes().getNamedItem("dn").getNodeValue();
147         
148         node = node.getFirstChild();
149         while(node != null)
150         {
151             if(node.getNodeType() == Node.ELEMENT_NODE)
152             {
153                 if(node.getNodeName().equals("attribute"))
154                     handleMapping(this.servicesAttributes, node);
155                 else if(node.getNodeName().equals("mapping"))
156                     handleMapping(this.servicesMapping, node);
157                 else
158                     Logging.getLogger().warning("unexepected node : " + node.getNodeName());
159             }
160             node = node.getNextSibling();
161         }
162     }
163     
164     /**
165      * Parse users node
166      *
167      * @param node the users node
168      */

169     private void handleUsers(Node node)
170     {
171         this.usersDn = node.getAttributes().getNamedItem("dn").getNodeValue();
172         
173         node = node.getFirstChild();
174         while(node != null)
175         {
176             if(node.getNodeType() == Node.ELEMENT_NODE)
177             {
178                 if(node.getNodeName().equals("attribute"))
179                     handleMapping(this.usersAttributes, node);
180                 else if(node.getNodeName().equals("mapping"))
181                     handleMapping(this.usersMapping, node);
182                 else
183                     Logging.getLogger().warning("unexepected node : " + node.getNodeName());
184             }
185             node = node.getNextSibling();
186         }
187     }
188     
189     /**
190      * Parse groups node
191      *
192      * @param node the groups node
193      */

194     private void handleGroups(Node node)
195     {
196         this.groupsDn = node.getAttributes().getNamedItem("dn").getNodeValue();
197         
198         node = node.getFirstChild();
199         while(node != null)
200         {
201             if(node.getNodeType() == Node.ELEMENT_NODE)
202             {
203                 if(node.getNodeName().equals("attribute"))
204                     handleMapping(this.groupsAttributes, node);
205                 else if(node.getNodeName().equals("mapping"))
206                     handleMapping(this.groupsMapping, node);
207                 else
208                     Logging.getLogger().warning("unexepected node : " + node.getNodeName());
209             }
210             node = node.getNextSibling();
211         }
212     }
213     
214     /**
215      * Parse mapping or attribute node
216      *
217      * @param node the node
218      */

219     private void handleMapping(HashMap JavaDoc map, Node node)
220     {
221         String JavaDoc name = node.getAttributes().getNamedItem("name").getNodeValue();
222         String JavaDoc value = node.getAttributes().getNamedItem("value").getNodeValue();
223         
224         map.put(name, value);
225     }
226
227     //-- getters
228

229     public String JavaDoc getLdapUrl()
230     {
231         return this.ldapUrl;
232     }
233     
234     public String JavaDoc getAuthType()
235     {
236         return this.authType;
237     }
238
239     public String JavaDoc getAuthBindDn()
240     {
241         return this.authBindDn;
242     }
243     
244     public String JavaDoc getAuthPassword()
245     {
246         return this.authPassword;
247     }
248     
249     public String JavaDoc getPluginsDn()
250     {
251         return this.pluginsDn;
252     }
253     
254     public HashMap JavaDoc getPluginsAttributes()
255     {
256         return this.pluginsAttributes;
257     }
258     
259     public HashMap JavaDoc getPluginsMapping()
260     {
261         return this.pluginsMapping;
262     }
263     
264     public String JavaDoc getServicesDn()
265     {
266         return this.servicesDn;
267     }
268     
269     public HashMap JavaDoc getServicesAttributes()
270     {
271         return this.servicesAttributes;
272     }
273     
274     public HashMap JavaDoc getServicesMapping()
275     {
276         return this.servicesMapping;
277     }
278     
279     public String JavaDoc getUsersDn()
280     {
281         return this.usersDn;
282     }
283     
284     public HashMap JavaDoc getUsersAttributes()
285     {
286         return this.usersAttributes;
287     }
288     
289     public HashMap JavaDoc getUsersMapping()
290     {
291         return this.usersMapping;
292     }
293     
294     public String JavaDoc getGroupsDn()
295     {
296         return this.groupsDn;
297     }
298     
299     public HashMap JavaDoc getGroupsAttributes()
300     {
301         return this.groupsAttributes;
302     }
303     
304     public HashMap JavaDoc getGroupsMapping()
305     {
306         return this.groupsMapping;
307     }
308 }
Popular Tags