1 /* 2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4969756 27 * @summary Test that the same native library coming from the same jar file can 28 * be loaded twice by two different MLets on the same JVM without conflict. 29 * @author Luis-Miguel Alventosa 30 * 31 * @run clean LibraryLoaderTest 32 * @run build LibraryLoaderTest 33 * @run main/othervm LibraryLoaderTest 34 */ 35 36 import java.io.File; 37 import java.util.Set; 38 import javax.management.Attribute; 39 import javax.management.MBeanServer; 40 import javax.management.MBeanServerFactory; 41 import javax.management.ObjectInstance; 42 import javax.management.ObjectName; 43 import javax.management.ReflectionException; 44 45 public class LibraryLoaderTest { 46 47 private static final String mletInfo[][] = { 48 {"testDomain:type=MLet,index=0", "UseNativeLib0.html"}, 49 {"testDomain:type=MLet,index=1", "UseNativeLib1.html"} 50 }; 51 52 public static void main(String args[]) throws Exception { 53 54 String osName = System.getProperty("os.name"); 55 System.out.println("os.name=" + osName); 56 String osArch = System.getProperty("os.arch"); 57 System.out.println("os.name=" + osArch); 58 59 // Check for supported platforms: 60 // 61 // Windows/x86 62 // 63 if ((!(osName.startsWith("Windows") && osArch.equals("x86")))) { 64 System.out.println( 65 "This test runs only on Windows/x86 platforms"); 66 System.out.println("Bye! Bye!"); 67 return; 68 } 69 70 String libPath = System.getProperty("java.library.path"); 71 System.out.println("java.library.path=" + libPath); 72 String testSrc = System.getProperty("test.src"); 73 System.out.println("test.src=" + testSrc); 74 String workingDir = System.getProperty("user.dir"); 75 System.out.println("user.dir=" + workingDir); 76 77 String urlCodebase; 78 if (testSrc.startsWith("/")) { 79 urlCodebase = 80 "file:" + testSrc.replace(File.separatorChar, '/') + "/"; 81 } else { 82 urlCodebase = 83 "file:/" + testSrc.replace(File.separatorChar, '/') + "/"; 84 } 85 86 // Create MBeanServer 87 // 88 MBeanServer server = MBeanServerFactory.newMBeanServer(); 89 90 // Create MLet instances and call getRandom on the loaded MBeans 91 // 92 for (int i = 0; i < mletInfo.length; i++) { 93 // Create ObjectName for MLet 94 // 95 ObjectName mlet = new ObjectName(mletInfo[i][0]); 96 server.createMBean("javax.management.loading.MLet", mlet); 97 System.out.println("MLet = " + mlet); 98 99 // Display old library directory and set it to test.classes 100 // 101 String libraryDirectory = 102 (String) server.getAttribute(mlet, "LibraryDirectory"); 103 System.out.println("Old Library Directory = " + 104 libraryDirectory); 105 Attribute attribute = 106 new Attribute("LibraryDirectory", workingDir); 107 server.setAttribute(mlet, attribute); 108 libraryDirectory = 109 (String) server.getAttribute(mlet, "LibraryDirectory"); 110 System.out.println("New Library Directory = " + 111 libraryDirectory); 112 113 // Get MBeans from URL 114 // 115 String mletURL = urlCodebase + mletInfo[i][1]; 116 System.out.println("MLet URL = " + mletURL); 117 Object[] params = new Object[] { mletURL }; 118 String[] signature = new String[] {"java.lang.String"}; 119 Object res[] = ((Set<?>) server.invoke(mlet, 120 "getMBeansFromURL", 121 params, 122 signature)).toArray(); 123 124 // Iterate through all the loaded MBeans 125 // 126 for (int j = 0; j < res.length; j++) { 127 // Now ensure none of the returned objects is a Throwable 128 // 129 if (res[j] instanceof Throwable) { 130 ((Throwable) res[j]).printStackTrace(System.out); 131 throw new Exception("Failed to load the MBean #" + j 132 ,(Throwable)res[j]); 133 } 134 135 // On each of the loaded MBeans, try to invoke their 136 // native operation 137 // 138 Object result = null; 139 try { 140 ObjectName mbean = 141 ((ObjectInstance) res[j]).getObjectName(); 142 result = server.getAttribute(mbean, "Random"); 143 System.out.println("MBean #" + j + " = " + mbean); 144 System.out.println("Random number = " + result); 145 } catch (ReflectionException e) { 146 e.getTargetException().printStackTrace(System.out); 147 throw new Exception ("A ReflectionException " 148 + "occured when attempting to invoke " 149 + "a native library based operation.", 150 e.getTargetException()); 151 } 152 } 153 } 154 } 155 }