Search in sources :

Example 66 with ReflectionException

use of javax.management.ReflectionException in project jdk8u_jdk by JetBrains.

the class RequiredModelMBean method invokeMethod.

/*
     * Invoke the given method, and throw the somewhat unpredictable
     * appropriate exception if the method itself gets an exception.
     */
private Object invokeMethod(String opName, final Method method, final Object targetObject, final Object[] opArgs) throws MBeanException, ReflectionException {
    try {
        final Throwable[] caughtException = new Throwable[1];
        AccessControlContext stack = AccessController.getContext();
        Object rslt = javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Object>() {

            @Override
            public Object run() {
                try {
                    ReflectUtil.checkPackageAccess(method.getDeclaringClass());
                    return MethodUtil.invoke(method, targetObject, opArgs);
                } catch (InvocationTargetException e) {
                    caughtException[0] = e;
                } catch (IllegalAccessException e) {
                    caughtException[0] = e;
                }
                return null;
            }
        }, stack, acc);
        if (caughtException[0] != null) {
            if (caughtException[0] instanceof Exception) {
                throw (Exception) caughtException[0];
            } else if (caughtException[0] instanceof Error) {
                throw (Error) caughtException[0];
            }
        }
        return rslt;
    } catch (RuntimeErrorException ree) {
        throw new RuntimeOperationsException(ree, "RuntimeException occurred in RequiredModelMBean " + "while trying to invoke operation " + opName);
    } catch (RuntimeException re) {
        throw new RuntimeOperationsException(re, "RuntimeException occurred in RequiredModelMBean " + "while trying to invoke operation " + opName);
    } catch (IllegalAccessException iae) {
        throw new ReflectionException(iae, "IllegalAccessException occurred in " + "RequiredModelMBean while trying to " + "invoke operation " + opName);
    } catch (InvocationTargetException ite) {
        Throwable mmbTargEx = ite.getTargetException();
        if (mmbTargEx instanceof RuntimeException) {
            throw new MBeanException((RuntimeException) mmbTargEx, "RuntimeException thrown in RequiredModelMBean " + "while trying to invoke operation " + opName);
        } else if (mmbTargEx instanceof Error) {
            throw new RuntimeErrorException((Error) mmbTargEx, "Error occurred in RequiredModelMBean while trying " + "to invoke operation " + opName);
        } else if (mmbTargEx instanceof ReflectionException) {
            throw (ReflectionException) mmbTargEx;
        } else {
            throw new MBeanException((Exception) mmbTargEx, "Exception thrown in RequiredModelMBean " + "while trying to invoke operation " + opName);
        }
    } catch (Error err) {
        throw new RuntimeErrorException(err, "Error occurred in RequiredModelMBean while trying " + "to invoke operation " + opName);
    } catch (Exception e) {
        throw new ReflectionException(e, "Exception occurred in RequiredModelMBean while " + "trying to invoke operation " + opName);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) 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) AccessControlContext(java.security.AccessControlContext) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 67 with ReflectionException

use of javax.management.ReflectionException in project jdk8u_jdk by JetBrains.

the class XSheet method displayMBeanOperationsNode.

// Call on EDT
private void displayMBeanOperationsNode(final DefaultMutableTreeNode node) {
    final XNodeInfo uo = (XNodeInfo) node.getUserObject();
    if (!uo.getType().equals(Type.OPERATIONS)) {
        return;
    }
    mbean = (XMBean) uo.getData();
    SwingWorker<MBeanInfo, Void> sw = new SwingWorker<MBeanInfo, Void>() {

        @Override
        public MBeanInfo doInBackground() throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
            return mbean.getMBeanInfo();
        }

        @Override
        protected void done() {
            try {
                MBeanInfo mbi = get();
                if (mbi != null) {
                    if (!isSelectedNode(node, currentNode)) {
                        return;
                    }
                    mbeanOperations.loadOperations(mbean, mbi);
                    invalidate();
                    mainPanel.removeAll();
                    JPanel borderPanel = new JPanel(new BorderLayout());
                    borderPanel.setBorder(BorderFactory.createTitledBorder(Messages.OPERATION_INVOCATION));
                    borderPanel.add(new JScrollPane(mbeanOperations));
                    mainPanel.add(borderPanel, BorderLayout.CENTER);
                    southPanel.setVisible(false);
                    southPanel.removeAll();
                    validate();
                    repaint();
                }
            } catch (Exception e) {
                Throwable t = Utils.getActualException(e);
                if (JConsole.isDebug()) {
                    System.err.println("Problem displaying MBean " + "operations for MBean [" + mbean.getObjectName() + "]");
                    t.printStackTrace();
                }
                showErrorDialog(t.toString(), Messages.PROBLEM_DISPLAYING_MBEAN);
            }
        }
    };
    sw.execute();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) MBeanInfo(javax.management.MBeanInfo) BorderLayout(java.awt.BorderLayout) SwingWorker(javax.swing.SwingWorker) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) IOException(java.io.IOException)

