summaryrefslogtreecommitdiffstats
path: root/java/org/tinyb/PlatformToolkit.java
blob: 99f9f0e7b8149324093e0f5ed8595cbe8147452e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tinyb;

import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Miscellaneous platform utilities, allowed to be used within same Java package.
 */
final class PlatformToolkit {
    public enum OSType {
        UNIX, MACOS, IOS, WINDOWS;
    }

    /** Lower case system property '{@code os.name}'. */
    static final String os_name;
    /** Lower case system property '{@code os.arch}' */
    static final String os_arch;
    private static final String user_dir;
    private static final String java_user_lib_path;
    private static final String java_boot_lib_path;

    /**
     * Unique platform denominator composed as '{@link #os_name}' + '-' + '{@link #os_arch}'.
     */
    static final String os_and_arch;
    static final OSType OS_TYPE;
    private static final boolean isOSX;

    private static final String prefix;
    private static final String suffix;

    static {
        {
            final String[] props =
                    AccessController.doPrivileged(new PrivilegedAction<String[]>() {
                        @Override
                        public String[] run() {
                            final String[] props = new String[5];
                            int i=0;
                            props[i++] = System.getProperty("os.name").toLowerCase(); // 0
                            props[i++] = System.getProperty("os.arch").toLowerCase(); // 1
                            props[i++] = System.getProperty("user.dir"); // 2
                            props[i++] = System.getProperty("java.library.path"); // 3
                            props[i++] = System.getProperty("sun.boot.library.path"); // 4
                            return props;
                        }
                    });
            int i=0;
            os_name = props[i++];
            os_arch = props[i++];
            user_dir = props[i++];
            java_user_lib_path = props[i++];
            java_boot_lib_path = props[i++];
        }

        if( null != os_name && os_name.length() > 0 && null != os_arch && os_arch.length() > 0 ) {
            os_and_arch = os_name+"-"+os_arch;
        } else {
            os_and_arch = null;
        }

        if ( os_name.startsWith("mac os x") ||
                os_name.startsWith("darwin") ) {
            OS_TYPE = OSType.MACOS;
            isOSX = true;
        } else if ( os_name.startsWith("ios") ) {
            OS_TYPE = OSType.IOS;
            isOSX = true;
        } else if ( os_name.startsWith("windows") ) {
            OS_TYPE = OSType.WINDOWS;
            isOSX = false;
        } else {
            OS_TYPE = OSType.UNIX;
            isOSX = false;
        }

        switch (OS_TYPE) {
        case WINDOWS:
            prefix = "";
            suffix = ".dll";
            break;

        case MACOS:
        case IOS:
            prefix = "lib";
            suffix = ".dylib";
            break;

        case UNIX:
        default:
            prefix = "lib";
            suffix = ".so";
            break;
        }
    }

    /**
     * Produces a list of potential full native library pathnames, denoted by its {@code libBaseName}.
     *
     * <p>
     * Basic order of library locations
     * <pre>
     * User locations:
     *   - current working directory + {@link #os_and_arch}
     *   - iterate through paths within 'java.library.path', adding {@link #os_and_arch} to each
     *   - current working directory
     *   - iterate through paths within 'java.library.path'
     *
     * System locations:
     *   - optional OSX path
     *   - iterate through paths within 'sun.boot.library.path'
     * </pre>
     * </p>
     *
     * <p>
     * Example:
     * <pre>
         /usr/local/projects/direct_bt/dist-amd64/linux-amd64/libdirect_bt.so (addPath cwd.os_and_arch)
         /usr/local/projects/direct_bt/dist-amd64/lib/linux-amd64/libdirect_bt.so (addPath java-user-libpath.os_and_arch:0)
         /usr/local/projects/direct_bt/dist-amd64/libdirect_bt.so (addPath cwd)
         /usr/local/projects/direct_bt/dist-amd64/lib/libdirect_bt.so (addPath java-user-libpath:0)
         /usr/lib/jvm/java-14-openjdk-amd64/lib/libdirect_bt.so (addPath java-boot-libpath:0)
     * </pre>
     *
     * @param libBaseName library basename without prefix (like 'lib') or suffix like '.so'.
     * @param searchSystemPath
     * @param searchSystemPathFirst
     * @param loader
     * @return
     */
    private static final List<String> enumerateLibraryPaths(final String libBaseName,
            final boolean searchSystemPath,
            final boolean searchSystemPathFirst,
            final ClassLoader loader) {
        final List<String> paths = new ArrayList<String>();

        if ( libBaseName == null || libBaseName.length() == 0 ) {
            return paths;
        }

        final String libPlatformName = getPlatformName(libBaseName);

        if( searchSystemPath && searchSystemPathFirst ) {
            // Add probable Mac OS X-specific paths
            if ( isOSX ) {
                // Add historical location
                addPath("osx-1", "/Library/Frameworks/" + libBaseName + ".framework", libPlatformName, paths);
                // Add current location
                addPath("osx-2", "/System/Library/Frameworks/" + libBaseName + ".framework", libPlatformName, paths);
            }
            addMultiPaths("java-boot-libpath", java_boot_lib_path, libPlatformName, paths);
        }

        addPath("cwd.os_and_arch", user_dir+File.separator+os_and_arch, libPlatformName, paths);
        addMultiPaths2("java-user-libpath.os_and_arch", java_user_lib_path, os_and_arch, libPlatformName, paths);

        addPath("cwd", user_dir, libPlatformName, paths);
        addMultiPaths("java-user-libpath", java_user_lib_path, libPlatformName, paths);

        if( searchSystemPath && !searchSystemPathFirst ) {
            // Add probable Mac OS X-specific paths
            if ( isOSX ) {
                // Add historical location
                addPath("osx-1", "/Library/Frameworks/" + libBaseName + ".Framework", libPlatformName, paths);
                // Add current location
                addPath("osx-2", "/System/Library/Frameworks/" + libBaseName + ".Framework", libPlatformName, paths);
            }
            addMultiPaths("java-boot-libpath", java_boot_lib_path, libPlatformName, paths);
        }

        return paths;
    }


