1 17 18 19 package org.apache.catalina.ant.jmx; 20 21 22 import javax.management.Attribute ; 23 import javax.management.MBeanAttributeInfo ; 24 import javax.management.MBeanInfo ; 25 import javax.management.MBeanServerConnection ; 26 import javax.management.ObjectName ; 27 28 import org.apache.tools.ant.BuildException; 29 30 31 64 65 public class JMXAccessorSetTask extends JMXAccessorTask { 66 67 69 private String attribute; 70 private String value; 71 private String type; 72 private boolean convert = false ; 73 74 76 79 private static final String info = "org.apache.catalina.ant.JMXAccessorSetTask/1.0"; 80 81 86 public String getInfo() { 87 88 return (info); 89 90 } 91 92 94 97 public String getAttribute() { 98 return attribute; 99 } 100 101 104 public void setAttribute(String attribute) { 105 this.attribute = attribute; 106 } 107 108 111 public String getValue() { 112 return value; 113 } 114 117 public void setValue(String value) { 118 this.value = value; 119 } 120 121 122 125 public String getType() { 126 return type; 127 } 128 129 132 public void setType(String valueType) { 133 this.type = valueType; 134 } 135 136 137 140 public boolean isConvert() { 141 return convert; 142 } 143 146 public void setConvert(boolean convert) { 147 this.convert = convert; 148 } 149 151 159 public String jmxExecute(MBeanServerConnection jmxServerConnection) 160 throws Exception { 161 162 if (getName() == null) { 163 throw new BuildException("Must specify a 'name'"); 164 } 165 if ((attribute == null || value == null)) { 166 throw new BuildException( 167 "Must specify a 'attribute' and 'value' for set"); 168 } 169 return jmxSet(jmxServerConnection, getName()); 170 } 171 172 177 protected String jmxSet(MBeanServerConnection jmxServerConnection, 178 String name) throws Exception { 179 Object realValue; 180 if (type != null) { 181 realValue = convertStringToType(value, type); 182 } else { 183 if (isConvert()) { 184 String mType = getMBeanAttributeType(jmxServerConnection, name, 185 attribute); 186 realValue = convertStringToType(value, mType); 187 } else 188 realValue = value; 189 } 190 jmxServerConnection.setAttribute(new ObjectName (name), new Attribute ( 191 attribute, realValue)); 192 return null; 193 } 194 195 196 197 205 protected String getMBeanAttributeType( 206 MBeanServerConnection jmxServerConnection, 207 String name, 208 String attribute) throws Exception { 209 ObjectName oname = new ObjectName (name); 210 String mattrType = null; 211 MBeanInfo minfo = jmxServerConnection.getMBeanInfo(oname); 212 MBeanAttributeInfo attrs[] = minfo.getAttributes(); 213 if (attrs != null) { 214 for (int i = 0; mattrType == null && i < attrs.length; i++) { 215 if (attribute.equals(attrs[i].getName())) 216 mattrType = attrs[i].getType(); 217 } 218 } 219 return mattrType; 220 } 221 } 222 | Popular Tags |