/** * Test for the method loadClass() * * @see javax.management.loading.mlet#loadClass() */ public Result testMLetLoadClass() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "mlet1.conf" }, new String[] { String.class.getName() }); mlet.loadClass("FirstRemoteClass"); } catch (Exception e) { //e.printStackTrace(); return failed("Unexpected Exception occur"); } return result(); }
/** * Test for the method getURLs() * * @see javax.management.loading.mlet#getURLs() */ public Result testGetURLs() throws Exception { MLet mlet = new MLet(); try { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); mlet.addURL(MLET_URL); } catch (Exception e) { //e.printStackTrace(); return failed("Unexpected Exception occur"); } if (mlet.getURLs().length == 1 ) return passed(); else return failed("Wrong number of URLs returned by getURLs() method"); }
/** * Test for the method getLibraryDirectory() * * @see javax.management.loading.mlet#getLibraryDirectory() */ public Result testNativeLib() throws Exception { try{ MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); mlet.getMBeansFromURL(MLET_URL+"mlet2.conf"); String libPath = mlet.getLibraryDirectory(); //System.out.println(libPath); if(libPath.equals(null)) return failed("method MLet.getLibraryDirectory()return wrong value = " + libPath); }catch(Exception e){ //e.printStackTrace(); return failed("Unexpected Exception occur"); } return result(); }
/** * Test for the method * getLibraryDirectory() * setLibraryDirectory() * * @see javax.management.loading.mlet#setLibraryDirectory() */ public Result testSetLibraryDirectory() throws Exception { try{ MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); mlet.getMBeansFromURL(MLET_URL+"mlet2.conf"); String libPath = mlet.getLibraryDirectory(); mlet.setLibraryDirectory(libPath); if(libPath.equals(null)) return failed("method MLet.getLibraryDirectory()return wrong value = " + libPath); }catch(Exception e){ //e.printStackTrace(); return failed("Unexpected Exception occur"); } return result(); }
/** * Test for the method loadClass() * * @see javax.management.loading.mlet#loadClass() */ public Result testClassCheck() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL",new Object[] { MLET_URL+"mlet1.conf" },new String[] { String.class.getName() }); //mlet.getMBeansFromURL(MLET_URL+"/mlet-1/mlet1.conf"); Class type = mlet.loadClass("FirstRemoteClass"); //System.out.println(type); if(type.equals(null)) return failed("method MLet.loadClass return wrong value = " + type); } catch (Exception e) { e.printStackTrace(); return failed("Unexpected Exception occur"); } return result(); }
/** * Test for the method loadClass() * * @see javax.management.loading.mlet#loadClass() */ public Result testExceptionClassCheck() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL",new Object[] { MLET_URL+"/mlet-1/mlet1.conf" },new String[] { String.class.getName() }); Class type = mlet.loadClass("IAmNotAClass"); System.out.println(type); if(type.equals(null)) return failed("method MLet.loadClass return wrong value = " + type); } catch (Exception e) { //e.printStackTrace(); return passed("Expected Exception occur"); } return result(); }
/** * Test for mandatory: Tags CODE, ARCHIVE, NAME */ public Result testBaseChk() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "BaseChk.conf" }, new String[] { String.class.getName() }); if (!server.isRegistered(new ObjectName( "test:name=SecondRemoteClass"))) return failed("MBean SecondRemoteClass does not registered by the server"); } catch (Exception e) { return failed("Unexpected Exception"); } return result(); }
/** * Test for mandatory: Tags OBJECT, ARCHIVE, NAME */ public Result testSerChk() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "SerChk.conf" }, new String[] { String.class.getName() }); if (!server.isRegistered(new ObjectName("test:name=AnSerTest"))) return failed("MBean AnSerTest(ser class) does not registered by the server"); server.getMBeanInfo(new ObjectName("test:name=AnSerTest")).getDescription(); } catch (Exception e) { e.printStackTrace(); return failed("Unexpected Exception"); } return result(); }
/** * Test for mandatory: Tags CODE, OBJECT. * Exception should be thrown if it is exist in conf mlet text file */ public Result testCODEAndOBJECT() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "CODEAndOBJECT.conf" }, new String[] { String.class .getName() }); } catch (Exception e) { return failed("Unexpected Exception"); } return result(); }
/** * Exception should be thrown if in conf mlet text file exist unexpected end of file */ public Result testUnexpCfg() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "unexp.conf" }, new String[] { String.class.getName() }); } catch (MBeanException e) { //e.printStackTrace(); return passed(); } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return failed("javax.management.MBeanException is absence"); }
/** * Exception should be thrown if in conf mlet text file absence mandatory ARCHIVE tag */ public Result testMissArc() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "MissArc.conf" }, new String[] { String.class .getName() }); } catch (MBeanException e) { //e.printStackTrace(); return passed(); } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return failed("javax.management.MBeanException is absence"); }
/** * Exception should be thrown if in conf mlet text file out of end */ public Result testOutOfEnd() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "OutOfEnd.conf" }, new String[] { String.class.getName() }); } catch (MBeanException e) { //e.printStackTrace(); return passed(); } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return failed("javax.management.MBeanException is absence"); }
/** * Exception should be thrown if in conf mlet text is empty */ public Result testEmptyConf() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "empty.conf" }, new String[] { String.class.getName() }); } catch (MBeanException e) { //e.printStackTrace(); return passed(); } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return failed("javax.management.MBeanException is absence"); }
/** * Test for mandatory: Tags CODE, ARCHIVE, NAME, CODEBASE and arglist * Check that mlet service work properly with list of remote classes */ public Result testListOfMlets() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "ListMLet.conf" }, new String[] { String.class.getName() }); } catch (Exception exp) { exp.printStackTrace(); return failed("Unexpected Exception"); } return result(); }
/** * Test for mandatory: Tags CODE, ARCHIVE, NAME * Check that mlet service work properly with comments in mlet text conf file */ public Result testMletsComments() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "ComMLet.conf" }, new String[] { String.class.getName() }); } catch (Exception exp) { exp.printStackTrace(); return failed("Unexpected Exception"); } return result(); }
/** * Test for mandatory: Tags CODE, OBJECT, ARCHIVE, NAME, CODEBASE and arglist * Exception should be thrown if in conf mlet text file list of remote classes */ public Result testWrongListOfCalsses() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "DiffNameChk.conf" }, new String[] { String.class .getName() }); if (!server.isRegistered(new ObjectName( "test:name=SecondRemoteClass"))) return failed("MBean SecondRemoteClass does not registered by the server"); if (!server.isRegistered(new ObjectName( "test:name=FirstRemoteClass"))) return failed("MBean SecondRemoteClass does not registered by the server"); } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return result(); }
/** * Exception should be thrown if in conf mlet text file absence mandatory CODE tag */ public Result testMissCode() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "MissCode.conf" }, new String[] { String.class.getName() }); } catch (MBeanException e) { //e.printStackTrace(); return passed(); } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return failed("javax.management.MBeanException is absence"); }
/** * Exception should be thrown if conf mlet text file not a conf mlet file */ public Result testNoconf() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "not.conf" }, new String[] { String.class.getName() }); } catch (MBeanException e) { //e.printStackTrace(); return passed(); } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return failed("javax.management.MBeanException is absence"); }
/** * Check that Security Manager work properly with mlet service */ public Result testBaseSecurityChk2() throws Exception { try { System.setProperty("java.security.policy", POLICY_FILE); Policy.getPolicy().refresh(); System.setSecurityManager(new SecurityManager()); MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet1"); server.registerMBean(mlet, name); server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "mlet.conf" }, new String[] { String.class.getName() }); boolean res = server.isRegistered(new ObjectName( "test:name=FirstRemoteClass")); System.out.println("Is MBean register: " + res); } catch (Exception e) { e.printStackTrace(); } return result(); }
public static URLClassLoader makeMLet ( String url ) throws MalformedURLException, Exception { URLClassLoader ucl = new MLet(new URL[] { new URL(url) }); Reflections.setFieldValue(ucl, "parent", null); Reflections.setFieldValue(ucl, "domains", new HashSet<>()); Reflections.setFieldValue(ucl, "defaultDomain", null); Reflections.setFieldValue(ucl, "acc", null); Reflections.setFieldValue(Reflections.getFieldValue(ucl, "ucp"), "acc", null); return ucl; }
public ObjectName registerMLet(MBeanServer server) throws Exception { final ObjectName name = new ObjectName("test:type=MLet"); if (server.isRegistered(name)) { return name; } final MLet mlet = new MLet(new URL[0], ClassLoader.getSystemClassLoader()); return server.registerMBean(mlet, name).getObjectName(); }
@BeforeClass public static void setupClass() { testedMethods.clear(); try { Method m = MLet.class.getDeclaredMethod( CONSTRUCT_PARAMETER, String.class, String.class ); m.setAccessible(true); testedMethods.put(CONSTRUCT_PARAMETER, m); } catch (Exception ex) { throw new Error(ex); } }
public static void main(String[] args) throws Exception { // The address of the connector server JMXServiceURL url = new JMXServiceURL("rmi", "localhost", 0, "/jndi/jmx"); // Create and connect the connector client JMXConnector cntor = JMXConnectorFactory.connect(url, null); // The connection represent, on client-side, the remote MBeanServer MBeanServerConnection connection = cntor.getMBeanServerConnection(); // The listener that will receive notifications from a remote MBean NotificationListener listener = new NotificationListener() { public void handleNotification(Notification notification, Object handback) { System.out.println(notification); } }; // The MBeanServerDelegate emits notifications about registration/unregistration of MBeans ObjectName delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"); connection.addNotificationListener(delegateName, listener, null, null); // Give chance to the notification machinery to setup Thread.sleep(1000); // Now register a remote MBean, for example an MLet, so that the MBeanServerDelegate // will emit notifications for its registration ObjectName name = ObjectName.getInstance("examples:mbean=mlet"); // First notification connection.createMBean(MLet.class.getName(), name, null); // Second notification connection.unregisterMBean(name); }
public PatchingAgent() throws FileNotFoundException, IOException { String path = System.getProperty("patch.classpath"); if (path == null) { path = "."; } url = new URL(path); patchClasspath = new File(url.getFile()); patchingLoader = new MLet(new URL[] { url }, this.getClass().getClassLoader()); }
/** * Create MBean server, register Hello MBean. */ public final void init() throws Exception { // Get the Platform MBean Server mbs = MBeanServerFactory.createMBeanServer(); // Construct the ObjectName for the Hello MBean name = new ObjectName( "org.apache.harmony.test.func.api.javax.management:type=Hello"); // Create the Hello MBean mbean = new Hello(); // Register the Hello MBean mbs.registerMBean(mbean, name); // Construct the ObjectName for the notification listener lName = new ObjectName( "org.apache.harmony.test.func.api.javax.management:type=Listener"); // Register the notification listener mbs.registerMBean(mbean, lName); // Register class loader. loader = new ObjectName("test.loader:type=Loader"); mbs.registerMBean(new MLet(), loader); // Instantiate the security manager mgr = new MySecurityManager(); }
/** * Test for the constructor MLet() * * @see javax.management.loading#MLet() */ public Result testRegister() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); } catch (Exception e) { //e.printStackTrace(); } return result(); }
/** * Test for mandatory: Tags CODE, OBJECT, ARCHIVE, NAME, CODEBASE and arglist * Exception should be thrown if in conf mlet text file list of mlets */ public Result testWrongListOfMlets() throws Exception { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet"); server.registerMBean(mlet, name); Object obj = server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "WrongListMlet.conf" }, new String[] { String.class.getName() }); Set set = (Set) obj; Iterator i = set.iterator(); while (i.hasNext()) { Object obj2 = i.next(); if (obj2 instanceof javax.management.InstanceAlreadyExistsException) { return passed("Expected exception"); } if(obj2 instanceof Exception) { return failed("Unexpected exception"); } } } catch (Exception exp) { //exp.printStackTrace(); return failed("Unexpected Exception"); } return result(); }
/** * Check that corresponding exception are thrown */ public Result testBaseSecurityChk1() throws Exception { try { System.setProperty("java.security.policy", ""); Policy.getPolicy().refresh(); System.setSecurityManager(new SecurityManager()); MBeanServer server = MBeanServerFactory.createMBeanServer(); MLet mlet = new MLet(); ObjectName name = new ObjectName("test:name=mlet1"); server.registerMBean(mlet, name); Object obj = server.invoke(name, "getMBeansFromURL", new Object[] { MLET_URL + "mlet.conf" }, new String[] { String.class.getName() }); boolean res = server.isRegistered(new ObjectName( "test:name=FirstRemoteClass")); System.out.println("Is MBean register: " + res); if (!res) { Set set = (Set) obj; ((Exception) (set.iterator().next())).printStackTrace(); return passed(); } else { return failed("Exception doesn't throw"); } } catch (Exception e) { e.printStackTrace(); return passed(); } }