001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.xml;
024:
025: import java.io.File;
026: import java.io.OutputStream;
027: import java.io.OutputStreamWriter;
028: import java.io.PrintWriter;
029: import java.net.MalformedURLException;
030: import java.net.URI;
031: import java.net.URISyntaxException;
032: import java.net.URL;
033: import java.nio.charset.Charset;
034: import java.security.GeneralSecurityException;
035: import java.security.Key;
036: import java.text.ParseException;
037: import java.util.Enumeration;
038: import java.util.Iterator;
039: import java.util.Map;
040: import java.util.Properties;
041: import java.util.Set;
042: import java.util.Map.Entry;
043:
044: import org.isqlviewer.sql.ConnectionProfile;
045: import org.isqlviewer.sql.JdbcService;
046: import org.isqlviewer.util.BasicUtilities;
047: import org.isqlviewer.util.IsqlToolkit;
048: import org.isqlviewer.util.StringUtilities;
049: import org.xml.sax.Attributes;
050: import org.xml.sax.EntityResolver;
051: import org.xml.sax.InputSource;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.XMLReader;
054: import org.xml.sax.helpers.DefaultHandler;
055:
056: /**
057: * TODO Add ServiceDigester Overview JavaDoc.
058: * <p>
059: *
060: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
061: * @version 1.0
062: */
063: public class ServiceDigester extends DefaultHandler {
064:
065: public static final int VERSION_2 = 2;
066: public static final int VERSION_3 = 3;
067:
068: private static final String V3X_DTD_DECLARATION = "<!DOCTYPE isql-service PUBLIC \"-//iSQL-Viewer.org.//DTD JDBC Service Definition 3.0.0//EN\" \"http://isqlviewer.org/dtd/service-version3.dtd\">";
069:
070: public static final String REMOTE_URL = "url";
071: public static final String REMOTE_VERSION = "version";
072: public static final String REMOTE_SYNC_TYPE = "sync-type";
073: public static final String REMOTE_RESOURCES = "local-resources";
074:
075: private static final String TAG_ROOT = "isql-service";
076: private static final String V2_TAG_AUTH = "authorization";
077: private static final String V2_TAG_EXTENDED_PROPERTIES = "extended-properties";
078: private static final String V2_APPLICATION_PROPERTIES = "jdbc-properties";
079: private static final String V2_TAG_RSRC = "resources";
080: private static final String V2_TAG_PROP = "property";
081: private static final String V2_TAG_JAR = "jar";
082: private static final String V2_TAG_CFG = "config";
083: private static final String V2_TAG_REMOTE = "remote-config";
084: private static final String V2_ATTRIB_SECURE = "secure";
085: private static final String V2_ATTRIB_SCHEMA = "preferred-schema";
086: private static final String V2_ATTRIB_NAME = "name";
087: private static final String V2_ATTRIB_VALUE = "value";
088: private static final String V2_ATTRIB_DRVR = "driver";
089: private static final String V2_ATTRIB_USER = "user";
090: private static final String V2_ATTRIB_PASS = "password";
091: private static final String V2_ATTRIB_HREF = "href";
092:
093: private static final String V3_TAG_DESCRIPTION = "description";
094: private static final String V3_TAG_DRIVER = "driver";
095: private static final String V3_TAG_URL = "url";
096: private static final String V3_TAG_AUTHORIZATION = "authorization";
097: private static final String V3_TAG_PRINCIPAL = "principal";
098: private static final String V3_TAG_CREDENTIALS = "credentials";
099: private static final String V3_TAG_CONNECTION_PROFILE = "connection-profile";
100: private static final String V3_TAG_QUERY_TIMEOUT = "query-timeout";
101: private static final String V3_TAG_LOGIN_TIMEOUT = "login-timeout";
102: private static final String V3_TAG_JDBC_TRACING = "jdbc-tracing";
103: private static final String V3_TAG_PREFERRED_SCHEMA = "preferred-schema";
104: private static final String V3_TAG_PREFERRED_CATALOG = "preferred-catalog";
105: private static final String V3_TAG_RESOURCES = "resources";
106: private static final String V3_TAG_JAR = "jar";
107: private static final String V3_ATTRIB_VERSION = "version";
108: private static final String V3_ATTRIB_HREF = "href";
109: private static final String V3_ATTRIB_ENCRYPTED = "encrypted";
110: // private static final String V3_TAG_CONNECTION_LOGGING = "connection-logging";
111: // private static final String V3_TAG_LOG_SIZE = "log-size";
112: // private static final String V3_TAG_LOG_HOME = "log-home";
113: // private static final String V3_TAG_LOG_PATTERN = "log-pattern";
114:
115: private static final String ATTRIB_URL = REMOTE_URL;
116:
117: private static final byte[] xorKey = new byte[] { (byte) 0x01,
118: (byte) 0x08, (byte) 0x10, (byte) 0x18, (byte) 0x02,
119: (byte) 0x0A, (byte) 0x12, (byte) 0x1A, (byte) 0x04,
120: (byte) 0x0C, (byte) 0x14, (byte) 0x1C, (byte) 0x06,
121: (byte) 0x0E, (byte) 0x16, (byte) 0x1E, (byte) 0x08,
122: (byte) 0x10, (byte) 0x18, (byte) 0x20, (byte) 0x0A,
123: (byte) 0x12, (byte) 0x1A, (byte) 0x22 };
124:
125: private JdbcService reference = new JdbcService();
126: private ConnectionProfile profile = new ConnectionProfile();
127: private Properties driverProperties = new Properties();
128: private int documentVersion = VERSION_2;
129: private boolean ignoreRemoteTag = false;
130: private boolean inDriverProperties = false;
131: private boolean inEnvironmentProperties = false;
132: private boolean do21XDecryption = false;
133: private StringBuffer cdataBuffer = new StringBuffer("");
134:
135: public static JdbcService parseService(InputSource source,
136: EntityResolver resolver) throws Exception {
137:
138: XMLReader xmlreader = BasicUtilities.createXMLReader(false,
139: true);
140: ServiceDigester digester = new ServiceDigester();
141: if (resolver != null) {
142: xmlreader.setEntityResolver(resolver);
143: }
144: xmlreader.setContentHandler(digester);
145: xmlreader.setErrorHandler(digester);
146: xmlreader.parse(source);
147: return digester.reference;
148: }
149:
150: public static void writeService(int version, OutputStream out,
151: JdbcService service) {
152:
153: writeService(version, out, service, Charset.forName("UTF-8"));
154: }
155:
156: public static void writeService(int version, OutputStream out,
157: JdbcService service, Charset cs) {
158:
159: OutputStreamWriter osw = new OutputStreamWriter(out, cs);
160: PrintWriter pw = new PrintWriter(osw, false);
161: pw.print("<?xml version=\"1.0\" encoding=\"");
162: pw.print(cs.name());
163: pw.println("\" ?>");
164: try {
165: switch (version) {
166: case VERSION_2:
167: v2printServiceRoot(service, pw);
168: break;
169: case VERSION_3:
170: pw.println(V3X_DTD_DECLARATION);
171: v3printServiceRoot(service, pw);
172: break;
173: default:
174: throw new IllegalArgumentException("");
175: }
176: } finally {
177: pw.flush();
178: pw.close();
179: pw = null;
180: osw = null;
181: }
182: }
183:
184: private ServiceDigester() {
185:
186: }
187:
188: @Override
189: public void characters(char[] ch, int start, int length)
190: throws SAXException {
191:
192: cdataBuffer.append(new String(ch, start, length).trim());
193: }
194:
195: @Override
196: public void startElement(String ln, String ns, String name,
197: Attributes attr) throws SAXException {
198:
199: if (TAG_ROOT.equalsIgnoreCase(name)) {
200: String val = attr.getValue(V3_ATTRIB_VERSION);
201: if (val == null) {
202: documentVersion = VERSION_2;
203: } else {
204: documentVersion = Integer.parseInt(val);
205: }
206: }
207: switch (documentVersion) {
208: case VERSION_2:
209: v2startElement(name, attr);
210: break;
211: case VERSION_3:
212: v3startElement(name, attr);
213: break;
214: }
215: }
216:
217: @Override
218: public void endElement(String ln, String ns, String name) {
219:
220: String cdata = cdataBuffer.toString();
221: cdataBuffer.setLength(0);
222: switch (documentVersion) {
223: case VERSION_2:
224: break;
225: case VERSION_3:
226: v3endElement(name, cdata);
227: break;
228: }
229: }
230:
231: @Override
232: public void endDocument() throws SAXException {
233:
234: reference.setProfile(profile);
235: }
236:
237: private void v3endElement(String name, String cdata) {
238:
239: String tag = name.toLowerCase();
240: String interpreted = cdata;
241: if (interpreted != null) {
242: try {
243: interpreted = StringUtilities.substituteVariables(
244: cdata, System.getProperties());
245: } catch (ParseException e) {
246: }
247: }
248: if (V3_TAG_DESCRIPTION.equals(tag)) {
249: reference.setDescription(interpreted);
250: } else if (V3_TAG_DRIVER.equals(tag)) {
251: reference.setDriverClass(interpreted);
252: } else if (V3_TAG_URL.equals(tag)) {
253: reference.setUrl(interpreted);
254: } else if (V3_TAG_PRINCIPAL.equals(tag)) {
255: reference.setPrincipal(interpreted);
256: } else if (V3_TAG_CREDENTIALS.equals(tag)) {
257: String credentials = cdata;
258: if (profile.isCredentialsSecure()) {
259: Key encryptionKey = IsqlToolkit.getEncryptionKey();
260: String transformation = encryptionKey.getAlgorithm();
261: try {
262: credentials = StringUtilities.decryptText(
263: credentials, encryptionKey, transformation);
264: } catch (GeneralSecurityException ignored) {
265: }
266: }
267: reference.setCredentials(credentials);
268: } else if (V3_TAG_PREFERRED_CATALOG.equals(tag)) {
269: profile.setPreferredCatalog(interpreted);
270: } else if (V3_TAG_PREFERRED_SCHEMA.equals(tag)) {
271: profile.setPreferredSchema(interpreted);
272: } else if (V3_TAG_QUERY_TIMEOUT.equals(tag)) {
273: profile.setQueryTimeout(Integer.parseInt(interpreted));
274: } else if (V3_TAG_LOGIN_TIMEOUT.equals(tag)) {
275: profile.setLoginTimeout(Integer.parseInt(interpreted));
276: } else if (V3_TAG_JDBC_TRACING.equals(tag)) {
277: profile
278: .setTracingEnabled(Boolean
279: .parseBoolean(interpreted));
280: } else if (inDriverProperties
281: && V2_TAG_EXTENDED_PROPERTIES.equals(tag)) {
282: reference.setEnvironment(driverProperties);
283: driverProperties.clear();
284: inDriverProperties = false;
285: }
286: }
287:
288: private void v3startElement(String name, Attributes attr) {
289:
290: String tag = name.toLowerCase().trim();
291: if (TAG_ROOT.equals(tag)) {
292: String value = attr.getValue(V2_ATTRIB_NAME);
293: if (value != null) {
294: try {
295: value = StringUtilities.substituteVariables(value,
296: System.getProperties());
297: } catch (ParseException e) {
298: }
299: }
300: reference.setName(value);
301: } else if (V2_TAG_EXTENDED_PROPERTIES.equals(tag)) {
302: inDriverProperties = true;
303: } else if (V2_TAG_JAR.equals(tag)) {
304: String uri = attr.getValue(V2_ATTRIB_HREF);
305: if (uri != null && uri.trim().length() >= 1) {
306: try {
307: uri = StringUtilities.substituteVariables(uri,
308: System.getProperties());
309: } catch (ParseException e) {
310: }
311: profile.addResource(new File(uri));
312: }
313: } else if (V3_TAG_AUTHORIZATION.equals(tag)) {
314: Boolean isEncrypted = Boolean.valueOf(attr
315: .getValue(V3_ATTRIB_ENCRYPTED));
316: profile.setCredentialsPersistent(true);
317: profile.setCredentialsSecure(isEncrypted.booleanValue());
318: } else if (inDriverProperties && V2_TAG_PROP.equals(tag)) {
319: String key = attr.getValue(V2_ATTRIB_NAME);
320: String value = attr.getValue(V2_ATTRIB_VALUE);
321: if (value == null) {
322: return;
323: }
324: if (key != null && key.trim().length() > 0) {
325: driverProperties.setProperty(key, value);
326: }
327: }
328: }
329:
330: private void v2startElement(String name, Attributes attr) {
331:
332: String tag = name.toLowerCase().trim();
333: if (TAG_ROOT.equals(tag)) {
334: if (!ignoreRemoteTag) {
335: reference.setName(attr.getValue(V2_ATTRIB_NAME));
336: }
337: try {
338: do21XDecryption = Boolean.valueOf(
339: attr.getValue(V2_ATTRIB_SECURE)).booleanValue();
340: } catch (Throwable t) {
341: }
342: } else if (V2_TAG_REMOTE.equals(tag)) {
343: // TODO re-enable remote services...
344: } else if (V2_APPLICATION_PROPERTIES.equals(tag)) {
345: inEnvironmentProperties = true;
346: } else if (V2_TAG_EXTENDED_PROPERTIES.equals(tag)) {
347: inDriverProperties = true;
348: } else if (V2_TAG_CFG.equals(tag)) {
349: if (attr.getIndex(V2_ATTRIB_SCHEMA) >= 0) {
350: profile.setPreferredSchema(attr
351: .getValue(V2_ATTRIB_SCHEMA));
352: }
353: reference.setUrl(attr.getValue(ATTRIB_URL));
354: reference.setDriverClass(attr.getValue(V2_ATTRIB_DRVR));
355: } else if (V2_TAG_AUTH.equals(tag)) {
356: profile.setCredentialsPersistent(true);
357: String credentials = attr.getValue(V2_ATTRIB_PASS);
358: if (credentials != null && credentials.trim().length() >= 1) {
359: credentials = StringUtilities.decodeBase64(credentials);
360: if (do21XDecryption) {
361: credentials = v2decryptString(credentials);
362: }
363: }
364: reference.setCredentials(credentials);
365: reference.setPrincipal(attr.getValue(V2_ATTRIB_USER));
366: } else if (V2_TAG_PROP.equals(tag)) {
367: String key = attr.getValue(V2_ATTRIB_NAME);
368: String val = attr.getValue(V2_ATTRIB_VALUE);
369: if (key != null && key.trim().length() > 0) {
370: if (inDriverProperties) {
371: driverProperties.setProperty(key, val);
372: } else if (inEnvironmentProperties) {
373: if (ConnectionProfile.CONNECTION_KEEPALIVE
374: .equals(key)) {
375: profile.setKeepAlive(Boolean.valueOf(val)
376: .booleanValue());
377: } else if (ConnectionProfile.JDBC_ESCAPE_PROC
378: .equals(key)) {
379: profile.setEscapeProcessing(Boolean
380: .valueOf(val).booleanValue());
381: } else if (ConnectionProfile.JDBC_GENERATE_KEYS
382: .equals(key)) {
383: profile.setResultsetKeys(Boolean.valueOf(val)
384: .booleanValue());
385: } else if (ConnectionProfile.JDBC_MAX_FIELD_SIZE
386: .equals(key)) {
387: try {
388: profile
389: .setMaxFieldSize(Long
390: .parseLong(val));
391: } catch (NumberFormatException nfe) {
392:
393: }
394: } else if (ConnectionProfile.JDBC_MAX_ROWS
395: .equals(key)) {
396: try {
397: profile.setMaxRowCount(Long.parseLong(val));
398: } catch (NumberFormatException nfe) {
399:
400: }
401: } else if (ConnectionProfile.JDBC_REVERSE_FETCHING
402: .equals(key)) {
403: profile.setReverseFetching(Boolean.valueOf(val)
404: .booleanValue());
405: } else if (ConnectionProfile.JDBC_TRACE_ENABLED
406: .equals(key)) {
407: profile.setTracingEnabled(Boolean.valueOf(val)
408: .booleanValue());
409: } else if (ConnectionProfile.LOGIN_TIMEOUT
410: .equals(key)) {
411: try {
412: profile.setLoginTimeout(Integer
413: .parseInt(val));
414: } catch (NumberFormatException nfe) {
415:
416: }
417: } else if (ConnectionProfile.MAX_EXCEPTION_CHAIN_LENGTH
418: .equals(key)) {
419: try {
420: profile.setMaxExceptionLength(Integer
421: .parseInt(val));
422: } catch (NumberFormatException nfe) {
423:
424: }
425: } else if (ConnectionProfile.PREFERRED_CATALOG
426: .equals(key)) {
427: profile.setPreferredCatalog(val);
428: } else if (ConnectionProfile.PREFERRED_SCHEMA
429: .equals(key)) {
430: profile.setPreferredSchema(val);
431: } else if (ConnectionProfile.PRINT_STACKTRACES
432: .equals(key)) {
433: profile.setDebuggingEnabled(Boolean
434: .valueOf(val).booleanValue());
435: } else if (ConnectionProfile.QUERY_TIMEOUT
436: .equals(key)) {
437: try {
438: profile.setQueryTimeout(Integer
439: .parseInt(val));
440: } catch (NumberFormatException nfe) {
441:
442: }
443: }
444: }
445: }
446: } else if (V2_TAG_JAR.equals(tag)) {
447: String uri = attr.getValue(V2_ATTRIB_HREF);
448: if (uri != null && uri.trim().length() >= 1) {
449: try {
450: URI fileURI = new URI(new URL(uri).toExternalForm());
451: profile.addResource(new File(fileURI));
452: } catch (MalformedURLException error) {
453: // TODO add warning here //
454: } catch (URISyntaxException e) {
455: // TODO add warning here //
456: }
457: }
458: }
459: }
460:
461: private static String v2encryptString(final String text) {
462:
463: if (text == null) {
464: return "";
465: }
466:
467: String content = text;
468: if (content.length() < xorKey.length) {
469: int pad = xorKey.length - content.length();
470: byte[] padkey = new byte[pad];
471: for (int i = 0; i < pad; i++) {
472: padkey[i] = xorKey[i];
473: }
474: content = content.concat(new String(padkey));
475: }
476:
477: byte[] pass = content.getBytes();
478: byte[] xorStr = new byte[pass.length];
479: int j = 0;
480: for (int i = 0; i < pass.length; i++) {
481: if (j >= xorKey.length) {
482: j = 0;
483: }
484: xorStr[i] = (byte) (xorKey[j++] ^ (pass[i]));
485: }
486: String str = new String(xorStr);
487: return str.trim();
488: }
489:
490: private static String v2decryptString(String text) {
491:
492: if (text == null) {
493: return "";
494: }
495:
496: byte[] bytes = text.getBytes();
497: byte[] xorStr = new byte[bytes.length];
498: int j = 0;
499: for (int i = 0; i < bytes.length; i++) {
500: if (j >= xorKey.length)
501: j = 0;
502: xorStr[i] = (byte) ((bytes[i]) ^ xorKey[j++]);
503: }
504: String newStr = new String(xorStr);
505: newStr.replace('\u0000', ' ');
506: return newStr.trim();
507: }
508:
509: private static void v3printServiceRoot(JdbcService service,
510: PrintWriter ps) {
511:
512: ps.print("<");
513: ps.print(TAG_ROOT);
514: ps.print(" ");
515: ps.print(V3_ATTRIB_VERSION);
516: ps.print("=\"");
517: ps.print(Integer.toString(VERSION_3));
518: ps.print("\" ");
519: ps.print(V2_ATTRIB_NAME);
520: ps.print("=\"");
521: ps.print(StringUtilities.encodeXMLEntities(service.getName()));
522: ps.println("\" >");
523: v3printBasicElements(service, ps);
524: v3printProfileElements(service, ps);
525: ps.print("</");
526: ps.print(TAG_ROOT);
527: ps.println(">");
528: }
529:
530: /**
531: * @param service
532: * @param ps
533: */
534: private static void v3printProfileElements(JdbcService service,
535: PrintWriter ps) {
536:
537: ConnectionProfile profile = service.getProfile();
538: ps.print("\t<");
539: ps.print(V3_TAG_CONNECTION_PROFILE);
540: ps.println('>');
541: ps.print("\t\t<");
542: ps.print(V3_TAG_QUERY_TIMEOUT);
543: ps.print('>');
544: ps.print(Integer.toString(profile.getQueryTimeout()));
545: ps.print("</");
546: ps.print(V3_TAG_QUERY_TIMEOUT);
547: ps.println('>');
548: ps.print("\t\t<");
549: ps.print(V3_TAG_LOGIN_TIMEOUT);
550: ps.print('>');
551: ps.print(Integer.toString(profile.getLoginTimeout()));
552: ps.print("</");
553: ps.print(V3_TAG_LOGIN_TIMEOUT);
554: ps.println('>');
555: ps.print("\t\t<");
556: ps.print(V3_TAG_JDBC_TRACING);
557: ps.print('>');
558: ps.print(Boolean.toString(profile.isTracingEnabled()));
559: ps.print("</");
560: ps.print(V3_TAG_JDBC_TRACING);
561: ps.println('>');
562: ps.print("\t\t<");
563: ps.print(V3_TAG_PREFERRED_SCHEMA);
564: ps.print('>');
565: ps.print(StringUtilities.encodeXMLEntities(profile
566: .getPreferredSchema()));
567: ps.print("</");
568: ps.print(V3_TAG_PREFERRED_SCHEMA);
569: ps.println('>');
570: ps.print("\t\t<");
571: ps.print(V3_TAG_PREFERRED_CATALOG);
572: ps.print('>');
573: ps.print(StringUtilities.encodeXMLEntities(profile
574: .getPreferredCatalog()));
575: ps.print("</");
576: ps.print(V3_TAG_PREFERRED_CATALOG);
577: ps.println('>');
578:
579: ps.print("\t\t<");
580: ps.print(V2_TAG_EXTENDED_PROPERTIES);
581: ps.println('>');
582: Properties environment = service.getEnvironment();
583: Set<Entry<Object, Object>> entries = environment.entrySet();
584: for (Entry<Object, Object> entry : entries) {
585: String key = entry.getKey().toString();
586: String value = entry.getValue().toString();
587: ps.print("\t\t\t<");
588: ps.print(V2_TAG_PROP);
589: ps.print(" ");
590: ps.print(V2_ATTRIB_NAME);
591: ps.print("=\"");
592: ps.print(StringUtilities.encodeXMLEntities(key));
593: ps.print("\" ");
594: ps.print(V2_ATTRIB_VALUE);
595: ps.print("=\"");
596: ps.print(StringUtilities.encodeXMLEntities(value));
597: ps.println("\" />");
598: }
599: ps.print("\t\t</");
600: ps.print(V2_TAG_EXTENDED_PROPERTIES);
601: ps.println('>');
602:
603: ps.print("\t\t<");
604: ps.print(V3_TAG_RESOURCES);
605: ps.println('>');
606: Enumeration<File> elements = profile.resourceElements();
607: while (elements.hasMoreElements()) {
608: File next = elements.nextElement();
609: ps.print("\t\t\t<");
610: ps.print(V3_TAG_JAR);
611: ps.print(" ");
612: ps.print(V3_ATTRIB_HREF);
613: ps.print("=\"");
614: ps.print(StringUtilities.encodeXMLEntities(next
615: .getAbsolutePath()));
616: ps.println("\" />");
617: }
618: ps.print("\t\t</");
619: ps.print(V3_TAG_RESOURCES);
620: ps.println('>');
621: ps.print("\t</");
622: ps.print(V3_TAG_CONNECTION_PROFILE);
623: ps.println('>');
624: }
625:
626: private static void v3printBasicElements(JdbcService service,
627: PrintWriter ps) {
628:
629: ps.print("\t<");
630: ps.print(V3_TAG_DESCRIPTION);
631: ps.print('>');
632: ps.print(StringUtilities.encodeXMLEntities(service
633: .getDescription()));
634: ps.print("</");
635: ps.print(V3_TAG_DESCRIPTION);
636: ps.println('>');
637: ps.print("\t<");
638: ps.print(V3_TAG_DRIVER);
639: ps.print('>');
640: ps.print(StringUtilities.encodeXMLEntities(service
641: .getDriverClass()));
642: ps.print("</");
643: ps.print(V3_TAG_DRIVER);
644: ps.println('>');
645: ps.print("\t<");
646: ps.print(V3_TAG_URL);
647: ps.print('>');
648: ps.print(StringUtilities.encodeXMLEntities(service.getUrl()));
649: ps.print("</");
650: ps.print(V3_TAG_URL);
651: ps.println('>');
652: ConnectionProfile profile = service.getProfile();
653: if (profile.isCredentialsPersistent()) {
654: ps.print("\t<");
655: ps.print(V3_TAG_AUTHORIZATION);
656: ps.print(" ");
657: ps.print(V3_ATTRIB_ENCRYPTED);
658: ps.print("=\"");
659: ps.print(Boolean.toString(profile.isCredentialsSecure()));
660: ps.println("\" >");
661: ps.print("\t\t<");
662: ps.print(V3_TAG_PRINCIPAL);
663: ps.print('>');
664: ps.print(StringUtilities.encodeXMLEntities(service
665: .getPrincipal()));
666: ps.print("</");
667: ps.print(V3_TAG_PRINCIPAL);
668: ps.println('>');
669: ps.print("\t\t<");
670: ps.print(V3_TAG_CREDENTIALS);
671: ps.print('>');
672: String credentials = service.getCredentials();
673: if (profile.isCredentialsSecure()) {
674: Key encryptionKey = IsqlToolkit.getEncryptionKey();
675: String transformation = encryptionKey.getAlgorithm();
676: try {
677: credentials = StringUtilities.encryptText(
678: credentials, encryptionKey, transformation);
679: } catch (GeneralSecurityException ignored) {
680: }
681: }
682: ps.print(StringUtilities.encodeXMLEntities(credentials));
683: ps.print("</");
684: ps.print(V3_TAG_CREDENTIALS);
685: ps.println('>');
686: ps.print("\t</");
687: ps.print(V3_TAG_AUTHORIZATION);
688: ps.println('>');
689: }
690: }
691:
692: private static void v2printServiceRoot(JdbcService service,
693: PrintWriter ps) {
694:
695: ConnectionProfile profile = service.getProfile();
696: ps.print("<");
697: ps.print(TAG_ROOT);
698: ps.print(" ");
699: ps.print(V2_ATTRIB_SECURE);
700: ps.print("=\"");
701: ps.print(profile.isCredentialsSecure());
702: ps.print("\" ");
703: ps.print(V2_ATTRIB_NAME);
704: ps.print("=\"");
705: ps.print(StringUtilities.encodeXMLEntities(service.getName()));
706: ps.println("\" >");
707: v2printAuthorizationTag(service, ps);
708: v2printServiceConfig(service, ps);
709: ps.print("</");
710: ps.print(TAG_ROOT);
711: ps.println(">");
712: }
713:
714: private static void v2printAuthorizationTag(JdbcService service,
715: PrintWriter ps) {
716:
717: ConnectionProfile profile = service.getProfile();
718: if (profile.isCredentialsPersistent()) {
719: ps.print("\t<");
720: ps.print(V2_TAG_AUTH);
721: ps.print(" ");
722: ps.print(V2_ATTRIB_USER);
723: ps.print("=\"");
724: ps.print(StringUtilities.encodeXMLEntities(service
725: .getPrincipal()));
726: ps.print("\" ");
727: ps.print(V2_ATTRIB_PASS);
728: ps.print("=\"");
729: String credentials = service.getCredentials();
730: if (credentials != null && credentials.trim().length() >= 1) {
731: if (profile.isCredentialsSecure()) {
732: credentials = v2encryptString(credentials);
733: }
734: credentials = StringUtilities.encodeBase64(credentials);
735: ps.print(credentials.trim());
736: }
737: ps.println("\" />");
738: }
739: }
740:
741: private static void v2printServiceConfig(JdbcService service,
742: PrintWriter ps) {
743:
744: ConnectionProfile profile = service.getProfile();
745: String s = null;
746:
747: ps.print("\t<");
748: ps.print(V2_TAG_CFG);
749: ps.print(" ");
750: s = profile.getPreferredSchema();
751: if (s != null && s.trim().length() >= 1) {
752: ps.print(V2_ATTRIB_SCHEMA);
753: ps.print("=\"");
754: ps.print(StringUtilities.encodeXMLEntities(s));
755: ps.print("\" ");
756: }
757:
758: s = service.getDriverClass();
759: ps.print(V2_ATTRIB_DRVR);
760: ps.print("=\"");
761: ps.print(StringUtilities.encodeXMLEntities(s));
762: ps.print("\" ");
763: s = service.getUrl();
764: ps.print(ATTRIB_URL);
765: ps.print("=\"");
766: ps.print(StringUtilities.encodeXMLEntities(s));
767: ps.println("\" >");
768:
769: Properties properties = service.getEnvironment();
770: v2printProperties(ps, properties, 2, V2_TAG_EXTENDED_PROPERTIES);
771:
772: properties = profile.toProperties();
773: v2printProperties(ps, properties, 2, V2_APPLICATION_PROPERTIES);
774:
775: Enumeration elements = profile.resourceElements();
776: if (elements.hasMoreElements()) {
777: ps.print("\t\t<");
778: ps.print(V2_TAG_RSRC);
779: ps.println(">");
780: while (elements.hasMoreElements()) {
781: URL url = (URL) elements.nextElement();
782: if (url == null) {
783: continue;
784: }
785: ps.print("\t\t\t<");
786: ps.print(V2_TAG_JAR);
787: ps.print(" ");
788: ps.print(V2_ATTRIB_HREF);
789: ps.print("=\"");
790: ps.print(StringUtilities.encodeXMLEntities(url
791: .toExternalForm()));
792: ps.println("\" />");
793: }
794: ps.print("\t\t</");
795: ps.print(V2_TAG_RSRC);
796: ps.println(">");
797: }
798: ps.print("\t</");
799: ps.print(V2_TAG_CFG);
800: ps.println(">");
801: }
802:
803: /**
804: * @param ps
805: * @param extProps
806: */
807: private static void v2printProperties(PrintWriter ps,
808: Properties properties, int depth, String propertyId) {
809:
810: if (properties != null && !properties.isEmpty()) {
811: for (int i = 0; i < depth; i++) {
812: ps.print('\t');
813: }
814: ps.print('<');
815: ps.print(propertyId);
816: ps.println('>');
817: Iterator itr = properties.entrySet().iterator();
818: while (itr.hasNext()) {
819: Map.Entry entry = (Map.Entry) itr.next();
820: String key = (String) entry.getKey();
821: String val = (String) entry.getValue();
822: for (int i = 0; i < depth + 1; i++) {
823: ps.print('\t');
824: }
825: ps.print('<');
826: ps.print(V2_TAG_PROP);
827: ps.print(" ");
828: ps.print(V2_ATTRIB_NAME);
829: ps.print("=\"");
830: ps.print(StringUtilities.encodeXMLEntities(key).trim());
831: ps.print("\" ");
832: ps.print(V2_ATTRIB_VALUE);
833: ps.print("=\"");
834: ps.print(StringUtilities.encodeXMLEntities(val).trim());
835: ps.println("\" />");
836: }
837: for (int i = 0; i < depth; i++) {
838: ps.print('\t');
839: }
840: ps.print("</");
841: ps.print(propertyId);
842: ps.println(">");
843: }
844: }
845: }
|