Skip to content
Snippets Groups Projects
Commit 439715c6 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

ISS-1145: if fails Authentication then start complete reconnect

parent 0023d16c
No related branches found
No related tags found
1 merge request!35ISS-1145: if fails Authentication then start complete reconnect
......@@ -32,6 +32,7 @@ import javax.xml.ws.WebServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cz.it4i.fiji.haas_java_client.HaasFileTransferReconnectingAfterAuthFail.Supplier;
import cz.it4i.fiji.haas_java_client.proxy.ArrayOfCommandTemplateParameterValueExt;
import cz.it4i.fiji.haas_java_client.proxy.ArrayOfEnvironmentVariableExt;
import cz.it4i.fiji.haas_java_client.proxy.ArrayOfTaskFileOffsetExt;
......@@ -226,8 +227,7 @@ public class HaaSClient {
try {
return createFileTransfer(jobId, notifier);
}
catch (RemoteException | ServiceException | UnsupportedEncodingException
| JSchException e)
catch (RemoteException | ServiceException e)
{
throw new HaaSClientException(e);
}
......@@ -356,32 +356,43 @@ public class HaaSClient {
return sessionID;
}
private HaaSFileTransferImp createFileTransfer(final long jobId,
private HaaSFileTransfer createFileTransfer(final long jobId,
final TransferFileProgress progress) throws RemoteException,
UnsupportedEncodingException, ServiceException, JSchException
ServiceException
{
final P_FileTransferPool pool = filetransferPoolMap.computeIfAbsent(jobId,
id -> new P_FileTransferPool(id));
final FileTransferMethodExt ft = pool.obtain();
try {
return new HaaSFileTransferImp(ft, getScpClient(ft), progress) {
return new HaasFileTransferReconnectingAfterAuthFail(getHaasFileTransferFactory(pool, progress),
() -> pool.reconnect());
}
@Override
public void close() {
super.close();
try {
pool.release();
}
catch (RemoteException | ServiceException e) {
throw new HaaSClientException(e);
private Supplier<HaaSFileTransfer> getHaasFileTransferFactory(
P_FileTransferPool pool, TransferFileProgress progress)
{
return () -> {
final FileTransferMethodExt ft = pool.obtain();
try {
return new HaaSFileTransferImp(ft, getScpClient(ft), progress) {
@Override
public void close() {
super.close();
try {
pool.release();
}
catch (RemoteException | ServiceException e) {
throw new HaaSClientException(e);
}
}
}
};
}
catch (UnsupportedEncodingException | JSchException e) {
pool.release();
throw e;
}
};
}
catch (UnsupportedEncodingException | JSchException e) {
pool.release();
throw new HaaSClientException(e);
}
};
}
private HaaSDataTransfer createDataTransfer(final long jobId,
......@@ -661,7 +672,22 @@ public class HaaSClient {
this.factory = () -> getFileTransfer().getFileTransferMethod(jobId,
getSessionID());
this.destroyer = val -> getFileTransfer().endFileTransfer(jobId, val,
sessionID);
getSessionID());
}
public void reconnect() {
try {
if (holded != null) {
destroyer.accept(holded);
}
sessionID = null;
if (holded != null) {
holded = factory.get();
}
}
catch (RemoteException | ServiceException exc) {
throw new HaaSClientException(exc);
}
}
public synchronized FileTransferMethodExt obtain() throws RemoteException,
......
package cz.it4i.fiji.haas_java_client;
import java.io.InterruptedIOException;
import java.nio.file.Path;
import java.rmi.RemoteException;
import java.util.List;
import javax.xml.rpc.ServiceException;
import cz.it4i.fiji.scpclient.AuthFailException;
import cz.it4i.fiji.scpclient.TransferFileProgress;
public class HaasFileTransferReconnectingAfterAuthFail implements HaaSFileTransfer {
private static final int MAX_ATTEMPTS_FOR_RECONNECTION = 5;
public interface Supplier <T> {
T get() throws ServiceException, RemoteException;
}
private Supplier<HaaSFileTransfer> haasFileTransferFactory;
private HaaSFileTransfer haasFileTransfer;
private Runnable reconnectCommand;
public HaasFileTransferReconnectingAfterAuthFail(
Supplier<HaaSFileTransfer> haasFileTransferFactory,
Runnable reconnectCommand) throws RemoteException, ServiceException
{
super();
this.haasFileTransferFactory = haasFileTransferFactory;
this.haasFileTransfer = haasFileTransferFactory.get();
this.reconnectCommand = reconnectCommand;
}
@Override
public void close() {
this.haasFileTransfer.close();
}
@Override
public void upload(UploadingFile file) throws InterruptedIOException {
doWithRepeatedReconnect(() -> {
haasFileTransfer.upload(file);
return null;
});
}
@Override
public void download(String files, Path workDirectory)
throws InterruptedIOException
{
doWithRepeatedReconnect(() -> {
haasFileTransfer.download(files, workDirectory);
return null;
});
}
@Override
public List<Long> obtainSize(List<String> files)
throws InterruptedIOException
{
return doWithRepeatedReconnect(() -> haasFileTransfer.obtainSize(files));
}
@Override
public List<String> getContent(List<String> logs) {
try {
return doWithRepeatedReconnect(() -> haasFileTransfer.getContent(logs));
}
catch (InterruptedIOException exc) {
throw new RuntimeException(exc);
}
}
@Override
public void setProgress(TransferFileProgress progress) {
try {
doWithRepeatedReconnect(() -> {
haasFileTransfer.setProgress(progress);
return null;
});
}
catch (InterruptedIOException exc) {
throw new RuntimeException(exc);
}
}
private interface CallableInteruptable<T> {
T call() throws InterruptedIOException;
}
private <T> T doWithRepeatedReconnect(CallableInteruptable<T> runnable)
throws InterruptedIOException
{
int attempts = 0;
do {
try {
return runnable.call();
}
catch (HaaSClientException e) {
if (e.getCause() instanceof AuthFailException) {
if (attempts <= MAX_ATTEMPTS_FOR_RECONNECTION) {
attempts++;
reconnect();
continue;
}
}
throw e;
}
}
while (true);
}
private void reconnect() {
haasFileTransfer.close();
reconnectCommand.run();
try {
haasFileTransfer = haasFileTransferFactory.get();
}
catch (RemoteException | ServiceException exc) {
throw new HaaSClientException(exc);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment