1 23 24 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 ; 115 import java.util.*; 116 117 public abstract class AdminTest 118 { 119 122 public static void main(String [] args) { 123 int length = args.length; 124 129 runAdminMbeanTests(); 131 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 Object retObject; 146 147 registry.generateAndRegisterAllDottedNames(configContext, "abc.def.xyz"); 148 if(configContext!=null) 149 return; 150 153 title("REGISTRY (in XPath value order)"); 154 registry.sortRegistryEntries(registry.SORT_BY_XPATH); 155 156 String [] location; 159 BaseAdminMBean mbean; 160 AttributeList attrs; 161 162 title("ejb-container INSTANTIATION"); 164 location = new String []{"testdomain", "server-config"}; 165 mbean = registry.instantiateMBean("ejb-container", location, null, configContext); 166 println(""+mbean.getAttribute("cache_resize_quantity")); 167 168 title("ejb-container set steady_pool_size to 20"); 170 printAllAttributes("************BEFORE SET ******", " ", mbean); 171 172 mbean.setAttribute(new Attribute("steady_pool_size", (Object )"20")); 173 printAllAttributes("\n************AFTER SET******", " ", mbean); 174 175 mbean.setAttribute(new Attribute("steady_pool_size", (Object )"2")); 176 177 title("resources Instantiate()"); 179 location = new String []{"testdomain"}; 180 mbean = registry.instantiateMBean("resources", location, null, configContext); 181 182 title("resources-> getJdbcResource()"); 184 retObject = mbean.invoke("getJdbcResource", null, null); printObj("Returned object:", " ", retObject); 186 187 title("jdbc-resource-> Instantiate(jdbc/PointBase)"); 189 mbean = registry.instantiateMBean("jdbc-resource", new String []{"testdomain","jdbc/__TimerPool"}, null, configContext); 190 println("returned jdbc-resource[jdbc/PointBase] mbean:"+mbean); 191 192 title("jdbc-resource[jdbc/PointBase]-> getAttributes()"); 194 195 title("resources Instantiate()"); 197 location = new String []{"testdomain"}; 198 mbean = registry.instantiateMBean("resources", location, null, configContext); 199 200 title("resources-> getCustomResource()"); 202 retObject = mbean.invoke("getCustomResource", null, null); printObj("Returned object:", " ", retObject); 204 205 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}, new String []{attrs.getClass().getName()}); 215 printObj("Returned object:", " ", retObject); 216 217 title("resources-> getCustomResourceByJndiName(testJndiName2)"); 219 retObject = mbean.invoke("getCustomResourceByJndiName", new Object []{"testJndiName2"}, new String []{"java.lang.String"}); 220 printObj("Returned object:", " ", retObject); 221 222 title("resources-> getCustomResource()"); 224 retObject = mbean.invoke("getCustomResource", null, null); printObj("Returned object:", " ", retObject); 226 227 title("custom-resource-> Instantiate(testJndiName2)"); 229 mbean = registry.instantiateMBean("custom-resource", new String []{"testdomain","testJndiName2"}, null, configContext); 230 printObj("returned custom-resource[testJndiName2] mbean:",mbean); 231 232 title("custom-resource[testJndiName2]-> getAttributes()"); 234 printAllAttributes("Attributes:", " ", mbean); 235 236 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 title("custom-resource[testJndiName2]-> getAttributes()"); 243 printAllAttributes("Attributes:", " ", mbean); 244 255 260 title("resources-> getCustomResource()"); 261 location = new String []{"testdomain"}; 262 mbean = registry.instantiateMBean("resources", location, null, configContext); 263 retObject = mbean.invoke("getCustomResource", null, null); printObj("Returned object:"," ",retObject); 265 title("resources-> removeCustomResourceByJndiName(testJndiName2)"); 266 retObject = mbean.invoke("removeCustomResourceByJndiName", new Object []{"testJndiName2"}, new String []{"java.lang.String"}); 267 printObj("Returned object:", " ", retObject); 268 269 title("resources-> getCustomResource()"); 271 location = new String []{"testdomain"}; 272 mbean = registry.instantiateMBean("resources", location, null, configContext); 273 retObject = mbean.invoke("getCustomResource", null, null); printObj("Returned object:"," ",retObject); 275 276 title("applications-> Instantantiate()"); 278 location = new String []{"testdomain"}; 279 mbean = registry.instantiateMBean("applications", location, null, configContext); 280 281 298 title("thread-pools Instantiate()"); 300 location = new String []{"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 []{attrs}, new String []{attrs.getClass().getName()}); 312 printObj("Returned object:", " ", retObject); 313 314 title("jdbc-connection-pool[PointBasePool] Instantiate()"); 316 location = new String []{"testdomain", "PointBasePool"}; 317 mbean = registry.instantiateMBean("jdbc-connection-pool", location, null, configContext); 318 printObj("Returned object:"," ",mbean); 319 title("jdbc-connection-pool[PointBasePool] PrintAllAttributes"); 321 printAllAttributes("Attributes:", " ", mbean); 322 title("jdbc-connection-pool[PointBasePool] PrintAllProperties"); 324 printAllProperties("Properties:", " ", mbean); 325 title("set properties: 'testPropName1' and 'testPropName2'"); 327 setConfigMbeanProperty("testPropName1", "testPropValue1", mbean); 328 setConfigMbeanProperty("testPropName2", "testPropValue2", mbean); 329 title("jdbc-connection-pool[PointBasePool] PrintAllProperties"); 331 printAllProperties("Properties:", " ", mbean); 332 title("remove properties: 'testPropName1' and 'testPropName2'"); 334 setConfigMbeanProperty("testPropName1", null, mbean); 335 setConfigMbeanProperty("testPropName2", null, mbean); 336 title("jdbc-connection-pool[PointBasePool] PrintAllProperties"); 338 printAllProperties("Properties:", " ", mbean); 339 title("jdbc-connection-pool.getDefaultAttributeValue[max-pool-size]"); 341 println("Value:"+mbean.getAttribute("max_pool_size")); 342 retObject = mbean.invoke("getDefaultAttributeValue", new Object []{"max_pool_size"}, new String []{"java.lang.String"}); 343 println("Default:"+retObject); 344 title("thread-pools Instantiate()"); 346 location = new String []{"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 []{"abc","def","xyz"})); 353 printObj("Value:", " ", mbean.getAttribute("jvm_options")); 354 title("auth-realm['file'] Instantiate()"); 356 location = new String []{"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 [] groups = new String []{"testGroup1","testGroup2","testGroup3"}; 364 retObject = mbean.invoke("updateUser", new Object []{"testUser", "testPassword", groups}, new String []{"java.lang.String", "java.lang.String", groups.getClass().getName()}); 365 printObj("updateUser:Returned object:", " ", retObject); 366 retObject = mbean.invoke("getUserGroupNames", new Object []{"testUser"}, new String []{"java.lang.String"}); 367 printObj("getUserGroupNames:Returned object:", " ", retObject); 368 retObject = mbean.invoke("getGroupNames", new Object []{}, new String []{}); 369 printObj("getGroupNames:Returned object:", " ", retObject); 370 retObject = mbean.invoke("getUserNames", new Object []{}, new String []{}); 371 printObj("getUserNames:Returned object:", " ", retObject); 372 title("resources-> getCustomResource()"); 373 title("domain Instantiate()"); 375 location = new String []{"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 Exception e = mbe.getTargetException(); 387 if(e instanceof InvocationTargetException ) 388 { 389 InvocationTargetException ite = (InvocationTargetException )e; 390 Throwable 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 e) 401 { 402 if(e instanceof InvocationTargetException ) 403 { 404 InvocationTargetException ite = (InvocationTargetException )e; 405 Throwable 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 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 retObject; 431 432 title("REGISTRY"); 434 println(registry.toString()); 435 String [] location; 436 BaseAdminMBean mbean; 437 AttributeList attrs; 438 439 } 440 458 catch (Exception e) 459 { 460 if(e instanceof InvocationTargetException ) 461 { 462 InvocationTargetException ite = (InvocationTargetException )e; 463 Throwable 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 t) 474 { 475 println(t.getMessage()); 476 t.printStackTrace(); 477 } 478 println("Bye....!"); 479 } 480 481 private static void title(String str) 482 { 483 System.out.println("\n\n************* " + str + " *******************"); 484 } 485 private static void println(String str) 486 { 487 System.out.println(str); 488 } 489 490 private static void printObj(String title, String pref, Object obj) 491 { 492 println(title); 493 printObj(pref, obj); 494 } 495 496 private static void printObj(String pref, Object obj) 497 { 498 if(pref==null) 499 pref=""; 500 if(obj==null) 501 println(pref+"null"); 502 if(obj instanceof Object []) 503 { 504 Object [] objs = (Object [])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, 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 title, String pref, DynamicMBean mbean) 533 { 534 printObj(title, pref, mbean.getAttributes(new String []{""})); 535 } 536 537 private static void printAllProperties(String title, String pref, DynamicMBean mbean) throws Exception 538 { 539 Object ret = mbean.invoke("getProperties", null, null); 540 printObj(title, pref, ret); 542 } 543 private static Object getConfigMbeanProperty(String name, DynamicMBean mbean) throws Exception 544 { 545 return mbean.invoke("getPropertyValue", new Object []{name}, new String []{"java.lang.String"}); 546 } 547 private static void setConfigMbeanProperty(String name, Object value, DynamicMBean mbean) throws Exception 548 { 549 Object ret = mbean.invoke("setProperty", new Object []{new Attribute(name, value)}, new String []{"javax.management.Attribute"}); 550 } 551 } 552 | Popular Tags |