1 package org.apache.tools.ant.taskdefs.optional.jmx; 2 3 52 53 54 55 import java.util.Iterator ; 56 import java.util.LinkedList ; 57 import java.util.List ; 58 import javax.management.Attribute ; 59 import javax.management.MBeanAttributeInfo ; 60 import org.apache.tools.ant.BuildException; 61 import org.apache.tools.ant.Project; 62 import org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory; 63 64 65 66 77 public class ConfigureMBeanTask extends AbstractMBeanTask { 78 79 private List setAttributeList = new LinkedList (); private List getAttributeList = new LinkedList (); 82 87 public void addSetAttribute(SetAttribute prop) { 88 setAttributeList.add(prop); 89 } 90 91 96 public void addGetAttribute(GetAttribute prop) { 97 getAttributeList.add(prop); 98 } 99 100 104 public static class SetAttribute { 105 private String name; private StringBuffer value = new StringBuffer (); 107 108 112 public void setName(String name) { 113 this.name = name; 114 } 115 116 120 public void setValue(String value) { 121 this.value.append(value); 122 } 123 124 128 public String getName() { 129 return name; 130 } 131 132 public void addText(String text) { 133 this.value.append(text); 134 } 135 136 public String getValue() { 137 return value.toString(); 138 } 139 } 140 141 146 public static class GetAttribute { 147 String name; String property; 149 150 155 public void setName(String name) { 156 this.name = name; 157 } 158 159 164 public void setProperty(String property) { 165 this.property = property; 166 } 167 168 172 public String getName() { 173 return name; 174 } 175 176 181 public String getProperty() { 182 if ( (property == null) || (property.equals("")) ) { 183 return name; 184 } 185 return property; 186 } 187 } 188 189 190 196 protected void execute(javax.management.MBeanServer mbserver) throws BuildException { 197 198 try { 199 javax.management.ObjectName mbeanName = getObjectName(); 200 201 if (mbserver.isRegistered(mbeanName)) { 202 203 java.util.Map attributeInfo = getAttributes(mbserver.getMBeanInfo(mbeanName)); 204 205 executeForSetAttribute(mbserver,attributeInfo); 208 209 executeForGetAttribute(mbserver); 212 213 } else { 214 throw new BuildException("Cannot find MBean. " + toString()); 215 } 216 } catch (javax.management.InstanceNotFoundException x) { 217 throw new BuildException(x); 218 } catch (javax.management.IntrospectionException x) { 219 throw new BuildException(x); 220 } catch (javax.management.ReflectionException x) { 221 throw new BuildException(x); 222 } catch (javax.management.MalformedObjectNameException x) { 223 throw new BuildException(x); 224 } 225 } 226 227 232 protected void executeForSetAttribute(javax.management.MBeanServer mbserver, 233 java.util.Map attributeInfo) { 234 Iterator setAttrIt = setAttributeList.iterator(); 235 236 while (setAttrIt.hasNext()) { 237 SetAttribute attr = (SetAttribute) setAttrIt.next(); 238 239 try { 240 MBeanAttributeInfo attrInfo = (MBeanAttributeInfo ) attributeInfo.get(attr.getName()); 241 242 Object value = ValueFactory.getInstance().valueOf(getProject().replaceProperties(attr.getValue()),attrInfo.getType()); 243 244 Attribute attribute = new Attribute (attrInfo.getName(),value); 245 246 mbserver.setAttribute(getObjectName(),attribute); 247 248 } catch (Exception x) { 249 x.printStackTrace(); 250 String message = "Cannot set mbean attribute value. attributeName["+attr.getName()+"], attributeProperty["+attr.value+"], " + this.toString(); 251 if (getFailOnError()) { 252 throw new BuildException(message); 253 } else { 254 log("Warning: " + message,Project.MSG_WARN); 255 } 256 } 257 } 258 } 259 260 264 protected void executeForGetAttribute(javax.management.MBeanServer mbserver) { 265 Iterator getAttrIt = getAttributeList.iterator(); 266 while (getAttrIt.hasNext()) { 267 GetAttribute attr = (GetAttribute) getAttrIt.next(); 268 269 try { 270 Object value = mbserver.getAttribute(getObjectName(),attr.getName()); 271 if (value != null) { 272 getProject().setProperty(attr.getProperty(),ValueFactory.toString(value)); 273 } else { 274 getProject().setProperty(attr.getProperty(),""); 275 } 276 } catch (Exception x) { 277 String message = "Cannot get mbean attribute value. attributeName["+attr.getName()+"], property["+attr.getProperty()+"], " + toString(); 278 if (getFailOnError()) { 279 throw new BuildException(message); 280 } else { 281 log("Warning: " + message,Project.MSG_WARN); 282 } 283 } 284 285 } 286 } 287 288 } 289 290 | Popular Tags |