001: /* CVS ID $Id: XMLSystemData.java,v 1.1.1.1 2002/10/02 18:42:54 wastl Exp $ */
002: package net.wastl.webmail.xml;
003:
004: import net.wastl.webmail.config.*;
005: import net.wastl.webmail.server.*;
006:
007: import java.util.*;
008:
009: import org.w3c.dom.*;
010:
011: /*
012: * XMLSystemData.java
013: *
014: * Created: Sat Mar 4 16:07:30 2000
015: *
016: * Copyright (C) 2000 Sebastian Schaffert
017: *
018: * This program is free software; you can redistribute it and/or
019: * modify it under the terms of the GNU General Public License
020: * as published by the Free Software Foundation; either version 2
021: * of the License, or (at your option) any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
031: */
032: /**
033: *
034: *
035: * This class represents methods for accessing WebMail's system configuration in a
036: * XML tree.
037: *
038: *
039: * @author Sebastian Schaffert
040: * @version $Revision: 1.1.1.1 $
041: */
042:
043: public class XMLSystemData extends ConfigStore {
044:
045: protected Document root;
046:
047: protected Element sysdata;
048:
049: /* Save the time when this document has been loaded. Might be used to reload
050: a document with a higher modification time
051: */
052: protected long loadtime;
053:
054: public XMLSystemData(Document d, ConfigScheme cs) {
055: super (cs);
056: root = d;
057: sysdata = root.getDocumentElement();
058: if (sysdata == null) {
059: sysdata = root.createElement("SYSDATA");
060: root.appendChild(sysdata);
061: }
062: loadtime = System.currentTimeMillis();
063: }
064:
065: public long getLoadTime() {
066: return loadtime;
067: }
068:
069: public void setLoadTime(long time) {
070: loadtime = time;
071: }
072:
073: public Document getRoot() {
074: return root;
075: }
076:
077: public Element getSysData() {
078: return sysdata;
079: }
080:
081: public DocumentFragment getDocumentFragment() {
082: DocumentFragment df = root.createDocumentFragment();
083: df.appendChild(sysdata);
084: return df;
085: }
086:
087: protected String getConfigRaw(String key) {
088: NodeList nl = sysdata.getElementsByTagName("KEY");
089: for (int i = 0; i < nl.getLength(); i++) {
090: Element e = (Element) nl.item(i);
091: if (XMLCommon.getElementTextValue(e).equals(key)) {
092: Element p = (Element) e.getParentNode();
093: NodeList valuel = p.getElementsByTagName("VALUE");
094: if (valuel.getLength() >= 0) {
095: return XMLCommon
096: .getElementTextValue((Element) valuel
097: .item(0));
098: }
099: }
100: }
101: return null;
102: }
103:
104: public void setConfigRaw(String groupname, String key,
105: String value, String type) {
106: String curval = getConfigRaw(key);
107: if (curval == null || !curval.equals(value)) {
108: // System.err.println("XMLSystemData: "+groupname+"/"+key+" = "+value);
109: /* Find all GROUP elements */
110: NodeList groupl = sysdata.getElementsByTagName("GROUP");
111: int i = 0;
112: for (i = 0; i < groupl.getLength(); i++) {
113: Element group = (Element) groupl.item(i);
114: if (group.getAttribute("name").equals(groupname)) {
115: /* If the group name matches, find all keys */
116: NodeList keyl = group.getElementsByTagName("KEY");
117: int j = 0;
118: for (j = 0; j < keyl.getLength(); j++) {
119: Element keyelem = (Element) keyl.item(j);
120: if (key.equals(XMLCommon
121: .getElementTextValue(keyelem))) {
122: /* If the key already exists, replace it */
123: Element conf = (Element) keyelem
124: .getParentNode();
125: group.replaceChild(createConfigElement(key,
126: value, type), conf);
127: return;
128: }
129: }
130: /* If the key was not found, append it */
131: if (j >= keyl.getLength()) {
132: group.appendChild(createConfigElement(key,
133: value, type));
134: return;
135: }
136: }
137: }
138: if (i >= groupl.getLength()) {
139: Element group = createConfigGroup(groupname);
140: group
141: .appendChild(createConfigElement(key, value,
142: type));
143: }
144: }
145: }
146:
147: protected Element createConfigGroup(String groupname) {
148: Element group = root.createElement("GROUP");
149: group.setAttribute("name", groupname);
150: sysdata.appendChild(group);
151: return group;
152: }
153:
154: protected void deleteConfigGroup(String groupname) {
155: NodeList nl = sysdata.getElementsByTagName("GROUP");
156: for (int i = 0; i < nl.getLength(); i++) {
157: if (((Element) nl.item(i)).getAttribute("name").equals(
158: groupname)) {
159: sysdata.removeChild(nl.item(i));
160: }
161: }
162: }
163:
164: protected Element getConfigElementByKey(String key) {
165: NodeList nl = sysdata.getElementsByTagName("KEY");
166:
167: Element config = null;
168: for (int i = 0; i < nl.getLength(); i++) {
169: Element keyelem = (Element) nl.item(i);
170: Element parent = (Element) keyelem.getParentNode();
171: if (XMLCommon.getElementTextValue(keyelem).equals(key)
172: && parent.getTagName().equals("CONFIG")) {
173: config = parent;
174: break;
175: }
176: }
177: return config;
178: }
179:
180: public void initChoices() {
181: Enumeration enum=getConfigKeys();
182: while(enum.hasMoreElements()) {
183: initChoices((String)enum.nextElement());
184: }
185: }
186:
187: public void initChoices(String key) {
188: Element config=getConfigElementByKey(key);
189:
190: XMLCommon.genericRemoveAll(config,"CHOICE");
191:
192:
193: ConfigParameter param=scheme.getConfigParameter(key);
194: if(param instanceof ChoiceConfigParameter) {
195: Enumeration enum=((ChoiceConfigParameter)param).choices();
196: while(enum.hasMoreElements()) {
197: Element choice=root.createElement("CHOICE");
198: choice.appendChild(root.createTextNode((String)enum.nextElement()));
199: config.appendChild(choice);
200: }
201: }
202: }
203:
204: protected Element createConfigElement(String key, String value, String type) {
205: Element config=root.createElement("CONFIG");
206: Element keyelem=root.createElement("KEY");
207: Element desc=root.createElement("DESCRIPTION");
208: Element valueelem=root.createElement("VALUE");
209: keyelem.appendChild(root.createTextNode(key));
210: desc.appendChild(root.createTextNode(scheme.getDescription(key)));
211: valueelem.appendChild(root.createTextNode(value));
212: config.appendChild(keyelem);
213: config.appendChild(desc);
214: config.appendChild(valueelem);
215: config.setAttribute("type",type);
216: ConfigParameter param=scheme.getConfigParameter(key);
217: if(param instanceof ChoiceConfigParameter) {
218: Enumeration enum=((ChoiceConfigParameter)param).choices();
219: while(enum.hasMoreElements()) {
220: Element choice=root.createElement("CHOICE");
221: choice.appendChild(root.createTextNode((String)enum.nextElement()));
222: config.appendChild(choice);
223: }
224: }
225: return config;
226: }
227:
228: public Enumeration getVirtualDomains() {
229: final NodeList nl = sysdata.getElementsByTagName("DOMAIN");
230: return new Enumeration() {
231: int i = 0;
232:
233: public boolean hasMoreElements() {
234: return i < nl.getLength();
235: }
236:
237: public Object nextElement() {
238: Element elem = (Element) nl.item(i++);
239: String value = XMLCommon.getTagValue(elem, "NAME");
240: return value == null ? "unknown" + (i - 1) : value;
241: }
242: };
243: }
244:
245: public WebMailVirtualDomain getVirtualDomain(String domname) {
246: NodeList nodel=sysdata.getElementsByTagName("DOMAIN");
247: Element elem=null;
248: int j;
249: for(j=0;j<nodel.getLength();j++) {
250: elem=(Element)nodel.item(j);
251: elem.normalize();
252: NodeList namel=elem.getElementsByTagName("NAME");
253: if(namel.getLength()>0) {
254: if(XMLCommon.getElementTextValue((Element)namel.item(0)).equals(domname)) {
255: break;
256: }
257: }
258: }
259: if(j<nodel.getLength() && elem != null) {
260: final Element domain=elem;
261: return new WebMailVirtualDomain() {
262:
263: public String getDomainName() {
264: String value=XMLCommon.getTagValue(domain,"NAME");
265: return value==null?"unknown":value;
266: }
267: public void setDomainName(String name) throws Exception {
268: XMLCommon.setTagValue(domain,"NAME",name,true,"Virtual Domain names must be unique!");
269: }
270:
271: public String getDefaultServer() {
272: String value=XMLCommon.getTagValue(domain,"DEFAULT_HOST");
273: return value==null?"unknown":value;
274: }
275:
276: public void setDefaultServer(String name) {
277: XMLCommon.setTagValue(domain,"DEFAULT_HOST",name);
278: }
279:
280: public String getAuthenticationHost() {
281: String value=XMLCommon.getTagValue(domain,"AUTHENTICATION_HOST");
282: return value==null?"unknown":value;
283: }
284:
285: public void setAuthenticationHost(String name) {
286: XMLCommon.setTagValue(domain,"AUTHENTICATION_HOST",name);
287: }
288:
289: public boolean isAllowedHost(String host) {
290: if(getHostsRestricted()) {
291: Vector v=new Vector();
292: v.addElement(getDefaultServer());
293: Enumeration e=getAllowedHosts();
294: while(e.hasMoreElements()) {
295: v.addElement(e.nextElement());
296: }
297: Enumeration enum=v.elements();
298: while(enum.hasMoreElements()) {
299: String next=(String)enum.nextElement();
300: if(host.toUpperCase().endsWith(next.toUpperCase())) {
301: return true;
302: }
303: }
304: return false;
305: } else {
306: return true;
307: }
308: }
309:
310: public void setAllowedHosts(String hosts) {
311: NodeList nl=domain.getElementsByTagName("ALLOWED_HOST");
312: for(int i=0;i<nl.getLength();i++) {
313: domain.removeChild(nl.item(i));
314: }
315: StringTokenizer tok=new StringTokenizer(hosts,", ");
316: while(tok.hasMoreElements()) {
317: Element ahost=root.createElement("ALLOWED_HOST");
318: XMLCommon.setElementTextValue(ahost,tok.nextToken());
319: domain.appendChild(ahost);
320: }
321: }
322:
323: public Enumeration getAllowedHosts() {
324: final NodeList nl=domain.getElementsByTagName("ALLOWED_HOST");
325: return new Enumeration() {
326: int i=0;
327: public boolean hasMoreElements() {
328: return i<nl.getLength();
329: }
330:
331: public Object nextElement() {
332: String value=XMLCommon.getElementTextValue((Element)nl.item(i++));
333: return value==null?"error":value;
334: }
335: };
336: }
337:
338: public void setHostsRestricted(boolean b) {
339: NodeList nl=domain.getElementsByTagName("RESTRICTED");
340: for(int i=0;i<nl.getLength();i++) {
341: domain.removeChild(nl.item(i));
342: }
343: if(b) {
344: domain.appendChild(root.createElement("RESTRICTED"));
345: }
346: }
347:
348: public boolean getHostsRestricted() {
349: NodeList nl=domain.getElementsByTagName("RESTRICTED");
350: return nl.getLength()>0;
351: }
352: };
353: } else {
354: return null;
355: }
356: }
357:
358: /**
359: * This is just completely useless, since you can change virtual domains directly.
360: * It should be removed ASAP
361: */
362: public void setVirtualDomain(String name,
363: WebMailVirtualDomain domain) {
364: System.err
365: .println("Called useless net.wastl.webmail.xml.XMLSystemData::setVirtualDomain/2");
366: }
367:
368: public void deleteVirtualDomain(String name) {
369: NodeList nl = sysdata.getElementsByTagName("NAME");
370: for (int i = 0; i < nl.getLength(); i++) {
371: if (nl.item(i).getParentNode().getNodeName().equals(
372: "DOMAIN")
373: && XMLCommon.getElementTextValue(
374: (Element) nl.item(i)).equals(name)) {
375: sysdata.removeChild(nl.item(i).getParentNode());
376: }
377: }
378: WebMailServer.getStorage()
379: .log(
380: Storage.LOG_INFO,
381: "XMLSystemData: Deleted WebMail virtual domain "
382: + name);
383: }
384:
385: public void createVirtualDomain(String name) throws Exception {
386: WebMailVirtualDomain dom = getVirtualDomain(name);
387: if (dom != null) {
388: throw new Exception("Domain names must be unique!");
389: }
390: Element domain = root.createElement("DOMAIN");
391: sysdata.appendChild(domain);
392: domain.appendChild(root.createElement("NAME"));
393: domain.appendChild(root.createElement("DEFAULT_HOST"));
394: domain.appendChild(root.createElement("AUTHENTICATION_HOST"));
395: domain.appendChild(root.createElement("ALLOWED_HOST"));
396: XMLCommon.setTagValue(domain, "NAME", name);
397: XMLCommon.setTagValue(domain, "DEFAULT_HOST", "localhost");
398: XMLCommon.setTagValue(domain, "AUTHENTICATION_HOST",
399: "localhost");
400: XMLCommon.setTagValue(domain, "ALLOWED_HOST", "localhost");
401: WebMailServer.getStorage()
402: .log(
403: Storage.LOG_INFO,
404: "XMLSystemData: Created WebMail virtual domain "
405: + name);
406: }
407:
408: } // XMLSystemData
|