Search in sources :

Example 11 with ManagedBean

use of org.apache.tomcat.util.modeler.ManagedBean in project tomcat by apache.

the class MbeansDescriptorsIntrospectionSource method createManagedBean.

/**
     * XXX Find if the 'className' is the name of the MBean or
     *       the real class ( I suppose first )
     * XXX Read (optional) descriptions from a .properties, generated
     *       from source
     * XXX Deal with constructors
     *
     * @param registry The Bean registry (not used)
     * @param domain The bean domain (not used)
     * @param realClass The class to analyze
     * @param type The bean type
     * @return ManagedBean The create MBean
     */
public ManagedBean createManagedBean(Registry registry, String domain, Class<?> realClass, String type) {
    ManagedBean mbean = new ManagedBean();
    Method[] methods = null;
    Hashtable<String, Method> attMap = new Hashtable<>();
    // key: attribute val: getter method
    Hashtable<String, Method> getAttMap = new Hashtable<>();
    // key: attribute val: setter method
    Hashtable<String, Method> setAttMap = new Hashtable<>();
    // key: operation val: invoke method
    Hashtable<String, Method> invokeAttMap = new Hashtable<>();
    methods = realClass.getMethods();
    initMethods(realClass, methods, attMap, getAttMap, setAttMap, invokeAttMap);
    try {
        Enumeration<String> en = attMap.keys();
        while (en.hasMoreElements()) {
            String name = en.nextElement();
            AttributeInfo ai = new AttributeInfo();
            ai.setName(name);
            Method gm = getAttMap.get(name);
            if (gm != null) {
                //ai.setGetMethodObj( gm );
                ai.setGetMethod(gm.getName());
                Class<?> t = gm.getReturnType();
                if (t != null)
                    ai.setType(t.getName());
            }
            Method sm = setAttMap.get(name);
            if (sm != null) {
                //ai.setSetMethodObj(sm);
                Class<?> t = sm.getParameterTypes()[0];
                if (t != null)
                    ai.setType(t.getName());
                ai.setSetMethod(sm.getName());
            }
            ai.setDescription("Introspected attribute " + name);
            if (log.isDebugEnabled())
                log.debug("Introspected attribute " + name + " " + gm + " " + sm);
            if (gm == null)
                ai.setReadable(false);
            if (sm == null)
                ai.setWriteable(false);
            if (sm != null || gm != null)
                mbean.addAttribute(ai);
        }
        for (Entry<String, Method> entry : invokeAttMap.entrySet()) {
            String name = entry.getKey();
            Method m = entry.getValue();
            if (m != null) {
                OperationInfo op = new OperationInfo();
                op.setName(name);
                op.setReturnType(m.getReturnType().getName());
                op.setDescription("Introspected operation " + name);
                Class<?>[] parms = m.getParameterTypes();
                for (int i = 0; i < parms.length; i++) {
                    ParameterInfo pi = new ParameterInfo();
                    pi.setType(parms[i].getName());
                    pi.setName("param" + i);
                    pi.setDescription("Introspected parameter param" + i);
                    op.addParameter(pi);
                }
                mbean.addOperation(op);
            } else {
                log.error("Null arg method for [" + name + "]");
            }
        }
        if (log.isDebugEnabled())
            log.debug("Setting name: " + type);
        mbean.setName(type);
        return mbean;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
Also used : OperationInfo(org.apache.tomcat.util.modeler.OperationInfo) Hashtable(java.util.Hashtable) Method(java.lang.reflect.Method) ParameterInfo(org.apache.tomcat.util.modeler.ParameterInfo) AttributeInfo(org.apache.tomcat.util.modeler.AttributeInfo) ManagedBean(org.apache.tomcat.util.modeler.ManagedBean)

Example 12 with ManagedBean

use of org.apache.tomcat.util.modeler.ManagedBean in project tomcat by apache.

the class MbeansDescriptorsIntrospectionSource method execute.

public void execute() throws Exception {
    if (registry == null)
        registry = Registry.getRegistry(null, null);
    try {
        ManagedBean managed = createManagedBean(registry, null, (Class<?>) source, type);
        if (managed == null)
            return;
        managed.setName(type);
        registry.addManagedBean(managed);
    } catch (Exception ex) {
        log.error("Error reading descriptors ", ex);
    }
}
Also used : ManagedBean(org.apache.tomcat.util.modeler.ManagedBean)

Example 13 with ManagedBean

use of org.apache.tomcat.util.modeler.ManagedBean in project tomcat by apache.

the class NamingResourcesMBean method addResourceLink.

/**
     * Add a resource link reference for this web application.
     *
     * @param resourceLinkName New resource link reference name
     * @param type New resource link reference type
     * @return the object name of the new resource link
     * @throws MalformedObjectNameException if the object name was invalid
     */
public String addResourceLink(String resourceLinkName, String type) throws MalformedObjectNameException {
    NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
    if (nresources == null) {
        return null;
    }
    ContextResourceLink resourceLink = nresources.findResourceLink(resourceLinkName);
    if (resourceLink != null) {
        throw new IllegalArgumentException("Invalid resource link name - already exists'" + resourceLinkName + "'");
    }
    resourceLink = new ContextResourceLink();
    resourceLink.setName(resourceLinkName);
    resourceLink.setType(type);
    nresources.addResourceLink(resourceLink);
    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("ContextResourceLink");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resourceLink);
    return oname.toString();
}
Also used : ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ManagedBean(org.apache.tomcat.util.modeler.ManagedBean) ObjectName(javax.management.ObjectName)

Example 14 with ManagedBean

use of org.apache.tomcat.util.modeler.ManagedBean in project tomcat by apache.

the class NamingResourcesMBean method addEnvironment.

// ------------------------------------------------------------- Operations
/**
     * Add an environment entry for this web application.
     *
     * @param envName New environment entry name
     * @param type The type of the new environment entry
     * @param value The value of the new environment entry
     * @return the object name of the new environment entry
     * @throws MalformedObjectNameException if the object name was invalid
     */
public String addEnvironment(String envName, String type, String value) throws MalformedObjectNameException {
    NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
    if (nresources == null) {
        return null;
    }
    ContextEnvironment env = nresources.findEnvironment(envName);
    if (env != null) {
        throw new IllegalArgumentException("Invalid environment name - already exists '" + envName + "'");
    }
    env = new ContextEnvironment();
    env.setName(envName);
    env.setType(type);
    env.setValue(value);
    nresources.addEnvironment(env);
    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("ContextEnvironment");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), env);
    return oname.toString();
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ManagedBean(org.apache.tomcat.util.modeler.ManagedBean) ObjectName(javax.management.ObjectName)

