Path: blob/master/test/jdk/javax/management/loading/LibraryLoader/LibraryLoaderTest.java
41154 views
/*1* Copyright (c) 2004, 2020, 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 496975626* @summary Test that the same native library coming from the same jar file can27* be loaded twice by two different MLets on the same JVM without conflict.28* @author Luis-Miguel Alventosa29*30* @run clean LibraryLoaderTest31* @run build LibraryLoaderTest32* @run main/othervm LibraryLoaderTest33*/3435import java.io.File;36import java.util.Set;37import javax.management.Attribute;38import javax.management.MBeanServer;39import javax.management.MBeanServerFactory;40import javax.management.ObjectInstance;41import javax.management.ObjectName;42import javax.management.ReflectionException;4344public class LibraryLoaderTest {4546private static final String mletInfo[][] = {47{"testDomain:type=MLet,index=0", "UseNativeLib0.html"},48{"testDomain:type=MLet,index=1", "UseNativeLib1.html"}49};5051public static void main(String args[]) throws Exception {5253String osName = System.getProperty("os.name");54System.out.println("os.name=" + osName);55String osArch = System.getProperty("os.arch");56System.out.println("os.name=" + osArch);5758// Check for supported platforms:59//60// Windows/x8661//62if ((!(osName.startsWith("Windows") && osArch.equals("x86")))) {63System.out.println(64"This test runs only on Windows/x86 platforms");65System.out.println("Bye! Bye!");66return;67}6869String libPath = System.getProperty("java.library.path");70System.out.println("java.library.path=" + libPath);71String testSrc = System.getProperty("test.src");72System.out.println("test.src=" + testSrc);73String workingDir = System.getProperty("user.dir");74System.out.println("user.dir=" + workingDir);7576String urlCodebase;77if (testSrc.startsWith("/")) {78urlCodebase =79"file:" + testSrc.replace(File.separatorChar, '/') + "/";80} else {81urlCodebase =82"file:/" + testSrc.replace(File.separatorChar, '/') + "/";83}8485// Create MBeanServer86//87MBeanServer server = MBeanServerFactory.newMBeanServer();8889// Create MLet instances and call getRandom on the loaded MBeans90//91for (int i = 0; i < mletInfo.length; i++) {92// Create ObjectName for MLet93//94ObjectName mlet = new ObjectName(mletInfo[i][0]);95server.createMBean("javax.management.loading.MLet", mlet);96System.out.println("MLet = " + mlet);9798// Display old library directory and set it to test.classes99//100String libraryDirectory =101(String) server.getAttribute(mlet, "LibraryDirectory");102System.out.println("Old Library Directory = " +103libraryDirectory);104Attribute attribute =105new Attribute("LibraryDirectory", workingDir);106server.setAttribute(mlet, attribute);107libraryDirectory =108(String) server.getAttribute(mlet, "LibraryDirectory");109System.out.println("New Library Directory = " +110libraryDirectory);111112// Get MBeans from URL113//114String mletURL = urlCodebase + mletInfo[i][1];115System.out.println("MLet URL = " + mletURL);116Object[] params = new Object[] { mletURL };117String[] signature = new String[] {"java.lang.String"};118Object res[] = ((Set<?>) server.invoke(mlet,119"getMBeansFromURL",120params,121signature)).toArray();122123// Iterate through all the loaded MBeans124//125for (int j = 0; j < res.length; j++) {126// Now ensure none of the returned objects is a Throwable127//128if (res[j] instanceof Throwable) {129((Throwable) res[j]).printStackTrace(System.out);130throw new Exception("Failed to load the MBean #" + j131,(Throwable)res[j]);132}133134// On each of the loaded MBeans, try to invoke their135// native operation136//137Object result = null;138try {139ObjectName mbean =140((ObjectInstance) res[j]).getObjectName();141result = server.getAttribute(mbean, "Random");142System.out.println("MBean #" + j + " = " + mbean);143System.out.println("Random number = " + result);144} catch (ReflectionException e) {145e.getTargetException().printStackTrace(System.out);146throw new Exception ("A ReflectionException "147+ "occured when attempting to invoke "148+ "a native library based operation.",149e.getTargetException());150}151}152}153}154}155156157