Example 68 with ReflectionException

use of javax.management.ReflectionException in project jdk8u_jdk by JetBrains.

the class DcmdMBeanPermissionsTest method main.

public static void main(final String[] args) {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName on = null;
    try {
        on = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
    } catch (MalformedObjectNameException ex) {
        ex.printStackTrace();
        throw new RuntimeException("TEST FAILED");
    }
    MBeanInfo info = null;
    try {
        info = mbs.getMBeanInfo(on);
    } catch (InstanceNotFoundException | IntrospectionException | ReflectionException ex) {
        ex.printStackTrace();
        throw new RuntimeException("TEST FAILED");
    }
    CustomSecurityManager sm = new CustomSecurityManager();
    System.setSecurityManager(sm);
    // Set of permission required to run the test cleanly
    // Some permissions are required by the MBeanServer and other
    // platform services (RuntimePermission("createClassLoader"),
    // ReflectPermission("suppressAccessChecks"),
    // java.util.logging.LoggingPermission("control"),
    // RuntimePermission("exitVM.97")).
    // Other permissions are required by commands being invoked
    // in the test (for instance, RuntimePermission("modifyThreadGroup")
    // and RuntimePermission("modifyThread") are checked when
    // runFinalization() is invoked by the gcRunFinalization command.
    sm.grantPermission(new RuntimePermission("createClassLoader"));
    sm.grantPermission(new ReflectPermission("suppressAccessChecks"));
    sm.grantPermission(new java.util.logging.LoggingPermission("control", ""));
    sm.grantPermission(new java.lang.RuntimePermission("exitVM.97"));
    sm.grantPermission(new java.lang.RuntimePermission("modifyThreadGroup"));
    sm.grantPermission(new java.lang.RuntimePermission("modifyThread"));
    for (MBeanOperationInfo opInfo : info.getOperations()) {
        Permission opPermission = new MBeanPermission(info.getClassName(), opInfo.getName(), on, "invoke");
        sm.grantPermission(opPermission);
        testOperation(mbs, sm, on, opInfo);
        sm.denyPermission(opPermission);
    }
    System.out.println("TEST PASSED");
}
Also used : ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanPermission(javax.management.MBeanPermission) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) ObjectName(javax.management.ObjectName) ReflectPermission(java.lang.reflect.ReflectPermission) MBeanPermission(javax.management.MBeanPermission) Permission(java.security.Permission) ReflectPermission(java.lang.reflect.ReflectPermission) MBeanServer(javax.management.MBeanServer)

Example 69 with ReflectionException

use of javax.management.ReflectionException in project jdk8u_jdk by JetBrains.

the class LibraryLoaderTest method main.

