author | Stephen Talley <stephen.talley@oracle.com> |
Mon, 28 Mar 2011 10:53:34 -0400 | |
changeset 685 | 767674b0a2fb |
parent 219 | 57841c113efe |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
5 |
* Common Development and Distribution License (the "License"). |
|
6 |
* You may not use this file except in compliance with the License. |
|
7 |
* |
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
21 |
||
22 |
/* |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
23 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
0 | 24 |
*/ |
25 |
||
219
57841c113efe
6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
26 |
package org.opensolaris.os.vp.util.misc; |
0 | 27 |
|
28 |
import java.io.File; |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
29 |
import java.net.*; |
0 | 30 |
import java.security.CodeSource; |
31 |
||
32 |
/** |
|
33 |
* Common functionality for all org.opensolaris.os subpackages. |
|
34 |
*/ |
|
35 |
public class LibraryUtil |
|
36 |
{ |
|
37 |
public interface LibraryLoader |
|
38 |
{ |
|
39 |
/** |
|
40 |
* A function that calls System.load(filename). |
|
41 |
* Must be implemented in a class loaded by the |
|
42 |
* classloader the native library should be loaded by. |
|
43 |
*/ |
|
44 |
public void load(String filename); |
|
45 |
} |
|
46 |
||
47 |
/** |
|
48 |
* If the given class comes from jar file with a path of the |
|
49 |
* form path/lib/java/jarfile.jar, returns path/lib. This lets |
|
50 |
* us cleaning find co-located native libraries (i.e. without |
|
51 |
* employing a LD_LIBRARY_PATH-setting wrapper script). |
|
52 |
*/ |
|
53 |
static private String classdir(Class c) |
|
54 |
{ |
|
55 |
try { |
|
56 |
CodeSource cs = c.getProtectionDomain().getCodeSource(); |
|
57 |
if (cs == null) |
|
58 |
return (null); |
|
59 |
File f = new File(cs.getLocation().toURI()); |
|
60 |
if (!f.getName().endsWith(".jar")) |
|
61 |
return (null); |
|
62 |
||
63 |
/* |
|
64 |
* We could obtain the absolute file here, |
|
65 |
* but I don't think we should care about |
|
66 |
* people setting relative class paths while |
|
67 |
* running in lib/ or lib/java/. There's |
|
68 |
* probably greater hazard in assuming we can |
|
69 |
* traverse the absolute path. |
|
70 |
*/ |
|
71 |
f = f.getParentFile(); |
|
72 |
if (f == null || !(f.getName() != "java")) |
|
73 |
return (null); |
|
74 |
f = f.getParentFile(); |
|
75 |
if (f == null || !(f.getName() != "lib")) |
|
76 |
return (null); |
|
77 |
||
78 |
/* |
|
79 |
* We made it. Return the path. |
|
80 |
* |
|
81 |
* XXX: Contrary to the above comment, |
|
82 |
* Runtime.load() insists that this path be |
|
83 |
* absolute. |
|
84 |
*/ |
|
85 |
return (f.getPath()); |
|
86 |
} catch (SecurityException e) { |
|
87 |
/* Couldn't access the protection domain. */ |
|
88 |
return (null); |
|
89 |
} catch (IllegalArgumentException e) { |
|
90 |
/* Code source wasn't a file. */ |
|
91 |
return (null); |
|
92 |
} catch (URISyntaxException e) { |
|
93 |
/* Code source wasn't a file. */ |
|
94 |
return (null); |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
static private void loadLibrary(LibraryLoader loader, String paths[], |
|
99 |
String name, boolean debug) |
|
100 |
{ |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
101 |
StringBuilder buffer = new StringBuilder(); |
0 | 102 |
buffer.append("Unable to find ").append(name).append(':'); |
103 |
||
104 |
for (String p : paths) { |
|
105 |
try { |
|
106 |
String path = p + "/" + name; |
|
107 |
if (loader != null) |
|
108 |
loader.load(path); |
|
109 |
else |
|
110 |
System.load(path); |
|
111 |
if (debug) |
|
112 |
System.err.println("loaded " + name + |
|
113 |
" from " + p); |
|
114 |
return; |
|
115 |
} catch (UnsatisfiedLinkError e) { |
|
116 |
/* |
|
117 |
* Unfortunately, there are many |
|
118 |
* reasons why we might get an |
|
119 |
* UnsatisfiedLinkError, but there is |
|
120 |
* no way to distinguish them other |
|
121 |
* than to case off of their messages. |
|
122 |
* (Of particular interest is the |
|
123 |
* "already loaded" error.) |
|
124 |
* |
|
125 |
* Until we are ready to soil our hands |
|
126 |
* doing so, callers of loadLibrary |
|
127 |
* will get a single, conflated |
|
128 |
* exception. Sigh. |
|
129 |
*/ |
|
130 |
buffer.append("\n ").append(p).append(": "). |
|
131 |
append(e.getMessage()); |
|
132 |
||
133 |
} |
|
134 |
} |
|
135 |
throw (new UnsatisfiedLinkError(buffer.toString())); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Loads a library. |
|
140 |
* <p> |
|
141 |
* System.load() always loads the specified library in the |
|
142 |
* caller's classloader. To permit loading native libraries in |
|
143 |
* classloaders other than LibraryUtil's, the caller may supply |
|
144 |
* an implementation of LibraryLoader defined by the desired |
|
145 |
* classloader. |
|
146 |
*/ |
|
147 |
static public void loadLibrary(Class c, LibraryLoader loader, |
|
148 |
String name, boolean debug) |
|
149 |
{ |
|
150 |
String path = System.getProperty("java.library.path"); |
|
151 |
String classdir = classdir(c); |
|
152 |
if (classdir != null) |
|
153 |
path = classdir + ":" + classdir + "/64:" + path; |
|
154 |
path = path + ":/usr/lib/64"; /* XXX: Java bug 6254947 */ |
|
155 |
String[] paths = path.split(":"); |
|
156 |
||
157 |
if (debug) { |
|
158 |
String root = System.getenv("ROOT"); |
|
159 |
if (root != null && root.length() > 0) { |
|
160 |
System.err.println( |
|
161 |
"Prepending $ROOT to library path."); |
|
162 |
String[] npaths = new String[paths.length * 2]; |
|
163 |
for (int i = 0; i < paths.length; i++) { |
|
164 |
npaths[i] = root + "/" + paths[i]; |
|
165 |
npaths[i + paths.length] = paths[i]; |
|
166 |
} |
|
167 |
paths = npaths; |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
loadLibrary(loader, paths, name, debug); |
|
172 |
} |
|
173 |
} |