Search in sources :

Example 41 with ReflectionException

use of javax.management.ReflectionException in project bamboobsc by billchen198318.

the class HostUtils method getHttpPort.

public static int getHttpPort() {
    int port = 8080;
    MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
    ObjectName name;
    try {
        name = new ObjectName("Catalina", "type", "Server");
        try {
            Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
            Service[] services = server.findServices();
            for (Service service : services) {
                for (Connector connector : service.findConnectors()) {
                    ProtocolHandler protocolHandler = connector.getProtocolHandler();
                    if (protocolHandler instanceof Http11Protocol || protocolHandler instanceof Http11AprProtocol || protocolHandler instanceof Http11NioProtocol) {
                        port = connector.getPort();
                    }
                }
            }
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
        } catch (MBeanException e) {
            e.printStackTrace();
        } catch (ReflectionException e) {
            e.printStackTrace();
        }
    } catch (MalformedObjectNameException e) {
        e.printStackTrace();
    }
    return port;
}
Also used : Connector(org.apache.catalina.connector.Connector) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MalformedObjectNameException(javax.management.MalformedObjectNameException) Http11NioProtocol(org.apache.coyote.http11.Http11NioProtocol) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) InstanceNotFoundException(javax.management.InstanceNotFoundException) Service(org.apache.catalina.Service) Http11AprProtocol(org.apache.coyote.http11.Http11AprProtocol) ObjectName(javax.management.ObjectName) ProtocolHandler(org.apache.coyote.ProtocolHandler) Http11Protocol(org.apache.coyote.http11.Http11Protocol) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer)

Example 42 with ReflectionException

use of javax.management.ReflectionException in project tomcat by apache.

the class BaseModelMBean method invoke.

/**
     * Invoke a particular method on this MBean, and return any returned
     * value.
     *
     * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation will
     * attempt to invoke this method on the MBean itself, or (if not
     * available) on the managed resource object associated with this
     * MBean.</p>
     *
     * @param name Name of the operation to be invoked
     * @param params Array containing the method parameters of this operation
     * @param signature Array containing the class names representing
     *  the signature of this operation
     *
     * @exception MBeanException if the initializer of an object
     *  throws an exception
     * @exception ReflectionException if a Java reflection exception
     *  occurs when invoking a method
     */
