KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > AdminTest


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  * $Id: AdminTest.java,v 1.3 2005/12/25 03:47:27 tcfujii Exp $
26  * @author: alexkrav
27  *
28  * $Log: AdminTest.java,v $
29  * Revision 1.3 2005/12/25 03:47:27 tcfujii
30  * Updated copyright text and year.
31  *
32  * Revision 1.2 2005/06/27 21:19:39 tcfujii
33  * Issue number: CDDL header updates.
34  *
35  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
36  * GlassFish first drop
37  *
38  * Revision 1.18 2004/11/14 07:04:15 tcfujii
39  * Updated copyright text and/or year.
40  *
41  * Revision 1.17 2004/02/20 03:56:05 qouyang
42  *
43  *
44  * First pass at code merge.
45  *
46  * Details for the merge will be published at:
47  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
48  *
49  * Revision 1.16.4.1 2004/02/02 07:25:12 tcfujii
50  * Copyright updates notices; reviewer: Tony Ng
51  *
52  * Revision 1.16 2003/09/20 17:17:51 sridatta
53  * changing from standard-pe-config to server-config
54  * ql pass
55  * sqe smoke pass
56  * cts samples pass
57  *
58  * Revision 1.15 2003/08/15 23:09:51 kravtch
59  * calls to notifyRegisterMBean/UnregisterMBean from posrRegister/postDeregister
60  * removeChild support is added;
61  * new test cases for dotted names testing
62  *
63  * Revision 1.14 2003/08/14 23:01:36 kravtch
64  * test cases added for:
65  * jvm-options (set/get String[] attribute);
66  * security-map;
67  * auth-realm;
68  *
69  * Revision 1.13 2003/08/07 00:41:03 kravtch
70  * - new DTD related changes;
71  * - properties support added;
72  * - getDefaultAttributeValue() implemented for config MBeans;
73  * - merge Jsr77 and config activity in runtime mbeans;
74  *
75  * Revision 1.12 2003/07/29 18:59:34 kravtch
76  * MBeanRegistryEntry:
77  * - support for toFormatString();
78  * - instantiateMBean() method modified to instantiate runtime MBeans as well;
79  * MBeanRegistryFactory:
80  * - fixed bug in getRuntimeRegistry();
81  * MBeanNamingInfo:
82  * - less strict requirements for parm_list_array size in constructor (can be more then needed);
83  * BaseRuntimeMBean:
84  * - exception ClassCastException("Managed resource is not a Jsr77ModelBean") handling;
85  * ManagedJsr77MdlBean:
86  * - call managed bean bug fixed ( getDeclaredMethod()->getMethod())
87  * admin/dtds/runtime-mbeans-descriptors.xml - modified to represent new runtime mbeans;
88  *
89  * Revision 1.11 2003/07/18 20:14:42 kravtch
90  * 1. ALL config mbeans are now covered by descriptors.xml
91  * 2. new infrastructure for runtime mbeans is added
92  * 3. generic constructors added to jsr77Mdl beans (String[])
93  * 4. new test cases are added to admintest
94  * 5. MBeanRegistryFactory has now different methods to obtain admin/runtime registries
95  * 6. runtime-descriptors xml-file is added to build
96  *
97  * Revision 1.10 2003/06/25 20:03:35 kravtch
98  * 1. java file headers modified
99  * 2. properties handling api is added
100  * 3. fixed bug for xpathes containing special symbols;
101  * 4. new testcases added for jdbc-resource
102  * 5. introspector modified by not including base classes operations;
103  *
104  *
105 */

