001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005 Dirk von der Weiden <dvdw@imail.de>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library located in LGPL.txt in the
021: * license directory; if not, write to the
022: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: * Boston, MA 02111-1307, USA.
024: *
025: * If this agreement does not cover your requirements, please contact us
026: * via email to get detailed information about the commercial license
027: * or our service offerings!
028: *
029: */
030: // @@
031: package de.ug2t.kernel;
032:
033: import java.io.*;
034: import java.nio.charset.*;
035: import java.util.*;
036:
037: import de.ug2t.connector.*;
038: import de.ug2t.process.*;
039: import de.ug2t.xmlScript.*;
040:
041: /**
042: * @author Dirk
043: *
044: * date: 02.01.2004 project: WiSer-Framework
045: *
046: * <p>
047: * The envorinment class is a collection of static methods to get information
048: * about the runtime environment of a application
049: * </p>
050: */
051: public final class KeEnvironment {
052: private static String pem_rootDir = "";
053: private static ICoXmlParameterGetter pem_dg = null;
054: private static boolean pem_init = false;
055:
056: // @@
057:
058: private static String pem_tmpDir = "";
059:
060: private static String pem_semaphor = "";
061:
062: private static String CHARACTERSET = "ISO-8859-1";
063: private static Charset CHARACTERSETOBJ = null;
064:
065: static {
066: KeRegisteredObject
067: .pcmf_registerGlobal("System.out", System.out);
068: };
069:
070: static class KeUrlUTF8Encoder {
071: final static String[] hex = { "%00", "%01", "%02", "%03",
072: "%04", "%05", "%06", "%07", "%08", "%09", "%0a", "%0b",
073: "%0c", "%0d", "%0e", "%0f", "%10", "%11", "%12", "%13",
074: "%14", "%15", "%16", "%17", "%18", "%19", "%1a", "%1b",
075: "%1c", "%1d", "%1e", "%1f", "%20", "%21", "%22", "%23",
076: "%24", "%25", "%26", "%27", "%28", "%29", "%2a", "%2b",
077: "%2c", "%2d", "%2e", "%2f", "%30", "%31", "%32", "%33",
078: "%34", "%35", "%36", "%37", "%38", "%39", "%3a", "%3b",
079: "%3c", "%3d", "%3e", "%3f", "%40", "%41", "%42", "%43",
080: "%44", "%45", "%46", "%47", "%48", "%49", "%4a", "%4b",
081: "%4c", "%4d", "%4e", "%4f", "%50", "%51", "%52", "%53",
082: "%54", "%55", "%56", "%57", "%58", "%59", "%5a", "%5b",
083: "%5c", "%5d", "%5e", "%5f", "%60", "%61", "%62", "%63",
084: "%64", "%65", "%66", "%67", "%68", "%69", "%6a", "%6b",
085: "%6c", "%6d", "%6e", "%6f", "%70", "%71", "%72", "%73",
086: "%74", "%75", "%76", "%77", "%78", "%79", "%7a", "%7b",
087: "%7c", "%7d", "%7e", "%7f", "%80", "%81", "%82", "%83",
088: "%84", "%85", "%86", "%87", "%88", "%89", "%8a", "%8b",
089: "%8c", "%8d", "%8e", "%8f", "%90", "%91", "%92", "%93",
090: "%94", "%95", "%96", "%97", "%98", "%99", "%9a", "%9b",
091: "%9c", "%9d", "%9e", "%9f", "%a0", "%a1", "%a2", "%a3",
092: "%a4", "%a5", "%a6", "%a7", "%a8", "%a9", "%aa", "%ab",
093: "%ac", "%ad", "%ae", "%af", "%b0", "%b1", "%b2", "%b3",
094: "%b4", "%b5", "%b6", "%b7", "%b8", "%b9", "%ba", "%bb",
095: "%bc", "%bd", "%be", "%bf", "%c0", "%c1", "%c2", "%c3",
096: "%c4", "%c5", "%c6", "%c7", "%c8", "%c9", "%ca", "%cb",
097: "%cc", "%cd", "%ce", "%cf", "%d0", "%d1", "%d2", "%d3",
098: "%d4", "%d5", "%d6", "%d7", "%d8", "%d9", "%da", "%db",
099: "%dc", "%dd", "%de", "%df", "%e0", "%e1", "%e2", "%e3",
100: "%e4", "%e5", "%e6", "%e7", "%e8", "%e9", "%ea", "%eb",
101: "%ec", "%ed", "%ee", "%ef", "%f0", "%f1", "%f2", "%f3",
102: "%f4", "%f5", "%f6", "%f7", "%f8", "%f9", "%fa", "%fb",
103: "%fc", "%fd", "%fe", "%ff" };
104:
105: /**
106: * For internal use only
107: */
108: public static String fastEncode(String xString) {
109: xString = xString.trim();
110: char l_target[] = new char[xString.length() * 10];
111: int l_tp = 0;
112: int i = 0;
113:
114: for (; i < xString.length(); i++) {
115: int ch = xString.charAt(i);
116: if (('A' <= ch && ch <= 'Z')
117: || ('a' <= ch && ch <= 'z')
118: || ('0' <= ch && ch <= '9')
119: || (ch == '-' || ch == '_' || ch == '.'
120: || ch == '!' || ch == '~' || ch == '*'
121: || ch == '\'' || ch == '(' || ch == '/' || ch == ')')) {
122: l_target[l_tp++] = (char) ch;
123: continue;
124: } else if (ch <= 0x00ff) {
125: String l_res = hex[ch];
126: l_res.getChars(0, 3, l_target, l_tp);
127: l_tp += 3;
128:
129: continue;
130: } else if (ch <= 0x07FF) {
131: String l_res = hex[0xc0 | (ch >> 6)];
132: l_res.getChars(0, 3, l_target, l_tp);
133: l_tp += 3;
134: l_res = hex[0x80 | (ch & 0x3F)];
135: l_res.getChars(0, 3, l_target, l_tp);
136: l_tp += 3;
137:
138: continue;
139: } else {
140: String l_res = hex[0xe0 | (ch >> 12)];
141: l_res.getChars(0, 3, l_target, l_tp);
142: l_tp += 3;
143: l_res = hex[0x80 | ((ch >> 6) & 0x3F)];
144: l_res.getChars(0, 3, l_target, l_tp);
145: l_tp += 3;
146: l_res = hex[0x80 | (ch & 0x3F)];
147: l_res.getChars(0, 3, l_target, l_tp);
148: l_tp += 3;
149:
150: continue;
151: }
152: }
153:
154: return (new String(l_target, 0, l_tp));
155: }
156: }
157:
158: /**
159: * <p>
160: * Gets the current character set
161: * </p>
162: * <p>
163: *
164: * </p>
165: * <p>
166: *
167: * @return character set as String
168: * </p>
169: */
170: public static String pcmf_getCharacterSet() {
171: return (KeEnvironment.CHARACTERSET);
172: }
173:
174: /**
175: * <p>
176: * Encodes a String to an URL safe form
177: * </p>
178: * <p>
179: *
180: * </p>
181: * <p>
182: *
183: * @return encoded String
184: * </p>
185: */
186: public static String pcmf_encodeURL(String xToEncode) {
187: try {
188: return (KeUrlUTF8Encoder.fastEncode(xToEncode));
189: } catch (Exception e) {
190: KeLog.pcmf_log("ug2t", "unknown characterset: "
191: + KeEnvironment.CHARACTERSET, null, KeLog.ERROR);
192: return (xToEncode);
193: }
194: }
195:
196: /**
197: * <p>
198: * Gets the current character set
199: * </p>
200: * <p>
201: *
202: * </p>
203: * <p>
204: *
205: * @return character set as Charset object
206: * </p>
207: */
208: public static Charset pcmf_getCharacterSetObject() {
209: return (KeEnvironment.CHARACTERSETOBJ);
210: }
211:
212: /**
213: * <p>
214: * Gets the root directory of the application all paths used bay the
215: * application are relative to this directory
216: * </p>
217: * <p>
218: *
219: * </p>
220: * <p>
221: *
222: * @return root directory
223: * </p>
224: */
225: public static final String pcmf_getRootDir() {
226: return pem_rootDir;
227: };
228:
229: /**
230: * For internal use only
231: */
232: public static final void pcmf_setRootDir(String xRoot) {
233: xRoot = xRoot.replace('\\', File.separator.charAt(0));
234:
235: File l_file = new File(xRoot.trim());
236:
237: pem_rootDir = l_file.getAbsolutePath() + File.separator;
238:
239: try {
240: CHARACTERSET = KeEnvironment.pcmf_getParameter(
241: "environment.xml", "runtime", "ENCODING")
242: .toString();
243: CHARACTERSETOBJ = (Charset) Charset.availableCharsets()
244: .get(KeEnvironment.CHARACTERSET);
245: KeLog.pcmf_log("ug2t", "special encoding found, use "
246: + CHARACTERSET, null, KeLog.MESSAGE);
247: } catch (Exception e) {
248: CHARACTERSETOBJ = (Charset) Charset.availableCharsets()
249: .get(KeEnvironment.CHARACTERSET);
250: KeLog.pcmf_log("ug2t", "no special encoding found, use "
251: + CHARACTERSET, null, KeLog.MESSAGE);
252: }
253: };
254:
255: /**
256: * For internal use only
257: */
258: public static final void pcmf_setTmpDir(String xTmp) {
259: xTmp = xTmp.replace('\\', File.separator.charAt(0));
260:
261: File l_file = new File(xTmp.trim());
262:
263: if (l_file.exists() == false)
264: l_file.mkdir();
265:
266: pem_tmpDir = l_file.getAbsolutePath() + File.separator;
267:
268: KeTools.pcmf_deleteFiles(l_file, false);
269: }
270:
271: /**
272: * <p>
273: * Gets the temporary directory of the application, it is shared between all
274: * sessions. The content of this directory is cleaned up during application
275: * startup
276: * </p>
277: * <p>
278: *
279: * </p>
280: * <p>
281: *
282: * @return temporary directory
283: * </p>
284: */
285: public static String pcmf_getTmpDir() {
286: return (KeEnvironment.pem_tmpDir);
287: }
288:
289: /**
290: * <p>
291: * Gets the temporary directory of the session. The content of this directory
292: * is cleaned when the session is removed startup
293: * </p>
294: * <p>
295: *
296: * </p>
297: * <p>
298: *
299: * @return temporary directory
300: * </p>
301: */
302: public static String pcmf_getSessionTmpDir() {
303: String l_dname = KeEnvironment.pem_tmpDir
304: + Thread.currentThread().getName() + File.separator;
305: File l_file = new File(l_dname);
306: if (l_file.exists() == false)
307: l_file.mkdirs();
308:
309: return (l_dname);
310: }
311:
312: /**
313: * <p>
314: * Builds the real absolute path of a relative path.
315: * </p>
316: * <p>
317: *
318: * </p>
319: * <p>
320: *
321: * @return real path
322: * </p>
323: */
324: public static final String pcmf_buildPath(String xPath) {
325: xPath = xPath.replace('\\', File.separator.charAt(0));
326:
327: File l_file = new File(xPath.trim());
328: if (l_file.isAbsolute())
329: return (xPath);
330: else
331: return (pem_rootDir + xPath);
332: };
333:
334: /**
335: * For internal use only
336: */
337: public static final void pcmf_initScript(String xFile,
338: String xScript) {
339: try {
340: synchronized (pem_semaphor) {
341: if (!KeEnvironment.pem_init) {
342: ScXmlScript l_script = new ScXmlScript(xFile);
343: l_script.pcmf_execProc(xScript);
344: KeEnvironment.pem_init = true;
345: } else
346: KeLog.pcmf_log("N.A:",
347: "cannot initialize environment twice",
348: null, KeLog.ERROR);
349: }
350: ;
351: } catch (Exception e) {
352: KeLog.pcmf_logException("ug2t", null, e);
353: }
354: ;
355:
356: return;
357: };
358:
359: /**
360: * <p>
361: * Gets a parameter from a xml configuration file.
362: * </p>
363: * <p>
364: *
365: * </p>
366: * <p>
367: *
368: * @param xSource
369: * filename
370: * @param xCluster
371: * parameter block
372: * @param xKey
373: * parameter key
374: * @return parameter value or object
375: * </p>
376: */
377: public static final Object pcmf_getParameter(String xSource,
378: String xCluster, String xKey) throws Exception {
379: if (KeEnvironment.pem_dg == null)
380: KeEnvironment.pem_dg = new CoSynchronizedXmlParameterGetter(
381: new CoXmlParameterGetter());
382:
383: pem_dg.pcmf_setSource(xSource);
384:
385: return (pem_dg.pcmf_getParameter(xSource, xCluster, xKey));
386: };
387:
388: /**
389: * <p>
390: * Gets a parameter from a xml configuration file which is referenced by the given URI
391: * </p>
392: * <p>
393: *
394: * </p>
395: * <p>
396: *
397: * @param xSource
398: * filename
399: * @param xCluster
400: * parameter block
401: * @param xKey
402: * parameter key
403: * @return parameter value or object
404: * </p>
405: */
406: public static final Object pcmf_getParameterFromURI(String xSource,
407: String xCluster, String xKey) throws Exception {
408: if (KeEnvironment.pem_dg == null)
409: KeEnvironment.pem_dg = new CoSynchronizedXmlParameterGetter(
410: new CoXmlParameterGetter());
411:
412: pem_dg.pcmf_setSourceURI(xSource);
413:
414: return (pem_dg.pcmf_getParameter(xSource, xCluster, xKey));
415: };
416:
417: /**
418: * <p>
419: * Gets a parameter from a xml configuration file.
420: * </p>
421: * <p>
422: *
423: * </p>
424: * <p>
425: *
426: * @param xSource
427: * filename
428: * @param xCluster
429: * parameter block
430: * @param xKey
431: * parameter key
432: * @param xNull
433: * default return value if null
434: * @return parameter value or object
435: * </p>
436: */
437: public static final Object pcmf_getParameter(String xSource,
438: String xCluster, String xKey, Object xNull) {
439: try {
440: if (KeEnvironment.pem_dg == null)
441: KeEnvironment.pem_dg = new CoSynchronizedXmlParameterGetter(
442: new CoXmlParameterGetter());
443:
444: pem_dg.pcmf_setSource(xSource);
445:
446: Object l_value = pem_dg.pcmf_getParameter(xSource,
447: xCluster, xKey);
448: if (l_value == null)
449: return (xNull);
450: else
451: return (l_value);
452: } catch (Exception e) {
453: return (xNull);
454: }
455: };
456:
457: /**
458: * <p>
459: * Sets or adds a parameter in a loaded configuration file. This function does
460: * not save chaged values.
461: * </p>
462: * <p>
463: *
464: * </p>
465: * <p>
466: *
467: * @param xSource
468: * filename
469: * @param xCluster
470: * parameter block
471: * @param xKey
472: * parameter key
473: * @param xValue
474: * parameter value or object
475: * @return old parameter value or object or null
476: * </p>
477: */
478: public static final Object pcmf_setParameter(String xSource,
479: String xCluster, String xKey, Object xValue)
480: throws Exception {
481: if (KeEnvironment.pem_dg == null)
482: KeEnvironment.pem_dg = new CoSynchronizedXmlParameterGetter(
483: new CoXmlParameterGetter());
484:
485: pem_dg.pcmf_setSource(xSource);
486:
487: return (pem_dg.pcmf_setParameter(xSource, xCluster, xKey,
488: xValue));
489: };
490:
491: /**
492: * <p>
493: * Adds a new parameter block to a configuration. This function does not save
494: * chaged values.
495: * </p>
496: * <p>
497: *
498: * </p>
499: * <p>
500: *
501: * @param xSource
502: * filename
503: * @param xCluster
504: * parameter block
505: * @return old cluster or null
506: * </p>
507: */
508: public static final Object pcmf_addParameterCluster(String xSource,
509: String xCluster) throws Exception {
510: if (KeEnvironment.pem_dg == null)
511: KeEnvironment.pem_dg = new CoSynchronizedXmlParameterGetter(
512: new CoXmlParameterGetter());
513:
514: pem_dg.pcmf_setSource(xSource);
515:
516: return (pem_dg.pcmf_addCluster(xSource, xCluster));
517: };
518:
519: /**
520: * <p>
521: * Gets an iterator over all key values of a parameter block
522: * </p>
523: * <p>
524: *
525: * </p>
526: * <p>
527: *
528: * @param xSource
529: * filename
530: * @param xCluster
531: * parameter block
532: * @return Iterator
533: * </p>
534: */
535: public static final Iterator pcmf_getParameterKeys(String xSource,
536: String xCluster) throws Exception {
537: if (KeEnvironment.pem_dg == null)
538: KeEnvironment.pem_dg = new CoSynchronizedXmlParameterGetter(
539: new CoXmlParameterGetter());
540:
541: pem_dg.pcmf_setSource(xSource);
542:
543: return (pem_dg.pcmf_getKeys(xSource, xCluster));
544: };
545:
546: /**
547: * For internal use only
548: */
549: public static final ICoXmlParameterGetter pcmf_getParameterGetter() {
550: if (KeEnvironment.pem_dg == null)
551: KeEnvironment.pem_dg = new CoSynchronizedXmlParameterGetter(
552: new CoXmlParameterGetter());
553:
554: return (pem_dg);
555: };
556:
557: /**
558: * Clears all parameter caches
559: */
560: public static final void pcmf_clearParameterGetter() {
561: KeEnvironment.pem_dg = null;
562: };
563:
564: // @@
565:
566: }
|