001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.daemon.configuration;
027:
028: import com.sshtools.daemon.vfs.*;
029:
030: import org.apache.commons.logging.*;
031:
032: import org.xml.sax.*;
033: import org.xml.sax.helpers.*;
034:
035: import java.io.*;
036:
037: import java.util.*;
038:
039: import javax.xml.parsers.*;
040:
041: /**
042: *
043: *
044: * @author $author$
045: * @version $Revision: 1.13 $
046: */
047: public class PlatformConfiguration extends DefaultHandler {
048: private static Log log = LogFactory
049: .getLog(PlatformConfiguration.class);
050: private static final String PLATFORM_ELEMENT = "PlatformConfiguration";
051: private static final String NATIVE_PROCESS_ELEMENT = "NativeProcessProvider";
052: private static final String NATIVE_AUTH_ELEMENT = "NativeAuthenticationProvider";
053: private static final String NFS_ELEMENT = "NativeFileSystemProvider";
054: private static final String NATIVE_SETTING_ELEMENT = "NativeSetting";
055: private static final String VFSMOUNT_ELEMENT = "VFSMount";
056: private static final String VFSROOT_ELEMENT = "VFSRoot";
057: private static final String VFSPERMISSION_ELEMENT = "VFSPermission";
058: private static final String PATH_ATTRIBUTE = "path";
059: private static final String MOUNT_ATTRIBUTE = "mount";
060: private static final String NAME_ATTRIBUTE = "name";
061: private static final String VALUE_ATTRIBUTE = "value";
062: private static final String PERMISSIONS_ATTRIBUTE = "permissions";
063: private String currentElement = null;
064: private Map nativeSettings = new HashMap();
065: private String nativeProcessProvider = null;
066: private String nativeAuthenticationProvider = null;
067: private String nativeFileSystemProvider = null;
068: private Map vfsMounts = new HashMap();
069: private VFSMount vfsRoot = null;
070:
071: /**
072: * Creates a new PlatformConfiguration object.
073: *
074: * @param in
075: *
076: * @throws SAXException
077: * @throws ParserConfigurationException
078: * @throws IOException
079: */
080: protected PlatformConfiguration(InputStream in)
081: throws SAXException, ParserConfigurationException,
082: IOException {
083: reload(in);
084: }
085:
086: /**
087: *
088: *
089: * @param in
090: *
091: * @throws SAXException
092: * @throws ParserConfigurationException
093: * @throws IOException
094: */
095: public void reload(InputStream in) throws SAXException,
096: ParserConfigurationException, IOException {
097: SAXParserFactory saxFactory = SAXParserFactory.newInstance();
098: SAXParser saxParser = saxFactory.newSAXParser();
099: saxParser.parse(in, new PlatformConfigurationSAXHandler());
100: }
101:
102: /**
103: *
104: *
105: * @return
106: */
107: public Map getVFSMounts() {
108: return vfsMounts;
109: }
110:
111: /**
112: *
113: *
114: * @return
115: */
116: public String getNativeAuthenticationProvider() {
117: return nativeAuthenticationProvider;
118: }
119:
120: /**
121: *
122: *
123: * @return
124: */
125: public String getNativeFileSystemProvider() {
126: return nativeFileSystemProvider;
127: }
128:
129: /**
130: *
131: *
132: * @return
133: */
134: public String getNativeProcessProvider() {
135: return nativeProcessProvider;
136: }
137:
138: /**
139: *
140: *
141: * @param name
142: *
143: * @return
144: */
145: public String getSetting(String name) {
146: return (String) nativeSettings.get(name);
147: }
148:
149: /**
150: *
151: *
152: * @param name
153: * @param defaultValue
154: *
155: * @return
156: */
157: public String getSetting(String name, String defaultValue) {
158: if (nativeSettings.containsKey(name)) {
159: return (String) nativeSettings.get(name);
160: } else {
161: return defaultValue;
162: }
163: }
164:
165: /**
166: *
167: *
168: * @param name
169: *
170: * @return
171: */
172: public boolean containsSetting(String name) {
173: return nativeSettings.containsKey(name);
174: }
175:
176: /**
177: *
178: *
179: * @return
180: */
181: public VFSMount getVFSRoot() {
182: return vfsRoot;
183: }
184:
185: /**
186: *
187: *
188: * @return
189: */
190: public String toString() {
191: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
192: xml += ("<!-- Platform Configuration file, Determines the behaviour of platform specific services -->\n<"
193: + PLATFORM_ELEMENT + ">\n");
194: xml += " <!-- The process provider for executing and redirecting a process -->\n";
195: xml += (" <" + NATIVE_PROCESS_ELEMENT + ">"
196: + nativeProcessProvider + "</" + NATIVE_PROCESS_ELEMENT + ">\n");
197: xml += " <!-- The authentication provider for authenticating users and obtaining user information -->\n";
198: xml += (" <" + NATIVE_AUTH_ELEMENT + ">"
199: + nativeAuthenticationProvider + "</"
200: + NATIVE_AUTH_ELEMENT + ">\n");
201: xml += " <!-- Native settings which may be used by the process or authentication provider -->\n";
202:
203: Map.Entry entry;
204: Iterator it = nativeSettings.entrySet().iterator();
205:
206: while (it.hasNext()) {
207: entry = (Map.Entry) it.next();
208: xml += (" " + "<" + NATIVE_SETTING_ELEMENT + " "
209: + NAME_ATTRIBUTE + "=\""
210: + entry.getKey().toString() + "\" "
211: + VALUE_ATTRIBUTE + "=\""
212: + entry.getValue().toString() + "\"/>\n");
213: }
214:
215: if (vfsRoot != null) {
216: xml += (" " + "<" + VFSROOT_ELEMENT + " path=\""
217: + vfsRoot + "\"/>\n");
218: }
219:
220: it = vfsMounts.entrySet().iterator();
221:
222: String path;
223: String mount;
224:
225: while (it.hasNext()) {
226: entry = (Map.Entry) it.next();
227: path = (String) entry.getValue();
228: mount = (String) entry.getKey();
229: xml += (" "
230: + "<"
231: + VFSMOUNT_ELEMENT
232: + " "
233: + (mount.equals(path) ? ""
234: : (MOUNT_ATTRIBUTE + "=\""
235: + entry.getKey().toString() + "\" "))
236: + PATH_ATTRIBUTE + "=\""
237: + entry.getValue().toString() + "\"/>\n");
238: }
239:
240: xml += ("</" + PLATFORM_ELEMENT + ">");
241:
242: return xml;
243: }
244:
245: class PlatformConfigurationSAXHandler extends DefaultHandler {
246: private VFSMount currentMount = null;
247:
248: public void startElement(String uri, String localName,
249: String qname, Attributes attrs) throws SAXException {
250: if (currentElement == null) {
251: if (qname.equals(PLATFORM_ELEMENT)) {
252: currentElement = qname;
253: }
254:
255: nativeProcessProvider = null;
256: nativeAuthenticationProvider = null;
257: nativeSettings.clear();
258: } else {
259: if (currentElement.equals(PLATFORM_ELEMENT)) {
260: if (!qname.equals(NATIVE_PROCESS_ELEMENT)
261: && !qname.equals(NATIVE_AUTH_ELEMENT)
262: && !qname.equals(NATIVE_SETTING_ELEMENT)
263: && !qname.equals(VFSMOUNT_ELEMENT)
264: && !qname.equals(VFSROOT_ELEMENT)
265: && !qname.equals(NFS_ELEMENT)) {
266: throw new SAXException("Unexpected element "
267: + qname);
268: }
269: } else if (currentElement.equals(VFSMOUNT_ELEMENT)) {
270: if (!qname.equals(VFSPERMISSION_ELEMENT)) {
271: throw new SAXException("Unexpected element "
272: + qname);
273: }
274: } else {
275: throw new SAXException("Unexpected element "
276: + qname);
277: }
278:
279: currentElement = qname;
280:
281: if (qname.equals(NATIVE_SETTING_ELEMENT)) {
282: String name = attrs.getValue(NAME_ATTRIBUTE);
283: String value = attrs.getValue(VALUE_ATTRIBUTE);
284:
285: if ((name == null) || (value == null)) {
286: throw new SAXException(
287: "Required attributes missing for NativeSetting element");
288: }
289:
290: log.debug("NativeSetting " + name + "=" + value);
291: nativeSettings.put(name, value);
292: }
293:
294: if (qname.equals(VFSPERMISSION_ELEMENT)) {
295: String name = attrs.getValue(NAME_ATTRIBUTE);
296: String permissions = attrs
297: .getValue(PERMISSIONS_ATTRIBUTE);
298: currentMount.setPermissions(new VFSPermission(name,
299: permissions));
300: }
301:
302: if (qname.equals(VFSMOUNT_ELEMENT)) {
303: String path = attrs.getValue(PATH_ATTRIBUTE);
304: String mount = attrs.getValue(MOUNT_ATTRIBUTE);
305: String permissions = attrs
306: .getValue(PERMISSIONS_ATTRIBUTE);
307:
308: if ((path != null) && (mount != null)) {
309: // verify the mount - must start with / and be unique
310: if (!mount.trim().equals("/")) {
311: try {
312: currentMount = new VFSMount(mount, path);
313:
314: if (permissions == null) {
315: currentMount
316: .setPermissions(new VFSPermission(
317: "default"));
318: } else {
319: currentMount
320: .setPermissions(new VFSPermission(
321: "default",
322: permissions));
323: }
324:
325: if (!vfsMounts.containsKey(currentMount
326: .getMount())) {
327: vfsMounts.put(currentMount
328: .getMount(), currentMount);
329: } else {
330: throw new SAXException("The mount "
331: + mount
332: + " is already defined");
333: }
334: } catch (IOException ex1) {
335: throw new SAXException(
336: "VFSMount element is invalid mount="
337: + mount + " path="
338: + path);
339: }
340: } else {
341: throw new SAXException(
342: "The root mount / cannot be configured, use <VFSRoot path=\""
343: + path + "\"/> instead");
344: }
345: } else {
346: throw new SAXException("Required "
347: + PATH_ATTRIBUTE
348: + " attribute for element <"
349: + VFSMOUNT_ELEMENT + "> is missing");
350: }
351: }
352:
353: if (qname.equals(VFSROOT_ELEMENT)) {
354: if (vfsRoot != null) {
355: throw new SAXException(
356: "Only one VFSRoot can be defined");
357: }
358:
359: String path = attrs.getValue(PATH_ATTRIBUTE);
360: String permissions = attrs
361: .getValue(PERMISSIONS_ATTRIBUTE);
362:
363: try {
364: vfsRoot = new VFSMount("/", path);
365:
366: if (permissions != null) {
367: vfsRoot.setPermissions(new VFSPermission(
368: "default", permissions));
369: } else {
370: vfsRoot.setPermissions(new VFSPermission(
371: "default"));
372: }
373:
374: vfsRoot.setRoot(true);
375: } catch (IOException ex) {
376: throw new SAXException(
377: "VFSRoot element is invalid path="
378: + path);
379: }
380: }
381: }
382: }
383:
384: public void characters(char[] ch, int start, int length)
385: throws SAXException {
386: if (currentElement == null) {
387: throw new SAXException("Unexpected characters found");
388: }
389:
390: if (currentElement.equals(NATIVE_AUTH_ELEMENT)) {
391: nativeAuthenticationProvider = new String(ch, start,
392: length).trim();
393: log.debug("NativeAuthenticationProvider="
394: + nativeAuthenticationProvider);
395:
396: return;
397: }
398:
399: if (currentElement.equals(NATIVE_PROCESS_ELEMENT)) {
400: nativeProcessProvider = new String(ch, start, length)
401: .trim();
402: log.debug("NativeProcessProvider="
403: + nativeProcessProvider);
404:
405: return;
406: }
407:
408: if (currentElement.equals(NFS_ELEMENT)) {
409: nativeFileSystemProvider = new String(ch, start, length)
410: .trim();
411: log.debug("NativeFileSystemProvider="
412: + nativeFileSystemProvider);
413:
414: return;
415: }
416: }
417:
418: public void endElement(String uri, String localName,
419: String qname) throws SAXException {
420: if (currentElement == null) {
421: throw new SAXException("Unexpected end element for "
422: + qname);
423: }
424:
425: if (!currentElement.equals(qname)) {
426: throw new SAXException("Unexpected end element found");
427: }
428:
429: if (currentElement.equals(PLATFORM_ELEMENT)) {
430: currentElement = null;
431:
432: return;
433: }
434:
435: if (currentElement.equals(VFSPERMISSION_ELEMENT)) {
436: currentElement = VFSMOUNT_ELEMENT;
437: } else if (!currentElement.equals(NATIVE_SETTING_ELEMENT)
438: && !currentElement.equals(NATIVE_AUTH_ELEMENT)
439: && !currentElement.equals(NATIVE_PROCESS_ELEMENT)
440: && !currentElement.equals(NFS_ELEMENT)
441: && !currentElement.equals(VFSMOUNT_ELEMENT)
442: && !currentElement.equals(VFSROOT_ELEMENT)) {
443: throw new SAXException("Unexpected end element for "
444: + qname);
445: } else {
446: currentElement = PLATFORM_ELEMENT;
447: }
448: }
449: }
450: }
|