KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > ConfigVariable


1 /*
2  * Copyright (C) 2000 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  */

20 package fr.dyade.aaa.util;
21
22 import java.util.Hashtable JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24
25 public class ConfigVariable{
26
27   static Hashtable JavaDoc primitives = new Hashtable JavaDoc();
28   static{
29     primitives.put("int", Integer JavaDoc.class);
30     primitives.put("long", Long JavaDoc.class);
31     primitives.put("double", Double JavaDoc.class);
32     primitives.put("float", Float JavaDoc.class);
33     primitives.put("short", Short JavaDoc.class);
34     primitives.put("boolean", Boolean JavaDoc.class);
35   }
36
37   private String JavaDoc name;
38   private Class JavaDoc type;
39   private Object JavaDoc defaultValue;
40   private String JavaDoc info;
41
42   ConfigVariable(String JavaDoc name, Class JavaDoc type, Object JavaDoc defaultValue, String JavaDoc info){
43     this.name = name;
44     this. type = type;
45     this.defaultValue = defaultValue;
46     this.info = info;
47   }
48
49   public final String JavaDoc getName(){
50     return name;
51   }
52
53   public final Class JavaDoc getType(){
54     return type;
55   }
56
57   public final Object JavaDoc getDefault(){
58     return defaultValue;
59   }
60
61   public final String JavaDoc getInfo(){
62     return info;
63   }
64
65
66   public static Class JavaDoc getClass(String JavaDoc type) throws Exception JavaDoc{
67     if (type == null || type.equals(""))
68       throw new Exception JavaDoc ("Type not defined");
69     Object JavaDoc o = primitives.get(type);
70     if (o != null)
71       return (Class JavaDoc)o;
72     try{
73       return Class.forName(type);
74     }catch(ClassNotFoundException JavaDoc e){
75         // allow to type Integer instead of java.lang.Integer
76
return Class.forName("java.lang." + type);
77     }
78   }
79
80   public static Object JavaDoc getObject(Class JavaDoc c, String JavaDoc value) throws Exception JavaDoc{
81     if (c.equals(java.lang.String JavaDoc.class))
82       return value;Class JavaDoc cp[] = new Class JavaDoc[1];
83     cp[0] = java.lang.String JavaDoc.class;
84     Method JavaDoc m = c.getMethod("valueOf", cp);
85     Object JavaDoc op[] = new Object JavaDoc[1];
86     op[0] = value;
87     Object JavaDoc res = m.invoke(null, op);
88     return res;
89   }
90
91
92 }
93
Popular Tags