1 17 18 package org.apache.catalina.ant.jmx; 19 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 23 import javax.management.MBeanServerConnection ; 24 import javax.management.ObjectName ; 25 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.ProjectComponent; 28 import org.apache.tools.ant.taskdefs.condition.Condition; 29 30 93 public class JMXAccessorCondition extends ProjectComponent implements Condition { 94 95 97 private String url = null; 98 private String host = "localhost"; 99 private String port = "8050"; 100 private String password = null; 101 private String username = null; 102 private String name = null; 103 private String attribute; 104 private String value; 105 private String operation = "==" ; 106 private String type = "long" ; 107 private String ref = "jmx.server"; 108 private String unlessCondition; 109 private String ifCondition; 110 111 113 116 private static final String info = "org.apache.catalina.ant.JMXAccessorCondition/1.1"; 117 118 123 public String getInfo() { 124 125 return (info); 126 127 } 128 130 133 public String getOperation() { 134 return operation; 135 } 136 139 public void setOperation(String operation) { 140 this.operation = operation; 141 } 142 143 146 public String getType() { 147 return type; 148 } 149 152 public void setType(String type) { 153 this.type = type; 154 } 155 158 public String getAttribute() { 159 return attribute; 160 } 161 164 public void setAttribute(String attribute) { 165 this.attribute = attribute; 166 } 167 170 public String getHost() { 171 return host; 172 } 173 176 public void setHost(String host) { 177 this.host = host; 178 } 179 182 public String getName() { 183 return name; 184 } 185 188 public void setName(String objectName) { 189 this.name = objectName; 190 } 191 194 public String getPassword() { 195 return password; 196 } 197 200 public void setPassword(String password) { 201 this.password = password; 202 } 203 206 public String getPort() { 207 return port; 208 } 209 212 public void setPort(String port) { 213 this.port = port; 214 } 215 218 public String getUrl() { 219 return url; 220 } 221 224 public void setUrl(String url) { 225 this.url = url; 226 } 227 230 public String getUsername() { 231 return username; 232 } 233 236 public void setUsername(String username) { 237 this.username = username; 238 } 239 242 public String getValue() { 243 return value; 244 } 245 public void setValue(String value) { 247 this.value = value; 248 } 249 250 253 public String getRef() { 254 return ref; 255 } 256 259 public void setRef(String refId) { 260 this.ref = refId; 261 } 262 265 public String getIf() { 266 return ifCondition; 267 } 268 272 public void setIf(String c) { 273 ifCondition = c; 274 } 275 278 public String getUnless() { 279 return unlessCondition; 280 } 281 282 287 public void setUnless(String c) { 288 unlessCondition = c; 289 } 290 291 297 protected MBeanServerConnection getJMXConnection() 298 throws MalformedURLException , IOException { 299 return JMXAccessorTask.accessJMXConnection( 300 getProject(), 301 getUrl(), getHost(), 302 getPort(), getUsername(), getPassword(), ref); 303 } 304 305 309 protected String accessJMXValue() { 310 try { 311 Object result = getJMXConnection().getAttribute( 312 new ObjectName (name), attribute); 313 if(result != null) 314 return result.toString(); 315 } catch (Exception e) { 316 } 318 return null; 319 } 320 321 325 protected boolean testIfCondition() { 326 if (ifCondition == null || "".equals(ifCondition)) { 327 return true; 328 } 329 return getProject().getProperty(ifCondition) != null; 330 } 331 332 337 protected boolean testUnlessCondition() { 338 if (unlessCondition == null || "".equals(unlessCondition)) { 339 return true; 340 } 341 return getProject().getProperty(unlessCondition) == null; 342 } 343 344 349 public boolean eval() { 350 if (operation == null) { 351 throw new BuildException("operation attribute is not set"); 352 } 353 if (value == null) { 354 throw new BuildException("value attribute is not set"); 355 } 356 if ((name == null || attribute == null)) { 357 throw new BuildException( 358 "Must specify a 'attribute', name for equals condition"); 359 } 360 if (testIfCondition() && testUnlessCondition()) { 361 String jmxValue = accessJMXValue(); 362 if (jmxValue != null) { 363 String op = getOperation(); 364 if ("==".equals(op)) { 365 return jmxValue.equals(value); 366 } else if ("!=".equals(op)) { 367 return !jmxValue.equals(value); 368 } else { 369 if ("long".equals(type)) { 370 long jvalue = Long.parseLong(jmxValue); 371 long lvalue = Long.parseLong(value); 372 if (">".equals(op)) { 373 return jvalue > lvalue; 374 } else if (">=".equals(op)) { 375 return jvalue >= lvalue; 376 } else if ("<".equals(op)) { 377 return jvalue < lvalue; 378 } else if ("<=".equals(op)) { 379 return jvalue <= lvalue; 380 } 381 } else if ("double".equals(type)) { 382 double jvalue = Double.parseDouble(jmxValue); 383 double dvalue = Double.parseDouble(value); 384 if (">".equals(op)) { 385 return jvalue > dvalue; 386 } else if (">=".equals(op)) { 387 return jvalue >= dvalue; 388 } else if ("<".equals(op)) { 389 return jvalue < dvalue; 390 } else if ("<=".equals(op)) { 391 return jvalue <= dvalue; 392 } 393 } 394 } 395 } 396 return false; 397 } 398 return true; 399 } 400 } 401 402 | Popular Tags |