001: /**
002: * $Id: SimpleWebServiceParameter.java,v 1.7 2003/12/01 10:47:36 vv138407 Exp $
003: * Copyright 2002-2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.providers.simplewebservice;
014:
015: import java.lang.reflect.Constructor;
016:
017: import com.sun.portal.providers.simplewebservice.util.XList;
018: import com.sun.portal.providers.simplewebservice.util.SimpleWebServiceTypeConstants;
019:
020: /**
021: * The SimpleWebServiceParameter class holds the meta-data as well as the
022: * data itself for a single input/output parameter for a web service method
023: * as read from the defaults or the servlet request.
024: * <P>
025: *
026: * The input/output parameters for a web service can be primitive or
027: * complex (embedded Javabean).
028: * <P>
029: *
030: * In case of a complex type, the 'class' member variable used is "XList.class"
031: * and the value is an instance of XList. An instance of XList holds a list
032: * of SimpleWebServiceParameters.
033: * <P>
034: *
035: * The default SimpleWebServiceParameter Constructor takes either an
036: * XList instance or the String representation of the primitive type.
037: * <P>
038: *
039: * In the case of primitive types, no validation is performed at the time of
040: * construction to check if the String value provided is legal or not.
041: * This String value can be retrieved using the method getFormValue() and
042: * can be used to display to the user the values entered in the form or
043: * in the defaults.
044: * <P>
045: *
046: * The String value that has been converted to the correct primitive
047: * object can be obtained using the getValue() method. This method
048: * throws an IllegalParameterException if the conversion of the String
049: * to the correct format fails.
050: *
051: */
052: public class SimpleWebServiceParameter {
053:
054: private String name = null;
055: private String fullName = null;
056: private Class type = null;
057: private Object value = null;
058:
059: private boolean arrayType = false;
060: private String arrayTypeName = null;
061: private boolean isRepeatable = false;
062:
063: /**
064: * Constructor that uses a ParameterDescriptor as the meta-data information
065: * and the instance of XList object for the member variable value.
066: *
067: * @param desc the ParameterDescriptor
068: * @param value value of the SimpleWebServiceParameter
069: */
070: public SimpleWebServiceParameter(ParameterDescriptor desc,
071: XList value) {
072: this .name = desc.getName();
073: this .fullName = desc.getFullName();
074: this .type = desc.getType();
075: this .arrayType = desc.isArrayType();
076: this .arrayTypeName = desc.getArrayTypeName();
077: this .value = value;
078: this .isRepeatable = desc.isRepeatable();
079: }
080:
081: /**
082: * Constructor that uses a ParameterDescriptor as the meta-data information
083: * and a String as the value read from defaults or from the input form.
084: *
085: * @param desc the ParameterDescriptor
086: * @param strValue string value of the SimpleWebServiceParameter
087: */
088: public SimpleWebServiceParameter(ParameterDescriptor desc,
089: String strValue) {
090: this .name = desc.getName();
091: this .fullName = desc.getFullName();
092: this .type = desc.getType();
093: this .value = strValue;
094: this .isRepeatable = desc.isRepeatable();
095: }
096:
097: /**
098: * Gets the name of the parameter.
099: * <P>
100: *
101: * @return the name of the parameter
102: */
103: public String getName() {
104: return name;
105: }
106:
107: /**
108: * Gets the type of the parameter.
109: * <P>
110: *
111: * @return the type of the parameter
112: */
113: public Class getType() {
114: return type;
115: }
116:
117: public boolean isRepeatable() {
118: return isRepeatable;
119: }
120:
121: /**
122: * Gets the value of the paramerter.
123: * <P>
124: *
125: * For primitive types, this method converts the String value to the
126: * correct Class type and returns it. For a complex type, it returns
127: * the method returns an instance of XList.
128: * <P>
129: *
130: * @return the value of the parameter
131: * @throws IllegalArgumentException if there was a
132: * problem getting the Value from the parameter.
133: */
134:
135: public Object getValue() throws IllegalArgumentException {
136:
137: if ((value != null) && value.getClass().equals(XList.class)) {
138: return value;
139: } else {
140: return convertStringToObject((String) value);
141: }
142:
143: }
144:
145: /**
146: * Returns the string representation of the value as entered
147: * through the constructor. Returns an empty string if the value is null.
148: * <P>
149: *
150: * @return the String representation of the value.
151: */
152: public String getStrValue() {
153: if (value == null) {
154: return "";
155: } else {
156: return value.toString();
157: }
158: }
159:
160: /**
161: * String that provides hints on what kind of value is expected in the input
162: * form
163: * <P>
164: *
165: * @return the type hint.
166: */
167: public String getTypeHint() {
168: String classTypeStr = getType().getName();
169: return classTypeStr
170: .substring(classTypeStr.lastIndexOf(".") + 1);
171: }
172:
173: /**
174: * Returns the full name of instance with parent hierarchy prepended with
175: * underscore.
176: * <P>
177: *
178: * If the instance is one of the top level paraemeters, it returns the
179: * name of the instance. If the instance is a a member of complex type
180: * (embedded Javabean), the method returns the name prepended with the
181: * parent's name and an underscore.
182: * <P>
183: *
184: * @return the full name of the parameter.
185: */
186: public String getFullName() {
187: return fullName;
188: }
189:
190: /*
191: * This method takes a string and converts it in to an object using the type information
192: * in SimpleWebServiceParameter.
193: *
194: * @param valueString
195: * @exception com.sun.portal.providers.simplewebservice.IllegalArgumentException
196: */
197: private Object convertStringToObject(String valueString)
198: throws IllegalArgumentException {
199:
200: Object value = null;
201:
202: if (valueString != null) {
203:
204: //----------------------------------------------
205: // Look only for the constructor that takes a string
206:
207: try {
208: Class[] signature = new Class[] { String.class };
209: Constructor cons = getType().getConstructor(signature);
210: if (cons != null) {
211: Object[] constructorValues = new Object[] { valueString };
212: value = cons.newInstance(constructorValues);
213: }
214: } catch (Exception ex) {
215: String errMessage = "Invalid value for : " + getName();
216: throw new IllegalArgumentException(errMessage);
217: }
218: }
219: return value;
220: }
221:
222: /**
223: * Determines if the parameter is representing an array.
224: *
225: * @return true if the parameter represents an array, else false.
226: */
227: public boolean isArrayType() {
228: return this .arrayType;
229: }
230:
231: /**
232: * Sets the parameter to represent an array.
233: *
234: * @see SimpleWebServiceParameter#isArrayType
235: */
236: public String getArrayTypeName() {
237: return this .arrayTypeName;
238: }
239:
240: /**
241: * Determines if the parameter is representing a simple type.
242: *
243: * @return true if the parameter represents a simple type, else false.
244: */
245: public boolean isSimpleType() {
246: return isSimpleType(this .type);
247: }
248:
249: /**
250: * Determines if the parameter is representing a simple type array.
251: *
252: * @return true if the parameter represents a simple type array, else false.
253: */
254: public boolean isSimpleTypeArray() {
255: return isSimpleType(this .arrayTypeName);
256: }
257:
258: /**
259: * Returns the string representation of the parameter.
260: * <P>
261: *
262: * @return the string representation of the parameter.
263: */
264: public String toString() {
265: return "[name=" + name + "] " + "[fullName=" + fullName + "] "
266: + "[type=" + type + "] " + "[value=" + value + "] ";
267: }
268:
269: private boolean isSimpleType(Class elementType) {
270: if (elementType == null)
271: return false;
272:
273: return (elementType.equals(java.lang.String.class)
274: || elementType.equals(Integer.class)
275: || elementType.equals(Float.class)
276: || elementType.equals(Double.class)
277: || elementType.equals(Boolean.class)
278: || elementType.equals(Long.class)
279: || elementType.equals(Byte.class) || elementType
280: .equals(Short.class));
281: }
282:
283: private boolean isSimpleType(String elementType) {
284: if (elementType == null)
285: return false;
286:
287: return (elementType
288: .endsWith(SimpleWebServiceTypeConstants.STRING)
289: || elementType
290: .endsWith(SimpleWebServiceTypeConstants.INT)
291: || elementType
292: .endsWith(SimpleWebServiceTypeConstants.FLOAT)
293: || elementType
294: .endsWith(SimpleWebServiceTypeConstants.DOUBLE)
295: || elementType
296: .endsWith(SimpleWebServiceTypeConstants.BOOLEAN)
297: || elementType
298: .endsWith(SimpleWebServiceTypeConstants.LONG)
299: || elementType
300: .endsWith(SimpleWebServiceTypeConstants.BYTE) || elementType
301: .endsWith(SimpleWebServiceTypeConstants.SHORT));
302: }
303:
304: }
|