0N/A/*
4944N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
2756N/A * @bug 4969756
0N/A * @summary Test that the same native library coming from the same jar file can
0N/A * be loaded twice by two different MLets on the same JVM without conflict.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean LibraryLoaderTest
0N/A * @run build LibraryLoaderTest
0N/A * @run main LibraryLoaderTest
0N/A */
0N/A
0N/Aimport java.io.File;
2553N/Aimport java.util.Set;
2553N/Aimport javax.management.Attribute;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.ObjectInstance;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.ReflectionException;
0N/A
0N/Apublic class LibraryLoaderTest {
0N/A
0N/A private static final String mletInfo[][] = {
0N/A {"testDomain:type=MLet,index=0", "UseNativeLib0.html"},
0N/A {"testDomain:type=MLet,index=1", "UseNativeLib1.html"}
0N/A };
0N/A
0N/A public static void main (String args[]) {
0N/A
0N/A String osName = System.getProperty("os.name");
0N/A System.out.println("os.name=" + osName);
0N/A String osArch = System.getProperty("os.arch");
0N/A System.out.println("os.name=" + osArch);
0N/A
0N/A // Check for supported platforms:
2756N/A //
0N/A // Solaris/SPARC and Windows/x86
0N/A //
0N/A if ((!(osName.equals("SunOS") && osArch.equals("sparc"))) &&
0N/A (!(osName.startsWith("Windows") && osArch.equals("x86")))) {
0N/A System.out.println(
0N/A "This test runs only on Solaris/SPARC and Windows/x86 platforms");
0N/A System.out.println("Bye! Bye!");
0N/A return;
0N/A }
0N/A
0N/A String libPath = System.getProperty("java.library.path");
0N/A System.out.println("java.library.path=" + libPath);
0N/A String testSrc = System.getProperty("test.src");
0N/A System.out.println("test.src=" + testSrc);
0N/A String workingDir = System.getProperty("user.dir");
0N/A System.out.println("user.dir=" + workingDir);
0N/A
0N/A String urlCodebase;
0N/A if (testSrc.startsWith("/")) {
0N/A urlCodebase =
0N/A "file:" + testSrc.replace(File.separatorChar, '/') + "/";
0N/A } else {
0N/A urlCodebase =
0N/A "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
0N/A }
0N/A
0N/A try {
0N/A // Create MBeanServer
0N/A //
0N/A MBeanServer server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A // Create MLet instances and call getRandom on the loaded MBeans
0N/A //
0N/A for (int i = 0; i < mletInfo.length; i++) {
0N/A // Create ObjectName for MLet
0N/A //
0N/A ObjectName mlet = new ObjectName(mletInfo[i][0]);
0N/A server.createMBean("javax.management.loading.MLet", mlet);
0N/A System.out.println("MLet = " + mlet);
0N/A
0N/A // Display old library directory and set it to test.classes
0N/A //
2546N/A String libraryDirectory =
2546N/A (String) server.getAttribute(mlet, "LibraryDirectory");
2546N/A System.out.println("Old Library Directory = " +
0N/A libraryDirectory);
0N/A Attribute attribute =
0N/A new Attribute("LibraryDirectory", workingDir);
0N/A server.setAttribute(mlet, attribute);
0N/A libraryDirectory =
0N/A (String) server.getAttribute(mlet, "LibraryDirectory");
0N/A System.out.println("New Library Directory = " +
0N/A libraryDirectory);
0N/A
0N/A // Get MBeans from URL
0N/A //
0N/A String mletURL = urlCodebase + mletInfo[i][1];
0N/A System.out.println("MLet URL = " + mletURL);
0N/A Object[] params = new Object[] { mletURL };
0N/A String[] signature = new String[] {"java.lang.String"};
0N/A Object res[] = ((Set) server.invoke(mlet,
0N/A "getMBeansFromURL",
0N/A params,
0N/A signature)).toArray();
0N/A
0N/A // Iterate through all the loaded MBeans
0N/A //
0N/A for (int j = 0; j < res.length; j++) {
0N/A // Now ensure none of the returned objects is a Throwable
0N/A //
0N/A if (res[j] instanceof Throwable) {
0N/A ((Throwable) res[j]).printStackTrace(System.out);
0N/A System.out.println("Failed to load the MBean #" + j +
0N/A ". The shown Throwable was caught.");
0N/A System.exit(1);
0N/A }
0N/A
0N/A // On each of the loaded MBeans, try to invoke their
0N/A // native operation
0N/A //
0N/A Object result = null;
0N/A try {
0N/A ObjectName mbean =
0N/A ((ObjectInstance) res[j]).getObjectName();
0N/A result = server.getAttribute(mbean, "Random");
0N/A System.out.println("MBean #" + j + " = " + mbean);
0N/A System.out.println("Random number = " + result);
0N/A } catch (ReflectionException e) {
2546N/A e.getTargetException().printStackTrace(System.out);
2546N/A System.out.println("A ReflectionException, wrapping " +
0N/A "the shown exception, occured when" +
0N/A " attempting to invoke a native " +
0N/A "library based operation.");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A e.printStackTrace(System.out);
0N/A System.out.println(e.getMessage());
0N/A System.out.println("Unexpected error");
0N/A System.exit(1);
0N/A }
0N/A System.out.println("Bye! Bye!");
0N/A }
0N/A}
0N/A