1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/jndi/config/MethodConfigUserObject.java,v 1.4 2004/02/13 02:40:54 sebb Exp $ 2 /* 3 * Copyright 2001-2004 The Apache Software Foundation. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.ejb.jndi.config; 20 21 import java.lang.Character; 22 23 import org.apache.jmeter.ejb.jndi.config.MethodConfigUserObjectException; 24 import org.apache.log4j.Category; 25 /** 26 * Given the class of the parameter and its string value this class will 27 * attempt to create an appropriate object to represent it e.g. if given 28 * a class of int and value 8, a Integer object with the 8 value will be 29 * created. Failing which a MethodConfigUserObjectException will be thrown. 30 * 31 * @author Khor Soon Hin 32 * @version $Revision: 1.4 $ Last Updated: $Date: 2004/02/13 02:40:54 $ 33 * Created 2001 Jan 08 34 */ 35 public class MethodConfigUserObject 36 { 37 private static Category catClass = Category.getInstance( 38 MethodConfigUserObject.class.getName()); 39 40 protected static final String INTEGER = "int"; 41 protected static final String LONG = "long"; 42 protected static final String FLOAT = "float"; 43 protected static final String DOUBLE = "double"; 44 protected static final String BOOLEAN = "boolean"; 45 protected static final String CHAR = "char"; 46 protected static final String BYTE = "byte"; 47 protected static final String SHORT = "short"; 48 protected static final String STRING_CLASS = "java.lang.String"; 49 50 protected Object object = null; 51 protected Class type = null; 52 53 public MethodConfigUserObject(Class type, String value) 54 throws MethodConfigUserObjectException 55 { 56 if(type == null || value == null) 57 { 58 throw new MethodConfigUserObjectException( 59 "Parameters of MethodConfigUserObject constructor cannot be null"); 60 } 61 this.type = type; 62 // ensure that the class type is one of the 8 primitives 63 try 64 { 65 if(type.getName().equals(INTEGER)) 66 { 67 object = new Integer(value); 68 } 69 else if(type.getName().equals(LONG)) 70 { 71 object = new Long(value); 72 } 73 else if(type.getName().equals(FLOAT)) 74 { 75 object = new Float(value); 76 } 77 else if(type.getName().equals(DOUBLE)) 78 { 79 object = new Double(value); 80 } 81 else if(type.getName().equals(BOOLEAN)) 82 { 83 object = Boolean.valueOf(value); 84 } 85 else if(type.getName().equals(CHAR)) 86 { 87 if(value.length() == 1) 88 { 89 object = new Character(value.charAt(0)); 90 } 91 else 92 { 93 throw new MethodConfigUserObjectException( 94 "Value format not compatible with class"); 95 } 96 } 97 else if(type.getName().equals(BYTE)) 98 { 99 object = new Byte(value); 100 } 101 else if(type.getName().equals(SHORT)) 102 { 103 object = new Short(value); 104 } 105 else if(type.getName().equals(STRING_CLASS)) 106 { 107 object = new String(value); 108 } 109 } 110 catch(NumberFormatException e) 111 { 112 throw new MethodConfigUserObjectException( 113 "Value format not compatible with class"); 114 } 115 } 116 117 public Object getObject() 118 { 119 return object; 120 } 121 122 public Class getType() 123 { 124 return type; 125 } 126 127 public String toString() 128 { 129 StringBuffer strbuff = new StringBuffer(); 130 strbuff.append(type.getName()); 131 strbuff.append(" : "); 132 strbuff.append(object); 133 return strbuff.toString(); 134 } 135 }