bump product version to 6.4.0.3
[LibreOffice.git] / jurt / com / sun / star / lib / util / NativeLibraryLoader.java
blobeb5c6af34e90f4d50e217fd01fc821469a206db6
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.lib.util;
21 import java.io.File;
22 import java.net.URL;
23 import java.net.URLClassLoader;
25 /**
26 * Helper functions to locate and load native files.
28 * <p>The methods in this class are designed to find the requested resources in
29 * as many cases as possible. They search various places, roughly from most
30 * specific to most general. This works well if a component is known to bring
31 * with it a certain resource, and that resource has to be found. However, it
32 * might not work very well in cases where you want to check whether a
33 * component brings with it a certain resource or not: a similarly named
34 * resource from another component might be found by the eager search
35 * algorithm.</p>
37 public final class NativeLibraryLoader {
38 /**
39 * Load a system library, using a given class loader to locate the library.
41 * <p>This is similar to <code>System.loadLibrary</code>.</p>
43 * @param loader a class loader; may be null.
44 * @param libname the library name; how this name is mapped to a system
45 * library name is system dependent.
47 public static void loadLibrary(ClassLoader loader, String libname) {
48 String sysname = System.mapLibraryName(libname);
49 // At least Oracle's 1.7.0_51 now maps to .dylib rather than .jnilib:
50 if (System.getProperty("os.name").startsWith("Mac")
51 && sysname.endsWith(".dylib"))
53 sysname
54 = sysname.substring(0, sysname.length() - "dylib".length())
55 + "jnilib";
57 File path = getResource(loader, sysname);
58 if (path == null) {
59 // If the library cannot be found as a class loader resource, try
60 // the global System.loadLibrary as a last resort:
61 System.loadLibrary(libname);
62 } else {
63 System.load(path.getAbsolutePath());
67 /**
68 * Locate a system resource, using a given class loader.
70 * <p>This is similar to <code>ClassLoader.getResource</code>, but only works
71 * for local resources (local files), and adds additional functionality for
72 * <code>URLClassLoaders</code>.</p>
74 * @param loader a class loader; may be null.
75 * @param name a resource name (that is, the name of a file).
76 * @return a File locating the resource, or null if the resource was not
77 * found.
79 public static File getResource(ClassLoader loader, String name) {
80 if (loader != null) {
81 File path = UrlToFileMapper.mapUrlToFile(loader.getResource(name));
82 if (path != null) {
83 return path;
86 // URLClassLoaders work on lists of URLs, which are typically URLs
87 // locating JAR files (scheme://auth/dir1/dir2/some.jar). The following
88 // code looks for resource name beside the JAR file
89 // (scheme://auth/dir1/dir2/name) and one directory up
90 // (scheme://auth/dir1/name). The second step is important in a typical
91 // OOo installation, where the JAR files are in the program/classes
92 // directory while the shared libraries are in the program directory.
93 if (!(loader instanceof URLClassLoader)) {
94 return null;
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;
112 // On macOS, dir is now the Resources dir,
113 // we want to look in Frameworks
114 if (System.getProperty("os.name").startsWith("Mac")
115 && dir.getName().equals("Resources")) {
116 dir = dir.getParentFile();
117 path = new File(dir, "Frameworks/" + name);
118 if (path.exists()) {
119 return path;
126 return null;
129 private NativeLibraryLoader() {} // do not instantiate
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */