1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package com.sun.star.lib.util; 24 25 import java.io.File; 26 import java.net.URL; 27 import java.net.URLClassLoader; 28 29 /** Helper functions to locate and load native files. 30 31 The methods in this class are designed to find the requested resources in as 32 many cases as possible. They search various places, roughly from most 33 specific to most general. This works well if a component is known to bring 34 with it a certain resource, and that resource has to be found. However, it 35 might not work very well in cases where you want to check whether a 36 component brings with it a certain resource or not: a similarly named 37 resource from another component might be found by the eager search 38 algorithm. 39 */ 40 public final class NativeLibraryLoader { 41 /** Load a system library, using a given class loader to locate the library. 42 43 This is similar to System.loadLibrary. 44 45 @param loader a class loader; may be null 46 47 @param libname the library name; how this name is mapped to a system 48 library name is system dependent 49 */ loadLibrary(ClassLoader loader, String libname)50 public static void loadLibrary(ClassLoader loader, String libname) { 51 String syslibname = System.mapLibraryName(libname); 52 53 File path = getResource(loader, System.mapLibraryName(libname)); 54 if (path == null) { 55 // check special jni lib extension on Mac 56 if (syslibname.contains("dylib")) { 57 syslibname = syslibname.replace("dylib", "jnilib"); 58 } 59 path = getResource(loader, syslibname); 60 } 61 62 if (path == null) { 63 // If the library cannot be found as a class loader resource, try 64 // the global System.loadLibrary as a last resort: 65 System.loadLibrary(libname); 66 } else { 67 System.load(path.getAbsolutePath()); 68 } 69 } 70 71 /** Locate a system resource, using a given class loader. 72 73 This is similar to ClassLoader.getResource, but only works for local 74 resources (local files), and adds additional functionality for 75 URLClassLoaders. 76 77 @param loader a class loader; may be null 78 79 @param name a resource name (that is, the name of a file) 80 81 @return a File locating the resource, or null if the resource was not 82 found 83 */ getResource(ClassLoader loader, String name)84 public static File getResource(ClassLoader loader, String name) { 85 if (loader != null) { 86 File path = UrlToFileMapper.mapUrlToFile(loader.getResource(name)); 87 if (path != null) { 88 return path; 89 } 90 } 91 // URLClassLoaders work on lists of URLs, which are typically URLs 92 // locating JAR files (scheme://auth/dir1/dir2/some.jar). The following 93 // code looks for resource name beside the JAR file 94 // (scheme://auth/dir1/dir2/name) and one directory up 95 // (scheme://auth/dir1/name). The second step is important in a typical 96 // OOo installation, where the JAR files are in the program/classes 97 // directory while the shared libraries are in the program directory. 98 if (loader instanceof URLClassLoader) { 99 URL[] urls = ((URLClassLoader) loader).getURLs(); 100 for (int i = 0; i < urls.length; ++i) { 101 File path = UrlToFileMapper.mapUrlToFile(urls[i]); 102 if (path != null) { 103 File dir = path.isDirectory() ? path : path.getParentFile(); 104 if (dir != null) { 105 path = new File(dir, name); 106 if (path.exists()) { 107 return path; 108 } 109 dir = dir.getParentFile(); 110 if (dir != null) { 111 path = new File(dir, name); 112 if (path.exists()) { 113 return path; 114 } 115 } 116 } 117 } 118 } 119 } 120 return null; 121 } 122 NativeLibraryLoader()123 private NativeLibraryLoader() {} // do not instantiate 124 } 125