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 */ 50 public static void loadLibrary(ClassLoader loader, String libname) { 51 File path = getResource(loader, System.mapLibraryName(libname)); 52 if (path == null) { 53 // If the library cannot be found as a class loader resource, try 54 // the global System.loadLibrary as a last resort: 55 System.loadLibrary(libname); 56 } else { 57 System.load(path.getAbsolutePath()); 58 } 59 } 60 61 /** Locate a system resource, using a given class loader. 62 63 This is similar to ClassLoader.getResource, but only works for local 64 resources (local files), and adds additional functionality for 65 URLClassLoaders. 66 67 @param loader a class loader; may be null 68 69 @param name a resource name (that is, the name of a file) 70 71 @return a File locating the resource, or null if the resource was not 72 found 73 */ 74 public static File getResource(ClassLoader loader, String name) { 75 if (loader != null) { 76 File path = UrlToFileMapper.mapUrlToFile(loader.getResource(name)); 77 if (path != null) { 78 return path; 79 } 80 } 81 // URLClassLoaders work on lists of URLs, which are typically URLs 82 // locating JAR files (scheme://auth/dir1/dir2/some.jar). The following 83 // code looks for resource name beside the JAR file 84 // (scheme://auth/dir1/dir2/name) and one directory up 85 // (scheme://auth/dir1/name). The second step is important in a typical 86 // OOo installation, where the JAR files are in the program/classes 87 // directory while the shared libraries are in the program directory. 88 if (loader instanceof URLClassLoader) { 89 URL[] urls = ((URLClassLoader) loader).getURLs(); 90 for (int i = 0; i < urls.length; ++i) { 91 File path = UrlToFileMapper.mapUrlToFile(urls[i]); 92 if (path != null) { 93 File dir = path.isDirectory() ? path : path.getParentFile(); 94 if (dir != null) { 95 path = new File(dir, name); 96 if (path.exists()) { 97 return path; 98 } 99 dir = dir.getParentFile(); 100 if (dir != null) { 101 path = new File(dir, name); 102 if (path.exists()) { 103 return path; 104 } 105 } 106 } 107 } 108 } 109 } 110 return null; 111 } 112 113 private NativeLibraryLoader() {} // do not instantiate 114 } 115