KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > ejb > jndi > config > MethodConfigUserObject


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 JavaDoc;
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 JavaDoc INTEGER = "int";
41   protected static final String JavaDoc LONG = "long";
42   protected static final String JavaDoc FLOAT = "float";
43   protected static final String JavaDoc DOUBLE = "double";
44   protected static final String JavaDoc BOOLEAN = "boolean";
45   protected static final String JavaDoc CHAR = "char";
46   protected static final String JavaDoc BYTE = "byte";
47   protected static final String JavaDoc SHORT = "short";
48   protected static final String JavaDoc STRING_CLASS = "java.lang.String";
49
50   protected Object JavaDoc object = null;
51   protected Class JavaDoc type = null;
52
53   public MethodConfigUserObject(Class JavaDoc type, String JavaDoc 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 JavaDoc(value);
68       }
69       else if(type.getName().equals(LONG))
70       {
71         object = new Long JavaDoc(value);
72       }
73       else if(type.getName().equals(FLOAT))
74       {
75         object = new Float JavaDoc(value);
76       }
77       else if(type.getName().equals(DOUBLE))
78       {
79         object = new Double JavaDoc(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 JavaDoc(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 JavaDoc(value);
100       }
101       else if(type.getName().equals(SHORT))
102       {
103         object = new Short JavaDoc(value);
104       }
105       else if(type.getName().equals(STRING_CLASS))
106       {
107         object = new String JavaDoc(value);
108       }
109     }
110     catch(NumberFormatException JavaDoc e)
111     {
112       throw new MethodConfigUserObjectException(
113     "Value format not compatible with class");
114     }
115   }
116
117   public Object JavaDoc getObject()
118   {
119     return object;
120   }
121
122   public Class JavaDoc getType()
123   {
124     return type;
125   }
126
127   public String JavaDoc toString()
128   {
129     StringBuffer JavaDoc strbuff = new StringBuffer JavaDoc();
130     strbuff.append(type.getName());
131     strbuff.append(" : ");
132     strbuff.append(object);
133     return strbuff.toString();
134   }
135 }
136
Popular Tags