public static void main(String[] args) throws Exception { Class thisClass = MethodResultTest.class; Class exoticClass = Exotic.class; String exoticClassName = Exotic.class.getName(); ClassLoader testClassLoader = thisClass.getClassLoader(); if (!(testClassLoader instanceof URLClassLoader)) { System.out.println("TEST INVALID: Not loaded by a " + "URLClassLoader: " + testClassLoader); System.exit(1); } URLClassLoader tcl = (URLClassLoader) testClassLoader; URL[] urls = tcl.getURLs(); ClassLoader shadowLoader = new ShadowLoader( urls, testClassLoader, new String[] { exoticClassName, ExoticMBeanInfo.class.getName(), ExoticException.class.getName() }); Class cl = shadowLoader.loadClass(exoticClassName); if (cl == exoticClass) { System.out.println( "TEST INVALID: Shadow class loader loaded " + "same class as test class loader"); System.exit(1); } Thread.currentThread().setContextClassLoader(shadowLoader); ObjectName on = new ObjectName("a:b=c"); MBeanServer mbs = MBeanServerFactory.newMBeanServer(); mbs.createMBean(Thing.class.getName(), on); final String[] protos = {"rmi", "iiop", "jmxmp"}; boolean ok = true; for (int i = 0; i < protos.length; i++) { try { ok &= test(protos[i], mbs, on); System.out.println(); } catch (Exception e) { System.out.println("TEST FAILED WITH EXCEPTION:"); e.printStackTrace(System.out); ok = false; } } if (ok) System.out.println("Test passed"); else { System.out.println("TEST FAILED"); System.exit(1); } }
private static void check(String what, MBeanNotificationInfo[] mbnis) { System.out.print(what + ": checking notification info: "); if (mbnis.length == 0) { System.out.println("NONE (suspicious)"); suspicious.add(what); return; } // Each MBeanNotificationInfo.getName() should be an existent // Java class that is Notification or a subclass of it for (int j = 0; j < mbnis.length; j++) { String notifClassName = mbnis[j].getName(); Class notifClass; try { notifClass = Class.forName(notifClassName); } catch (Exception e) { System.out.print("FAILED(" + notifClassName + ": " + e + ") "); failed.add(what); continue; } if (!Notification.class.isAssignableFrom(notifClass)) { System.out.print("FAILED(" + notifClassName + ": not a Notification) "); failed.add(what); continue; } System.out.print("OK(" + notifClassName + ") "); } System.out.println(); }
private static boolean checkType( String what, Object object, Class wrongClass, boolean isException) { final String type = isException ? "exception" : "object"; final String rendered = isException ? "thrown" : "returned"; System.out.println("For " + type + " " + rendered + " by " + what + ":"); if (wrongClass.isInstance(object)) { System.out.println("TEST FAILS: " + type + " loaded by test " + "classloader"); return false; } String className = object.getClass().getName(); if (!className.equals(wrongClass.getName())) { System.out.println( "TEST FAILS: " + rendered + " " + type + " has wrong class name: " + className); return false; } System.out.println( "Test passes: " + rendered + " " + type + " has same class name but is not same class"); return true; }
static { // FIXME: RMIServerImpl_Stub is generated at build time // after jconsole is built. We need to investigate if // the Makefile can be fixed to build jconsole in the // right order. As a workaround for now, we dynamically // load RMIServerImpl_Stub class instead of statically // referencing it. Class<? extends Remote> serverStubClass = null; try { serverStubClass = Class.forName(rmiServerImplStubClassName).asSubclass(Remote.class); } catch (ClassNotFoundException e) { // should never reach here throw (InternalError) new InternalError(e.getMessage()).initCause(e); } rmiServerImplStubClass = serverStubClass; }
private static void checkStub(Remote stub, Class<? extends Remote> stubClass) { // Check remote stub is from the expected class. // if (stub.getClass() != stubClass) { if (!Proxy.isProxyClass(stub.getClass())) { throw new SecurityException("Expecting a " + stubClass.getName() + " stub!"); } else { InvocationHandler handler = Proxy.getInvocationHandler(stub); if (handler.getClass() != RemoteObjectInvocationHandler.class) { throw new SecurityException( "Expecting a dynamic proxy instance with a " + RemoteObjectInvocationHandler.class.getName() + " invocation handler!"); } else { stub = (Remote) handler; } } } // Check RemoteRef in stub is from the expected class // "sun.rmi.server.UnicastRef2". // RemoteRef ref = ((RemoteObject) stub).getRef(); if (ref.getClass() != UnicastRef2.class) { throw new SecurityException( "Expecting a " + UnicastRef2.class.getName() + " remote reference in stub!"); } // Check RMIClientSocketFactory in stub is from the expected class // "javax.rmi.ssl.SslRMIClientSocketFactory". // LiveRef liveRef = ((UnicastRef2) ref).getLiveRef(); RMIClientSocketFactory csf = liveRef.getClientSocketFactory(); if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class) { throw new SecurityException( "Expecting a " + SslRMIClientSocketFactory.class.getName() + " RMI client socket factory in stub!"); } }
public static void main(String[] args) throws Exception { System.out.println( "Checking that all known MBeans that are " + "NotificationBroadcasters have sane " + "MBeanInfo.getNotifications()"); System.out.println("Checking platform MBeans..."); checkPlatformMBeans(); URL codeBase = ClassLoader.getSystemResource("javax/management/MBeanServer.class"); if (codeBase == null) { throw new Exception("Could not determine codeBase for " + MBeanServer.class); } System.out.println(); System.out.println("Looking for standard MBeans..."); String[] classes = findStandardMBeans(codeBase); System.out.println("Testing standard MBeans..."); for (int i = 0; i < classes.length; i++) { String name = classes[i]; Class<?> c; try { c = Class.forName(name); } catch (Throwable e) { System.out.println(name + ": cannot load (not public?): " + e); continue; } if (!NotificationBroadcaster.class.isAssignableFrom(c)) { System.out.println(name + ": not a NotificationBroadcaster"); continue; } if (Modifier.isAbstract(c.getModifiers())) { System.out.println(name + ": abstract class"); continue; } NotificationBroadcaster mbean; Constructor<?> constr; try { constr = c.getConstructor(); } catch (Exception e) { System.out.println(name + ": no public no-arg constructor: " + e); continue; } try { mbean = (NotificationBroadcaster) constr.newInstance(); } catch (Exception e) { System.out.println(name + ": no-arg constructor failed: " + e); continue; } check(mbean); } System.out.println(); System.out.println("Testing some explicit cases..."); check(new RelationService(false)); /* We can't do this: check(new RequiredModelMBean()); because the Model MBean spec more or less forces us to use the names GENERIC and ATTRIBUTE_CHANGE for its standard notifs. */ checkRMIConnectorServer(); System.out.println(); if (!suspicious.isEmpty()) System.out.println("SUSPICIOUS CLASSES: " + suspicious); if (failed.isEmpty()) System.out.println("TEST PASSED"); else { System.out.println("TEST FAILED: " + failed); System.exit(1); } }