|
1 | 1 | package hudson.os;
|
2 | 2 |
|
3 |
| -import org.jruby.ext.posix.JavaPOSIX; |
4 |
| -import org.jruby.ext.posix.POSIX; |
5 |
| -import org.jruby.ext.posix.POSIXFactory; |
6 |
| -import org.jruby.ext.posix.POSIXHandler; |
7 |
| -import org.jruby.ext.posix.POSIX.ERRORS; |
8 |
| - |
9 | 3 | import java.io.File;
|
10 | 4 | import java.io.InputStream;
|
11 | 5 | import java.io.PrintStream;
|
12 | 6 | import java.util.Map;
|
13 | 7 | import java.util.logging.Logger;
|
| 8 | +import jnr.constants.platform.Errno; |
| 9 | +import jnr.posix.POSIX; |
| 10 | +import jnr.posix.POSIXFactory; |
| 11 | +import jnr.posix.util.DefaultPOSIXHandler; |
14 | 12 |
|
15 | 13 | /**
|
16 | 14 | * POSIX API wrapper.
|
17 |
| - * |
| 15 | + * Formerly used the jna-posix library, but this has been superseded by jnr-posix. |
18 | 16 | * @author Kohsuke Kawaguchi
|
19 | 17 | */
|
20 | 18 | public class PosixAPI {
|
21 |
| - public static POSIX get() { |
| 19 | + |
| 20 | + private static POSIX posix; |
| 21 | + |
| 22 | + /** |
| 23 | + * Load the JNR implementation of the POSIX APIs for the current platform. |
| 24 | + * Runtime exceptions will be of type {@link PosixException}. |
| 25 | + * @return some implementation (even on Windows or unsupported Unix) |
| 26 | + * @since 1.518 |
| 27 | + */ |
| 28 | + public static synchronized POSIX jnr() { |
| 29 | + if (posix == null) { |
| 30 | + posix = POSIXFactory.getPOSIX(new DefaultPOSIXHandler() { |
| 31 | + @Override public void error(Errno error, String extraData) { |
| 32 | + throw new PosixException("native error " + error.description() + " " + extraData, convert(error)); |
| 33 | + } |
| 34 | + @Override public void error(Errno error, String methodName, String extraData) { |
| 35 | + throw new PosixException("native error calling " + methodName + ": " + error.description() + " " + extraData, convert(error)); |
| 36 | + } |
| 37 | + private org.jruby.ext.posix.POSIX.ERRORS convert(Errno error) { |
| 38 | + try { |
| 39 | + return org.jruby.ext.posix.POSIX.ERRORS.valueOf(error.name()); |
| 40 | + } catch (IllegalArgumentException x) { |
| 41 | + return org.jruby.ext.posix.POSIX.ERRORS.EIO; // PosixException.message has real error anyway |
| 42 | + } |
| 43 | + } |
| 44 | + }, true); |
| 45 | + } |
22 | 46 | return posix;
|
23 | 47 | }
|
24 | 48 |
|
25 | 49 | /**
|
26 |
| - * @deprecated as of 1.448 |
27 |
| - * Use {@link #supportsNative()}. |
| 50 | + * @deprecated use {@link #jnr} and {@link POSIX#isNative} |
28 | 51 | */
|
| 52 | + @Deprecated |
29 | 53 | public boolean isNative() {
|
30 | 54 | return supportsNative();
|
31 | 55 | }
|
32 | 56 |
|
33 | 57 | /**
|
34 |
| - * Determine if the jna-posix library could not provide native support, and |
35 |
| - * used a fallback java implementation which does not support many operations. |
| 58 | + * @deprecated use {@link #jnr} and {@link POSIX#isNative} |
36 | 59 | */
|
| 60 | + @Deprecated |
37 | 61 | public static boolean supportsNative() {
|
38 |
| - return !(posix instanceof JavaPOSIX); |
| 62 | + return !(jnaPosix instanceof org.jruby.ext.posix.JavaPOSIX); |
39 | 63 | }
|
40 |
| - |
41 |
| - private static final POSIX posix = POSIXFactory.getPOSIX(new POSIXHandler() { |
42 |
| - public void error(ERRORS errors, String s) { |
| 64 | + |
| 65 | + private static org.jruby.ext.posix.POSIX jnaPosix; |
| 66 | + /** @deprecated Use {@link #jnr} instead. */ |
| 67 | + @Deprecated |
| 68 | + public static synchronized org.jruby.ext.posix.POSIX get() { |
| 69 | + if (jnaPosix == null) { |
| 70 | + jnaPosix = org.jruby.ext.posix.POSIXFactory.getPOSIX(new org.jruby.ext.posix.POSIXHandler() { |
| 71 | + public void error(org.jruby.ext.posix.POSIX.ERRORS errors, String s) { |
43 | 72 | throw new PosixException(s,errors);
|
44 | 73 | }
|
45 | 74 |
|
@@ -87,6 +116,9 @@ public PrintStream getErrorStream() {
|
87 | 116 | return System.err;
|
88 | 117 | }
|
89 | 118 | }, true);
|
| 119 | + } |
| 120 | + return jnaPosix; |
| 121 | + } |
90 | 122 |
|
91 | 123 | private static final Logger LOGGER = Logger.getLogger(PosixAPI.class.getName());
|
92 | 124 | }
|
0 commit comments