KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > deployment > api > EnvEntryDesc


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Christophe Ney
22  * --------------------------------------------------------------------------
23  * $Id: EnvEntryDesc.java,v 1.3 2004/05/26 07:55:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.deployment.api;
28
29 import org.objectweb.jonas_lib.deployment.xml.EnvEntry;
30
31 /**
32  * This class represents the description of an EnvEntry object
33  * @author Christophe Ney
34  * @author Florent Benoit
35  */

36 public class EnvEntryDesc {
37
38     /**
39      * The env entry name.
40      */

41     private String JavaDoc name;
42
43     /**
44      * The env entry type.
45      */

46     private Class JavaDoc type;
47
48     /**
49      * The env entry value.
50      */

51     private Object JavaDoc value;
52
53     /**
54      * Construct a descriptor for an env-entry tag.
55      * @param env the env-entry resulting of the xml parsing.
56      * @throws DeploymentDescException when missing information for
57      * creating the EnvEntryDesc.
58      */

59     public EnvEntryDesc(EnvEntry env) throws DeploymentDescException {
60         name = env.getEnvEntryName();
61         String JavaDoc t = env.getEnvEntryType();
62         String JavaDoc v = null;
63         if (env.getEnvEntryValue() != null) {
64             v = env.getEnvEntryValue();
65         }
66         try {
67             if (t.equals(Boolean JavaDoc.class.getName())) {
68                 type = Boolean JavaDoc.class;
69                 if (v != null) {
70                     if (v.equalsIgnoreCase("true")) {
71                         value = Boolean.TRUE;
72                     } else if (v.equalsIgnoreCase("false")) {
73                         value = Boolean.FALSE;
74                     } else {
75                         throw new DeploymentDescException(v + " is not a valid value for env-entry " + name);
76                     }
77                 }
78             } else if (t.equals(String JavaDoc.class.getName())) {
79                 type = String JavaDoc.class;
80                 value = v;
81             } else if (t.equals(Integer JavaDoc.class.getName())) {
82                 type = Integer JavaDoc.class;
83                 if (v != null) {
84                     value = new Integer JavaDoc(v);
85                 }
86             } else if (t.equals(Character JavaDoc.class.getName())) {
87                 type = Character JavaDoc.class;
88                 if (v != null) {
89                     if (v.length() != 1) {
90                         throw new DeploymentDescException("The value '" + v + "' is not a valid value for env-entry of type java.lang.Character.");
91                     }
92                     value = new Character JavaDoc(v.charAt(0));
93                 }
94             } else if (t.equals(Double JavaDoc.class.getName())) {
95                 type = Double JavaDoc.class;
96                 if (v != null) {
97                     value = new Double JavaDoc(v);
98                 }
99             } else if (t.equals(Byte JavaDoc.class.getName())) {
100                 type = Byte JavaDoc.class;
101                 if (v != null) {
102                     value = new Byte JavaDoc(v);
103                 }
104             } else if (t.equals(Short JavaDoc.class.getName())) {
105                 type = Short JavaDoc.class;
106                 if (v != null) {
107                     value = new Short JavaDoc(v);
108                 }
109             } else if (t.equals(Long JavaDoc.class.getName())) {
110                 type = Long JavaDoc.class;
111                 if (v != null) {
112                     value = new Long JavaDoc(v);
113                 }
114             } else if (t.equals(Float JavaDoc.class.getName())) {
115                 type = Float JavaDoc.class;
116                 if (v != null) {
117                     value = new Float JavaDoc(v);
118                 }
119             } else {
120                 throw new DeploymentDescException(t + " is not a valid type for env-entry " + name);
121             }
122         } catch (NumberFormatException JavaDoc e) {
123             throw new DeploymentDescException(v + " is not a valid value for env-entry " + name, e);
124         }
125
126     }
127
128     /**
129      * Get the name of the environemt entry.
130      * @return Name for environment entry
131      */

132     public String JavaDoc getName() {
133             return name;
134     }
135
136     /**
137      * Get the fully-qualified Java type of the environemt entry.
138      * Type is needed since value is optional
139      * The possibles values are:
140      * java.lang.Boolean
141      * java.lang.Character
142      * java.lang.String
143      * java.lang.Integer
144      * java.lang.Double
145      * java.lang.Byte
146      * java.lang.Short
147      * java.lang.Long
148      * java.lang.Float
149      * @return Class the fully-qualified Java type of the environemt entry.
150      */

151     public Class JavaDoc getType() {
152             return type;
153     }
154
155     /**
156      * Assessor for existence of value for the descriptor
157      * @return true if a value is available
158      */

159     public boolean hasValue() {
160             return value != null;
161     }
162
163     /**
164      * Get the value of the environment entry.
165      * @return value for the environment entry (must be set)
166      */

167     public Object JavaDoc getValue() {
168         if (value == null) {
169             throw new Error JavaDoc("Value not set for env-entry " + name);
170         }
171         return value;
172     }
173
174     /**
175      * String representation of the object for test purpose
176      * @return String representation of this object
177      */

178     public String JavaDoc toString() {
179         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
180         ret.append("\ngetName()=" + getName());
181         ret.append("\ngetType()=" + getType());
182         if (hasValue()) {
183             ret.append("\ngetValue()=" + getValue().toString());
184         }
185         return ret.toString();
186     }
187
188 }
189
Popular Tags