Path: blob/master/test/jdk/javax/management/mxbean/MXBeanWeirdParamTest.java
41152 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 805886526* @summary Checks that a serialized instance is not transmitted from an MXBean.27* All the communication should be done via Open Types28* @author Olivier Lagneau29* @modules java.management.rmi30* @library /test/lib31* @compile Basic.java32* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanWeirdParamTest33*/3435import java.util.Map;36import java.util.List;37import java.util.ArrayList;38import java.util.Arrays;3940import java.lang.Process;41import java.lang.management.ManagementFactory;4243import javax.management.MBeanServer;44import javax.management.MBeanServerConnection;45import javax.management.remote.JMXConnector;46import javax.management.remote.JMXConnectorFactory;47import javax.management.remote.JMXConnectorServer;48import javax.management.remote.JMXConnectorServerFactory;49import javax.management.remote.JMXServiceURL;5051import javax.management.ObjectName;52import javax.management.openmbean.CompositeType;53import javax.management.openmbean.CompositeData;54import javax.management.openmbean.CompositeDataSupport;55import javax.management.openmbean.OpenType;56import javax.management.openmbean.SimpleType;57import javax.management.openmbean.TabularDataSupport;58import javax.management.openmbean.TabularType;5960import jdk.test.lib.JDKToolFinder;61import jdk.test.lib.process.ProcessTools;6263public class MXBeanWeirdParamTest {6465private static String BASIC_MXBEAN_CLASS_NAME = "Basic";6667private static final String CLIENT_CLASS_MAIN =68"MXBeanWeirdParamTest$ClientSide";6970private JMXConnectorServer cs;7172/*73* First Debug properties and arguments are collect in expected74* map (argName, value) format, then calls original test's run method.75*/76public static void main(String args[]) throws Exception {7778System.out.println("=================================================");7980// Parses parameters81Utils.parseDebugProperties();82Map<String, Object> map = Utils.parseParameters(args) ;8384// Run test85MXBeanWeirdParamTest test = new MXBeanWeirdParamTest();86test.run(map);8788}8990/*91* Create the MBeansServe side of the test and returns its address92*/93private JMXServiceURL createServerSide() throws Exception {94final int NINETY_SECONDS = 90;9596// We will use the platform mbean server97MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();9899JMXServiceURL url = new JMXServiceURL("rmi", null, 0);100cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);101cs.start();102103Utils.waitReady(cs, NINETY_SECONDS);104105JMXServiceURL addr = cs.getAddress();106return addr;107}108109110/*111* Creating command-line for running subprocess JVM:112*113* JVM command line is like:114* {test_jdk}/bin/java {defaultopts} -cp {test.class.path} {testopts} main115*116* {defaultopts} are the default java options set by the framework.117*118*/119private List<String> buildCommandLine() {120List<String> opts = new ArrayList<>();121opts.add(JDKToolFinder.getJDKTool("java"));122opts.addAll(Arrays.asList(jdk.test.lib.Utils.getTestJavaOpts()));123// We need to set WEIRD_PARAM propertty on the client-side124opts.add("-DWEIRD_PARAM");125opts.add("-cp");126opts.add(System.getProperty("test.class.path", "test.class.path"));127opts.add(CLIENT_CLASS_MAIN);128129return opts;130}131132/**133* Runs MXBeanWeirdParamTest$ClientSide with the passed options and redirects134* subprocess standard I/O to the current (parent) process. This provides a135* trace of what happens in the subprocess while it is runnning (and before136* it terminates).137*138* @param serviceUrlStr string representing the JMX service Url to connect to.139*/140private int runClientSide(String serviceUrlStr) throws Exception {141142// Building command-line143List<String> opts = buildCommandLine();144opts.add(serviceUrlStr);145146// Launch separate JVM subprocess147int exitCode = 0;148String[] optsArray = opts.toArray(new String[0]);149ProcessBuilder pb = new ProcessBuilder(optsArray);150Process p = ProcessTools.startProcess("MXBeanWeirdParamTest$ClientSide", pb);151152// Handling end of subprocess153try {154exitCode = p.waitFor();155if (exitCode != 0) {156System.out.println(157"Subprocess unexpected exit value of [" + exitCode +158"]. Expected 0.\n");159}160} catch (InterruptedException e) {161System.out.println("Parent process interrupted with exception : \n " + e + " :" );162163// Parent thread unknown state, killing subprocess.164p.destroyForcibly();165166throw new RuntimeException(167"Parent process interrupted with exception : \n " + e + " :" );168} finally {169return exitCode;170}171172}173174public void run(Map<String, Object> args) throws Exception {175176System.out.println("MXBeanWeirdParamTest::run: Start") ;177int errorCount = 0;178179try {180// Initialise the server side181JMXServiceURL urlToUse = createServerSide();182183// Run client side184errorCount = runClientSide(urlToUse.toString());185186if ( errorCount == 0 ) {187System.out.println("MXBeanWeirdParamTest::run: Done without any error") ;188} else {189System.out.println("MXBeanWeirdParamTest::run: Done with "190+ errorCount191+ " error(s)") ;192throw new RuntimeException("errorCount = " + errorCount);193}194195cs.stop();196197} catch(Exception e) {198throw new RuntimeException(e);199}200201}202203private static class ClientSide {204public static void main(String args[]) throws Exception {205206int errorCount = 0 ;207String msgTag = "ClientSide::main: ";208209try {210211// Get a connection to remote mbean server212JMXServiceURL addr = new JMXServiceURL(args[0]);213JMXConnector cc = JMXConnectorFactory.connect(addr);214MBeanServerConnection mbsc = cc.getMBeanServerConnection();215216// ----217System.out.println(msgTag + "Create and register the MBean");218ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;219mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);220System.out.println(msgTag +"---- OK\n") ;221222// ----223System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");224Object result = mbsc.getAttribute(objName, "SqeParameterAtt");225System.out.println(msgTag +"(OK) Got result of class "226+ result.getClass().getName());227System.out.println(msgTag +"Received CompositeData is " + result);228System.out.println(msgTag +"---- OK\n") ;229230// ----231// We use the value returned by getAttribute to perform the invoke.232System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");233mbsc.invoke(objName, "doWeird",234new Object[]{result},235new String[]{"javax.management.openmbean.CompositeData"});236System.out.println(msgTag +"---- OK\n") ;237238// ----239// We build the CompositeData ourselves that time.240System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");241String typeName = "SqeParameter";242String[] itemNames = new String[] {"glop"};243OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};244CompositeType rowType = new CompositeType(typeName, typeName,245itemNames, itemNames, openTypes);246Object[] itemValues = {"HECTOR"};247CompositeData data =248new CompositeDataSupport(rowType, itemNames, itemValues);249TabularType tabType = new TabularType(typeName, typeName,250rowType, new String[]{"glop"});251TabularDataSupport tds = new TabularDataSupport(tabType);252tds.put(data);253System.out.println(msgTag +"Source CompositeData is " + data);254mbsc.invoke(objName, "doWeird",255new Object[]{data},256new String[]{"javax.management.openmbean.CompositeData"});257System.out.println(msgTag +"---- OK\n") ;258259// ----260System.out.println(msgTag +"Unregister the MBean");261mbsc.unregisterMBean(objName);262System.out.println(msgTag +"---- OK\n") ;263264// Terminate the JMX Client265cc.close();266267} catch(Exception e) {268Utils.printThrowable(e, true) ;269errorCount++;270throw new RuntimeException(e);271} finally {272System.exit(errorCount);273}274}275}276}277278279