    private static final String getPlatformName(final String libBaseName) {
        return prefix + libBaseName + suffix;
    }
    private static final String getCanonicalPath(final String path) {
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
            @Override
            public String run() {
                try {
                    final File f = new File(path);
                    // f.getCanonicalPath() also resolved '.', '..' and symbolic links in contrast to f.getAbsolutePath()
                    return f.getCanonicalPath();
                } catch (final Throwable t) {
                    if( BluetoothFactory.DEBUG ) {
                        System.err.println("getAbsolutePath("+path+") failed: "+t.getMessage());
                    }
                    return null;
                }
            } } );
    }
    private static final void addPath(final String msg, final String path, final String platformName, final List<String> paths) {
        if( null != path && path.length() > 0 ) {
            final String fullpath = path + File.separator + platformName;
            final String abspath = getCanonicalPath(fullpath);
            if( null != abspath ) {
                final boolean isDup = paths.contains(abspath);
                if( BluetoothFactory.DEBUG ) {
                    System.err.println("  "+abspath+" (addPath "+msg+", dropped duplicate "+isDup+")");
                }
                if( !isDup ) {
                    paths.add(abspath);
                }
            }
        }
    }
    private static final void addMultiPaths(final String msg, final String pathList, final String platformName, final List<String> paths) {
        if( null != pathList && pathList.length() > 0 ) {
            final StringTokenizer tokenizer = new StringTokenizer(pathList, File.pathSeparator);
            int i=0;
            while (tokenizer.hasMoreTokens()) {
                addPath(msg+":"+i, tokenizer.nextToken(), platformName, paths);
                i++;
            }
        }
    }
    private static final void addMultiPaths2(final String msg, final String pathList, final String subDir, final String platformName, final List<String> paths) {
        if( null != pathList && pathList.length() > 0 && null != subDir && subDir.length() > 0 ) {
            final StringTokenizer tokenizer = new StringTokenizer(pathList, File.pathSeparator);
            int i=0;
            while (tokenizer.hasMoreTokens()) {
                final String path = tokenizer.nextToken() + File.separator + subDir;
                addPath(msg+":"+i, path, platformName, paths);
                i++;
            }
        }
    }


    /**
     * Loads a native library, denoted by its {@code libBaseName}.
     *
     * <p>
     * Basic order of library locations
     * <pre>
     * User locations:
     *   - current working directory + {@link #os_and_arch}
     *   - iterate through paths within 'java.library.path', adding {@link #os_and_arch} to each
     *   - current working directory
     *   - iterate through paths within 'java.library.path'
     *
     * System locations:
     *   - optional OSX path
     *   - iterate through paths within 'sun.boot.library.path'
     * </pre>
     * </p>
     *
     * <p>
     * If the above fails, {@link System#loadLibrary(String)} is called using the plain {@code libBaseName},
     * exhausting all simple locations and methods.
     * </p>
     *
     * <p>
     * Example:
     * <pre>
         /usr/local/projects/direct_bt/dist-amd64/linux-amd64/libdirect_bt.so (addPath cwd.os_and_arch)
         /usr/local/projects/direct_bt/dist-amd64/lib/linux-amd64/libdirect_bt.so (addPath java-user-libpath.os_and_arch:0)
         /usr/local/projects/direct_bt/dist-amd64/libdirect_bt.so (addPath cwd)
         /usr/local/projects/direct_bt/dist-amd64/lib/libdirect_bt.so (addPath java-user-libpath:0)
         /usr/lib/jvm/java-14-openjdk-amd64/lib/libdirect_bt.so (addPath java-boot-libpath:0)
     * </pre>
     *
     * @param libBaseName library basename without prefix (like 'lib') or suffix like '.so'.
     * @param cl
     * @param t holder to store the last Throwable, if any
     * @return {@code true} if successful, otherwise {@code false}.
     */
    static boolean loadLibrary(final String libBaseName, final ClassLoader cl, final Throwable[] t) {
        if( BluetoothFactory.DEBUG ) {
            System.err.println();
            System.err.println("PlatformToolkit.loadLibrary: libBaseName "+libBaseName+":");
        }
        final List<String> possiblePaths = enumerateLibraryPaths(libBaseName, true /* searchSystemPath */, false /* searchSystemPathFirst */, cl);
        if( BluetoothFactory.DEBUG ) {
            System.err.println();
        }

        // Iterate down these and see which one if any we can actually find.
        for (final Iterator<String> iter = possiblePaths.iterator(); iter.hasNext(); ) {
            final String path = iter.next();
            try {
                System.load(path);
                if( BluetoothFactory.DEBUG ) {
                    System.err.println("  "+path+" success");
                }
                return true;
            } catch (final Throwable t0) {
                if( BluetoothFactory.DEBUG ) {
                    System.err.println("  "+path+" failed: "+t0.getMessage());
                }
                t[0] = t0;
            }
        }

        // Fall back to loadLibrary
        try {
            System.loadLibrary(libBaseName);
            if( BluetoothFactory.DEBUG ) {
                System.err.println("  "+libBaseName+" success");
            }
            return true;
        } catch (final Throwable t0) {
            if( BluetoothFactory.DEBUG ) {
                System.err.println("  "+libBaseName+" failed: "+t0.getMessage());
            }
            t[0] = t0;
        }
        return false;
    }
}