Implemented java.lang.AutoCloseable

This commit is contained in:
Gideon le Grange 2015-04-28 09:52:31 +02:00
parent db373c41d8
commit 61691cb9a6
2 changed files with 27 additions and 19 deletions

View File

@ -10,7 +10,7 @@ import me.legrange.mikrotik.impl.ApiConnectionImpl;
* *
* @author GideonLeGrange * @author GideonLeGrange
*/ */
public abstract class ApiConnection { public abstract class ApiConnection implements AutoCloseable {
/** default TCP port used by Mikrotik API */ /** default TCP port used by Mikrotik API */
public static final int DEFAULT_PORT = 8728; public static final int DEFAULT_PORT = 8728;
@ -146,4 +146,8 @@ public abstract class ApiConnection {
*/ */
public abstract void setTimeout(int timeout) throws MikrotikApiException; public abstract void setTimeout(int timeout) throws MikrotikApiException;
@Override
public abstract void close() throws ApiConnectionException;
} }

View File

@ -54,19 +54,7 @@ public final class ApiConnectionImpl extends ApiConnection {
@Override @Override
public void disconnect() throws ApiConnectionException { public void disconnect() throws ApiConnectionException {
if (!connected) { close();
throw new ApiConnectionException(("Not/no longer connected to remote Mikrotik"));
}
connected = false;
processor.interrupt();
reader.interrupt();
try {
in.close();
out.close();
sock.close();
} catch (IOException ex) {
throw new ApiConnectionException(String.format("Error closing socket: %s", ex.getMessage()), ex);
}
} }
@Override @Override
@ -101,12 +89,28 @@ public final class ApiConnectionImpl extends ApiConnection {
public void setTimeout(int timeout) throws MikrotikApiException { public void setTimeout(int timeout) throws MikrotikApiException {
if (timeout > 0) { if (timeout > 0) {
this.timeout = timeout; this.timeout = timeout;
} } else {
else {
throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout)); throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout));
} }
} }
@Override
public void close() throws ApiConnectionException {
if (!connected) {
throw new ApiConnectionException(("Not/no longer connected to remote Mikrotik"));
}
connected = false;
processor.interrupt();
reader.interrupt();
try {
in.close();
out.close();
sock.close();
} catch (IOException ex) {
throw new ApiConnectionException(String.format("Error closing socket: %s", ex.getMessage()), ex);
}
}
private List<Map<String, String>> execute(Command cmd, int timeout) throws MikrotikApiException { private List<Map<String, String>> execute(Command cmd, int timeout) throws MikrotikApiException {
SyncListener l = new SyncListener(); SyncListener l = new SyncListener();
execute(cmd, l); execute(cmd, l);
@ -481,7 +485,7 @@ public final class ApiConnectionImpl extends ApiConnection {
while (!complete && (waitTime > 0)) { while (!complete && (waitTime > 0)) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
wait(waitTime); wait(waitTime);
waitTime = waitTime - (int)(System.currentTimeMillis() - start); waitTime = waitTime - (int) (System.currentTimeMillis() - start);
if ((waitTime <= 0) && !complete) { if ((waitTime <= 0) && !complete) {
err = new ApiConnectionException(String.format("Command timed out after %d ms", timeout)); err = new ApiConnectionException(String.format("Command timed out after %d ms", timeout));
} }