KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > adaptor > http > CommandProcessorUtil


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.adaptor.http;
10
11 import java.lang.reflect.Constructor JavaDoc;
12 import java.text.DateFormat JavaDoc;
13 import java.text.ParseException JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Date JavaDoc;
16 import javax.management.ObjectInstance JavaDoc;
17 import javax.management.openmbean.OpenType JavaDoc;
18
19 /**
20  * Contains utilities common to CommandProcessor objects
21  *
22  * @version $Revision: 1.3 $
23  */

24 public class CommandProcessorUtil
25 {
26    // contains all date and date time format instances
27
// for the current locale
28
private static final DateFormat JavaDoc[] allFormats = new DateFormat JavaDoc[]{
29       DateFormat.getDateInstance(),
30       DateFormat.getTimeInstance(),
31       DateFormat.getDateTimeInstance(),
32       // first pure date format
33
DateFormat.getDateInstance(DateFormat.SHORT),
34       DateFormat.getDateInstance(DateFormat.MEDIUM),
35       DateFormat.getDateInstance(DateFormat.LONG),
36       DateFormat.getDateInstance(DateFormat.FULL),
37       // pure time format
38
DateFormat.getTimeInstance(DateFormat.SHORT),
39       DateFormat.getTimeInstance(DateFormat.MEDIUM),
40       DateFormat.getTimeInstance(DateFormat.LONG),
41       DateFormat.getTimeInstance(DateFormat.FULL),
42       // combinations
43
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT),
44       DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM),
45       DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG),
46       DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL),
47
48       DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT),
49       DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM),
50       DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG),
51       DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL),
52
53       DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT),
54       DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM),
55       DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG),
56       DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL),
57
58       DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT),
59       DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM),
60       DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG),
61       DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL)
62    };
63
64    private static final String JavaDoc[] BASIC_TYPES = new String JavaDoc[]{
65       "int", "long", "short", "byte", "float", "double", "boolean"
66    };
67
68    /**
69     * Creates a parameter object of the given type containing a given value.
70     * If the type is unknown null is returned
71     *
72     * @param parameterType Indicates the type of the parameter, for instance java.lang.String
73     * @param parameterValue The value of the parameter as a String
74     * @return an object of parameterType type and value parameterValue or null if the type is unknown
75     * @throws Thrown in case there is a data conversion error
76     */

77    protected static Object JavaDoc createParameterValue(String JavaDoc parameterType, String JavaDoc parameterValue)
78            throws Exception JavaDoc
79    {
80       if (parameterType.equals("java.lang.String"))
81       {
82          return parameterValue;
83       }
84       else if (parameterType.equals("java.lang.Integer") || parameterType.equals("int"))
85       {
86          return Integer.valueOf(parameterValue);
87       }
88       else if (parameterType.equals("java.lang.Long") || parameterType.equals("long"))
89       {
90          return Long.valueOf(parameterValue);
91       }
92       else if (parameterType.equals("java.lang.Short") || parameterType.equals("short"))
93       {
94          return Short.valueOf(parameterValue);
95       }
96       else if (parameterType.equals("java.lang.Byte") || parameterType.equals("byte"))
97       {
98          return Byte.valueOf(parameterValue);
99       }
100       else if (parameterType.equals("java.lang.Float") || parameterType.equals("float"))
101       {
102          return Float.valueOf(parameterValue);
103       }
104       // changed java.lang.dobule to java.lang.double bronwen
105
else if (parameterType.equals("java.lang.Double") || parameterType.equals("double"))
106       {
107          return Double.valueOf(parameterValue);
108       }
109       else if (parameterType.equals("java.lang.Boolean") || parameterType.equals("boolean"))
110       {
111          return Boolean.valueOf(parameterValue);
112       }
113       else if (parameterType.equals("java.lang.Void"))
114       {
115          return Void.TYPE;
116       }
117       else if (parameterType.equals("java.util.Date"))
118       {
119          // this is tricky since Date can be written in many formats
120
// will use the Date format with current locale and several
121
// different formats
122
Date JavaDoc value = null;
123          for (int i = 0; i < allFormats.length; i++)
124          {
125             synchronized (allFormats[i])
126             {
127                try
128                {
129                   System.out.println(parameterValue + " " + allFormats[i]);
130                   value = allFormats[i].parse(parameterValue);
131                   // if succeful then break
132
break;
133                }
134                catch (ParseException JavaDoc e)
135                {
136                   // ignore, the format wasn't appropriate
137
}
138             }
139          }
140          if (value == null)
141          {
142             throw new ParseException JavaDoc("Not possible to parse", 0);
143          }
144          return value;
145       }
146       else if (parameterType.equals("java.lang.Number"))
147       {
148          Number JavaDoc value = null;
149          // try first as a long
150
try
151          {
152             value = Long.valueOf(parameterValue);
153          }
154          catch (NumberFormatException JavaDoc e)
155          {
156          }
157          // if not try as a double
158
if (value == null)
159          {
160             try
161             {
162                value = Double.valueOf(parameterValue);
163             }
164             catch (NumberFormatException JavaDoc e)
165             {
166             }
167          }
168          if (value == null)
169          {
170             throw new NumberFormatException JavaDoc("Not possible to parse");
171          }
172          return value;
173       }
174       if (parameterType.equals("java.lang.Character") || parameterType.equals("char"))
175       {
176          if (parameterValue.length() > 0)
177          {
178             return new Character JavaDoc(parameterValue.charAt(0));
179          }
180          else
181          {
182             throw new NumberFormatException JavaDoc("Can not initialize Character from empty String");
183          }
184       }
185       // tests whether the classes have a single string parameter value
186
// constructor. That covers the classes
187
// javax.management.ObjectName
188
// java.math.BigInteger
189
// java.math.BigDecimal
190

191       Class JavaDoc cls = null;
192       java.lang.reflect.Constructor JavaDoc ctor = null;
193       try
194       {
195          cls = Class.forName(parameterType);
196          ctor = cls.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
197          return ctor.newInstance(new Object JavaDoc[]{parameterValue});
198       }
199       catch (ClassNotFoundException JavaDoc cnfe)
200       {
201          // Can not find class. Not in our ClassLoader?
202
/** @todo Ask the MBeanServer to instantiate this class??? */
203          throw new IllegalArgumentException JavaDoc("Invalid parameter type: " + parameterType);
204       }
205       catch (NoSuchMethodException JavaDoc nsme)
206       {
207          // No public String constructor.
208
throw new IllegalArgumentException JavaDoc("Invalid parameter type: " + parameterType);
209       }
210       catch (Exception JavaDoc ex)
211       {
212          // Constructor might have thrown an exception?
213
// Security Exception ?
214
// IllegalAccessException? .... etc.
215
// Just rethrow. We can do little here <shrug>
216
/** @todo Log the exception */
217          throw ex;
218       }
219    }
220
221    /**
222     * Checks if the given type is primitive of can be initialized from String.<br>
223     * This is done by trying to load the class and checking if there is a public String
224     * only constructor.
225     *
226     * @param parameterType Indicates the type of the parameter, for instance java.lang.String
227     * @return true if the type is primitive or String initializable
228     * @throws Thrown in case there is a data conversion error
229     */

