merge the formfield patch from ooo-build
[ooovba.git] / jurt / com / sun / star / lib / util / NativeLibraryLoader.java
blob0920edd563c1f441e6f630cae247067c68cf8568
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: NativeLibraryLoader.java,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package com.sun.star.lib.util;
32 import java.io.File;
33 import java.net.URL;
34 import java.net.URLClassLoader;
36 /** Helper functions to locate and load native files.
38 The methods in this class are designed to find the requested resources in as
39 many cases as possible. They search various places, roughly from most
40 specific to most general. This works well if a component is known to bring
41 with it a certain resource, and that resource has to be found. However, it
42 might not work very well in cases where you want to check whether a
43 component brings with it a certain resource or not: a similarly named
44 resource from another component might be found by the eager search
45 algorithm.
47 public final class NativeLibraryLoader {
48 /** Load a system library, using a given class loader to locate the library.
50 This is similar to System.loadLibrary.
52 @param loader a class loader; may be null
54 @param libname the library name; how this name is mapped to a system
55 library name is system dependent
57 public static void loadLibrary(ClassLoader loader, String libname) {
58 File path = getResource(loader, System.mapLibraryName(libname));
59 if (path == null) {
60 // If the library cannot be found as a class loader resource, try
61 // the global System.loadLibrary as a last resort:
62 System.loadLibrary(libname);
63 } else {
64 System.load(path.getAbsolutePath());
68 /** Locate a system resource, using a given class loader.
70 This is similar to ClassLoader.getResource, but only works for local
71 resources (local files), and adds additional functionality for
72 URLClassLoaders.
74 @param loader a class loader; may be null
76 @param name a resource name (that is, the name of a file)
78 @return a File locating the resource, or null if the resource was not
79 found
81 public static File getResource(ClassLoader loader, String name) {
82 if (loader != null) {
83 File path = UrlToFileMapper.mapUrlToFile(loader.getResource(name));
84 if (path != null) {
85 return path;
88 // URLClassLoaders work on lists of URLs, which are typically URLs
89 // locating JAR files (scheme://auth/dir1/dir2/some.jar). The following
90 // code looks for resource name beside the JAR file
91 // (scheme://auth/dir1/dir2/name) and one directory up
92 // (scheme://auth/dir1/name). The second step is important in a typical
93 // OOo installation, where the JAR files are in the program/classes
94 // directory while the shared libraries are in the program directory.
95 if (loader instanceof URLClassLoader) {
96 URL[] urls = ((URLClassLoader) loader).getURLs();
97 for (int i = 0; i < urls.length; ++i) {
98 File path = UrlToFileMapper.mapUrlToFile(urls[i]);
99 if (path != null) {
100 File dir = path.isDirectory() ? path : path.getParentFile();
101 if (dir != null) {
102 path = new File(dir, name);
103 if (path.exists()) {
104 return path;
106 dir = dir.getParentFile();
107 if (dir != null) {
108 path = new File(dir, name);
109 if (path.exists()) {
110 return path;
117 return null;
120 private NativeLibraryLoader() {} // do not instantiate