xref: /AOO41X/main/jurt/com/sun/star/lib/util/NativeLibraryLoader.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.lib.util;
28 
29 import java.io.File;
30 import java.net.URL;
31 import java.net.URLClassLoader;
32 
33 /** Helper functions to locate and load native files.
34 
35     The methods in this class are designed to find the requested resources in as
36     many cases as possible.  They search various places, roughly from most
37     specific to most general.  This works well if a component is known to bring
38     with it a certain resource, and that resource has to be found.  However, it
39     might not work very well in cases where you want to check whether a
40     component brings with it a certain resource or not: a similarly named
41     resource from another component might be found by the eager search
42     algorithm.
43  */
44 public final class NativeLibraryLoader {
45     /** Load a system library, using a given class loader to locate the library.
46 
47         This is similar to System.loadLibrary.
48 
49         @param loader a class loader; may be null
50 
51         @param libname the library name; how this name is mapped to a system
52         library name is system dependent
53      */
54     public static void loadLibrary(ClassLoader loader, String libname) {
55         File path = getResource(loader, System.mapLibraryName(libname));
56         if (path == null) {
57             // If the library cannot be found as a class loader resource, try
58             // the global System.loadLibrary as a last resort:
59             System.loadLibrary(libname);
60         } else {
61             System.load(path.getAbsolutePath());
62         }
63     }
64 
65     /** Locate a system resource, using a given class loader.
66 
67         This is similar to ClassLoader.getResource, but only works for local
68         resources (local files), and adds additional functionality for
69         URLClassLoaders.
70 
71         @param loader a class loader; may be null
72 
73         @param name a resource name (that is, the name of a file)
74 
75         @return a File locating the resource, or null if the resource was not
76         found
77      */
78     public static File getResource(ClassLoader loader, String name) {
79         if (loader != null) {
80             File path = UrlToFileMapper.mapUrlToFile(loader.getResource(name));
81             if (path != null) {
82                 return path;
83             }
84         }
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) {
93             URL[] urls = ((URLClassLoader) loader).getURLs();
94             for (int i = 0; i < urls.length; ++i) {
95                 File path = UrlToFileMapper.mapUrlToFile(urls[i]);
96                 if (path != null) {
97                     File dir = path.isDirectory() ? path : path.getParentFile();
98                     if (dir != null) {
99                         path = new File(dir, name);
100                         if (path.exists()) {
101                             return path;
102                         }
103                         dir = dir.getParentFile();
104                         if (dir != null) {
105                             path = new File(dir, name);
106                             if (path.exists()) {
107                                 return path;
108                             }
109                         }
110                     }
111                 }
112             }
113         }
114         return null;
115     }
116 
117     private NativeLibraryLoader() {} // do not instantiate
118 }
119