1 22 package org.jboss.ant; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.Properties ; 29 import javax.management.Attribute ; 30 import javax.management.MBeanServerConnection ; 31 import javax.management.ObjectName ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Task; 36 import org.jboss.util.propertyeditor.PropertyEditors; 37 38 70 public class JMX extends Task 71 { 72 private String serverURL; 73 74 private String adapterName = "jmx/invoker/RMIAdaptor"; 75 76 private List ops = new ArrayList (); 77 78 private List editors = new ArrayList (); 79 80 87 public JMX() throws Exception 88 { 89 } 90 91 97 public void setServerURL(String serverURL) 98 { 99 this.serverURL = serverURL; 100 } 101 102 108 public void setAdapterName(String adapterName) 109 { 110 this.adapterName = adapterName; 111 } 112 113 120 public void addInvoke(Invoke invoke) 121 { 122 ops.add(invoke); 123 } 124 125 133 public void addSetAttribute(Setter setter) 134 { 135 ops.add(setter); 136 } 137 138 146 public void addGetAttribute(Getter getter) 147 { 148 ops.add(getter); 149 } 150 151 158 public void addPropertyEditor(PropertyEditorHolder peh) 159 { 160 editors.add(peh); 161 } 162 163 168 public void execute() throws BuildException 169 { 170 final ClassLoader origCL = Thread.currentThread().getContextClassLoader(); 171 try 172 { 173 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 174 try 175 { 176 for (int i = 0; i < editors.size(); i++) 177 { 178 ((PropertyEditorHolder)editors.get(i)).execute(); 179 } 181 182 } 183 catch (Exception e) 184 { 185 e.printStackTrace(); 186 throw new BuildException("Could not register property editors: " + e); 187 } 189 try 190 { 191 Properties props = new Properties (); 192 props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 193 props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); 194 195 if (serverURL == null || "".equals(serverURL)) 196 { 197 props.put(Context.PROVIDER_URL, "jnp://localhost:1099"); 198 } 199 else 200 { 201 props.put(Context.PROVIDER_URL, serverURL); 202 } 203 InitialContext ctx = new InitialContext (props);; 204 205 if (adapterName == null) { 207 adapterName = "jmx/rmi/RMIAdaptor"; } 209 210 Object obj = ctx.lookup(adapterName); 211 ctx.close(); 212 213 if (!(obj instanceof MBeanServerConnection )) { 214 throw new ClassCastException 215 ("Object not of type: MBeanServerConnection, but: " + 216 (obj == null ? "not found" : obj.getClass().getName())); 217 } 218 219 MBeanServerConnection server = (MBeanServerConnection ) obj; 220 221 for (int i = 0; i < ops.size(); i++) 222 { 223 Operation op = (Operation)ops.get(i); 224 op.execute(server, this); 225 } 227 228 } 229 catch (Exception e) 230 { 231 e.printStackTrace(); 232 throw new BuildException("problem: " + e); 233 } } 235 finally 236 { 237 Thread.currentThread().setContextClassLoader(origCL); 238 } 239 240 } 241 242 247 public static interface Operation 248 { 249 void execute(MBeanServerConnection server, Task parent) throws Exception ; 250 } 251 252 257 public static class Invoke 258 implements Operation 259 { 260 private ObjectName target; 261 private String property; 262 263 private String operation; 264 265 private List params = new ArrayList (); 266 267 273 public void setProperty(String property) 274 { 275 this.property = property; 276 } 277 278 284 public void setTarget(ObjectName target) 285 { 286 this.target = target; 287 } 288 289 295 public void setOperation(String operation) 296 { 297 this.operation = operation; 298 } 299 300 306 public void addParameter(Param param) 307 { 308 params.add(param); 309 } 310 311 public void execute(MBeanServerConnection server, Task parent) throws Exception 312 { 313 int paramCount = params.size(); 314 Object [] args = new Object [paramCount]; 315 String [] types = new String [paramCount]; 316 int pos = 0; 317 for (int i = 0; i < params.size(); i++) 318 { 319 Param p = (Param)params.get(i); 320 args[pos] = p.getValue(); 321 types[pos] = p.getType(); 322 pos++; 323 } Object result = server.invoke(target, operation, args, types); 325 if( (property != null) && (result != null) ) 326 { 327 parent.getProject().setProperty(property,result.toString()); 328 } 329 } 330 } 331 332 337 public static class Setter 338 implements Operation 339 { 340 private ObjectName target; 341 342 private String attribute; 343 344 private Param value; 345 346 352 public void setTarget(ObjectName target) 353 { 354 this.target = target; 355 } 356 357 362 public void setAttribute(String attribute) 363 { 364 this.attribute = attribute; 365 } 366 367 373 public void setValue(Param value) 374 { 375 this.value = value; 376 } 377 378 public void execute(MBeanServerConnection server, Task parent) throws Exception 379 { 380 Attribute att = new Attribute (attribute, value.getValue()); 381 server.setAttribute(target, att); 382 } 383 } 384 385 390 public static class Getter 391 implements Operation 392 { 393 private ObjectName target; 394 395 private String attribute; 396 397 private String property; 398 399 405 public void setTarget(ObjectName target) 406 { 407 this.target = target; 408 } 409 410 416 public void setAttribute(String attribute) 417 { 418 this.attribute = attribute; 419 } 420 421 427 public void setProperty(String property) 428 { 429 this.property = property; 430 } 431 432 public void execute(MBeanServerConnection server, Task parent) throws Exception 433 { 434 Object result = server.getAttribute(target,attribute); 435 if( (property != null) && (result != null) ) 436 { 437 parent.getProject().setProperty(property,result.toString()); 438 } 439 } 440 } 441 442 447 public static class Param 448 { 449 private String arg; 450 private String type; 451 452 458 public void setArg(String arg) 459 { 460 this.arg = arg; 461 } 462 463 public String getArg() 464 { 465 return arg; 466 } 467 468 474 public void setType(String type) 475 { 476 this.type = type; 477 } 478 479 public String getType() 480 { 481 return type; 482 } 483 484 492 public Object getValue() throws Exception 493 { 494 PropertyEditor editor = PropertyEditors.getEditor(type); 495 editor.setAsText(arg); 496 return editor.getValue(); 497 } 498 } 499 500 505 public static class PropertyEditorHolder 506 { 507 private String type; 508 private String editor; 509 510 516 public void setType(final String type) 517 { 518 this.type = type; 519 } 520 521 public String getType() 522 { 523 return type; 524 } 525 526 private Class getTypeClass() throws ClassNotFoundException 527 { 528 try 530 { 531 return Class.forName(type); 532 } 533 catch (ClassNotFoundException e) 534 { 535 } try 537 { 538 return getClass().getClassLoader().loadClass(type); 539 } 540 catch (ClassNotFoundException e) 541 { 542 } return Thread.currentThread().getContextClassLoader().loadClass(type); 544 } 545 546 552 public void setEditor(final String editor) 553 { 554 this.editor = editor; 555 } 556 557 public String getEditor() 558 { 559 return editor; 560 } 561 562 private Class getEditorClass() throws ClassNotFoundException 563 { 564 try 566 { 567 return Class.forName(editor); 568 } 569 catch (ClassNotFoundException e) 570 { 571 } try 573 { 574 return getClass().getClassLoader().loadClass(editor); 575 } 576 catch (ClassNotFoundException e) 577 { 578 } return Thread.currentThread().getContextClassLoader().loadClass(editor); 580 } 581 582 public void execute() throws ClassNotFoundException 583 { 584 PropertyEditorManager.registerEditor(getTypeClass(), getEditorClass()); 585 } 586 } 587 588 } | Popular Tags |