106
107 package com.sun.enterprise.admin;
108
109 import javax.management.*;
110
111 import com.sun.enterprise.admin.meta.*;
112 import com.sun.enterprise.config.ConfigContext;
113 import com.sun.enterprise.config.ConfigFactory;
114 import java.lang.reflect.InvocationTargetException JavaDoc;
115 import java.util.*;
116
117 public abstract class AdminTest
118 {
119     /**
120      * @param args the command line arguments
121      */

122     public static void main(String JavaDoc[] args) {
123         int length = args.length;
124 /* if (length < 2) {
125             usage();
126             System.exit(1);
127         }
128 */

129      //******* call T E S T S *******
130
runAdminMbeanTests();
131 // runRuntimeMbeanTests();
132

133     
134     }
135
136     static void runAdminMbeanTests()
137     {
138         println("Start....!");
139         try
140         {
141             MBeanRegistry registry = MBeanRegistryFactory.getMBeanRegistry("dtds/admin-mbeans-descriptors.xml");
142             ConfigContext configContext = ConfigFactory.createConfigContext("/home/kravtch/domain.xml");
143 // MBeanRegistry registry = MBeanRegistryFactory.getMBeanRegistry("k:\\export\\ias\\admin-core\\admin\\dtds\\admin-mbeans-descriptors2.xml");
144
// ConfigContext configContext = ConfigFactory.createConfigContext("k:\\domain2.xml");
145
Object JavaDoc retObject;
146             
147 registry.generateAndRegisterAllDottedNames(configContext, "abc.def.xyz");
148 if(configContext!=null)
149    return;
150 //***********************************************************************************************
151
// title("REGISTRY");
152

153             title("REGISTRY (in XPath value order)");
154             registry.sortRegistryEntries(registry.SORT_BY_XPATH);
155
156 // println(registry.toFormatString());
157
// println(registry.toString());
158
String JavaDoc[] location;
159             BaseAdminMBean mbean;
160             AttributeList attrs;
161
162             //***********************************************************************************************
163
title("ejb-container INSTANTIATION");
164             location = new String JavaDoc[]{"testdomain", "server-config"};
165             mbean = registry.instantiateMBean("ejb-container", location, null, configContext);
166             println(""+mbean.getAttribute("cache_resize_quantity"));
167
168             //***********************************************************************************************
169
title("ejb-container set steady_pool_size to 20");
170             printAllAttributes("************BEFORE SET ******", " ", mbean);
171             
172             mbean.setAttribute(new Attribute("steady_pool_size", (Object JavaDoc)"20"));
173             printAllAttributes("\n************AFTER SET******", " ", mbean);
174
175             mbean.setAttribute(new Attribute("steady_pool_size", (Object JavaDoc)"2"));
176
177             //***********************************************************************************************
178
title("resources Instantiate()");
179             location = new String JavaDoc[]{"testdomain"};
180             mbean = registry.instantiateMBean("resources", location, null, configContext);
181             
182             //***********************************************************************************************
183
title("resources-> getJdbcResource()");
184             retObject = mbean.invoke("getJdbcResource", null, null); //new Object[]{}, new String[]{});
185
printObj("Returned object:", " ", retObject);
186                     
187             //***********************************************************************************************
188
title("jdbc-resource-> Instantiate(jdbc/PointBase)");
189             mbean = registry.instantiateMBean("jdbc-resource", new String JavaDoc[]{"testdomain","jdbc/__TimerPool"}, null, configContext);
190             println("returned jdbc-resource[jdbc/PointBase] mbean:"+mbean);
191
192             //***********************************************************************************************
193
title("jdbc-resource[jdbc/PointBase]-> getAttributes()");
194    
195             //***********************************************************************************************
196
title("resources Instantiate()");
197             location = new String JavaDoc[]{"testdomain"};
198             mbean = registry.instantiateMBean("resources", location, null, configContext);
199             
200             //***********************************************************************************************
201
title("resources-> getCustomResource()");
202             retObject = mbean.invoke("getCustomResource", null, null); //new Object[]{}, new String[]{});
203
printObj("Returned object:", " ", retObject);
204
205             //***********************************************************************************************
206
title("resources-> createCustomResource(testJndiName2/testResType2/testFactoryClass2)");
207             attrs = new AttributeList();
208             attrs.add(new Attribute("jndi_name", "testJndiName2"));
209             attrs.add(new Attribute("res_type", "testResType2"));
210             attrs.add(new Attribute("factory_class", "testFactoryClass2"));
211             printObj("Input Attributes:", " ", attrs);
212 // retObject = mbean.invoke("createCustomResource", new Object[]{attrs, null, null},
213
// new String[]{attrs.getClass().getName(),"java.util.Properties","java.lang.String"});
214
retObject = mbean.invoke("createCustomResource", new Object JavaDoc[]{attrs}, new String JavaDoc[]{attrs.getClass().getName()});
215             printObj("Returned object:", " ", retObject);
216             
217             //***********************************************************************************************
218
title("resources-> getCustomResourceByJndiName(testJndiName2)");
219             retObject = mbean.invoke("getCustomResourceByJndiName", new Object JavaDoc[]{"testJndiName2"}, new String JavaDoc[]{"java.lang.String"});
220             printObj("Returned object:", " ", retObject);
221             
222             //***********************************************************************************************
223
title("resources-> getCustomResource()");
224             retObject = mbean.invoke("getCustomResource", null, null); //new Object[]{}, new String[]{});
225
printObj("Returned object:", " ", retObject);
226
227             //***********************************************************************************************
228
title("custom-resource-> Instantiate(testJndiName2)");
229             mbean = registry.instantiateMBean("custom-resource", new String JavaDoc[]{"testdomain","testJndiName2"}, null, configContext);
230             printObj("returned custom-resource[testJndiName2] mbean:",mbean);
231             
232             //***********************************************************************************************
233
title("custom-resource[testJndiName2]-> getAttributes()");
234             printAllAttributes("Attributes:", " ", mbean);
235
236             //***********************************************************************************************
237
title("custom-resource-> Instantiate(testJndiName2) using ObjectName");
238             mbean = registry.instantiateConfigMBean(new ObjectName("testdomain:type=custom-resource,jndi-name=testJndiName2,category=config"), null, configContext);
239             printObj("returned custom-resource[testJndiName2] mbean:",mbean);
240             
241             //***********************************************************************************************
242
title("custom-resource[testJndiName2]-> getAttributes()");
243             printAllAttributes("Attributes:", " ", mbean);
244             //***********************************************************************************************
245
/* title("custom-resource[testJndiName2]-> setProperty()");
246             setConfigMbeanProperty("testPropName1", "testPropValue1", mbean);
247             setConfigMbeanProperty("testPropName2", "testPropValue2", mbean);
248             setConfigMbeanProperty("testPropName3", "testPropValue3", mbean);
249             setConfigMbeanProperty("testPropName4", "testPropValue4", mbean);
250             
251             //***********************************************************************************************
252             title("custom-resource[testJndiName2]-> getProperties()");
253             printAllProperties("Properties:", " ", mbean);
254 */

255             //***********************************************************************************************
256
//title("custom-resource[testJndiName2]-> destroyConfigElement()");
257
//retObject = mbean.invoke("destroyConfigElement", null, null);
258
//println("Returned object after delete:"+retObject);
259

260             title("resources-> getCustomResource()");
261             location = new String JavaDoc[]{"testdomain"};
262             mbean = registry.instantiateMBean("resources", location, null, configContext);
263             retObject = mbean.invoke("getCustomResource", null, null); //new Object[]{}, new String[]{});
264
printObj("Returned object:"," ",retObject);
265             title("resources-> removeCustomResourceByJndiName(testJndiName2)");
266             retObject = mbean.invoke("removeCustomResourceByJndiName", new Object JavaDoc[]{"testJndiName2"}, new String JavaDoc[]{"java.lang.String"});
267             printObj("Returned object:", " ", retObject);
268             
269             //***********************************************************************************************
270
title("resources-> getCustomResource()");
271             location = new String JavaDoc[]{"testdomain"};
272             mbean = registry.instantiateMBean("resources", location, null, configContext);
273             retObject = mbean.invoke("getCustomResource", null, null); //new Object[]{}, new String[]{});
274
printObj("Returned object:"," ",retObject);
275             
276             //***********************************************************************************************
277
title("applications-> Instantantiate()");
278             location = new String JavaDoc[]{"testdomain"};
279             mbean = registry.instantiateMBean("applications", location, null, configContext);
280             
281 /* //***********************************************************************************************
282             title("applications-> deployApplication()");
283             retObject = mbean.invoke("deployApplication", new Object[]{"testName", "testLocation", null, null, null}, new String[]{"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String"});
284             printObj("Returned object:", " ", retObject);
285
286             //***********************************************************************************************
287             title("j2ee-application-> getAttributes()");
288             location = new String[]{"testdomain","testName"};
289             mbean = registry.instantiateMBean("j2ee-application", location, null, configContext);
290             printAllAttributes("Attributes:", " ", mbean);
291
292             //***********************************************************************************************
293             title("j2ee-application-> destroyConfigElement()");
294             retObject = mbean.invoke("destroyConfigElement", null, null);
295             printObj("Returned object:", " ", retObject);
296             configContext.flush();
297 */

298             //***********************************************************************************************
299
title("thread-pools Instantiate()");
300             location = new String JavaDoc[]{"testdomain", "server-config"};
301             mbean = registry.instantiateMBean("thread-pools", location, null, configContext);
302             printObj("Returned object:"," ",mbean);
303             title("thread-pools-> createThreadPool");
304             attrs = new AttributeList();
305             attrs.add(new Attribute("thread_pool_id", "mytestThreadPool"));
306             attrs.add(new Attribute("min_thread_pool_size", "100"));
307             attrs.add(new Attribute("max_thread_pool_size", "200"));
308             attrs.add(new Attribute("num_work_queues", "12"));
309             attrs.add(new Attribute("idle_thread_timeout_in_seconds", "50"));
310             printObj("Input Attributes:", " ", attrs);
311             retObject = mbean.invoke("createThreadPool", new Object JavaDoc[]{attrs}, new String JavaDoc[]{attrs.getClass().getName()});
312             printObj("Returned object:", " ", retObject);
313             
314             //***********************************************************************************************
315
title("jdbc-connection-pool[PointBasePool] Instantiate()");
316             location = new String JavaDoc[]{"testdomain", "PointBasePool"};
317             mbean = registry.instantiateMBean("jdbc-connection-pool", location, null, configContext);
318             printObj("Returned object:"," ",mbean);
319             //***********************************************************************************************
320
title("jdbc-connection-pool[PointBasePool] PrintAllAttributes");
321             printAllAttributes("Attributes:", " ", mbean);
322             //***********************************************************************************************
323
title("jdbc-connection-pool[PointBasePool] PrintAllProperties");
324             printAllProperties("Properties:", " ", mbean);
325             //***********************************************************************************************
326
title("set properties: 'testPropName1' and 'testPropName2'");
327             setConfigMbeanProperty("testPropName1", "testPropValue1", mbean);
328             setConfigMbeanProperty("testPropName2", "testPropValue2", mbean);
329             //***********************************************************************************************
330
title("jdbc-connection-pool[PointBasePool] PrintAllProperties");
331             printAllProperties("Properties:", " ", mbean);
332             //***********************************************************************************************
333
title("remove properties: 'testPropName1' and 'testPropName2'");
334             setConfigMbeanProperty("testPropName1", null, mbean);
335             setConfigMbeanProperty("testPropName2", null, mbean);
336             //***********************************************************************************************
337
title("jdbc-connection-pool[PointBasePool] PrintAllProperties");
338             printAllProperties("Properties:", " ", mbean);
339             //***********************************************************************************************
340
title("jdbc-connection-pool.getDefaultAttributeValue[max-pool-size]");
341             println("Value:"+mbean.getAttribute("max_pool_size"));
342             retObject = mbean.invoke("getDefaultAttributeValue", new Object JavaDoc[]{"max_pool_size"}, new String JavaDoc[]{"java.lang.String"});
343             println("Default:"+retObject);
344             //***********************************************************************************************
345
title("thread-pools Instantiate()");
346             location = new String JavaDoc[]{"testdomain", "server-config"};
347             mbean = registry.instantiateMBean("java-config", location, null, configContext);
348             printObj("Returned object:"," ",mbean);
349             title("jvm-options - String[] attribute test");
350             printObj("Value:", " ", mbean.getAttribute("jvm_options"));
351             title("jvm-options - String[] SETattribute test");
352             mbean.setAttribute(new Attribute("jvm_options",new String JavaDoc[]{"abc","def","xyz"}));
353             printObj("Value:", " ", mbean.getAttribute("jvm_options"));
354             //***********************************************************************************************
355
title("auth-realm['file'] Instantiate()");
356             location = new String JavaDoc[]{"testdomain", "server-config","file"};
357             mbean = registry.instantiateMBean("auth-realm", location, null, configContext);
358             printObj("Returned object:"," ",mbean);
359             title("auth-realm['file']-> getProperties()");
360             printAllProperties("Properties:", " ", mbean);
361             title("auth-realm['file']-> AddUser()");
362             
363             String JavaDoc[] groups = new String JavaDoc[]{"testGroup1","testGroup2","testGroup3"};
364             retObject = mbean.invoke("updateUser", new Object JavaDoc[]{"testUser", "testPassword", groups}, new String JavaDoc[]{"java.lang.String", "java.lang.String", groups.getClass().getName()});
365             printObj("updateUser:Returned object:", " ", retObject);
366             retObject = mbean.invoke("getUserGroupNames", new Object JavaDoc[]{"testUser"}, new String JavaDoc[]{"java.lang.String"});
367             printObj("getUserGroupNames:Returned object:", " ", retObject);
368             retObject = mbean.invoke("getGroupNames", new Object JavaDoc[]{}, new String JavaDoc[]{});
369             printObj("getGroupNames:Returned object:", " ", retObject);
370             retObject = mbean.invoke("getUserNames", new Object JavaDoc[]{}, new String JavaDoc[]{});
371             printObj("getUserNames:Returned object:", " ", retObject);
372             title("resources-> getCustomResource()");
373             //***********************************************************************************************
374
title("domain Instantiate()");
375             location = new String JavaDoc[]{"testdomain"};
376             mbean = registry.instantiateMBean("domain", location, null, configContext);
377             printObj("Returned object:"," ",retObject);
378             title("domain-> removeResources");
379             retObject = mbean.invoke("removeResources", null, null);
380             printObj("Returned object:", " ", retObject);
381             
382         }
383         catch (MBeanException mbe)
384         {
385             //mbe.printStackTrace();
386
Exception JavaDoc e = mbe.getTargetException();
387             if(e instanceof InvocationTargetException JavaDoc)
388             {
389                 InvocationTargetException JavaDoc ite = (InvocationTargetException JavaDoc)e;
390                 Throwable JavaDoc t = ite.getTargetException();
391                 println(t.getMessage());
392                 t.printStackTrace();
393             }
394             else
395             {
396                 println(e.getMessage());
397                 e.printStackTrace();
398             }
399         }
400         catch (Exception JavaDoc e)
401         {
402             if(e instanceof InvocationTargetException JavaDoc)
403             {
404                 InvocationTargetException JavaDoc ite = (InvocationTargetException JavaDoc)e;
405                 Throwable JavaDoc t = ite.getTargetException();
406                 println(t.getMessage());
407                 t.printStackTrace();
408             }
409             else
410             {
411                 println(e.getMessage());
412                 e.printStackTrace();
413             }
414         }
415         catch (Throwable JavaDoc t)
416         {
417                 println(t.getMessage());
418                 t.printStackTrace();
419         }
420         println("Bye....!");
421     }
422
423     static void runRuntimeMbeanTests()
424     {
425         println("Start....!");
426         try
427         {
428             MBeanRegistry registry = MBeanRegistryFactory.getMBeanRegistry("dtds/runtime-mbeans-descriptors.xml");
429             ConfigContext configContext = ConfigFactory.createConfigContext("/home/kravtch/domain.xml");
430             Object JavaDoc retObject;
431             
432             //***********************************************************************************************
433
title("REGISTRY");
434             println(registry.toString());
435             String JavaDoc[] location;
436             BaseAdminMBean mbean;
437             AttributeList attrs;
438
439         }
440 /* catch (MBeanException mbe)
441         {
442             //mbe.printStackTrace();
443             Exception e = mbe.getTargetException();
444             if(e instanceof InvocationTargetException)
445             {
446                 InvocationTargetException ite = (InvocationTargetException)e;
447                 Throwable t = ite.getTargetException();
448                 println(t.getMessage());
449                 t.printStackTrace();
450             }
451             else
452             {
453                 println(e.getMessage());
454                 e.printStackTrace();
455             }
456         }
457 */

458         catch (Exception JavaDoc e)
459         {
460             if(e instanceof InvocationTargetException JavaDoc)
461             {
462                 InvocationTargetException JavaDoc ite = (InvocationTargetException JavaDoc)e;
463                 Throwable JavaDoc t = ite.getTargetException();
464                 println(t.getMessage());
465                 t.printStackTrace();
466             }
467             else
468             {
469                 println(e.getMessage());
470                 e.printStackTrace();
471             }
472         }
473         catch (Throwable JavaDoc t)
474         {
475                 println(t.getMessage());
476                 t.printStackTrace();
477         }
478         println("Bye....!");
479     }
480
481     private static void title(String JavaDoc str)
482     {
483         System.out.println("\n\n************* " + str + " *******************");
484     }
485     private static void println(String JavaDoc str)
486     {
487         System.out.println(str);
488     }
489     
490     private static void printObj(String JavaDoc title, String JavaDoc pref, Object JavaDoc obj)
491     {
492             println(title);
493             printObj(pref, obj);
494     }
495     
496     private static void printObj(String JavaDoc pref, Object JavaDoc obj)
497     {
498         if(pref==null)
499             pref="";
500         if(obj==null)
501             println(pref+"null");
502         if(obj instanceof Object JavaDoc[])
503         {
504             Object JavaDoc[] objs = (Object JavaDoc[])obj;
505             if(objs.length==0)
506                 println(pref+"array.length = 0");
507             for(int i=0; i<objs.length; i++)
508             {
509                 printObj(pref+" ["+i+"] -> ", objs[i]);
510             }
511         }
512         else if(obj instanceof ArrayList)
513         {
514             ArrayList objs = (ArrayList)obj;
515             if(objs.size()==0)
516                 println(pref+"list.size() = 0");
517             for(int i=0; i<objs.size(); i++)
518             {
519                 printObj(pref/*+" ["+i+"] -> "*/, objs.get(i));
520             }
521         }
522         else if(obj instanceof Attribute)
523         {
524             println(pref+ ((Attribute)obj).getName() + "=" + ((Attribute)obj).getValue());
525         }
526         else
527         {
528             println(pref + obj);
529         }
530     }
531
532     private static void printAllAttributes(String JavaDoc title, String JavaDoc pref, DynamicMBean mbean)
533     {
534             printObj(title, pref, mbean.getAttributes(new String JavaDoc[]{""}));
535     }
536     
537     private static void printAllProperties(String JavaDoc title, String JavaDoc pref, DynamicMBean mbean) throws Exception JavaDoc
538     {
539         Object JavaDoc ret = mbean.invoke("getProperties", null, null);
540 // retObject = mbean.invoke("deployApplication", new Object[]{"testName", "testLocation", null, null, null}, new String[]{"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String"});
541
printObj(title, pref, ret);
542     }
543     private static Object JavaDoc getConfigMbeanProperty(String JavaDoc name, DynamicMBean mbean) throws Exception JavaDoc
544     {
545         return mbean.invoke("getPropertyValue", new Object JavaDoc[]{name}, new String JavaDoc[]{"java.lang.String"});
546     }
547     private static void setConfigMbeanProperty(String JavaDoc name, Object JavaDoc value, DynamicMBean mbean) throws Exception JavaDoc
548     {
549         Object JavaDoc ret = mbean.invoke("setProperty", new Object JavaDoc[]{new Attribute(name, value)}, new String JavaDoc[]{"javax.management.Attribute"});
550     }
551 }
552
Popular Tags