Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.tomcat.util.modeler.modules;
 
 import java.net.URL;
 import java.util.List;
 
 
This will create mbeans based on a config file. The format is an extended version of MLET. Classloading. We don't support any explicit classloader tag. A ClassLoader is just an mbean ( it can be the standard MLetMBean or a custom one ). XXX add a special attribute to reference the loader mbean, XXX figure out how to deal with private loaders
 
 public class MbeansSource extends ModelerSource implements MbeansSourceMBean
 {
     Registry registry;
     String type;
 
     // true if we are during the original loading
     boolean loading=true;
     List mbeans=new ArrayList();
     static boolean loaderLoaded=false;
     private Document document;
     private HashMap object2Node = new HashMap();
 
     long lastUpdate;
     long updateInterval=10000; // 10s
 
     public void setRegistry(Registry reg) {
         this.=reg;
     }          
 
     public void setLocationString loc ) {
         this.=loc;
     }

    
Used if a single component is loaded

Parameters:
type
 
     public void setTypeString type ) {
        this.=type;
     }
 
     public void setSourceObject source ) {
         this.=source;
     }
 
     public Object getSource() {
         return ;
     }
 
     public String getLocation() {
         return ;
     }
    
    
Return the list of mbeans created by this source. It can be used to implement runtime services.
 
     public List getMBeans() {
        return ;
    }
    public List loadDescriptorsRegistry registryString location,
                                 String typeObject source)
            throws Exception
    {
        setRegistry(registry);
        setLocation(location);
        setType(type);
        setSource(source);
        execute();
        return ;
    }
    
    public void start() throws Exception {
        .invoke("start"false);        
    }
    public void stop() throws Exception {
        .invoke("stop"false);        
    }
    
    public void init() throws Exception {
        if==nullexecute();
        if==null ) =Registry.getRegistry();
        
        .invoke("init"false);
    }
    
    public void destroy() throws Exception {
        .invoke("destroy"false);                
    }
    
    public void load() throws Exception {
        execute(); // backward compat
    }
    public void execute() throws Exception {
        if==null ) =Registry.getRegistry();
        try {
            InputStream stream=getInputStream();
            long t1=System.currentTimeMillis();
             = DomUtil.readXml(stream);
            // We don't care what the root node is.
            Node descriptorsN=.getDocumentElement();
            ifdescriptorsN == null ) {
                ..noDescriptorsFound();
                return;
            }
            Node firstMbeanN=DomUtil.getChild(descriptorsNnull);
            iffirstMbeanN==null ) {
                // maybe we have a single mlet
                if..isDebugEnabled() )
                    ..debug("No child " + descriptorsN);
                firstMbeanN=descriptorsN;
            }
            MBeanServer server=(MBeanServer)Registry.getServer();
            // XXX Not very clean...  Just a workaround
            if( !  ) {
                // Register a loader that will be find ant classes.
                ObjectName defaultLoadernew ObjectName("modeler",
                        "loader""modeler");
                MLet mlet=new MLetnew URL[0], this.getClass().getClassLoader());
                server.registerMBean(mletdefaultLoader);
                =true;
            }
        
            // Process nodes
            for (Node mbeanN = firstMbeanNmbeanN != null;
                 mbeanN= DomUtil.getNext(mbeanNnull.))
            {
                String nodeName=mbeanN.getNodeName();
                // mbean is the "official" name
                if"mbean".equals(nodeName) || "MLET".equals(nodeName) )
                {
                    String code=DomUtil.getAttributembeanN"code" );
                    String objectName=DomUtil.getAttributembeanN"objectName" );
                    ifobjectName==null ) {
                        objectName=DomUtil.getAttributembeanN"name" );
                    }
                    
                    if..isDebugEnabled())
                        ..debug"Processing mbean objectName=" + objectName +
                                " code=" + code);
                    // args can be grouped in constructor or direct childs
                    Node constructorN=DomUtil.getChild(mbeanN"constructor");
                    ifconstructorN == null ) constructorN=mbeanN;
                    processArg(constructorN);
                    try {
                        ObjectName oname=new ObjectName(objectName);
                        if( ! server.isRegisteredoname )) {
                            // We wrap everything in a model mbean.
                            // XXX need to support "StandardMBeanDescriptorsSource"
                            String modelMBean=BaseModelMBean.class.getName();                            
                            server.createMBean(modelMBeanoname,
                                    new Object[] { codethis},
                                    new String[] { String.class.getName(),
                                                  ModelerSource.class.getName() } 
                                    );
                            .add(oname);
                        }
                        .putonamembeanN );
                        // XXX Arguments, loader !!!
                    } catchException ex ) {
                        ..errorCreatingMbean(objectNameex);
                    }
                    Node firstAttN=DomUtil.getChild(mbeanN"attribute");
                    for (Node descN = firstAttNdescN != null;
                         descN = DomUtil.getNextdescN ))
                    {
                        processAttribute(serverdescNobjectName);
                    }
                } else if("jmx-operation".equals(nodeName) ) {
                    String name=DomUtil.getAttribute(mbeanN"objectName");
                    ifname==null )
                        name=DomUtil.getAttribute(mbeanN"name");
                    String operation=DomUtil.getAttribute(mbeanN"operation");
                    if..isDebugEnabled())
                        ..debug"Processing invoke objectName=" + name +
                                " code=" + operation);
                    try {
                        ObjectName oname=new ObjectName(name);
                        processArgmbeanN );
                        server.invokeonameoperationnullnull);
                    } catch (Exception e) {
                        ..errorInvoking(operationname);
                    }
                }
                ManagedBean managed=new ManagedBean();
                DomUtil.setAttributes(managedmbeanN);
                Node firstN;
                // process attribute info
                firstN=DomUtil.getChildmbeanN"attribute");
                for (Node descN = firstNdescN != null;
                     descN = DomUtil.getNextdescN ))
                {
                    AttributeInfo ci=new AttributeInfo();
                    DomUtil.setAttributes(cidescN);
                    managed.addAttributeci );
                }
            }
            long t2=System.currentTimeMillis();
            ..debug"Reading mbeans  " + (t2-t1));
            =false;
        } catchException ex ) {
        }
    }
    
    public void updateFieldObjectName onameString name
                             Object value )
    {
        if ) return;
        // nothing by default
        //log.info( "XXX UpdateField " + oname + " " + name + " " + value);
        Node n=(Node).getoname );
        ifn == null ) {
            ..nodeNotFound(oname);
            return;
        }
        Node attNode=DomUtil.findChildWithAtt(n"attribute""name"name);
        ifattNode == null ) {
            // found no existing attribute with this name
            attNode=n.getOwnerDocument().createElement("attribute");
            DomUtil.setAttribute(attNode"name"name);
            n.appendChild(attNode);
        } 
        String oldValue=DomUtil.getAttribute(attNode"value");
        ifoldValue != null ) {
            // we'll convert all values to text content
            DomUtil.removeAttributeattNode"value");
        }
        DomUtil.setText(attNodevalue.toString());
        //store();
    }
    
    
