Author: david.lloyd(a)jboss.com
Date: 2009-05-05 16:06:35 -0400 (Tue, 05 May 2009)
New Revision: 5134
Added:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemoteInterruptedException.java
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/Remoting.java
Log:
Convenience exception handling methods
Added:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemoteInterruptedException.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemoteInterruptedException.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemoteInterruptedException.java 2009-05-05
20:06:35 UTC (rev 5134)
@@ -0,0 +1,70 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.jboss.remoting3;
+
+/**
+ * The remote execution was interrupted due to an {@link InterruptedException}. Since it
is an error to propagate such
+ * an exception to a different thread, this exception may be thrown in its place.
+ */
+public class RemoteInterruptedException extends RuntimeException {
+
+ private static final long serialVersionUID = 7273630153650065304L;
+
+ /**
+ * Constructs a {@code RemoteInterruptedException} with no detail message. The cause
is not initialized, and may
+ * subsequently be initialized by a call to {@link #initCause(Throwable) initCause}.
+ */
+ public RemoteInterruptedException() {
+ }
+
+ /**
+ * Constructs a {@code RemoteInterruptedException} with the specified detail message.
The cause is not initialized, and
+ * may subsequently be initialized by a call to {@link #initCause(Throwable)
initCause}.
+ *
+ * @param msg the detail message
+ */
+ public RemoteInterruptedException(final String msg) {
+ super(msg);
+ }
+
+ /**
+ * Constructs a {@code RemoteInterruptedException} with the specified cause. The
detail message is set to:
+ * <pre>(cause == null ? null : cause.toString())</pre>
+ * (which typically contains the class and detail message of {@code cause}).
+ *
+ * @param cause the cause (which is saved for later retrieval by the {@link
#getCause()} method)
+ */
+ public RemoteInterruptedException(final Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * Constructs a {@code RemoteInterruptedException} with the specified detail message
and cause.
+ *
+ * @param msg the detail message
+ * @param cause the cause (which is saved for later retrieval by the {@link
#getCause()} method)
+ */
+ public RemoteInterruptedException(final String msg, final Throwable cause) {
+ super(msg, cause);
+ }
+}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/Remoting.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/Remoting.java 2009-05-05
08:24:28 UTC (rev 5133)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/Remoting.java 2009-05-05
20:06:35 UTC (rev 5134)
@@ -132,5 +132,50 @@
}
}
+ /**
+ * Convenience method to rethrow the cause of a {@code RemoteExecutionException} as a
specific type, in order
+ * to simplify application exception handling.
+ * <p/>
+ * A typical usage might look like this:
+ * <pre>
+ * try {
+ * client.invoke(request);
+ * } catch (RemoteExecutionException ree) {
+ * Remoting.rethrowAs(IOException.class, ree);
+ * Remoting.rethrowAs(RuntimeException.class, ree);
+ * Remoting.rethrowUnexpected(ree);
+ * }
+ * </pre>
+ * <p/>
+ * Note that if the nested exception is an {@link InterruptedException}, the type
that will actually be thrown
+ * will be {@link RemoteInterruptedException}.
+ *
+ * @param type the class of the exception
+ * @param original the remote execution exception
+ * @param <T> the exception type
+ * @throws T the exception, if it matches the given type
+ */
+ public static <T extends Throwable> void rethrowAs(Class<T> type,
RemoteExecutionException original) throws T {
+ final Throwable cause = original.getCause();
+ if (cause == null) {
+ return;
+ }
+ if (type.isAssignableFrom(cause.getClass())) {
+ if (cause instanceof InterruptedException) {
+ throw new RemoteInterruptedException(cause.getMessage(),
cause.getCause());
+ }
+ throw type.cast(cause);
+ }
+ return;
+ }
+
+ public static void rethrowUnexpected(RemoteExecutionException original) throws
IllegalStateException {
+ Throwable cause = original.getCause();
+ if (cause instanceof InterruptedException) {
+ cause = new RemoteInterruptedException(cause.getMessage(),
cause.getCause());
+ }
+ throw new IllegalStateException("Unexpected remote exception occurred",
cause);
+ }
+
private Remoting() { /* empty */ }
}