aboutsummaryrefslogtreecommitdiff
path: root/test/javax/management/loading/LibraryLoader/LibraryLoaderTest.java
blob: 819cc9c546f5ab8ac199e0fb3dd094c0d6f7c121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4969756
 * @summary Test that the same native library coming from the same jar file can
 * be loaded twice by two different MLets on the same JVM without conflict.
 * @author Luis-Miguel Alventosa
 * @run clean LibraryLoaderTest
 * @run build LibraryLoaderTest
 * @run main LibraryLoaderTest
 */

import java.io.File;
import java.util.Set;
import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;

public class LibraryLoaderTest {

    private static final String mletInfo[][] = {
        {"testDomain:type=MLet,index=0", "UseNativeLib0.html"},
        {"testDomain:type=MLet,index=1", "UseNativeLib1.html"}
    };

    public static void main (String args[]) {

        String osName = System.getProperty("os.name");
        System.out.println("os.name=" + osName);
        String osArch = System.getProperty("os.arch");
        System.out.println("os.name=" + osArch);

        // Check for supported platforms:
        //
        // Solaris/SPARC and Windows/x86
        //
        if ((!(osName.equals("SunOS") && osArch.equals("sparc"))) &&
            (!(osName.startsWith("Windows") && osArch.equals("x86")))) {
            System.out.println(
              "This test runs only on Solaris/SPARC and Windows/x86 platforms");
            System.out.println("Bye! Bye!");
            return;
        }

        String libPath = System.getProperty("java.library.path");
        System.out.println("java.library.path=" + libPath);
        String testSrc = System.getProperty("test.src");
        System.out.println("test.src=" + testSrc);
        String workingDir = System.getProperty("user.dir");
        System.out.println("user.dir=" + workingDir);

        String urlCodebase;
        if (testSrc.startsWith("/")) {
            urlCodebase =
                "file:" + testSrc.replace(File.separatorChar, '/') + "/";
        } else {
            urlCodebase =
                "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
        }

        try {
            // Create MBeanServer
            //
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            // Create MLet instances and call getRandom on the loaded MBeans
            //
            for (int i = 0; i < mletInfo.length; i++) {
                // Create ObjectName for MLet
                //
                ObjectName mlet = new ObjectName(mletInfo[i][0]);
                server.createMBean("javax.management.loading.MLet", mlet);
                System.out.println("MLet = " + mlet);

                // Display old library directory and set it to test.classes
                //
                String libraryDirectory =
                    (String) server.getAttribute(mlet, "LibraryDirectory");
                System.out.println("Old Library Directory = " +
                                   libraryDirectory);
                Attribute attribute =
                    new Attribute("LibraryDirectory", workingDir);
                server.setAttribute(mlet, attribute);
                libraryDirectory =
                    (String) server.getAttribute(mlet, "LibraryDirectory");
                System.out.println("New Library Directory = " +
                                   libraryDirectory);

                // Get MBeans from URL
                //
                String mletURL = urlCodebase + mletInfo[i][1];
                System.out.println("MLet URL = " + mletURL);
                Object[] params = new Object[] { mletURL };
                String[] signature = new String[] {"java.lang.String"};
                Object res[] = ((Set) server.invoke(mlet,
                                                    "getMBeansFromURL",
                                                    params,
                                                    signature)).toArray();

                // Iterate through all the loaded MBeans
                //
                for (int j = 0; j < res.length; j++) {
                    // Now ensure none of the returned objects is a Throwable
                    //
                    if (res[j] instanceof Throwable) {
                        ((Throwable) res[j]).printStackTrace(System.out);
                        System.out.println("Failed to load the MBean #" + j +
                                           ". The shown Throwable was caught.");
                        System.exit(1);
                    }

                    // On each of the loaded MBeans, try to invoke their
                    // native operation
                    //
                    Object result = null;
                    try {
                        ObjectName mbean =
                            ((ObjectInstance) res[j]).getObjectName();
                        result = server.getAttribute(mbean, "Random");
                        System.out.println("MBean #" + j + " = " + mbean);
                        System.out.println("Random number = " + result);
                    } catch (ReflectionException e) {
                        e.getTargetException().printStackTrace(System.out);
                        System.out.println("A ReflectionException, wrapping " +
                                           "the shown exception, occured when" +
                                           " attempting to invoke a native " +
                                           "library based operation.");
                        System.exit(1);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
            System.out.println(e.getMessage());
            System.out.println("Unexpected error");
            System.exit(1);
        }
        System.out.println("Bye! Bye!");
    }
}