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);
}
}
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();
}
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");
}
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());
}
}
}
}
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);
}
}
Aggregations