1 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 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 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 } 136 | Popular Tags |