remote: change key exception handlig so not to use java.rmi, which is 'not good enoug...
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibraryException.java
index ff40960c58a4b24e93771c777f722e1378514e2b..4cbb631cb3b0e916ae112b9148769e554c31ae93 100644 (file)
@@ -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:
+        * <ul>
+        * <li>the <tt>cause</tt> if {@link RemoteLibraryException#isWrapped()} is
+        * TRUE</li>
+        * <li><tt>this</tt> if {@link RemoteLibraryException#isWrapped()} is FALSE
+        * (</li>
+        * <li><tt>this</tt> if the <tt>cause</tt> is NULL (so we never return NULL)
+        * </li>
+        * </ul>
+        * It is never NULL.
+        * 
+        * @return the unwrapped exception or <tt>this</tt>, 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.
+        * <p>
+        * In other words, do not use <tt>this</tt> 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;
        }
 }