Example 15 with ManagedBean

use of org.apache.tomcat.util.modeler.ManagedBean in project tomcat by apache.

the class NamingResourcesMBean method addResource.

/**
     * Add a resource reference for this web application.
     *
     * @param resourceName New resource reference name
     * @param type New resource reference type
     * @return the object name of the new resource
     * @throws MalformedObjectNameException if the object name was invalid
     */
public String addResource(String resourceName, String type) throws MalformedObjectNameException {
    NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
    if (nresources == null) {
        return null;
    }
    ContextResource resource = nresources.findResource(resourceName);
    if (resource != null) {
        throw new IllegalArgumentException("Invalid resource name - already exists'" + resourceName + "'");
    }
    resource = new ContextResource();
    resource.setName(resourceName);
    resource.setType(type);
    nresources.addResource(resource);
    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("ContextResource");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resource);
    return oname.toString();
}
Also used : NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ManagedBean(org.apache.tomcat.util.modeler.ManagedBean) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) ObjectName(javax.management.ObjectName)

Aggregations

ManagedBean (org.apache.tomcat.util.modeler.ManagedBean)19 ObjectName (javax.management.ObjectName)16 DynamicMBean (javax.management.DynamicMBean)7 MBeanException (javax.management.MBeanException)7 MalformedObjectNameException (javax.management.MalformedObjectNameException)7 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)3 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)1 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)1 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)1 AttributeInfo (org.apache.tomcat.util.modeler.AttributeInfo)1 OperationInfo (org.apache.tomcat.util.modeler.OperationInfo)1 ParameterInfo (org.apache.tomcat.util.modeler.ParameterInfo)1