1 | /* |
2 | * $Id: NativeLibraryLoader.java,v 1.8 2005/06/29 07:35:21 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | |
13 | package com.moesol.bindings; |
14 | |
15 | import java.io.File; |
16 | import java.util.HashSet; |
17 | import java.util.Set; |
18 | import java.util.logging.Level; |
19 | import java.util.logging.Logger; |
20 | import java.util.prefs.Preferences; |
21 | |
22 | import com.moesol.util.DeveloperProperties; |
23 | |
24 | /** |
25 | * @author robert |
26 | */ |
27 | public class NativeLibraryLoader { |
28 | /** |
29 | * Calls System.loadLibrary and if that fails, uses Preferences to try to |
30 | * find the full path to the library. If that fails it uses |
31 | * DeveloperProperties to try to find the full path to the library. |
32 | * |
33 | * The preference tree com/moesol/bindings/NativeLibraryLoader is checked |
34 | * for name + ".library.path". |
35 | * |
36 | * The DeveloperProperties is checked for name + ".library.path". |
37 | * |
38 | * In both cases the value should be the directory that contains the |
39 | * library. |
40 | * |
41 | * @param name |
42 | * The library to load. |
43 | */ |
44 | public static void loadLibrary(String name) { |
45 | try { |
46 | System.loadLibrary(name); |
47 | } catch (UnsatisfiedLinkError e) { |
48 | if (!findAndLoad(name)) { |
49 | throw e; |
50 | } |
51 | } |
52 | } |
53 | private static boolean findAndLoad(String name) { |
54 | String directory; |
55 | |
56 | directory = findUsingDevProperties(name); |
57 | if (checkDirectory(directory, name)) { |
58 | load(directory, name); |
59 | return true; |
60 | } |
61 | |
62 | directory = findUsingPrefs(name); |
63 | if (checkDirectory(directory, name)) { |
64 | load(directory, name); |
65 | return true; |
66 | } |
67 | return false; |
68 | } |
69 | |
70 | static boolean checkDirectory(String directory, String name) { |
71 | if (directory == null) { |
72 | return false; |
73 | } |
74 | File check = new File(buildFullPathToLibrary(directory, name)); |
75 | return check.canRead(); |
76 | } |
77 | |
78 | /** |
79 | * @param name |
80 | * @return property key |
81 | */ |
82 | static String buildKeyForLibrary(String name) { |
83 | return name + ".library.path"; |
84 | } |
85 | /** |
86 | * Prepend directory_path and append the system shared library/dll extension |
87 | * to compute the full path to the library. |
88 | * |
89 | * @param director_path |
90 | * @param name |
91 | * @return full path to library |
92 | */ |
93 | static String buildFullPathToLibrary(String director_path, String name) { |
94 | return director_path + File.separator + System.mapLibraryName(name); |
95 | } |
96 | |
97 | /** |
98 | * @param name |
99 | * @return path using dev properties |
100 | */ |
101 | static String findUsingDevProperties(String name) { |
102 | return DeveloperProperties.getProperty(buildKeyForLibrary(name)); |
103 | } |
104 | static String findUsingPrefs(String name) { |
105 | Preferences package_prefs = Preferences.systemNodeForPackage(NativeLibraryLoader.class); |
106 | Preferences class_prefs = package_prefs.node("NativeLibraryLoader"); |
107 | return class_prefs.get(buildKeyForLibrary(name), null); |
108 | } |
109 | private static void load(String directory, String name) { |
110 | String full_path = buildFullPathToLibrary(directory, name); |
111 | maybeLog(full_path); |
112 | System.load(full_path); |
113 | } |
114 | /** |
115 | * If logging is on and we have not already logged this |
116 | * library, output a log message |
117 | * |
118 | * @param full_path |
119 | */ |
120 | private static void maybeLog(String full_path) { |
121 | if (logger.isLoggable(Level.INFO)) { |
122 | if (!s_loaded.contains(full_path)) { |
123 | logger.log(Level.INFO, "Loading: {0}", full_path); |
124 | s_loaded.add(full_path); |
125 | } |
126 | } |
127 | } |
128 | |
129 | private static Set s_loaded = new HashSet(); |
130 | private static Logger logger = Logger.getLogger(NativeLibraryLoader.class.getName()); |
131 | } |