oops
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibraryException.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.IOException;
4
5 /**
6 * Exceptions sent from remote to local.
7 *
8 * @author niki
9 */
10 public class RemoteLibraryException extends IOException {
11 private static final long serialVersionUID = 1L;
12
13 private boolean wrapped;
14
15 @SuppressWarnings("unused")
16 private RemoteLibraryException() {
17 // for serialization purposes
18 }
19
20 /**
21 * Wrap an {@link IOException} to allow it to pass across the network.
22 *
23 * @param cause
24 * the exception to wrap
25 * @param remote
26 * this exception is used to send the contained
27 * {@link IOException} to the other end of the network
28 */
29 public RemoteLibraryException(IOException cause, boolean remote) {
30 this(null, cause, remote);
31 }
32
33 /**
34 * Wrap an {@link IOException} to allow it to pass across the network.
35 *
36 * @param message
37 * the error message
38 * @param wrapped
39 * this exception is used to send the contained
40 * {@link IOException} to the other end of the network
41 */
42 public RemoteLibraryException(String message, boolean wrapped) {
43 this(message, null, wrapped);
44 }
45
46 /**
47 * Wrap an {@link IOException} to allow it to pass across the network.
48 *
49 * @param message
50 * the error message
51 * @param cause
52 * the exception to wrap
53 * @param wrapped
54 * this exception is used to send the contained
55 * {@link IOException} to the other end of the network
56 */
57 public RemoteLibraryException(String message, IOException cause,
58 boolean wrapped) {
59 super(message, cause);
60 this.wrapped = wrapped;
61 }
62
63 /**
64 * Return the actual exception we should return to the client code. It can
65 * be:
66 * <ul>
67 * <li>the <tt>cause</tt> if {@link RemoteLibraryException#isWrapped()} is
68 * TRUE</li>
69 * <li><tt>this</tt> if {@link RemoteLibraryException#isWrapped()} is FALSE
70 * (</li>
71 * <li><tt>this</tt> if the <tt>cause</tt> is NULL (so we never return NULL)
72 * </li>
73 * </ul>
74 * It is never NULL.
75 *
76 * @return the unwrapped exception or <tt>this</tt>, never NULL
77 */
78 public synchronized IOException unwrapException() {
79 Throwable ex = super.getCause();
80 if (!isWrapped() || !(ex instanceof IOException)) {
81 ex = this;
82 }
83
84 return (IOException) ex;
85 }
86
87 /**
88 * This exception is used to send the contained {@link IOException} to the
89 * other end of the network.
90 * <p>
91 * In other words, do not use <tt>this</tt> exception in client code when it
92 * has reached the other end of the network, but use its cause instead (see
93 * {@link RemoteLibraryException#unwrapException()}).
94 *
95 * @return TRUE if it is
96 */
97 public boolean isWrapped() {
98 return wrapped;
99 }
100 }