KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > test > CLISupportTestee


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/test/CLISupportTestee.java,v 1.3 2005/12/25 03:45:52 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:52 $
28  */

29  
30 package com.sun.cli.jmx.test;
31
32 import java.lang.reflect.Array JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34
35 import java.util.Properties JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import javax.management.*;
38
39
40 import com.sun.cli.util.ArrayConversion;
41 import com.sun.cli.util.ClassUtil;
42
43 import com.sun.cli.util.stringifier.ArrayStringifier;
44 import com.sun.cli.util.stringifier.SmartStringifier;
45
46 import com.sun.cli.jmx.util.InvokeHelper;
47
48 /*
49     A DynamicMBean used as a target for testing CLI
50  */

51 public final class CLISupportTestee implements DynamicMBean
52 {
53     final static String JavaDoc VIRTUAL_OPERATION_NAME = "virtualTest";
54         
55     final static String JavaDoc D = ","; // delimiter
56

57         private void
58     p( Object JavaDoc o )
59     {
60         System.out.println( o.toString() );
61     }
62     
63     final InvokeHelper mInvokeHelper;
64     
65     
66         public
67     CLISupportTestee()
68     {
69         mInvokeHelper = new InvokeHelper( this );
70     }
71    
72     
73 //-----------------------------------------------------------------------------
74

75         public String JavaDoc []
76     virtualTest()
77     {
78         return( new String JavaDoc [ 0 ] );
79     }
80     
81     
82         public String JavaDoc []
83     testFoo( long [][] p1 )
84     {
85         return( new String JavaDoc [ ] { "[[C", "char" } );
86     }
87     
88
89
90 //-----------------------------------------------------------------------------
91

92
93         public void
94     testNamed( java.util.Properties JavaDoc props )
95     {
96     }
97
98
99         public void
100     testNamed( String JavaDoc p1)
101     {
102     }
103
104
105         public void
106     testNamed( String JavaDoc p1, String JavaDoc p2 )
107     {
108     }
109
110         public void
111     testNamed( String JavaDoc p1, String JavaDoc p2, String JavaDoc p3)
112     {
113     }
114
115         public void
116     testNamed( String JavaDoc p1, String JavaDoc p2, String JavaDoc p3, Properties JavaDoc p4)
117     {
118     }
119 //-----------------------------------------------------------------------------
120

121         
122         public void
123     testPropertiesOnly( Properties JavaDoc p1 )
124     {
125     }
126     
127         public void
128     testProperties1Arg( String JavaDoc p1, Properties JavaDoc p2 )
129     {
130     }
131     
132         public void
133     testProperties2Args( String JavaDoc p1, Properties JavaDoc p2, String JavaDoc p3)
134     {
135     }
136     
137 //-----------------------------------------------------------------------------
138

139     private MBeanInfo mMBeanInfo = null;
140     
141     
142         public MBeanParameterInfo []
143     createMBeanParameterInfos( Method JavaDoc m)
144     {
145         final Class JavaDoc [] parameterClasses = m.getParameterTypes();
146         
147         final int numParams = Array.getLength( parameterClasses );
148         final MBeanParameterInfo infos [] = new MBeanParameterInfo[ numParams ];
149         
150         for( int i = 0; i < numParams; ++i )
151         {
152             // use parameter names of p1, p2, p3, etc
153
final String JavaDoc parameterName = "p" + (i+1);
154             
155             final MBeanParameterInfo info =
156                 new MBeanParameterInfo( parameterName,
157                                         parameterClasses[ i ].getName(),
158                                         "parameter " + i + 1
159                                         );
160             
161             infos[ i ] = info;
162         }
163         
164         return( infos );
165     }
166     
167         public MBeanOperationInfo []
168     createMBeanOperationInfos()
169     {
170         final Method JavaDoc [] allMethods = this.getClass().getDeclaredMethods();
171         
172         final ArrayList JavaDoc exportMethods = new ArrayList JavaDoc();
173         final int allCount = Array.getLength( allMethods );
174         for( int i = 0; i < allCount; ++i )
175         {
176             final Method JavaDoc m = allMethods[ i ];
177             final String JavaDoc name = m.getName();
178             
179             if ( name.startsWith( "test" ) )
180             {
181                 final String JavaDoc description = "tests '" + name + "' operation";
182                 final MBeanParameterInfo parameterInfo [] = createMBeanParameterInfos( m );
183                 
184                 // our generic invoker will return the same type of result for each--
185
// the signature that was invoked
186
// final String returnTypeString = m.getReturnType().getName();
187
final String JavaDoc returnTypeString = String JavaDoc [].class.getName();
188                 
189                 final MBeanOperationInfo info = new MBeanOperationInfo( name,
190                                                  description,
191                                                  parameterInfo,
192                                                  returnTypeString,
193                                                  MBeanOperationInfo.INFO );
194                 
195                 exportMethods.add( info );
196             }
197         }
198         
199         
200         // add the virtual methods
201
final MBeanOperationInfo [] virtualOperations = createVirtualOperationInfos();
202         for( int i = 0; i < Array.getLength( virtualOperations ); ++i )
203         {
204             exportMethods.add( virtualOperations[ i ] );
205         }
206         
207         MBeanOperationInfo [] infos = new MBeanOperationInfo[ exportMethods.size() ];
208         
209         infos = (MBeanOperationInfo [])exportMethods.toArray( infos );
210         
211         
212         return( infos );
213     }
214     
215
216     /*
217         All the base types we support
218      */

219     private final static Class JavaDoc [] BASE_CLASSES =
220     {
221         char.class,
222         Character JavaDoc.class,
223         boolean.class,
224         Boolean JavaDoc.class,
225         short.class,
226         Short JavaDoc.class,
227         int.class,
228         Integer JavaDoc.class,
229         long.class,
230         Long JavaDoc.class,
231         float.class,
232         Float JavaDoc.class,
233         double.class,
234         Double JavaDoc.class,
235         Object JavaDoc.class,
236         String JavaDoc.class,
237         Properties JavaDoc.class,
238         
239         Number JavaDoc.class,
240         java.math.BigInteger JavaDoc.class,
241         java.math.BigDecimal JavaDoc.class,
242         
243         java.net.URI JavaDoc.class,
244         java.net.URL JavaDoc.class
245     };
246     private final static int NUM_BASE_CLASSES = Array.getLength( BASE_CLASSES );
247     
248         public Class JavaDoc []
249     getSupportedClassList()
250     {
251         ArrayList JavaDoc classes = new ArrayList JavaDoc();
252         
253         for( int i = 0; i < NUM_BASE_CLASSES; ++i )
254         {
255             Class JavaDoc derivedClass = BASE_CLASSES[ i ];
256             
257             classes.add( derivedClass );
258             if ( derivedClass == Properties JavaDoc.class )
259                 continue;
260             
261             // 1D array
262
derivedClass = Array.newInstance( derivedClass, 0 ).getClass();
263             classes.add( derivedClass );
264             
265             // 2D array
266
derivedClass = Array.newInstance( derivedClass, 0 ).getClass();
267             classes.add( derivedClass );
268             
269             // we support any dimension arryas, but 3 is probably excessive from
270
// a testing standpoint
271
/*
272             // 3D array
273             derivedClass = Array.newInstance( derivedClass, 0 ).getClass();
274             classes.add( derivedClass );
275             */

276         }
277         
278         Class JavaDoc [] result = new Class JavaDoc[ classes.size() ];
279         classes.toArray( result );
280         return( result );
281     }
282     
283     
284         public MBeanOperationInfo
285     createVirtualOperationInfo( String JavaDoc operationName, Class JavaDoc [] argClasses )
286     {
287         final int numArgs = Array.getLength( argClasses );
288         
289         // create a method for each supported class
290
final ArrayList JavaDoc paramInfos = new ArrayList JavaDoc();
291         
292         for( int i = 0; i < numArgs; ++i )
293         {
294             final Class JavaDoc theClass = argClasses[ i ];
295             
296             // create single parameter with name of "pX", where X is the index of the param 1,2,3, etc
297
final String JavaDoc parameterName = "p" + (i + 1);
298             final String JavaDoc parameterDescription = "parameter " + (i+1);
299             final MBeanParameterInfo param = new MBeanParameterInfo( parameterName, theClass.getName(), parameterDescription );
300             
301             paramInfos.add( param );
302         }
303         
304         MBeanParameterInfo [] infos = new MBeanParameterInfo [ paramInfos.size() ];
305         paramInfos.toArray( infos );
306         
307         // create operation with return type of String [] (which will return the signature)
308
final String JavaDoc returnTypeString = String JavaDoc [].class.getName();
309         final String JavaDoc description = "virtual operation";
310         final MBeanOperationInfo info = new MBeanOperationInfo( operationName,
311                                                      description,
312                                                      infos,
313                                                      returnTypeString,
314                                                      MBeanOperationInfo.INFO );
315         return( info );
316     
317     }
318     
319     
320     
321         public MBeanOperationInfo []
322     createVirtualOperationInfos()
323     {
324         final String JavaDoc operationName = VIRTUAL_OPERATION_NAME;
325         
326         ArrayList JavaDoc ops = new ArrayList JavaDoc();
327         
328         /* create a method for each supported class consisting of a single parameter
329          */

330         final Class JavaDoc [] classes = getSupportedClassList();
331         final int numClasses = classes.length;
332         for( int i = 0; i < numClasses; ++i )
333         {
334             final Class JavaDoc [] classList = new Class JavaDoc [] { classes[ i ] };
335             final MBeanOperationInfo info = createVirtualOperationInfo( operationName, classList );
336             ops.add( info );
337         }
338         
339         // for each primitive type, create a signature with 3 parameters, each of which can vary between
340
// the primitive type and the Object form.
341
for( int i = 0; i < numClasses; ++i )
342         {
343             final Class JavaDoc theClass = classes[ i ];
344             if ( ! ClassUtil.IsPrimitiveClass( theClass ) )
345                 continue;
346                 
347             final Class JavaDoc objectClass = ClassUtil.PrimitiveClassToObjectClass( theClass );
348             
349             // generate all 8 variants
350
final Class JavaDoc [] both = new Class JavaDoc [] { theClass, objectClass };
351             for( int p1 = 0; p1 < 2; ++p1 )
352             {
353                 for( int p2 = 0; p2 < 2; ++p2 )
354                 {
355                     for( int p3 = 0; p3 < 2; ++p3 )
356                     {
357                         final Class JavaDoc [] classList = new Class JavaDoc [] { both[ p1 ], both[ p2 ], both[ p3 ] };
358                         MBeanOperationInfo info = createVirtualOperationInfo( operationName, classList );
359                         ops.add( info );
360                     }
361                 }
362             }
363         }
364         
365         
366         // Create all method signature for depth of 2 with all variants of all supported types
367
// CAUTION: this generates (num classes)^2 operations, about 7000 or so.
368
final int depth = 2;
369         final int numCombinations = numClasses * numClasses; // must match depth
370
for( int i = 0; i < numCombinations; ++i )
371         {
372             final Class JavaDoc [] classList = new Class JavaDoc [ depth ];
373         
374             // number of assignments must match depth
375
classList[ 0 ] = classes[ i % numClasses ];
376             classList[ 1 ] = classes[ (i / numClasses) % numClasses ];
377             
378             if ( classList[ 0 ] == Properties JavaDoc.class || classList[ 1 ] == Properties JavaDoc.class )
379             {
380                 // don't generate any methods with Properties; that is only for named invocation
381
continue;
382             }
383             assert( classList[ 0 ] != Properties JavaDoc.class && classList[ 1 ] != Properties JavaDoc.class );
384             
385             final MBeanOperationInfo info = createVirtualOperationInfo( operationName + i, classList );
386             ops.add( info );
387         }
388             
389         
390         MBeanOperationInfo [] infos = new MBeanOperationInfo[ ops.size() ];
391         infos = (MBeanOperationInfo [])ops.toArray( infos );
392         return( infos );
393     }
394     
395         public synchronized MBeanInfo
396     getMBeanInfo()
397     {
398         if ( mMBeanInfo == null )
399         {
400             final MBeanAttributeInfo [] attributeInfo = null;
401             final MBeanConstructorInfo [] constructorInfo = null;
402             final MBeanNotificationInfo [] notificationInfo = null;
403             final MBeanOperationInfo [] operationInfo = createMBeanOperationInfos();
404             
405             mMBeanInfo = new MBeanInfo( this.getClass().getName(),
406                 "Test MBean for the CLI support code",
407                 attributeInfo,
408                 constructorInfo,
409                 operationInfo,
410                 notificationInfo );
411         }
412         
413         return( mMBeanInfo );
414     }
415     
416     
417         public Object JavaDoc
418     getAttribute(String JavaDoc attribute)
419         throws AttributeNotFoundException, MBeanException, ReflectionException
420     {
421         throw new AttributeNotFoundException();
422     }
423     
424         public AttributeList
425     getAttributes(String JavaDoc[] attributes)
426     {
427         return new AttributeList();
428     }
429     
430     
431         public AttributeList
432     setAttributes(AttributeList attributes)
433     {
434         return new AttributeList();
435     }
436     
437     
438     
439         public void
440     setAttribute(Attribute attribute)
441         throws AttributeNotFoundException, InvalidAttributeValueException,
442         MBeanException, ReflectionException
443     {
444         throw new AttributeNotFoundException();
445     }
446     
447         public Object JavaDoc
448     invoke(
449         String JavaDoc actionName,
450         Object JavaDoc params[],
451         String JavaDoc signature[] )
452         throws MBeanException, ReflectionException
453     {
454         Object JavaDoc result = null;
455         
456         if ( actionName.startsWith( VIRTUAL_OPERATION_NAME ) )
457         {
458             return( signature );
459         }
460         
461         try
462         {
463             // p( "INVOKING HELPER: " + actionName + "(" + AutoStringifier.toString( signature ) + ")");
464
result = mInvokeHelper.invoke( actionName, params, signature );
465             
466             // ignore result, always return signature of invoked method
467
result = signature;
468             // p( "SUCCESS: " + actionName + "(" + AutoStringifier.toString( signature ) + ")");
469

470         }
471         catch( Exception JavaDoc e )
472         {
473             // e.printStackTrace();
474
throw new MBeanException( e );
475         }
476         
477         return( result );
478     }
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
Popular Tags