2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package com
.sun
.star
.lib
.util
;
22 import java
.net
.URLClassLoader
;
25 * Helper functions to locate and load native files.
27 * <p>The methods in this class are designed to find the requested resources in
28 * as many cases as possible. They search various places, roughly from most
29 * specific to most general. This works well if a component is known to bring
30 * with it a certain resource, and that resource has to be found. However, it
31 * might not work very well in cases where you want to check whether a
32 * component brings with it a certain resource or not: a similarly named
33 * resource from another component might be found by the eager search
36 public final class NativeLibraryLoader
{
38 * Load a system library, using a given class loader to locate the library.
40 * <p>This is similar to <code>System.loadLibrary</code>.</p>
42 * @param loader a class loader; may be null.
43 * @param libname the library name; how this name is mapped to a system
44 * library name is system dependent.
46 public static void loadLibrary(ClassLoader loader
, String libname
) {
47 String sysname
= System
.mapLibraryName(libname
);
48 // At least Oracle's 1.7.0_51 now maps to .dylib rather than .jnilib:
49 if (System
.getProperty("os.name").startsWith("Mac")
50 && sysname
.endsWith(".dylib"))
53 = sysname
.substring(0, sysname
.length() - "dylib".length())
56 File path
= getResource(loader
, sysname
);
58 // If the library cannot be found as a class loader resource, try
59 // the global System.loadLibrary as a last resort:
60 System
.loadLibrary(libname
);
62 System
.load(path
.getAbsolutePath());
67 * Locate a system resource, using a given class loader.
69 * <p>This is similar to <code>ClassLoader.getResource</code>, but only works
70 * for local resources (local files), and adds additional functionality for
71 * <code>URLClassLoaders</code>.</p>
73 * @param loader a class loader; may be null.
74 * @param name a resource name (that is, the name of a file).
75 * @return a File locating the resource, or null if the resource was not
78 public static File
getResource(ClassLoader loader
, String name
) {
80 File path
= UrlToFileMapper
.mapUrlToFile(loader
.getResource(name
));
85 // URLClassLoaders work on lists of URLs, which are typically URLs
86 // locating JAR files (scheme://auth/dir1/dir2/some.jar). The following
87 // code looks for resource name beside the JAR file
88 // (scheme://auth/dir1/dir2/name) and one directory up
89 // (scheme://auth/dir1/name). The second step is important in a typical
90 // OOo installation, where the JAR files are in the program/classes
91 // directory while the shared libraries are in the program directory.
92 if (!(loader
instanceof URLClassLoader
)) {
95 URL
[] urls
= ((URLClassLoader
) loader
).getURLs();
96 for (int i
= 0; i
< urls
.length
; ++i
) {
97 File path
= UrlToFileMapper
.mapUrlToFile(urls
[i
]);
99 File dir
= path
.isDirectory() ? path
: path
.getParentFile();
101 path
= new File(dir
, name
);
105 dir
= dir
.getParentFile();
107 path
= new File(dir
, name
);
111 // On OS X, dir is now the Resources dir,
112 // we want to look in Frameworks
113 if (System
.getProperty("os.name").startsWith("Mac")
114 && dir
.getName().equals("Resources")) {
115 dir
= dir
.getParentFile();
116 path
= new File(dir
, "Frameworks/" + name
);
128 private NativeLibraryLoader() {} // do not instantiate