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