230    protected static boolean canCreateParameterValue(String JavaDoc parameterType)
231    {
232       int count = OpenType.ALLOWED_CLASSNAMES.length;
233       for (int i = 0; i < count; i++)
234       {
235          if (OpenType.ALLOWED_CLASSNAMES[i].equals(parameterType))
236          {
237             return true;
238          }
239       }
240       count = BASIC_TYPES.length;
241       for (int i = 0; i < count; i++)
242       {
243          if (BASIC_TYPES[i].equals(parameterType))
244          {
245             return true;
246          }
247       }
248
249       Class JavaDoc cls = null;
250       try
251       {
252          cls = Class.forName(parameterType);
253          cls.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
254          // Yes we can load the class and it has a public String constructor.
255
return true;
256       }
257       catch (ClassNotFoundException JavaDoc cnfe)
258       {
259          // Can not find class. Not in our ClassLoader?
260
/** @todo Ask the MBeanServer to instantiate this class??? */
261          return false;
262       }
263       catch (NoSuchMethodException JavaDoc nsme)
264       {
265          // No public String constructor.
266
return false;
267       }
268       catch (Exception JavaDoc ex)
269       {
270          // Constructor might have thrown an exception?
271
// Security Exception ?
272
// IllegalAccessException? .... etc.
273
// Just rethrow. We can do little here <shrug>
274
/** @todo Log the exception */
275          return false;
276       }
277    }
278
279    public static Comparator JavaDoc createObjectNameComparator()
280    {
281       return new ToStringComparator();
282    }
283
284    public static Comparator JavaDoc createObjectInstanceComparator()
285    {
286       return new ObjectInstanceComparator();
287    }
288
289    public static Comparator JavaDoc createConstructorComparator()
290    {
291       return new ConstructorComparator();
292    }
293
294    public static Comparator JavaDoc createClassComparator()
295    {
296       return new ToStringComparator();
297    }
298
299    private static class ToStringComparator implements Comparator JavaDoc
300    {
301       public int compare(Object JavaDoc o1, Object JavaDoc o2)
302       {
303          return o1.toString().compareTo(o2.toString());
304       }
305    }
306
307    private static class ObjectInstanceComparator implements Comparator JavaDoc
308    {
309       private ToStringComparator comp = new ToStringComparator();
310
311       public int compare(Object JavaDoc o1, Object JavaDoc o2)
312       {
313          ObjectInstance JavaDoc oi1 = (ObjectInstance JavaDoc)o1;
314          ObjectInstance JavaDoc oi2 = (ObjectInstance JavaDoc)o2;
315          return comp.compare(oi1.getObjectName(), oi2.getObjectName());
316       }
317    }
318
319    private static class ConstructorComparator implements Comparator JavaDoc
320    {
321       public int compare(Object JavaDoc o1, Object JavaDoc o2)
322       {
323          Constructor JavaDoc c1 = (Constructor JavaDoc)o1;
324          Constructor JavaDoc c2 = (Constructor JavaDoc)o2;
325          // sort them by the parameter types;
326
Class JavaDoc[] params1 = c1.getParameterTypes();
327          Class JavaDoc[] params2 = c2.getParameterTypes();
328          if (params1.length == params2.length)
329          {
330             for (int i = 0; i < params1.length; i++)
331             {
332                if (!params1[i].equals(params2[i]))
333                {
334                   return params2[i].toString().compareTo(params1[i].toString());
335                }
336             }
337             return 0;
338          }
339          else
340          {
341             return params1.length - params2.length;
342          }
343       }
344    }
345
346 }
347
Popular Tags