From c48a9b6417092adbaef0703b95172624e4f22c1f Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Wed, 19 Nov 2008 15:06:57 +0100 Subject: [PATCH] client/api: "Inline" the session and authentication exceptions Inspired by protocol buffer API. --- .../main/java/remote/apps/cli/Authenticator.java | 3 +- ...AuthenticationCallbackUnsupportedException.java | 33 ------------ .../authentication/AuthenticationHandler.java | 37 +++++++++++++- .../main/java/remote/client/session/Session.java | 58 +++++++++++++++++----- .../SessionAlreadyAuthenticatedException.java | 17 ------- .../client/session/SessionStateException.java | 17 ------- .../java/remote/client/xrt/AbstractSession.java | 6 +-- 7 files changed, 83 insertions(+), 88 deletions(-) delete mode 100644 client/api/src/main/java/remote/client/authentication/AuthenticationCallbackUnsupportedException.java delete mode 100644 client/api/src/main/java/remote/client/session/SessionAlreadyAuthenticatedException.java delete mode 100644 client/api/src/main/java/remote/client/session/SessionStateException.java diff --git a/apps/cli/src/main/java/remote/apps/cli/Authenticator.java b/apps/cli/src/main/java/remote/apps/cli/Authenticator.java index cb93cd9..f0a522f 100644 --- a/apps/cli/src/main/java/remote/apps/cli/Authenticator.java +++ b/apps/cli/src/main/java/remote/apps/cli/Authenticator.java @@ -6,7 +6,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import remote.client.authentication.AuthenticationCallback; -import remote.client.authentication.AuthenticationCallbackUnsupportedException; import remote.client.authentication.AuthenticationHandler; import remote.client.authentication.AuthenticationNameCallback; import remote.client.authentication.AuthenticationPasswordCallback; @@ -72,7 +71,7 @@ class Authenticator implements AuthenticationHandler, ServiceEventListener { } public void authenticate(AuthenticationCallback[] callbacks) - throws AuthenticationCallbackUnsupportedException + throws UnsupportedCallbackException { for (AuthenticationCallback callback : callbacks) { String input = visitor.visit(callback); diff --git a/client/api/src/main/java/remote/client/authentication/AuthenticationCallbackUnsupportedException.java b/client/api/src/main/java/remote/client/authentication/AuthenticationCallbackUnsupportedException.java deleted file mode 100644 index f82eddf..0000000 --- a/client/api/src/main/java/remote/client/authentication/AuthenticationCallbackUnsupportedException.java +++ /dev/null @@ -1,33 +0,0 @@ -package remote.client.authentication; - -/** - * Unchecked exception thrown when a client does not support a given - * authentication callback. - */ -public class AuthenticationCallbackUnsupportedException extends Exception { - - private final AuthenticationCallback callback; - - /** - * Creates a new instance. - * - * @param callback that is not supported. - */ - public AuthenticationCallbackUnsupportedException(AuthenticationCallback callback) - { - super("Authentication callback " + callback.getClass().getName() + - " unsupported."); - this.callback = callback; - } - - /** - * Get the unsupported callback. - * - * @return the unsupported callback. - */ - public AuthenticationCallback getCallback() - { - return callback; - } - -} diff --git a/client/api/src/main/java/remote/client/authentication/AuthenticationHandler.java b/client/api/src/main/java/remote/client/authentication/AuthenticationHandler.java index bebfed7..8a45788 100644 --- a/client/api/src/main/java/remote/client/authentication/AuthenticationHandler.java +++ b/client/api/src/main/java/remote/client/authentication/AuthenticationHandler.java @@ -11,9 +11,42 @@ public interface AuthenticationHandler { * supplementary credentials are needed. * * @param callbacks containing information on how to authenticate. - * @throws remote.client.authentication.AuthenticationCallbackUnsupportedException + * @throws UnsupportedCallbackException + * if a callback is not supported by a handler. */ void authenticate(AuthenticationCallback[] callbacks) - throws AuthenticationCallbackUnsupportedException; + throws UnsupportedCallbackException; + + /** + * Exception thrown when a client does not support a given + * authentication callback. + */ + public class UnsupportedCallbackException extends RuntimeException { + + private final AuthenticationCallback callback; + + /** + * Creates a new instance. + * + * @param callback that is not supported. + */ + public UnsupportedCallbackException(AuthenticationCallback callback) + { + super("Authentication callback " + callback.getClass().getName() + + " unsupported."); + this.callback = callback; + } + + /** + * Get the unsupported callback. + * + * @return the unsupported callback. + */ + public AuthenticationCallback getCallback() + { + return callback; + } + + } } diff --git a/client/api/src/main/java/remote/client/session/Session.java b/client/api/src/main/java/remote/client/session/Session.java index 037220f..2b5ed11 100644 --- a/client/api/src/main/java/remote/client/session/Session.java +++ b/client/api/src/main/java/remote/client/session/Session.java @@ -9,16 +9,6 @@ import remote.client.control.MoteManager; public interface Session { /** - * Disconnect and destroy the session. - * - * Revokes control of all privileged motes. - * - * @throws SessionStateException - * If the session has already been destroyed. - */ - void destroy() throws SessionStateException; - - /** * Get the mote manager for this session. * * Manages information about the motes in the testbed. @@ -51,12 +41,54 @@ public interface Session { /** * Authenticate the session. - * + * * @param handler to use during the authentication process. - * @throws SessionAlreadyAuthenticatedException + * @throws AlreadyAuthenticatedException * if the session is already authenticated. */ void authenticate(AuthenticationHandler handler) - throws SessionAlreadyAuthenticatedException; + throws AlreadyAuthenticatedException; + + /** + * Disconnect and destroy the session. + * + * Revokes control of all privileged motes. + * + * @throws StateException + * If the session has already been destroyed. + */ + void destroy() throws StateException; + + /** + * Exception thrown when a client tries to authenticate a session + * that is already authenticated. + */ + public static class AlreadyAuthenticatedException extends IllegalStateException { + + /** + * Creates a new instance. + */ + public AlreadyAuthenticatedException() + { + super("Session is already authenticated."); + } + + } + + /** + * Exception thrown when a session operation + * is invoked after the session has been destroyed. + */ + public static class StateException extends IllegalStateException { + + /** + * Creates a new instance. + */ + public StateException() + { + super("Session is no longer valid."); + } + + } } diff --git a/client/api/src/main/java/remote/client/session/SessionAlreadyAuthenticatedException.java b/client/api/src/main/java/remote/client/session/SessionAlreadyAuthenticatedException.java deleted file mode 100644 index 331f321..0000000 --- a/client/api/src/main/java/remote/client/session/SessionAlreadyAuthenticatedException.java +++ /dev/null @@ -1,17 +0,0 @@ -package remote.client.session; - -/** - * Unchecked exception thrown when a client tries to authenticate a session - * that is already authenticated. - */ -public class SessionAlreadyAuthenticatedException extends IllegalArgumentException { - - /** - * Creates a new instance of SessionAlreadyAuthenticatedException. - */ - public SessionAlreadyAuthenticatedException() - { - super("Session is already authenticated."); - } - -} diff --git a/client/api/src/main/java/remote/client/session/SessionStateException.java b/client/api/src/main/java/remote/client/session/SessionStateException.java deleted file mode 100644 index 42f8739..0000000 --- a/client/api/src/main/java/remote/client/session/SessionStateException.java +++ /dev/null @@ -1,17 +0,0 @@ -package remote.client.session; - -/** - * Unchecked exception thrown when a session operation - * is invoked after the session has been destroyed. - */ -public class SessionStateException extends IllegalStateException { - - /** - * Creates a new instance of SessionStateException. - */ - public SessionStateException() - { - super("Session is no longer valid."); - } - -} diff --git a/client/xrt/src/main/java/remote/client/xrt/AbstractSession.java b/client/xrt/src/main/java/remote/client/xrt/AbstractSession.java index 6a0d3ab..5b31ee3 100644 --- a/client/xrt/src/main/java/remote/client/xrt/AbstractSession.java +++ b/client/xrt/src/main/java/remote/client/xrt/AbstractSession.java @@ -2,10 +2,8 @@ package remote.client.xrt; import remote.client.authentication.AuthenticationHandler; import remote.client.session.Session; -import remote.client.session.SessionAlreadyAuthenticatedException; import remote.client.session.SessionListener; import remote.client.session.SessionManager; -import remote.client.session.SessionStateException; import remote.client.spi.ServiceManager; import remote.client.spi.service.AuthenticationService; import remote.util.Cancellable; @@ -61,7 +59,7 @@ abstract class AbstractSession implements Session { public void destroy() { if (request == null) - throw new SessionStateException(); + throw new StateException(); if (!request.isDone()) request.cancel(); @@ -82,7 +80,7 @@ abstract class AbstractSession implements Session { public void authenticate(AuthenticationHandler handler) { if (authenticated) - throw new SessionAlreadyAuthenticatedException(); + throw new AlreadyAuthenticatedException(); this.request = new AuthenticationRequest(handler); } -- 2.11.4.GIT