Store the mbeans. XXX add a background thread to store it periodically
 
    public void save() {
        // XXX customize no often than ( based on standard descriptor ), etc.
        // It doesn't work very well if we call this on each set att - 
        // the triger will work for the first att, but all others will be delayed
        long time=System.currentTimeMillis();
        if!=null &&
                time -  >  ) {
            =time;
            try {
                FileOutputStream fos=new FileOutputStream();
                DomUtil.writeXml(fos);
            } catch (TransformerException e) {
                ..errorWritingMbeans(e);
            } catch (FileNotFoundException e) {
                ..errorWritingMbeans(e);
            }
        }
    }
    private void processAttribute(MBeanServer server,
                                  Node descNString objectName ) {
        String attName=DomUtil.getAttribute(descN"name");
        String value=DomUtil.getAttribute(descN"value");
        String type=null// DomUtil.getAttribute(descN, "type");
        ifvalue==null ) {
            // The value may be specified as CDATA
            value=DomUtil.getContent(descN);
        }
        try {
            if..isDebugEnabled())
                ..debug("Set attribute " + objectName + " " + attName +
                        " " + value);
            ObjectName oname=new ObjectName(objectName);
            // find the type
            iftype==null )
                type=.getType(  onameattName );
            iftype==null ) {
                ..attributeNotFound(attNameobjectName);
            } else {
                Object valueO=.convertValuetypevalue);
                server.setAttribute(onamenew Attribute(attNamevalueO));
            }
        } catchException ex) {
            ..errorProcessingAttribute(attNamevalueobjectNameex);
        }
    }
    private void processArg(Node mbeanN) {
        Node firstArgN=DomUtil.getChild(mbeanN"arg" );
        // process all args
        for (Node argN = firstArgNargN != null;
             argN = DomUtil.getNextargN ))
        {
            String type=DomUtil.getAttribute(argN"type");
            String value=DomUtil.getAttribute(argN"value");
            ifvalue==null ) {
                // The value may be specified as CDATA
                value=DomUtil.getContent(argN);
            }
        }
    }
New to GrepCode? Check out our FAQ X