001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Christophe Ney
022: * --------------------------------------------------------------------------
023: * $Id: EnvEntryDesc.java 7351 2005-09-07 17:16:59Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.deployment.api;
026:
027: import org.objectweb.jonas_lib.deployment.xml.EnvEntry;
028:
029: /**
030: * This class represents the description of an EnvEntry object
031: * @author Christophe Ney
032: * @author Florent Benoit
033: */
034: public class EnvEntryDesc {
035:
036: /**
037: * The env entry name.
038: */
039: private String name;
040:
041: /**
042: * The env entry type.
043: */
044: private Class type;
045:
046: /**
047: * The env entry value.
048: */
049: private Object value;
050:
051: /**
052: * Construct a descriptor for an env-entry tag.
053: * @param env the env-entry resulting of the xml parsing.
054: * @throws DeploymentDescException when missing information for
055: * creating the EnvEntryDesc.
056: */
057: public EnvEntryDesc(EnvEntry env) throws DeploymentDescException {
058: name = env.getEnvEntryName();
059: String t = env.getEnvEntryType();
060: String v = null;
061: if (env.getEnvEntryValue() != null) {
062: v = env.getEnvEntryValue();
063: }
064: try {
065: if (t.equals(Boolean.class.getName())) {
066: type = Boolean.class;
067: if (v != null) {
068: if (v.equalsIgnoreCase("true")) {
069: value = Boolean.TRUE;
070: } else if (v.equalsIgnoreCase("false")) {
071: value = Boolean.FALSE;
072: } else {
073: throw new DeploymentDescException(
074: v
075: + " is not a valid value for env-entry "
076: + name);
077: }
078: } else {
079: value = Boolean.FALSE;
080: }
081:
082: } else if (t.equals(String.class.getName())) {
083: type = String.class;
084: if (v != null) {
085: value = v;
086: } else {
087: value = new String();
088: }
089: } else if (t.equals(Integer.class.getName())) {
090: type = Integer.class;
091: if (v != null) {
092: value = new Integer(v);
093: } else {
094: value = new Integer(0);
095: }
096: } else if (t.equals(Character.class.getName())) {
097: type = Character.class;
098: if (v != null) {
099: if (v.length() != 1) {
100: throw new DeploymentDescException(
101: "The value '"
102: + v
103: + "' is not a valid value for env-entry of type java.lang.Character.");
104: }
105: value = new Character(v.charAt(0));
106: } else {
107: value = new Character("".charAt(0));
108: }
109: } else if (t.equals(Double.class.getName())) {
110: type = Double.class;
111: if (v != null) {
112: value = new Double(v);
113: } else {
114: value = new Double(0);
115: }
116: } else if (t.equals(Byte.class.getName())) {
117: type = Byte.class;
118: if (v != null) {
119: value = new Byte(v);
120: } else {
121: value = new Byte("");
122: }
123: } else if (t.equals(Short.class.getName())) {
124: type = Short.class;
125: if (v != null) {
126: value = new Short(v);
127: } else {
128: value = new Short("");
129: }
130: } else if (t.equals(Long.class.getName())) {
131: type = Long.class;
132: if (v != null) {
133: value = new Long(v);
134: } else {
135: value = new Long(0);
136: }
137: } else if (t.equals(Float.class.getName())) {
138: type = Float.class;
139: if (v != null) {
140: value = new Float(v);
141: } else {
142: value = new Float(0);
143: }
144: } else {
145: throw new DeploymentDescException(t
146: + " is not a valid type for env-entry " + name);
147: }
148: } catch (NumberFormatException e) {
149: throw new DeploymentDescException(v
150: + " is not a valid value for env-entry " + name, e);
151: }
152:
153: }
154:
155: /**
156: * Get the name of the environemt entry.
157: * @return Name for environment entry
158: */
159: public String getName() {
160: return name;
161: }
162:
163: /**
164: * Get the fully-qualified Java type of the environemt entry.
165: * Type is needed since value is optional
166: * The possibles values are:
167: * java.lang.Boolean
168: * java.lang.Character
169: * java.lang.String
170: * java.lang.Integer
171: * java.lang.Double
172: * java.lang.Byte
173: * java.lang.Short
174: * java.lang.Long
175: * java.lang.Float
176: * @return Class the fully-qualified Java type of the environemt entry.
177: */
178: public Class getType() {
179: return type;
180: }
181:
182: /**
183: * Assessor for existence of value for the descriptor
184: * @return true if a value is available
185: */
186: public boolean hasValue() {
187: return value != null;
188: }
189:
190: /**
191: * Get the value of the environment entry.
192: * @return value for the environment entry (must be set)
193: */
194: public Object getValue() {
195: // An env-entry value is optional, so no error should be thrown
196: /*
197: * if (value == null) {
198: * throw new Error("Value not set for env-entry " + name);
199: * }
200: */
201: return value;
202: }
203:
204: /**
205: * String representation of the object for test purpose
206: * @return String representation of this object
207: */
208: public String toString() {
209: StringBuffer ret = new StringBuffer();
210: ret.append("\ngetName()=" + getName());
211: ret.append("\ngetType()=" + getType());
212: if (hasValue()) {
213: ret.append("\ngetValue()=" + getValue().toString());
214: }
215: return ret.toString();
216: }
217:
218: }
|