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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * ident	"%Z%%M%	%I%	%E% SMI"
27  */
28 package org.opensolaris.os.dtrace;
29 
30 import java.io.File;
31 
32 /**
33  * Common functionality for all {@code org.opensolaris.os} subpackages.
34  */
35 class Utility
36 {
37     private static void
loadLibrary(String paths[], String name, boolean debug)38     loadLibrary(String paths[], String name, boolean debug)
39     {
40 	File file;
41 	for (String p : paths) {
42 	    file = new File(p);
43 	    // Allows LD_LIBRARY_PATH to include relative paths
44 	    p = file.getAbsolutePath();
45 	    try {
46 		System.load(p + "/" + name);
47 		if (debug) {
48 		    System.out.println("loaded " + name + " from " + p);
49 		}
50 		return;
51 	    } catch (UnsatisfiedLinkError e) {
52 	    }
53 	}
54 	throw (new UnsatisfiedLinkError("Unable to find " + name));
55     }
56 
57     /**
58      * Loads a library.
59      */
60     public static void
loadLibrary(String name, boolean debug)61     loadLibrary(String name, boolean debug)
62     {
63 	String path = System.getProperty("java.library.path");
64 	path = path + ":/usr/lib/64"; /* Java bug 6254947 */
65 	String[] paths = path.split(":");
66 
67 	if (debug) {
68 	    String root = System.getenv("ROOT");
69 	    if (root != null && root.length() > 0) {
70 		System.out.println("Prepending $ROOT to library path.");
71 		String[] npaths = new String[paths.length * 2];
72 		for (int i = 0; i < paths.length; i++) {
73 		    npaths[i] = root + "/" + paths[i];
74 		    npaths[i + paths.length] = paths[i];
75 		}
76 		paths = npaths;
77 	    }
78 	}
79 
80 	loadLibrary(paths, name, debug);
81     }
82 }
83