From: Niki Roo Date: Sat, 18 May 2019 11:15:20 +0000 (+0200) Subject: remote: change key exception handlig so not to use java.rmi, which is 'not good enoug... X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=533dc2b8abb2287becdc7bb03a937dff8fafea07;p=nikiroo-utils.git remote: change key exception handlig so not to use java.rmi, which is 'not good enough' for Android --- diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 4d8d512..a6c6854 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -41,12 +41,7 @@ public class RemoteLibrary extends BasicLibrary { Object rep = super.send(data); if (rep instanceof RemoteLibraryException) { RemoteLibraryException remoteEx = (RemoteLibraryException) rep; - IOException cause = remoteEx.getCause(); - if (cause == null) { - cause = new IOException("IOException"); - } - - throw cause; + throw remoteEx.unwrapException(); } return rep; @@ -446,6 +441,9 @@ public class RemoteLibrary extends BasicLibrary { /** * Stop the server. + * + * @throws IOException + * in case of I/O error (including bad key) */ public void exit() throws IOException { connectRemoteAction(new RemoteAction() { diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryException.java b/src/be/nikiroo/fanfix/library/RemoteLibraryException.java index ff40960..4cbb631 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryException.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryException.java @@ -10,6 +10,9 @@ import java.io.IOException; public class RemoteLibraryException extends IOException { private static final long serialVersionUID = 1L; + private boolean wrapped; + + @SuppressWarnings("unused") private RemoteLibraryException() { // for serialization purposes } @@ -18,14 +21,80 @@ public class RemoteLibraryException extends IOException { * Wrap an {@link IOException} to allow it to pass across the network. * * @param cause - * the excption to wrap + * the exception to wrap + * @param remote + * this exception is used to send the contained + * {@link IOException} to the other end of the network + */ + public RemoteLibraryException(IOException cause, boolean remote) { + this(null, cause, remote); + } + + /** + * Wrap an {@link IOException} to allow it to pass across the network. + * + * @param message + * the error message + * @param wrapped + * this exception is used to send the contained + * {@link IOException} to the other end of the network */ - public RemoteLibraryException(IOException cause) { - super(cause); + public RemoteLibraryException(String message, boolean wrapped) { + this(message, null, wrapped); } - @Override - public synchronized IOException getCause() { - return (IOException) super.getCause(); + /** + * Wrap an {@link IOException} to allow it to pass across the network. + * + * @param message + * the error message + * @param cause + * the exception to wrap + * @param wrapped + * this exception is used to send the contained + * {@link IOException} to the other end of the network + */ + public RemoteLibraryException(String message, IOException cause, + boolean wrapped) { + super(message, cause); + this.wrapped = wrapped; + } + + /** + * Return the actual exception we should return to the client code. It can + * be: + * + * It is never NULL. + * + * @return the unwrapped exception or this, never NULL + */ + public synchronized IOException unwrapException() { + Throwable ex = super.getCause(); + if (!isWrapped() || !(ex instanceof IOException)) { + ex = this; + } + + return (IOException) ex; + } + + /** + * This exception is used to send the contained {@link IOException} to the + * other end of the network. + *

+ * In other words, do not use this exception in client code when it + * has reached the other end of the network, but use its cause instead (see + * {@link RemoteLibraryException#unwrapException()}). + * + * @return TRUE if it is + */ + public boolean isWrapped() { + return wrapped; } } diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java index 0319439..43f61b0 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java @@ -2,7 +2,6 @@ package be.nikiroo.fanfix.library; import java.io.IOException; import java.net.URL; -import java.rmi.AccessException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -149,7 +148,7 @@ public class RemoteLibraryServer extends ServerObject { try { rep = doRequest(action, command, args, rw, whitelist); } catch (IOException e) { - rep = new RemoteLibraryException(e); + rep = new RemoteLibraryException(e, true); } commands.put(id, command); @@ -266,8 +265,8 @@ public class RemoteLibraryServer extends ServerObject { } } else if ("SAVE_STORY".equals(command)) { if (!rw) { - throw new AccessException("Read-Only remote library: " - + args[0]); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0], false); } List list = new ArrayList(); @@ -285,8 +284,8 @@ public class RemoteLibraryServer extends ServerObject { return story.getMeta().getLuid(); } else if ("IMPORT".equals(command)) { if (!rw) { - throw new AccessException("Read-Only remote library: " - + args[0]); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0], false); } Progress pg = createPgForwarder(action); @@ -296,8 +295,8 @@ public class RemoteLibraryServer extends ServerObject { return story.getMeta().getLuid(); } else if ("DELETE_STORY".equals(command)) { if (!rw) { - throw new AccessException("Read-Only remote library: " - + args[0]); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0], false); } Instance.getLibrary().delete((String) args[0]); @@ -315,8 +314,8 @@ public class RemoteLibraryServer extends ServerObject { } } else if ("SET_COVER".equals(command)) { if (!rw) { - throw new AccessException("Read-Only remote library: " - + args[0] + ", " + args[1]); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0] + ", " + args[1], false); } if ("SOURCE".equals(args[0])) { @@ -328,8 +327,8 @@ public class RemoteLibraryServer extends ServerObject { } } else if ("CHANGE_STA".equals(command)) { if (!rw) { - throw new AccessException("Read-Only remote library: " - + args[0] + ", " + args[1]); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0] + ", " + args[1], false); } Progress pg = createPgForwarder(action); @@ -338,8 +337,8 @@ public class RemoteLibraryServer extends ServerObject { forcePgDoneSent(pg); } else if ("EXIT".equals(command)) { if (!rw) { - throw new AccessException( - "Read-Only remote library: EXIT"); + throw new RemoteLibraryException( + "Read-Only remote library: EXIT", false); } stop(0, false);