1 /*
2  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * - Redistribution of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistribution in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23  * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26  * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27  * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29  * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30  * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed or intended for use
33  * in the design, construction, operation or maintenance of any nuclear
34  * facility.
35  *
36  * Sun gratefully acknowledges that this software was originally authored
37  * and developed by Kenneth Bradley Russell and Christopher John Kline.
38  */
39 
40 package com.sun.gluegen.runtime;
41 
42 import java.lang.reflect.InvocationTargetException;
43 import java.lang.reflect.Method;
44 import java.security.*;
45 
46 /** Class providing control over whether GlueGen loads the native code
47     associated with the NativeLibrary implementation. Alternative app
48     launchers such as those running within applets may want to disable
49     this default loading behavior and load the native code via another
50     (manual) mechanism. */
51 public class NativeLibLoader {
52   private static volatile boolean loadingEnabled = true;
53   private static volatile boolean didLoading;
54 
disableLoading()55   public static void disableLoading() {
56     loadingEnabled = false;
57   }
58 
enableLoading()59   public static void enableLoading() {
60     loadingEnabled = true;
61   }
62 
loadGlueGenRT()63   public static void loadGlueGenRT() {
64     if (!didLoading && loadingEnabled) {
65       synchronized (NativeLibLoader.class) {
66         if (!didLoading && loadingEnabled) {
67           didLoading = true;
68           AccessController.doPrivileged(new PrivilegedAction() {
69               public Object run() {
70                 loadLibraryInternal("gluegen-rt");
71                 return null;
72               }
73             });
74         }
75       }
76     }
77   }
78 
loadLibraryInternal(String libraryName)79   private static void loadLibraryInternal(String libraryName) {
80     String sunAppletLauncher = System.getProperty("sun.jnlp.applet.launcher");
81     boolean usingJNLPAppletLauncher = Boolean.valueOf(sunAppletLauncher).booleanValue();
82 
83     if (usingJNLPAppletLauncher) {
84         try {
85           Class jnlpAppletLauncherClass = Class.forName("org.jdesktop.applet.util.JNLPAppletLauncher");
86           Method jnlpLoadLibraryMethod = jnlpAppletLauncherClass.getDeclaredMethod("loadLibrary", new Class[] { String.class });
87           jnlpLoadLibraryMethod.invoke(null, new Object[] { libraryName });
88         } catch (Exception e) {
89           Throwable t = e;
90           if (t instanceof InvocationTargetException) {
91             t = ((InvocationTargetException) t).getTargetException();
92           }
93           if (t instanceof Error)
94             throw (Error) t;
95           if (t instanceof RuntimeException) {
96             throw (RuntimeException) t;
97           }
98           // Throw UnsatisfiedLinkError for best compatibility with System.loadLibrary()
99           throw (UnsatisfiedLinkError) new UnsatisfiedLinkError().initCause(e);
100         }
101     } else {
102       System.loadLibrary(libraryName);
103     }
104   }
105 }
106