1 17 18 19 package org.apache.catalina.ant.jmx; 20 21 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import javax.management.MBeanAttributeInfo ; 26 import javax.management.MBeanInfo ; 27 import javax.management.MBeanServerConnection ; 28 import javax.management.ObjectName ; 29 30 import org.apache.tools.ant.BuildException; 31 32 33 62 63 public class JMXAccessorQueryTask extends JMXAccessorTask { 64 65 67 private boolean attributebinding = false; 68 69 71 74 private static final String info = "org.apache.catalina.ant.JMXAccessorQueryTask/1.0"; 75 76 81 public String getInfo() { 82 83 return (info); 84 85 } 86 87 89 92 public boolean isAttributebinding() { 93 return attributebinding; 94 } 95 98 public void setAttributebinding(boolean attributeBinding) { 99 this.attributebinding = attributeBinding; 100 } 101 102 104 105 113 public String jmxExecute(MBeanServerConnection jmxServerConnection) 114 throws Exception { 115 116 if (getName() == null) { 117 throw new BuildException("Must specify a 'name'"); 118 } 119 return jmxQuery(jmxServerConnection, getName()); 120 121 } 122 123 124 132 protected String jmxQuery(MBeanServerConnection jmxServerConnection, 133 String qry) { 134 String isError = null; 135 Set names = null; 136 String resultproperty = getResultproperty(); 137 try { 138 names = jmxServerConnection.queryNames(new ObjectName (qry), null); 139 if (resultproperty != null) { 140 setProperty(resultproperty + ".Length",Integer.toString(names.size())); 141 } 142 } catch (Exception e) { 143 if (isEcho()) 144 handleErrorOutput(e.getMessage()); 145 return "Can't query mbeans " + qry; 146 } 147 148 if (resultproperty != null) { 149 Iterator it = names.iterator(); 150 int oindex = 0; 151 String pname = null; 152 while (it.hasNext()) { 153 ObjectName oname = (ObjectName ) it.next(); 154 pname = resultproperty + "." + Integer.toString(oindex) + "."; 155 oindex++; 156 setProperty(pname + "Name", oname.toString()); 157 if (isAttributebinding()) { 158 bindAttributes(jmxServerConnection, resultproperty, pname, oname); 159 160 } 161 } 162 } 163 return isError; 164 } 165 166 172 protected void bindAttributes(MBeanServerConnection jmxServerConnection, String resultproperty, String pname, ObjectName oname) { 173 if (jmxServerConnection != null && resultproperty != null 174 && pname != null && oname != null ) { 175 try { 176 MBeanInfo minfo = jmxServerConnection.getMBeanInfo(oname); 177 String code = minfo.getClassName(); 178 if ("org.apache.tomcat.util.modeler.BaseModelMBean".equals(code)) { 179 code = (String ) jmxServerConnection.getAttribute(oname, 180 "modelerType"); 181 } 182 MBeanAttributeInfo attrs[] = minfo.getAttributes(); 183 Object value = null; 184 185 for (int i = 0; i < attrs.length; i++) { 186 if (!attrs[i].isReadable()) 187 continue; 188 String attName = attrs[i].getName(); 189 if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 190 || attName.indexOf(" ") >= 0) { 191 continue; 192 } 193 194 try { 195 value = jmxServerConnection 196 .getAttribute(oname, attName); 197 } catch (Throwable t) { 198 if (isEcho()) 199 handleErrorOutput("Error getting attribute " 200 + oname + " " + pname + attName + " " 201 + t.toString()); 202 continue; 203 } 204 if (value == null) 205 continue; 206 if ("modelerType".equals(attName)) 207 continue; 208 createProperty(pname + attName, value); 209 } 210 } catch (Exception e) { 211 } 213 } 214 } 215 } 216 | Popular Tags |