EMMA Coverage Report (generated Mon Mar 20 21:34:30 PST 2006)
[all classes][com.moesol.bindings]

COVERAGE SUMMARY FOR SOURCE FILE [NativeLibraryLoader.java]

nameclass, %method, %block, %line, %
NativeLibraryLoader.java100% (1/1)91%  (10/11)79%  (111/141)72%  (26.7/37)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NativeLibraryLoader100% (1/1)91%  (10/11)79%  (111/141)72%  (26.7/37)
NativeLibraryLoader (): void 0%   (0/1)0%   (0/3)0%   (0/1)
findAndLoad (String): boolean 100% (1/1)46%  (12/26)44%  (4/9)
loadLibrary (String): void 100% (1/1)50%  (5/10)50%  (3/6)
findUsingPrefs (String): String 100% (1/1)75%  (15/20)92%  (2.8/3)
checkDirectory (String, String): boolean 100% (1/1)86%  (12/14)75%  (3/4)
<static initializer> 100% (1/1)94%  (15/16)96%  (1.9/2)
buildFullPathToLibrary (String, String): String 100% (1/1)100% (12/12)100% (1/1)
buildKeyForLibrary (String): String 100% (1/1)100% (9/9)100% (1/1)
findUsingDevProperties (String): String 100% (1/1)100% (4/4)100% (1/1)
load (String, String): void 100% (1/1)100% (9/9)100% (4/4)
maybeLog (String): void 100% (1/1)100% (18/18)100% (5/5)

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 
13package com.moesol.bindings;
14 
15import java.io.File;
16import java.util.HashSet;
17import java.util.Set;
18import java.util.logging.Level;
19import java.util.logging.Logger;
20import java.util.prefs.Preferences;
21 
22import com.moesol.util.DeveloperProperties;
23 
24/**
25 * @author robert
26 */
27public 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}

[all classes][com.moesol.bindings]
EMMA 2.0.5312 (C) Vladimir Roubtsov