fix #89 - use IllegalStateException from SSHClient when sanity-check assertions fail

This commit is contained in:
Shikhar Bhushan
2012-10-21 01:40:07 +05:30
parent 4c5da634ad
commit f3d4707ef0

View File

@@ -186,7 +186,7 @@ public class SSHClient
*/ */
public void auth(String username, AuthMethod... methods) public void auth(String username, AuthMethod... methods)
throws UserAuthException, TransportException { throws UserAuthException, TransportException {
assert isConnected(); checkConnected();
auth(username, Arrays.<AuthMethod>asList(methods)); auth(username, Arrays.<AuthMethod>asList(methods));
} }
@@ -201,7 +201,7 @@ public class SSHClient
*/ */
public void auth(String username, Iterable<AuthMethod> methods) public void auth(String username, Iterable<AuthMethod> methods)
throws UserAuthException, TransportException { throws UserAuthException, TransportException {
assert isConnected(); checkConnected();
auth.authenticate(username, (Service) conn, methods); auth.authenticate(username, (Service) conn, methods);
} }
@@ -365,7 +365,6 @@ public class SSHClient
throws IOException { throws IOException {
trans.disconnect(); trans.disconnect();
super.disconnect(); super.disconnect();
assert !isConnected();
} }
/** @return the associated {@link Connection} instance. */ /** @return the associated {@link Connection} instance. */
@@ -607,7 +606,8 @@ public class SSHClient
/** @return Instantiated {@link SCPFileTransfer} implementation. */ /** @return Instantiated {@link SCPFileTransfer} implementation. */
public SCPFileTransfer newSCPFileTransfer() { public SCPFileTransfer newSCPFileTransfer() {
assert isConnected() && isAuthenticated(); checkConnected();
checkAuthenticated();
return new SCPFileTransfer(this); return new SCPFileTransfer(this);
} }
@@ -619,7 +619,8 @@ public class SSHClient
*/ */
public SFTPClient newSFTPClient() public SFTPClient newSFTPClient()
throws IOException { throws IOException {
assert isConnected() && isAuthenticated(); checkConnected();
checkAuthenticated();
return new SFTPClient(new SFTPEngine(this).init()); return new SFTPClient(new SFTPEngine(this).init());
} }
@@ -636,10 +637,10 @@ public class SSHClient
@Override @Override
public Session startSession() public Session startSession()
throws ConnectionException, TransportException { throws ConnectionException, TransportException {
assert isConnected() && isAuthenticated(); checkConnected();
checkAuthenticated();
final SessionChannel sess = new SessionChannel(conn); final SessionChannel sess = new SessionChannel(conn);
sess.open(); sess.open();
assert sess.isOpen();
return sess; return sess;
} }
@@ -679,8 +680,7 @@ public class SSHClient
*/ */
protected void doKex() protected void doKex()
throws TransportException { throws TransportException {
assert trans.isRunning(); checkConnected();
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
trans.doKex(); trans.doKex();
log.debug("Key exchange took {} seconds", (System.currentTimeMillis() - start) / 1000.0); log.debug("Key exchange took {} seconds", (System.currentTimeMillis() - start) / 1000.0);
@@ -697,4 +697,16 @@ public class SSHClient
disconnect(); disconnect();
} }
private void checkConnected() {
if (!isConnected()) {
throw new IllegalStateException("Not connected");
}
}
private void checkAuthenticated() {
if (!isAuthenticated()) {
throw new IllegalStateException("Not authenticated");
}
}
} }