@Override
public Object invoke(String name, Object[] params, String[] signature) throws MBeanException, ReflectionException {
    if ((resource instanceof DynamicMBean) && !(resource instanceof BaseModelMBean)) {
        return ((DynamicMBean) resource).invoke(name, params, signature);
    }
    // Validate the input parameters
    if (name == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Method name is null"), "Method name is null");
    if (log.isDebugEnabled())
        log.debug("Invoke " + name);
    Method method = managedBean.getInvoke(name, params, signature, this, resource);
    // Invoke the selected method on the appropriate object
    Object result = null;
    try {
        if (method.getDeclaringClass().isAssignableFrom(this.getClass())) {
            result = method.invoke(this, params);
        } else {
            result = method.invoke(resource, params);
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        log.error("Exception invoking method " + name, t);
        if (t == null)
            t = e;
        if (t instanceof RuntimeException)
            throw new RuntimeOperationsException((RuntimeException) t, "Exception invoking method " + name);
        else if (t instanceof Error)
            throw new RuntimeErrorException((Error) t, "Error invoking method " + name);
        else
            throw new MBeanException((Exception) t, "Exception invoking method " + name);
    } catch (Exception e) {
        log.error("Exception invoking method " + name, e);
        throw new MBeanException(e, "Exception invoking method " + name);
    }
    // FIXME - should we validate the return type?
    return (result);
}
Also used : DynamicMBean(javax.management.DynamicMBean) RuntimeErrorException(javax.management.RuntimeErrorException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 43 with ReflectionException

use of javax.management.ReflectionException in project tomcat by apache.

the class ManagedBean method getInvoke.

public Method getInvoke(String aname, Object[] params, String[] signature, BaseModelMBean bean, Object resource) throws MBeanException, ReflectionException {
    Method method = null;
    if (params == null)
        params = new Object[0];
    if (signature == null)
        signature = new String[0];
    if (params.length != signature.length)
        throw new RuntimeOperationsException(new IllegalArgumentException("Inconsistent arguments and signature"), "Inconsistent arguments and signature");
    // Acquire the ModelMBeanOperationInfo information for
    // the requested operation
    OperationInfo opInfo = operations.get(createOperationKey(aname, signature));
    if (opInfo == null)
        throw new MBeanException(new ServiceNotFoundException("Cannot find operation " + aname), "Cannot find operation " + aname);
    // Prepare the signature required by Java reflection APIs
    // FIXME - should we use the signature from opInfo?
    Class<?>[] types = new Class[signature.length];
    for (int i = 0; i < signature.length; i++) {
        types[i] = BaseModelMBean.getAttributeClass(signature[i]);
    }
    // Locate the method to be invoked, either in this MBean itself
    // or in the corresponding managed resource
    // FIXME - Accessible methods in superinterfaces?
    Object object = null;
    Exception exception = null;
    try {
        object = bean;
        method = object.getClass().getMethod(aname, types);
    } catch (NoSuchMethodException e) {
        exception = e;
    }
    try {
        if ((method == null) && (resource != null)) {
            object = resource;
            method = object.getClass().getMethod(aname, types);
        }
    } catch (NoSuchMethodException e) {
        exception = e;
    }
    if (method == null) {
        throw new ReflectionException(exception, "Cannot find method " + aname + " with this signature");
    }
    return method;
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) ReflectionException(javax.management.ReflectionException) Method(java.lang.reflect.Method) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 44 with ReflectionException

use of javax.management.ReflectionException in project tomcat by apache.

the class ManagedBean method getSetter.

public Method getSetter(String aname, BaseModelMBean bean, Object resource) throws AttributeNotFoundException, ReflectionException {
    Method m = null;
    AttributeInfo attrInfo = attributes.get(aname);
    if (attrInfo == null)
        throw new AttributeNotFoundException(" Cannot find attribute " + aname);
    // Look up the actual operation to be used
    String setMethod = attrInfo.getSetMethod();
    if (setMethod == null)
        throw new AttributeNotFoundException("Cannot find attribute " + aname + " set method name");
    String argType = attrInfo.getType();
    Class<?>[] signature = new Class[] { BaseModelMBean.getAttributeClass(argType) };
    Object object = null;
    NoSuchMethodException exception = null;
    try {
        object = bean;
        m = object.getClass().getMethod(setMethod, signature);
    } catch (NoSuchMethodException e) {
        exception = e;
    }
    if (m == null && resource != null) {
        try {
            object = resource;
            m = object.getClass().getMethod(setMethod, signature);
            exception = null;
        } catch (NoSuchMethodException e) {
            exception = e;
        }
    }
    if (exception != null)
        throw new ReflectionException(exception, "Cannot find setter method " + setMethod + " " + resource);
    return m;
}
Also used : MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Method(java.lang.reflect.Method)

Example 45 with ReflectionException

use of javax.management.ReflectionException in project tomcat by apache.

the class BaseModelMBean method getAttribute.

// key: operation val: invoke method
//private Hashtable invokeAttMap=new Hashtable();
/**
     * Obtain and return the value of a specific attribute of this MBean.
     *
     * @param name Name of the requested attribute
     *
     * @exception AttributeNotFoundException if this attribute is not
     *  supported by this MBean
     * @exception MBeanException if the initializer of an object
     *  throws an exception
     * @exception ReflectionException if a Java reflection exception
     *  occurs when invoking the getter
     */
@Override
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // Validate the input parameters
    if (name == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name is null"), "Attribute name is null");
    if ((resource instanceof DynamicMBean) && !(resource instanceof BaseModelMBean)) {
        return ((DynamicMBean) resource).getAttribute(name);
    }
    Method m = managedBean.getGetter(name, this, resource);
    Object result = null;
    try {
        Class<?> declaring = m.getDeclaringClass();
        // but this is the catalina class.
        if (declaring.isAssignableFrom(this.getClass())) {
            result = m.invoke(this, NO_ARGS_PARAM);
        } else {
            result = m.invoke(resource, NO_ARGS_PARAM);
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t == null)
            t = e;
        if (t instanceof RuntimeException)
            throw new RuntimeOperationsException((RuntimeException) t, "Exception invoking method " + name);
        else if (t instanceof Error)
            throw new RuntimeErrorException((Error) t, "Error invoking method " + name);
        else
            throw new MBeanException(e, "Exception invoking method " + name);
    } catch (Exception e) {
        throw new MBeanException(e, "Exception invoking method " + name);
    }
    // FIXME - should we validate the return type?
    return (result);
}
Also used : DynamicMBean(javax.management.DynamicMBean) RuntimeErrorException(javax.management.RuntimeErrorException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

ReflectionException (javax.management.ReflectionException)72 InstanceNotFoundException (javax.management.InstanceNotFoundException)50 MBeanException (javax.management.MBeanException)46 AttributeNotFoundException (javax.management.AttributeNotFoundException)31 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)23 ObjectName (javax.management.ObjectName)22 Attribute (javax.management.Attribute)19 RuntimeOperationsException (javax.management.RuntimeOperationsException)17 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 IntrospectionException (javax.management.IntrospectionException)13 Method (java.lang.reflect.Method)12 AttributeList (javax.management.AttributeList)12 ServiceNotFoundException (javax.management.ServiceNotFoundException)12 IOException (java.io.IOException)11 MBeanInfo (javax.management.MBeanInfo)11 RuntimeErrorException (javax.management.RuntimeErrorException)11 MalformedObjectNameException (javax.management.MalformedObjectNameException)10 ListenerNotFoundException (javax.management.ListenerNotFoundException)9 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)9 MBeanRegistrationException (javax.management.MBeanRegistrationException)9