001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.store.ldap;
020:
021: import java.util.HashMap;
022:
023: import javax.xml.parsers.*;
024:
025: import org.lucane.common.Logging;
026: import org.w3c.dom.*;
027:
028: /**
029: * Server configuration
030: */
031: public class LdapConfig {
032: public static final String CONFIG_FILE = "etc/ldap-store.xml";
033:
034: private String ldapUrl;
035:
036: private String authType;
037: private String authBindDn;
038: private String authPassword;
039:
040: private String pluginsDn;
041: private HashMap pluginsAttributes = new HashMap();
042: private HashMap pluginsMapping = new HashMap();
043:
044: private String servicesDn;
045: private HashMap servicesAttributes = new HashMap();
046: private HashMap servicesMapping = new HashMap();
047:
048: private String usersDn;
049: private HashMap usersAttributes = new HashMap();
050: private HashMap usersMapping = new HashMap();
051:
052: private String groupsDn;
053: private HashMap groupsAttributes = new HashMap();
054: private HashMap groupsMapping = new HashMap();
055:
056: /**
057: * Constructor
058: *
059: * @param filename the XML config file
060: */
061: public LdapConfig(String filename) throws Exception {
062: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
063: .newDocumentBuilder();
064: Document document = builder.parse(filename);
065:
066: //-- root element
067: Node node = document.getFirstChild();
068: while (node != null && node.getNodeType() != Node.ELEMENT_NODE)
069: node = node.getNextSibling();
070:
071: if (node == null || !node.getNodeName().equals("ldap"))
072: throw new Exception("root element is different from 'ldap'");
073:
074: this .ldapUrl = node.getAttributes().getNamedItem("url")
075: .getNodeValue();
076:
077: node = node.getFirstChild();
078: while (node != null) {
079: if (node.getNodeType() == Node.ELEMENT_NODE) {
080: if (node.getNodeName().equals("authentication"))
081: handleAuthentication(node);
082: else if (node.getNodeName().equals("plugins"))
083: handlePlugins(node);
084: else if (node.getNodeName().equals("services"))
085: handleServices(node);
086: else if (node.getNodeName().equals("users"))
087: handleUsers(node);
088: else if (node.getNodeName().equals("groups"))
089: handleGroups(node);
090: else
091: Logging.getLogger().warning(
092: "unexepected node : " + node.getNodeName());
093: }
094: node = node.getNextSibling();
095: }
096: }
097:
098: /**
099: * Parse authentication node
100: *
101: * @param node the authentication node
102: */
103: private void handleAuthentication(Node node) {
104: this .authType = node.getAttributes().getNamedItem("type")
105: .getNodeValue();
106: this .authBindDn = node.getAttributes().getNamedItem("bind-dn")
107: .getNodeValue();
108: this .authPassword = node.getAttributes().getNamedItem(
109: "password").getNodeValue();
110: }
111:
112: /**
113: * Parse plugins node
114: *
115: * @param node the plugins node
116: */
117: private void handlePlugins(Node node) {
118: this .pluginsDn = node.getAttributes().getNamedItem("dn")
119: .getNodeValue();
120:
121: node = node.getFirstChild();
122: while (node != null) {
123: if (node.getNodeType() == Node.ELEMENT_NODE) {
124: if (node.getNodeName().equals("attribute"))
125: handleMapping(this .pluginsAttributes, node);
126: else if (node.getNodeName().equals("mapping"))
127: handleMapping(this .pluginsMapping, node);
128: else
129: Logging.getLogger().warning(
130: "unexepected node : " + node.getNodeName());
131: }
132: node = node.getNextSibling();
133: }
134: }
135:
136: /**
137: * Parse services node
138: *
139: * @param node the services node
140: */
141: private void handleServices(Node node) {
142: this .servicesDn = node.getAttributes().getNamedItem("dn")
143: .getNodeValue();
144:
145: node = node.getFirstChild();
146: while (node != null) {
147: if (node.getNodeType() == Node.ELEMENT_NODE) {
148: if (node.getNodeName().equals("attribute"))
149: handleMapping(this .servicesAttributes, node);
150: else if (node.getNodeName().equals("mapping"))
151: handleMapping(this .servicesMapping, node);
152: else
153: Logging.getLogger().warning(
154: "unexepected node : " + node.getNodeName());
155: }
156: node = node.getNextSibling();
157: }
158: }
159:
160: /**
161: * Parse users node
162: *
163: * @param node the users node
164: */
165: private void handleUsers(Node node) {
166: this .usersDn = node.getAttributes().getNamedItem("dn")
167: .getNodeValue();
168:
169: node = node.getFirstChild();
170: while (node != null) {
171: if (node.getNodeType() == Node.ELEMENT_NODE) {
172: if (node.getNodeName().equals("attribute"))
173: handleMapping(this .usersAttributes, node);
174: else if (node.getNodeName().equals("mapping"))
175: handleMapping(this .usersMapping, node);
176: else
177: Logging.getLogger().warning(
178: "unexepected node : " + node.getNodeName());
179: }
180: node = node.getNextSibling();
181: }
182: }
183:
184: /**
185: * Parse groups node
186: *
187: * @param node the groups node
188: */
189: private void handleGroups(Node node) {
190: this .groupsDn = node.getAttributes().getNamedItem("dn")
191: .getNodeValue();
192:
193: node = node.getFirstChild();
194: while (node != null) {
195: if (node.getNodeType() == Node.ELEMENT_NODE) {
196: if (node.getNodeName().equals("attribute"))
197: handleMapping(this .groupsAttributes, node);
198: else if (node.getNodeName().equals("mapping"))
199: handleMapping(this .groupsMapping, node);
200: else
201: Logging.getLogger().warning(
202: "unexepected node : " + node.getNodeName());
203: }
204: node = node.getNextSibling();
205: }
206: }
207:
208: /**
209: * Parse mapping or attribute node
210: *
211: * @param node the node
212: */
213: private void handleMapping(HashMap map, Node node) {
214: String name = node.getAttributes().getNamedItem("name")
215: .getNodeValue();
216: String value = node.getAttributes().getNamedItem("value")
217: .getNodeValue();
218:
219: map.put(name, value);
220: }
221:
222: //-- getters
223:
224: public String getLdapUrl() {
225: return this .ldapUrl;
226: }
227:
228: public String getAuthType() {
229: return this .authType;
230: }
231:
232: public String getAuthBindDn() {
233: return this .authBindDn;
234: }
235:
236: public String getAuthPassword() {
237: return this .authPassword;
238: }
239:
240: public String getPluginsDn() {
241: return this .pluginsDn;
242: }
243:
244: public HashMap getPluginsAttributes() {
245: return this .pluginsAttributes;
246: }
247:
248: public HashMap getPluginsMapping() {
249: return this .pluginsMapping;
250: }
251:
252: public String getServicesDn() {
253: return this .servicesDn;
254: }
255:
256: public HashMap getServicesAttributes() {
257: return this .servicesAttributes;
258: }
259:
260: public HashMap getServicesMapping() {
261: return this .servicesMapping;
262: }
263:
264: public String getUsersDn() {
265: return this .usersDn;
266: }
267:
268: public HashMap getUsersAttributes() {
269: return this .usersAttributes;
270: }
271:
272: public HashMap getUsersMapping() {
273: return this .usersMapping;
274: }
275:
276: public String getGroupsDn() {
277: return this .groupsDn;
278: }
279:
280: public HashMap getGroupsAttributes() {
281: return this .groupsAttributes;
282: }
283:
284: public HashMap getGroupsMapping() {
285: return this.groupsMapping;
286: }
287: }
|