KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > Config


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.TreeSet JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * class that encapsulates property-based JPF configuration. This is mainly an
40  * associative array with various typed accessors, and a structured
41  * initialization process. This implementation has the design constraint that it
42  * does not promote symbolic information to concrete types, which means that
43  * frequently accessed data should be promoted and cached in client classes.
44  * This in turn means we assume the data is not going to change at runtime.
45  * Major motivation for this mechanism is to avoid 'Option' classes that have
46  * concrete type fields, and hence are structural bottlenecks, i.e. every
47  * parameterized user extension (Heuristics, Scheduler etc.) require to update
48  * this single class. Note that Config is also not thread safe with respect to
49  * retrieving exceptions that occurrred during instantiation
50  *
51  * Another important caveat for both implementation and usage of Config is that
52  * it is supposed to be our master configuration mechanism, i.e. it is also used
53  * to configure other core services like logging. This means that Config
54  * initialization should not depend on these services. Initialization has to
55  * return at all times, recording potential problems for later handling. This is
56  * why we have to keep the Config data model and initialization fairly simple
57  * and robust.
58  */

59 public class Config extends Properties JavaDoc {
60
61   static final String JavaDoc TARGET_KEY = "target";
62
63   static final String JavaDoc TARGET_ARGS_KEY = "target_args";
64
65   /**
66    * this class wraps the various exceptions we might encounter esp. during
67    * reflection instantiation
68    */

69   public class Exception extends java.lang.Exception JavaDoc {
70     public Exception(String JavaDoc msg) {
71       super(msg);
72     }
73
74     public Exception(String JavaDoc msg, Throwable JavaDoc cause) {
75       super(msg, cause);
76     }
77
78     public Exception(String JavaDoc key, Class JavaDoc cls, String JavaDoc failure) {
79       super("error instantiating class " + cls.getName() + " for entry \""
80           + key + "\":" + failure);
81     }
82
83     public Exception(String JavaDoc key, Class JavaDoc cls, String JavaDoc failure, Throwable JavaDoc cause) {
84       this(key, cls, failure);
85       initCause(cause);
86     }
87
88     public String JavaDoc toString() {
89       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
90       sb.append("JPF configuration error: ");
91       sb.append(getMessage());
92
93       return sb.toString();
94     }
95   }
96
97   String JavaDoc fileName;
98
99   Object JavaDoc source;
100
101   boolean gotProperties;
102
103   /**
104    * all arguments that are not <key>= <value>pairs
105    */

106   String JavaDoc[] freeArgs;
107
108   /**
109    * this is our internal defaults ctor
110    */

111   private Config(String JavaDoc alternatePath, Class JavaDoc codeBase) {
112     gotProperties = loadFile("default.properties", alternatePath, codeBase);
113   }
114
115   public Config(String JavaDoc[] args, String JavaDoc fileName, String JavaDoc alternatePath,
116                 Class JavaDoc codeBase) {
117     super(new Config(alternatePath,
118         (codeBase == null) ? codeBase = getCallerClass(1) : codeBase));
119
120     this.fileName = fileName;
121     gotProperties = loadFile(fileName, alternatePath, codeBase);
122
123     if (args != null){
124       processArgs(args);
125     }
126     normalizeValues();
127   }
128
129   public boolean gotDefaultProperties() {
130     if (defaults != null && defaults instanceof Config) {
131       return ((Config) defaults).gotProperties();
132     }
133     return false;
134   }
135
136   public boolean gotProperties() {
137     return gotProperties;
138   }
139
140   public String JavaDoc getFileName() {
141     return fileName;
142   }
143
144   public String JavaDoc getSourceName() {
145     if (source == null) {
146       return null;
147     } else if (source instanceof File JavaDoc) {
148       return ((File JavaDoc) source).getAbsolutePath();
149     } else if (source instanceof URL JavaDoc) {
150       return source.toString();
151     } else {
152       return source.toString();
153     }
154   }
155
156   public String JavaDoc getDefaultsSourceName() {
157     if ((defaults != null) && (defaults instanceof Config)) {
158       return ((Config) defaults).getSourceName();
159     } else {
160       return null;
161     }
162   }
163
164   public Object JavaDoc getSource() {
165     return source;
166   }
167
168   public String JavaDoc[] getArgs() {
169     return freeArgs;
170   }
171
172   public void throwException(String JavaDoc msg) throws Exception JavaDoc {
173     throw new Exception JavaDoc(msg);
174   }
175
176   /**
177    * find callers class
178    *
179    * @param up -
180    * levels upwards from our caller (NOT counting ourselves)
181    * @return caller class, null if illegal 'up' value
182    */

183   public static Class JavaDoc getCallerClass(int up) {
184     int idx = up + 1; // don't count ourselves
185

186     StackTraceElement JavaDoc[] st = (new Throwable JavaDoc()).getStackTrace();
187     if ((up < 0) || (idx >= st.length)) {
188       return null;
189     } else {
190       try {
191         return Class.forName(st[idx].getClassName());
192       } catch (Throwable JavaDoc t) {
193         return null;
194       }
195     }
196   }
197
198   boolean loadFile(String JavaDoc fileName, String JavaDoc alternatePath, Class JavaDoc codeBase) {
199     InputStream JavaDoc is = null;
200
201     try {
202       // first, try to load from a file
203
File JavaDoc f = new File JavaDoc(fileName);
204       if (!f.exists()) {
205         // Ok, try alternatePath, if fileName wasn't absolute
206
if (!f.isAbsolute() && (alternatePath != null)) {
207           f = new File JavaDoc(alternatePath, fileName);
208         }
209       }
210
211       if (f.exists()) {
212         source = f;
213         is = new FileInputStream JavaDoc(f);
214       } else {
215         // if there is no file, try to load as a resource (jar)
216
Class JavaDoc clazz = (codeBase != null) ? codeBase : Config.class;
217         is = clazz.getResourceAsStream(fileName);
218         if (is != null) {
219           source = clazz.getResource(fileName); // a URL
220
}
221       }
222
223       if (is != null) {
224         load(is);
225         return true;
226       }
227     } catch (IOException JavaDoc iex) {
228       return false;
229     }
230
231     return false;
232   }
233
234   /**
235    * extract all "+ <key>= <val>" parameters, store/overwrite them in our
236    * dictionary, collect all other parameters in a String array
237    *
238    * @param args -
239    * array of String parameters to process
240    */

241   void processArgs(String JavaDoc[] args) {
242     int i;
243     String JavaDoc arg;
244     ArrayList JavaDoc list = new ArrayList JavaDoc();
245
246     for (i = 0; i < args.length; i++) {
247       String JavaDoc a = args[i];
248       if (a != null) {
249         if (a.charAt(0) == '+') {
250           int idx = a.indexOf("=");
251           if (idx > 0) {
252             setProperty(a.substring(1, idx), a.substring(idx + 1));
253           } else {
254             setProperty(a.substring(1), "");
255           }
256         } else {
257           list.add(a);
258         }
259       }
260     }
261
262     int n = list.size();
263     freeArgs = new String JavaDoc[n];
264     for (i = 0; i < n; i++) {
265       freeArgs[i] = (String JavaDoc) list.get(i);
266     }
267   }
268
269   /**
270    * return the index of the first free argument that does not start with an
271    * hyphen
272    */

273   public int getNonOptionArgIndex() {
274     if ((freeArgs == null) || (freeArgs.length == 0))
275       return -1;
276
277     for (int i = 0; i < freeArgs.length; i++) {
278       String JavaDoc a = freeArgs[i];
279       if (a != null) {
280         char c = a.charAt(0);
281         if (c != '-') {
282           return i;
283         }
284       }
285     }
286
287     return -1;
288   }
289
290   /**
291    * return the first non-option freeArg, or 'null' if there is none (usually
292    * denotes the application to start)
293    */

294   public String JavaDoc getTargetArg() {
295     int i = getNonOptionArgIndex();
296     if (i < 0) {
297       return getString(TARGET_KEY);
298     } else {
299       return freeArgs[i];
300     }
301   }
302
303   /**
304    * return all args that follow the first non-option freeArgs (usually denotes
305    * the parametsr to pass to the application to start)
306    */

307   public String JavaDoc[] getTargetArgParameters() {
308     int i = getNonOptionArgIndex();
309     if (i >= freeArgs.length - 1) {
310       String JavaDoc[] a = getStringArray(TARGET_ARGS_KEY);
311       if (a != null) {
312         return a;
313       } else {
314         return new String JavaDoc[0];
315       }
316     } else {
317       int n = freeArgs.length - (i + 1);
318       String JavaDoc[] a = new String JavaDoc[n];
319       System.arraycopy(freeArgs, i + 1, a, 0, n);
320       return a;
321     }
322   }
323
324   public String JavaDoc getArg(int i) {
325     if (freeArgs == null)
326       return null;
327     if (freeArgs.length - 1 < i)
328       return null;
329     if (i < 0)
330       return null;
331
332     return freeArgs[i];
333   }
334
335   /**
336    * turn standard type values (boolean etc.) into common formats
337    * ("true"/"false" for booleans)
338    */

339   void normalizeValues() {
340     for (Enumeration JavaDoc keys = keys(); keys.hasMoreElements();) {
341       String JavaDoc k = (String JavaDoc) keys.nextElement();
342       String JavaDoc v = getProperty(k);
343       
344       // trim heading and trailing blanks (at least Java 1.4.2 does not take care of trailing blanks)
345
String JavaDoc v0 = v;
346       v = v.trim();
347       if (v != v0) {
348         put(k, v);
349       }
350       
351       if ("true".equalsIgnoreCase(v) || "t".equalsIgnoreCase(v)
352           || "yes".equalsIgnoreCase(v) || "y".equalsIgnoreCase(v)) {
353         put(k, "true");
354       } else if ("false".equalsIgnoreCase(v) || "f".equalsIgnoreCase(v)
355           || "no".equalsIgnoreCase(v) || "n".equalsIgnoreCase(v)) {
356         put(k, "false");
357       }
358     }
359   }
360
361   public boolean getBoolean(String JavaDoc key) {
362     String JavaDoc v = getProperty(key);
363     return (v != null) && ("true".equals(v));
364   }
365
366   public boolean getBoolean(String JavaDoc key, boolean def) {
367     String JavaDoc v = getProperty(key);
368     if (v != null) {
369       return ("true".equals(v));
370     } else {
371       return def;
372     }
373   }
374
375   public int[] getIntArray (String JavaDoc key) throws Exception JavaDoc {
376     String JavaDoc v = getProperty(key);
377     
378     if (v != null) {
379       String JavaDoc[] sa = v.split("[:;, ]+");
380       int[] a = new int[sa.length];
381       int i = 0;
382       try {
383         for (; i<sa.length; i++) {
384           a[i] = Integer.parseInt(sa[i]);
385         }
386         return a;
387       } catch (NumberFormatException JavaDoc nfx) {
388         throw new Exception JavaDoc("illegal int[] element in '" + key + "' = \"" + sa[i] + '"');
389       }
390     } else {
391       return null;
392     }
393   }
394
395   public int getInt(String JavaDoc key) {
396     return getInt(key, 0);
397   }
398
399   public int getInt(String JavaDoc key, int defValue) {
400     String JavaDoc v = getProperty(key);
401     if (v != null) {
402       try {
403         return Integer.parseInt(v);
404       } catch (NumberFormatException JavaDoc nfx) {
405         return defValue;
406       }
407     }
408
409     return defValue;
410   }
411
412   public long getLong(String JavaDoc key) {
413     return getLong(key, 0L);
414   }
415
416   public long getLong(String JavaDoc key, long defValue) {
417     String JavaDoc v = getProperty(key);
418     if (v != null) {
419       try {
420         return Long.parseLong(v);
421       } catch (NumberFormatException JavaDoc nfx) {
422         return defValue;
423       }
424     }
425
426     return defValue;
427   }
428
429   public long[] getLongArray (String JavaDoc key) throws Exception JavaDoc {
430     String JavaDoc v = getProperty(key);
431     
432     if (v != null) {
433       String JavaDoc[] sa = v.split("[:;, ]+");
434       long[] a = new long[sa.length];
435       int i = 0;
436       try {
437         for (; i<sa.length; i++) {
438           a[i] = Long.parseLong(sa[i]);
439         }
440         return a;
441       } catch (NumberFormatException JavaDoc nfx) {
442         throw new Exception JavaDoc("illegal long[] element in " + key + " = " + sa[i]);
443       }
444     } else {
445       return null;
446     }
447   }
448
449   
450   public double getDouble (String JavaDoc key) {
451     return getDouble(key, 0.0);
452   }
453   
454   public double getDouble (String JavaDoc key, double defValue) {
455     String JavaDoc v = getProperty(key);
456     if (v != null) {
457       try {
458         return Double.parseDouble(v);
459       } catch (NumberFormatException JavaDoc nfx) {
460         return defValue;
461       }
462     }
463
464     return defValue;
465   }
466
467   public double[] getDoubleArray (String JavaDoc key) throws Exception JavaDoc {
468     String JavaDoc v = getProperty(key);
469     
470     if (v != null) {
471       String JavaDoc[] sa = v.split("[:;, ]+");
472       double[] a = new double[sa.length];
473       int i = 0;
474       try {
475         for (; i<sa.length; i++) {
476           a[i] = Double.parseDouble(sa[i]);
477         }
478         return a;
479       } catch (NumberFormatException JavaDoc nfx) {
480         throw new Exception JavaDoc("illegal double[] element in " + key + " = " + sa[i]);
481       }
482     } else {
483       return null;
484     }
485   }
486
487   
488   public String JavaDoc getString(String JavaDoc key) {
489     return getProperty(key);
490   }
491
492   public String JavaDoc getString(String JavaDoc key, String JavaDoc defValue) {
493     String JavaDoc s = getProperty(key);
494     if (s != null) {
495       return s;
496     } else {
497       return defValue;
498     }
499   }
500
501   /**
502    * same as getString(), except of that we look for '${ <key>}' patterns, and
503    * replace them with values if we find corresponding keys. Expansion is not
504    * done recursively (but could be)
505    */

506   public String JavaDoc getExpandedString(String JavaDoc key) {
507     int i, j = 0;
508     String JavaDoc s = getString(key);
509     if (s == null || s.length() == 0) {
510       return s;
511     }
512
513     while ((i = s.indexOf("${", j)) >= 0) {
514       if ((j = s.indexOf('}', i)) > 0) {
515         String JavaDoc k = s.substring(i + 2, j);
516         String JavaDoc v = getString(k, "");
517         if (v != null) {
518           s = s.substring(0, i) + v + s.substring(j + 1, s.length());
519           j = i + v.length();
520         } else {
521           s = s.substring(0, i) + s.substring(j + 1, s.length());
522           j = i;
523         }
524       }
525     }
526
527     return s;
528   }
529
530   /**
531    * return memory size in bytes, or 'defValue' if not in dictionary. Encoding
532    * can have a 'M' or 'k' postfix, values have to be positive integers (decimal
533    * notation)
534    */

535   public long getMemorySize(String JavaDoc key, long defValue) {
536     String JavaDoc v = getProperty(key);
537     long sz = defValue;
538
539     if (v != null) {
540       int n = v.length() - 1;
541       try {
542         char c = v.charAt(n);
543
544         if ((c == 'M') || (c == 'm')) {
545           sz = Long.parseLong(v.substring(0, n)) << 20;
546         } else if ((c == 'K') || (c == 'k')) {
547           sz = Long.parseLong(v.substring(0, n)) << 10;
548         } else {
549           sz = Long.parseLong(v);
550         }
551
552       } catch (NumberFormatException JavaDoc nfx) {
553         return defValue;
554       }
555     }
556
557     return sz;
558   }
559
560   public String JavaDoc[] getStringArray(String JavaDoc key) {
561     String JavaDoc v = getProperty(key);
562     if (v != null) {
563       return v.split("[:;, ]+");
564     }
565
566     return null;
567   }
568
569   public Class JavaDoc getClass(String JavaDoc key) throws Exception JavaDoc {
570     String JavaDoc v = getProperty(key);
571     if ((v != null) && (v.length() > 0)) {
572       try {
573         return Class.forName(v);
574       } catch (ClassNotFoundException JavaDoc cfx) {
575         throw new Exception JavaDoc("class not found " + v);
576       } catch (ExceptionInInitializerError JavaDoc ix) {
577         throw new Exception JavaDoc("class initialization of " + v + " failed: " + ix,
578             ix);
579       }
580     }
581
582     return null;
583   }
584
585   public Class JavaDoc getEssentialClass(String JavaDoc key) throws Exception JavaDoc {
586     Class JavaDoc cls = getClass(key);
587     if (cls == null) {
588       throw new Exception JavaDoc("no classname entry for: \"" + key + "\"");
589     }
590
591     return cls;
592   }
593
594   public Class JavaDoc[] getClasses(String JavaDoc key) throws Exception JavaDoc {
595     String JavaDoc[] v = getStringArray(key);
596     if (v != null) {
597       int n = v.length;
598       Class JavaDoc[] a = new Class JavaDoc[n];
599       for (int i = 0; i < n; i++) {
600         try {
601           a[i] = Class.forName(v[i]);
602         } catch (ClassNotFoundException JavaDoc cnfx) {
603           throw new Exception JavaDoc("class not found " + v[i]);
604         } catch (ExceptionInInitializerError JavaDoc ix) {
605           throw new Exception JavaDoc("class initialization of " + v[i] + " failed: "
606               + ix, ix);
607         }
608       }
609
610       return a;
611     }
612
613     return null;
614   }
615
616   public Object JavaDoc[] getInstances(String JavaDoc key, Class JavaDoc type) throws Exception JavaDoc {
617     Class JavaDoc[] c = getClasses(key);
618     if (c != null) {
619       Class JavaDoc[] argTypes = { Config.class };
620       Object JavaDoc[] args = { this };
621       Object JavaDoc[] a = new Object JavaDoc[c.length];
622
623       for (int i = 0; i < c.length; i++) {
624         a[i] = getInstance(key, c[i], type, argTypes, args);
625       }
626
627       return a;
628     }
629
630     return null;
631   }
632
633   public Object JavaDoc getInstance(String JavaDoc key, Class JavaDoc type) throws Exception JavaDoc {
634     Class JavaDoc[] argTypes = { Config.class };
635     Object JavaDoc[] args = { this };
636
637     return getInstance(key, type, argTypes, args);
638   }
639
640   public Object JavaDoc getInstance(String JavaDoc key, Class JavaDoc type, Class JavaDoc[] argTypes,
641                             Object JavaDoc[] args) throws Exception JavaDoc {
642     Class JavaDoc cls = getClass(key);
643     if (cls != null) {
644       return getInstance(key, cls, type, argTypes, args);
645     } else {
646       return null;
647     }
648   }
649
650   public Object JavaDoc getEssentialInstance(String JavaDoc key, Class JavaDoc type) throws Exception JavaDoc {
651     Class JavaDoc[] argTypes = { Config.class };
652     Object JavaDoc[] args = { this };
653     return getEssentialInstance(key, type, argTypes, args);
654   }
655
656   public Object JavaDoc getEssentialInstance(String JavaDoc key, Class JavaDoc type, Class JavaDoc[] argTypes,
657                                      Object JavaDoc[] args) throws Exception JavaDoc {
658     Class JavaDoc cls = getEssentialClass(key);
659     return getInstance(key, cls, type, argTypes, args);
660   }
661
662   /**
663    * this is our private instantiation workhorse try to instantiate an object of
664    * class 'cls' by using the following ordered set of ctors 1. <cls>(
665    * <argTypes>) 2. <cls>(Config) 3. <cls>() if all of that fails, or there was
666    * a 'type' provided the instantiated object does not comply with, return null
667    */

668   Object JavaDoc getInstance(String JavaDoc key, Class JavaDoc cls, Class JavaDoc type, Class JavaDoc[] argTypes,
669                      Object JavaDoc[] args) throws Exception JavaDoc {
670     Object JavaDoc o = null;
671     Constructor JavaDoc ctor = null;
672
673     if (cls == null) {
674       return null;
675     }
676
677     do {
678       try {
679         ctor = cls.getConstructor(argTypes);
680         o = ctor.newInstance(args);
681       } catch (NoSuchMethodException JavaDoc nmx) {
682         if (argTypes.length >= 1) {
683           if (argTypes[0] != Config.class) {
684             // fallback 1: try a single Config param
685
argTypes = new Class JavaDoc[1];
686             argTypes[0] = Config.class;
687             args = new Object JavaDoc[1];
688             args[0] = this;
689           } else {
690             // fallback 2: try the default ctor
691
argTypes = new Class JavaDoc[0];
692             args = new Object JavaDoc[0];
693           }
694         } else {
695           // Ok, there is no suitable ctor, bail out
696
throw new Exception JavaDoc(key, cls, "no suitable ctor found");
697         }
698       } catch (IllegalAccessException JavaDoc iacc) {
699         throw new Exception JavaDoc(key, cls, "\n> ctor not accessible: "
700             + getMethodSignature(ctor));
701       } catch (IllegalArgumentException JavaDoc iarg) {
702         throw new Exception JavaDoc(key, cls, "\n> illegal constructor arguments: "
703             + getMethodSignature(ctor));
704       } catch (InvocationTargetException JavaDoc ix) {
705         Throwable JavaDoc tx = ix.getTargetException();
706         if (tx instanceof Config.Exception) {
707           throw new Exception JavaDoc(tx.getMessage() + "\n> used within \"" + key
708               + "\" instantiation of " + cls);
709         } else {
710           throw new Exception JavaDoc(key, cls, "\n> exception in "
711               + getMethodSignature(ctor) + ":\n>> " + tx, tx);
712         }
713       } catch (InstantiationException JavaDoc ivt) {
714         throw new Exception JavaDoc(key, cls,
715             "\n> abstract class cannot be instantiated");
716       } catch (ExceptionInInitializerError JavaDoc eie) {
717         throw new Exception JavaDoc(key, cls, "\n> static initialization failed:\n>> "
718             + eie.getException(), eie.getException());
719       }
720     } while (o == null);
721
722     // check type
723
if ((type != null) && !type.isInstance(o)) {
724       throw new Exception JavaDoc(key, cls, "\n> instance not of type: "
725           + type.getName());
726     }
727
728     return o;
729   }
730
731   String JavaDoc getMethodSignature(Constructor JavaDoc ctor) {
732     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
733     sb.append(ctor.getName());
734     sb.append('(');
735     Class JavaDoc[] argTypes = ctor.getParameterTypes();
736     for (int i = 0; i < argTypes.length; i++) {
737       if (i > 0) {
738         sb.append(',');
739       }
740       sb.append(argTypes[i].getName());
741     }
742     sb.append(')');
743     return sb.toString();
744   }
745
746   /**
747    * check if any of the freeArgs matches a regular expression
748    *
749    * @param regex -
750    * regular expression to check for
751    * @return true if found, false if not found or no freeArgs
752    */

753   public boolean hasArg(String JavaDoc regex) {
754     if (freeArgs == null) {
755       return false;
756     }
757
758     for (int i = 0; i < freeArgs.length; i++) {
759       if (freeArgs[i].matches(regex)) {
760         return true;
761       }
762     }
763
764     return false;
765   }
766
767   public boolean hasValue(String JavaDoc key) {
768     String JavaDoc v = getProperty(key);
769     return ((v != null) && (v.length() > 0));
770   }
771
772   public boolean hasValueIgnoreCase(String JavaDoc key, String JavaDoc value) {
773     String JavaDoc v = getProperty(key);
774     if (v != null) {
775       return v.equalsIgnoreCase(value);
776     }
777
778     return false;
779   }
780
781   public int getChoiceIndexIgnoreCase(String JavaDoc key, String JavaDoc[] choices) {
782     String JavaDoc v = getProperty(key);
783
784     if ((v != null) && (choices != null)) {
785       for (int i = 0; i < choices.length; i++) {
786         if (v.equalsIgnoreCase(choices[i])) {
787           return i;
788         }
789       }
790     }
791
792     return -1;
793   }
794
795   public void print (PrintWriter JavaDoc pw) {
796     pw.println("----------- dictionary contents");
797     
798     // just how much do you have to do to get a sorted printout :(
799
TreeSet JavaDoc kset = new TreeSet JavaDoc();
800     for (Enumeration JavaDoc e = propertyNames(); e.hasMoreElements();) {
801       kset.add(e.nextElement());
802     }
803     for (Iterator JavaDoc it = kset.iterator(); it.hasNext();) {
804       String JavaDoc key = (String JavaDoc) it.next();
805       String JavaDoc val = getExpandedString(key);
806       pw.print(key);
807       pw.print(" = ");
808       pw.println(val);
809     }
810     
811     if ((freeArgs != null) && (freeArgs.length > 0)) {
812       pw.println("----------- free arguments");
813       for (int i = 0; i < freeArgs.length; i++) {
814         pw.println(freeArgs[i]);
815       }
816     }
817     
818     pw.flush();
819   }
820
821   public void printStatus(Logger JavaDoc log) {
822     log.config("configuration initialized from: " + getSourceName());
823
824     Config def = (Config) defaults;
825     if (def.source == null) {
826       log.warning("no defaults.properties found");
827     } else {
828       log.config("default configuration initialized from: "
829           + def.getSourceName());
830     }
831   }
832 }
833
Popular Tags