1 package org.apache.tomcat.util.modeler.modules; 2 3 import java.io.FileNotFoundException ; 4 import java.io.FileOutputStream ; 5 import java.io.InputStream ; 6 import java.net.URL ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 11 import javax.management.Attribute ; 12 import javax.management.MBeanServer ; 13 import javax.management.ObjectName ; 14 import javax.management.loading.MLet ; 15 import javax.xml.transform.TransformerException ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.apache.tomcat.util.DomUtil; 20 import org.apache.tomcat.util.modeler.AttributeInfo; 21 import org.apache.tomcat.util.modeler.BaseModelMBean; 22 import org.apache.tomcat.util.modeler.ManagedBean; 23 import org.apache.tomcat.util.modeler.Registry; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Node ; 26 27 28 38 public class MbeansSource extends ModelerSource implements MbeansSourceMBean 39 { 40 private static Log log = LogFactory.getLog(MbeansSource.class); 41 Registry registry; 42 String type; 43 44 boolean loading=true; 46 List mbeans=new ArrayList (); 47 static boolean loaderLoaded=false; 48 private Document document; 49 private HashMap object2Node = new HashMap (); 50 51 long lastUpdate; 52 long updateInterval=10000; 54 public void setRegistry(Registry reg) { 55 this.registry=reg; 56 } 57 58 public void setLocation( String loc ) { 59 this.location=loc; 60 } 61 62 66 public void setType( String type ) { 67 this.type=type; 68 } 69 70 public void setSource( Object source ) { 71 this.source=source; 72 } 73 74 public Object getSource() { 75 return source; 76 } 77 78 public String getLocation() { 79 return location; 80 } 81 82 85 public List getMBeans() { 86 return mbeans; 87 } 88 89 public List loadDescriptors( Registry registry, String location, 90 String type, Object source) 91 throws Exception 92 { 93 setRegistry(registry); 94 setLocation(location); 95 setType(type); 96 setSource(source); 97 execute(); 98 return mbeans; 99 } 100 101 public void start() throws Exception { 102 registry.invoke(mbeans, "start", false); 103 } 104 105 public void stop() throws Exception { 106 registry.invoke(mbeans, "stop", false); 107 } 108 109 public void init() throws Exception { 110 if( mbeans==null) execute(); 111 if( registry==null ) registry=Registry.getRegistry(); 112 113 registry.invoke(mbeans, "init", false); 114 } 115 116 public void destroy() throws Exception { 117 registry.invoke(mbeans, "destroy", false); 118 } 119 120 public void load() throws Exception { 121 execute(); } 123 124 public void execute() throws Exception { 125 if( registry==null ) registry=Registry.getRegistry(); 126 try { 127 InputStream stream=getInputStream(); 128 long t1=System.currentTimeMillis(); 129 document = DomUtil.readXml(stream); 130 131 Node descriptorsN=document.getDocumentElement(); 133 134 if( descriptorsN == null ) { 135 log.error("No descriptors found"); 136 return; 137 } 138 139 Node firstMbeanN=DomUtil.getChild(descriptorsN, null); 140 141 if( firstMbeanN==null ) { 142 if( log.isDebugEnabled() ) 144 log.debug("No child " + descriptorsN); 145 firstMbeanN=descriptorsN; 146 } 147 148 MBeanServer server=(MBeanServer )Registry.getServer(); 149 150 if( ! loaderLoaded ) { 152 ObjectName defaultLoader= new ObjectName ("modeler", 154 "loader", "modeler"); 155 MLet mlet=new MLet ( new URL [0], this.getClass().getClassLoader()); 156 server.registerMBean(mlet, defaultLoader); 157 loaderLoaded=true; 158 } 159 160 for (Node mbeanN = firstMbeanN; mbeanN != null; 162 mbeanN= DomUtil.getNext(mbeanN, null, Node.ELEMENT_NODE)) 163 { 164 String nodeName=mbeanN.getNodeName(); 165 166 if( "mbean".equals(nodeName) || "MLET".equals(nodeName) ) 168 { 169 String code=DomUtil.getAttribute( mbeanN, "code" ); 170 String objectName=DomUtil.getAttribute( mbeanN, "objectName" ); 171 if( objectName==null ) { 172 objectName=DomUtil.getAttribute( mbeanN, "name" ); 173 } 174 175 if( log.isDebugEnabled()) 176 log.debug( "Processing mbean objectName=" + objectName + 177 " code=" + code); 178 179 Node constructorN=DomUtil.getChild(mbeanN, "constructor"); 181 if( constructorN == null ) constructorN=mbeanN; 182 183 processArg(constructorN); 184 185 try { 186 ObjectName oname=new ObjectName (objectName); 187 if( ! server.isRegistered( oname )) { 188 String modelMBean=BaseModelMBean.class.getName(); 191 server.createMBean(modelMBean, oname, 192 new Object [] { code, this}, 193 new String [] { String .class.getName(), 194 ModelerSource.class.getName() } 195 ); 196 mbeans.add(oname); 197 } 198 object2Node.put( oname, mbeanN ); 199 } catch( Exception ex ) { 201 log.error( "Error creating mbean " + objectName, ex); 202 } 203 204 Node firstAttN=DomUtil.getChild(mbeanN, "attribute"); 205 for (Node descN = firstAttN; descN != null; 206 descN = DomUtil.getNext( descN )) 207 { 208 processAttribute(server, descN, objectName); 209 } 210 } else if("jmx-operation".equals(nodeName) ) { 211 String name=DomUtil.getAttribute(mbeanN, "objectName"); 212 if( name==null ) 213 name=DomUtil.getAttribute(mbeanN, "name"); 214 215 String operation=DomUtil.getAttribute(mbeanN, "operation"); 216 217 if( log.isDebugEnabled()) 218 log.debug( "Processing invoke objectName=" + name + 219 " code=" + operation); 220 try { 221 ObjectName oname=new ObjectName (name); 222 223 processArg( mbeanN ); 224 server.invoke( oname, operation, null, null); 225 } catch (Exception e) { 226 log.error( "Error in invoke " + name + " " + operation); 227 } 228 } 229 230 ManagedBean managed=new ManagedBean(); 231 DomUtil.setAttributes(managed, mbeanN); 232 Node firstN; 233 234 firstN=DomUtil.getChild( mbeanN, "attribute"); 236 for (Node descN = firstN; descN != null; 237 descN = DomUtil.getNext( descN )) 238 { 239 AttributeInfo ci=new AttributeInfo(); 240 DomUtil.setAttributes(ci, descN); 241 managed.addAttribute( ci ); 242 } 243 244 } 245 246 long t2=System.currentTimeMillis(); 247 log.info( "Reading mbeans " + (t2-t1)); 248 loading=false; 249 } catch( Exception ex ) { 250 log.error( "Error reading mbeans ", ex); 251 } 252 } 253 254 public void updateField( ObjectName oname, String name, 255 Object value ) 256 { 257 if( loading ) return; 258 Node n=(Node )object2Node.get( oname ); 261 if( n == null ) { 262 log.info( "Node not found " + oname ); 263 return; 264 } 265 Node attNode=DomUtil.findChildWithAtt(n, "attribute", "name", name); 266 if( attNode == null ) { 267 attNode=n.getOwnerDocument().createElement("attribute"); 269 DomUtil.setAttribute(attNode, "name", name); 270 n.appendChild(attNode); 271 } 272 String oldValue=DomUtil.getAttribute(attNode, "value"); 273 if( oldValue != null ) { 274 DomUtil.removeAttribute( attNode, "value"); 276 } 277 DomUtil.setText(attNode, value.toString()); 278 279 } 281 282 285 public void save() { 286 long time=System.currentTimeMillis(); 290 if( location!=null && 291 time - lastUpdate > updateInterval ) { 292 lastUpdate=time; 293 try { 294 FileOutputStream fos=new FileOutputStream (location); 295 DomUtil.writeXml(document, fos); 296 } catch (TransformerException e) { 297 log.error( "Error writing"); 298 } catch (FileNotFoundException e) { 299 log.error( "Error writing" ,e ); 300 } 301 } 302 } 303 304 private void processAttribute(MBeanServer server, 305 Node descN, String objectName ) { 306 String attName=DomUtil.getAttribute(descN, "name"); 307 String value=DomUtil.getAttribute(descN, "value"); 308 String type=null; if( value==null ) { 310 value=DomUtil.getContent(descN); 312 } 313 try { 314 if( log.isDebugEnabled()) 315 log.debug("Set attribute " + objectName + " " + attName + 316 " " + value); 317 ObjectName oname=new ObjectName (objectName); 318 if( type==null ) 320 type=registry.getType( oname, attName ); 321 322 if( type==null ) { 323 log.info("Can't find attribute " + objectName + " " + attName ); 324 325 } else { 326 Object valueO=registry.convertValue( type, value); 327 server.setAttribute(oname, new Attribute (attName, valueO)); 328 } 329 } catch( Exception ex) { 330 log.error("Error processing attribute " + objectName + " " + 331 attName + " " + value, ex); 332 } 333 334 } 335 336 private void processArg(Node mbeanN) { 337 Node firstArgN=DomUtil.getChild(mbeanN, "arg" ); 338 for (Node argN = firstArgN; argN != null; 340 argN = DomUtil.getNext( argN )) 341 { 342 String type=DomUtil.getAttribute(argN, "type"); 343 String value=DomUtil.getAttribute(argN, "value"); 344 if( value==null ) { 345 value=DomUtil.getContent(argN); 347 } 348 } 349 } 350 } 351 | Popular Tags |