public static void main(String[] args) throws Exception {
    String osName = System.getProperty("os.name");
    System.out.println("os.name=" + osName);
    String osArch = System.getProperty("os.arch");
    System.out.println("os.name=" + osArch);
    //
    if ((!(osName.equals("SunOS") && osArch.equals("sparc"))) && (!(osName.startsWith("Windows") && osArch.equals("x86")))) {
        System.out.println("This test runs only on Solaris/SPARC and Windows/x86 platforms");
        System.out.println("Bye! Bye!");
        return;
    }
    String libPath = System.getProperty("java.library.path");
    System.out.println("java.library.path=" + libPath);
    String testSrc = System.getProperty("test.src");
    System.out.println("test.src=" + testSrc);
    String workingDir = System.getProperty("user.dir");
    System.out.println("user.dir=" + workingDir);
    String urlCodebase;
    if (testSrc.startsWith("/")) {
        urlCodebase = "file:" + testSrc.replace(File.separatorChar, '/') + "/";
    } else {
        urlCodebase = "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
    }
    // Create MBeanServer
    //
    MBeanServer server = MBeanServerFactory.newMBeanServer();
    //
    for (int i = 0; i < mletInfo.length; i++) {
        // Create ObjectName for MLet
        //
        ObjectName mlet = new ObjectName(mletInfo[i][0]);
        server.createMBean("javax.management.loading.MLet", mlet);
        System.out.println("MLet = " + mlet);
        // Display old library directory and set it to test.classes
        //
        String libraryDirectory = (String) server.getAttribute(mlet, "LibraryDirectory");
        System.out.println("Old Library Directory = " + libraryDirectory);
        Attribute attribute = new Attribute("LibraryDirectory", workingDir);
        server.setAttribute(mlet, attribute);
        libraryDirectory = (String) server.getAttribute(mlet, "LibraryDirectory");
        System.out.println("New Library Directory = " + libraryDirectory);
        // Get MBeans from URL
        //
        String mletURL = urlCodebase + mletInfo[i][1];
        System.out.println("MLet URL = " + mletURL);
        Object[] params = new Object[] { mletURL };
        String[] signature = new String[] { "java.lang.String" };
        Object[] res = ((Set<?>) server.invoke(mlet, "getMBeansFromURL", params, signature)).toArray();
        //
        for (int j = 0; j < res.length; j++) {
            //
            if (res[j] instanceof Throwable) {
                ((Throwable) res[j]).printStackTrace(System.out);
                throw new Exception("Failed to load the MBean #" + j, (Throwable) res[j]);
            }
            // On each of the loaded MBeans, try to invoke their
            // native operation
            //
            Object result = null;
            try {
                ObjectName mbean = ((ObjectInstance) res[j]).getObjectName();
                result = server.getAttribute(mbean, "Random");
                System.out.println("MBean #" + j + " = " + mbean);
                System.out.println("Random number = " + result);
            } catch (ReflectionException e) {
                e.getTargetException().printStackTrace(System.out);
                throw new Exception("A ReflectionException " + "occured when attempting to invoke " + "a native library based operation.", e.getTargetException());
            }
        }
    }
}
Also used : ReflectionException(javax.management.ReflectionException) Set(java.util.Set) Attribute(javax.management.Attribute) ObjectInstance(javax.management.ObjectInstance) ReflectionException(javax.management.ReflectionException) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 70 with ReflectionException

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

the class MX4JModelMBean method invokeMethod.

private Object invokeMethod(Object target, String methodName, Class[] params, Object[] args) throws MBeanException, ReflectionException {
    // First try on this instance, then on the target
    Object realTarget = null;
    Method method = null;
    try {
        realTarget = this;
        method = realTarget.getClass().getMethod(methodName, params);
    } catch (NoSuchMethodException x) {
        realTarget = target;
    }
    if (realTarget == null)
        throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_COULD_NOT_FIND_TARGET.toLocalizedString()));
    if (method == null) {
        try {
            method = realTarget.getClass().getMethod(methodName, params);
        } catch (NoSuchMethodException x) {
            throw new ReflectionException(x);
        }
    }
    try {
        Object value = method.invoke(realTarget, args);
        Logger logger = getLogger();
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Method invocation returned value: " + value);
        return value;
    } catch (IllegalAccessException x) {
        throw new ReflectionException(x);
    } catch (IllegalArgumentException x) {
        throw new MBeanException(x);
    } catch (InvocationTargetException x) {
        Throwable t = x.getTargetException();
        if (t instanceof Error)
            throw new MBeanException(new RuntimeErrorException((Error) t));
        else
            throw new MBeanException((Exception) t);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) RuntimeErrorException(javax.management.RuntimeErrorException) Method(java.lang.reflect.Method) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException)

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