components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/misc/LibraryUtil.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     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 (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.misc;
       
    27 
       
    28 import java.io.File;
       
    29 import java.net.*;
       
    30 import java.security.CodeSource;
       
    31 
       
    32 /**
       
    33  * Common functionality for all com.oracle.solaris 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 	{
       
   101 		StringBuilder buffer = new StringBuilder();
       
   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 }