Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add CheckedPOSIX - wraps POSIX calls with a try {} catch block to tra…

…nslate UnsatisfiedLinkError to handler.unimplementedError() calls.
  • Loading branch information...
commit b892ff7bacfa9a091a6a815d7952e984f89007fd 1 parent b80b9a2
authored March 17, 2013
319  src/main/java/jnr/posix/CheckedPOSIX.java
... ...
@@ -0,0 +1,319 @@
  1
+package jnr.posix;
  2
+
  3
+import jnr.constants.platform.Signal;
  4
+import jnr.constants.platform.Sysconf;
  5
+import jnr.posix.util.MethodName;
  6
+import jnr.posix.util.ProcessMaker;
  7
+
  8
+import java.io.FileDescriptor;
  9
+import java.io.IOException;
  10
+import java.util.Collection;
  11
+
  12
+final class CheckedPOSIX implements POSIX {
  13
+    private final POSIX posix;
  14
+    private final POSIXHandler handler;
  15
+    
  16
+    CheckedPOSIX(POSIX posix, POSIXHandler handler) {
  17
+        this.posix = posix;
  18
+        this.handler = handler;
  19
+    }
  20
+
  21
+    private <T> T unimplementedNull() {
  22
+        handler.unimplementedError(MethodName.getCallerMethodName());
  23
+        return null;
  24
+    }
  25
+
  26
+    private int unimplementedInt() {
  27
+        handler.unimplementedError(MethodName.getCallerMethodName());
  28
+        return -1;
  29
+    }
  30
+
  31
+    private boolean unimplementedBool() {
  32
+        handler.unimplementedError(MethodName.getCallerMethodName());
  33
+        return false;
  34
+    }
  35
+
  36
+    public ProcessMaker newProcessMaker(String... command) {
  37
+        try { return posix.newProcessMaker(command); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  38
+    }
  39
+
  40
+    public ProcessMaker newProcessMaker() {
  41
+        try { return posix.newProcessMaker(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  42
+    }
  43
+
  44
+    public FileStat allocateStat() {
  45
+        try { return posix.allocateStat(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  46
+    }
  47
+
  48
+    public int chmod(String filename, int mode) {
  49
+        try { return posix.chmod(filename, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  50
+    }
  51
+
  52
+    public int chown(String filename, int user, int group) {
  53
+        try { return posix.chown(filename, user, group); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  54
+    }
  55
+
  56
+    public int endgrent() {
  57
+        try { return posix.endgrent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  58
+    }
  59
+
  60
+    public int endpwent() {
  61
+        try { return posix.endpwent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  62
+    }
  63
+
  64
+    public int errno() {
  65
+        return posix.errno();
  66
+    }
  67
+
  68
+    public void errno(int value) {
  69
+        posix.errno(value);
  70
+    }
  71
+
  72
+    public int exec(String path, String... args) {
  73
+        try { return posix.exec(path, args); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  74
+    }
  75
+
  76
+    public int exec(String path, String[] args, String[] envp) {
  77
+        try { return posix.exec(path, args, envp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  78
+    }
  79
+
  80
+    public int execv(String path, String[] argv) {
  81
+        try { return posix.execv(path, argv); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  82
+    }
  83
+
  84
+    public int execve(String path, String[] argv, String[] envp) {
  85
+        try { return posix.execve(path, argv, envp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  86
+    }
  87
+
  88
+    public int fork() {
  89
+        try { return posix.fork(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  90
+    }
  91
+
  92
+    public FileStat fstat(int fd) {
  93
+        try { return posix.fstat(fd); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  94
+    }
  95
+
  96
+    public int fstat(int fd, FileStat stat) {
  97
+        try { return posix.fstat(fd, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  98
+    }
  99
+
  100
+    public FileStat fstat(FileDescriptor descriptor) {
  101
+        try { return posix.fstat(descriptor); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  102
+    }
  103
+
  104
+    public int fstat(FileDescriptor descriptor, FileStat stat) {
  105
+        try { return posix.fstat(descriptor, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  106
+    }
  107
+
  108
+    public int getegid() {
  109
+        try { return posix.getegid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  110
+    }
  111
+
  112
+    public int geteuid() {
  113
+        try { return posix.geteuid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  114
+    }
  115
+
  116
+    public int getgid() {
  117
+        try { return posix.getgid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  118
+    }
  119
+
  120
+    public Group getgrent() {
  121
+        try { return posix.getgrent(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  122
+    }
  123
+
  124
+    public Group getgrgid(int which) {
  125
+        try { return posix.getgrgid(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  126
+    }
  127
+
  128
+    public Group getgrnam(String which) {
  129
+        try { return posix.getgrnam(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  130
+    }
  131
+
  132
+    public String getlogin() {
  133
+        try { return posix.getlogin(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  134
+    }
  135
+
  136
+    public int getpgid() {
  137
+        try { return posix.getpgid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  138
+    }
  139
+
  140
+    public int getpgid(int pid) {
  141
+        try { return posix.getpgid(pid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  142
+    }
  143
+
  144
+    public int getpgrp() {
  145
+        try { return posix.getpgrp(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  146
+    }
  147
+
  148
+    public int getpid() {
  149
+        try { return posix.getpid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  150
+    }
  151
+
  152
+    public int getppid() {
  153
+        try { return posix.getppid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  154
+    }
  155
+
  156
+    public int getpriority(int which, int who) {
  157
+        try { return posix.getpriority(which, who); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  158
+    }
  159
+
  160
+    public Passwd getpwent() {
  161
+        try { return posix.getpwent(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  162
+    }
  163
+
  164
+    public Passwd getpwnam(String which) {
  165
+        try { return posix.getpwnam(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  166
+    }
  167
+
  168
+    public Passwd getpwuid(int which) {
  169
+        try { return posix.getpwuid(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  170
+    }
  171
+
  172
+    public int getuid() {
  173
+        try { return posix.getuid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  174
+    }
  175
+
  176
+    public boolean isatty(FileDescriptor descriptor) {
  177
+        try { return posix.isatty(descriptor); } catch (UnsatisfiedLinkError ule) { return unimplementedBool(); }
  178
+    }
  179
+
  180
+    public int kill(int pid, int signal) {
  181
+        try { return posix.kill(pid, signal); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  182
+    }
  183
+
  184
+    public SignalHandler signal(Signal sig, SignalHandler handler) {
  185
+        try { return posix.signal(sig, handler); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  186
+    }
  187
+
  188
+    public int lchmod(String filename, int mode) {
  189
+        try { return posix.lchmod(filename, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  190
+    }
  191
+
  192
+    public int lchown(String filename, int user, int group) {
  193
+        try { return posix.lchown(filename, user, group); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  194
+    }
  195
+
  196
+    public int link(String oldpath, String newpath) {
  197
+        try { return posix.link(oldpath, newpath); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  198
+    }
  199
+
  200
+    public FileStat lstat(String path) {
  201
+        try { return posix.lstat(path); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  202
+    }
  203
+
  204
+    public int lstat(String path, FileStat stat) {
  205
+        try { return posix.lstat(path, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  206
+    }
  207
+
  208
+    public int mkdir(String path, int mode) {
  209
+        try { return posix.mkdir(path, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  210
+    }
  211
+
  212
+    public String readlink(String path) throws IOException {
  213
+        try { return posix.readlink(path); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  214
+    }
  215
+
  216
+    public int rmdir(String path) {
  217
+        try { return posix.rmdir(path); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  218
+    }
  219
+
  220
+    public int setegid(int egid) {
  221
+        try { return posix.setegid(egid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  222
+    }
  223
+
  224
+    public int seteuid(int euid) {
  225
+        try { return posix.seteuid(euid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  226
+    }
  227
+
  228
+    public int setgid(int gid) {
  229
+        try { return posix.setgid(gid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  230
+    }
  231
+
  232
+    public int setgrent() {
  233
+        try { return posix.setgrent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  234
+    }
  235
+
  236
+    public int setpgid(int pid, int pgid) {
  237
+        try { return posix.setpgid(pid, pgid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  238
+    }
  239
+
  240
+    public int setpgrp(int pid, int pgrp) {
  241
+        try { return posix.setpgrp(pid, pgrp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  242
+    }
  243
+
  244
+    public int setpriority(int which, int who, int prio) {
  245
+        try { return posix.setpriority(which, who, prio); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  246
+    }
  247
+
  248
+    public int setpwent() {
  249
+        try { return posix.setpwent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  250
+    }
  251
+
  252
+    public int setsid() {
  253
+        try { return posix.setsid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  254
+    }
  255
+
  256
+    public int setuid(int uid) {
  257
+        try { return posix.setuid(uid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  258
+    }
  259
+
  260
+    public FileStat stat(String path) {
  261
+        try { return posix.stat(path); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  262
+    }
  263
+
  264
+    public int stat(String path, FileStat stat) {
  265
+        try { return posix.stat(path, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  266
+    }
  267
+
  268
+    public int symlink(String oldpath, String newpath) {
  269
+        try { return posix.symlink(oldpath, newpath); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  270
+    }
  271
+
  272
+    public int umask(int mask) {
  273
+        try { return posix.umask(mask); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  274
+    }
  275
+
  276
+    public int utimes(String path, long[] atimeval, long[] mtimeval) {
  277
+        try { return posix.utimes(path, atimeval, mtimeval); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  278
+    }
  279
+
  280
+    public int wait(int[] status) {
  281
+        try { return posix.wait(status); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  282
+    }
  283
+
  284
+    public int waitpid(int pid, int[] status, int flags) {
  285
+        try { return posix.waitpid(pid, status, flags); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  286
+    }
  287
+
  288
+    public boolean isNative() {
  289
+        return posix.isNative();
  290
+    }
  291
+
  292
+    public LibC libc() {
  293
+        return posix.libc();
  294
+    }
  295
+
  296
+    public String getenv(String envName) {
  297
+        try { return posix.getenv(envName); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  298
+    }
  299
+
  300
+    public int setenv(String envName, String envValue, int overwrite) {
  301
+        try { return posix.setenv(envName, envValue, overwrite); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  302
+    }
  303
+
  304
+    public int unsetenv(String envName) {
  305
+        try { return posix.unsetenv(envName); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  306
+    }
  307
+
  308
+    public long posix_spawnp(String path, Collection<? extends SpawnFileAction> fileActions, Collection<? extends CharSequence> argv, Collection<? extends CharSequence> envp) {
  309
+        try { return posix.posix_spawnp(path, fileActions, argv, envp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  310
+    }
  311
+
  312
+    public long sysconf(Sysconf name) {
  313
+        try { return posix.sysconf(name); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
  314
+    }
  315
+
  316
+    public Times times() {
  317
+        try { return posix.times(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); }
  318
+    }
  319
+}
2  src/main/java/jnr/posix/POSIXFactory.java
@@ -36,7 +36,7 @@ static POSIX loadPOSIX(POSIXHandler handler, boolean useNativePOSIX) {
36 36
         if (useNativePOSIX) {
37 37
             try {
38 38
                 posix = loadNativePOSIX(handler);
39  
-
  39
+                posix = posix != null ? new CheckedPOSIX(posix, handler) : null;
40 40
                 // ENEBO: Should printing be done through a handler+log method?
41 41
                 if (handler.isVerbose()) {
42 42
                     if (posix != null) {

0 notes on commit b892ff7

Please sign in to comment.
Something went wrong with that request. Please try again.