Skip to content

Commit

Permalink
Better handling of failed connections - 3 attempts then returns null
Browse files Browse the repository at this point in the history
Also adds a meaningful message when the connection fails
Adds logger in OmeroHelper
Decreases init time when reading Omero dataset - get rid of rawPixStore.getResolutionDescriptions() call fix #28
  • Loading branch information
NicoKiaru committed Sep 17, 2024
1 parent 54437e1 commit 17fb9ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
49 changes: 31 additions & 18 deletions src/main/java/ch/epfl/biop/bdv/img/omero/OmeroHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.scijava.Context;
import org.scijava.command.CommandModule;
import org.scijava.command.CommandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.net.MalformedURLException;
Expand All @@ -53,6 +55,8 @@

public class OmeroHelper {

protected static final Logger logger = LoggerFactory.getLogger(OmeroHelper.class);

/**
* OMERO connection with credentials and simpleLogger
*
Expand Down Expand Up @@ -134,31 +138,40 @@ public static UnitsLength getUnitsLengthFromString(String unit_string) {

public static OMEROSession getGatewayAndSecurityContext(Context context, String host) throws Exception {
OMEROService omeroService = context.getService(OMEROService.class);

OMEROServer server = new OMEROServer(host, 4064);

OMEROSession omeroSession;
try {
omeroSession = omeroService.session(server);
return omeroService.session(server);
} catch (Exception e) {
System.err.println("The OMERO session for "+host+" needs to be initialized");
logger.info("The OMERO session for "+host+" needs to be initialized");
CommandService command = context.getService(CommandService.class);
boolean success = false;
Exception error;
try {
OmeroConnectCommand.message_in = "Please enter your "+host+" credentials:";
CommandModule module = command.run(OmeroConnectCommand.class, true, "host", host).get();
success = (Boolean) module.getOutput("success");
omeroSession = (OMEROSession) module.getOutput("omeroSession");
error = (DSOutOfServiceException) module.getOutput("error");
} catch (Exception commandException) {
commandException.printStackTrace();
omeroSession = null;
error = commandException;
int iAttempt = 0;
int nAttempts = 3;
String lastErrorMessage = "";
while ((iAttempt<nAttempts) && (!success)) {
iAttempt++;
Exception error;
try {
if (lastErrorMessage.isEmpty()) {
OmeroConnectCommand.message_in = "<html>Please enter your " + host + " credentials:</html>";
} else {
OmeroConnectCommand.message_in = "<html>Error:"+lastErrorMessage+"<br> Please re-enter your " + host + " credentials ("+iAttempt+"/"+nAttempts+"):</html>";
}
CommandModule module = command.run(OmeroConnectCommand.class, true, "host", host).get();
success = (Boolean) module.getOutput("success");
OMEROSession omeroSession = (OMEROSession) module.getOutput("omeroSession");
if (success) return omeroSession;
error = (Exception) module.getOutput("error");
if (error!=null) {
lastErrorMessage = error.getMessage();
}
} catch (Exception commandException) {
error = commandException;
}
if ((!success) && (iAttempt == nAttempts)) throw error;
}
if ((!success)&&(error!=null)) throw error;
}
return omeroSession;
return null;
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/ch/epfl/biop/bdv/img/omero/OmeroOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ public OmeroOpener(
exception = e;
}

if (exception != null) throw exception;
if (exception != null) {
// avoid asking again and again connection credentials
throw exception;
}

this.gateway = session.getGateway();
this.securityContext = session.getSecurityContext();
Expand Down Expand Up @@ -230,22 +233,19 @@ public OmeroOpener(
} else {
tileSize = new HashMap<>();
logger.debug("Get image size and tile sizes...");
Instant start = Instant.now();
ResolutionDescription[] resDesc = rawPixStore.getResolutionDescriptions();
Instant finish = Instant.now();
logger.debug("Done! Time elapsed : " + Duration.between(start,
finish));
int tileSizeX = rawPixStore.getTileSize()[0];
int tileSizeY = rawPixStore.getTileSize()[1];
int bytesWidth = rawPixStore.getByteWidth();

for (int level = 0; level < this.nMipmapLevels; level++) {
int[] sizes = new int[3];
sizes[0] = resDesc[level].sizeX;
sizes[1] = resDesc[level].sizeY;
rawPixStore.setResolutionLevel(this.nMipmapLevels - level - 1);
sizes[0] = rawPixStore.getRowSize() / bytesWidth;
sizes[1] = (int) ( (rawPixStore.getPlaneSize() / sizes[0]) / bytesWidth );
sizes[2] = pixels.getSizeZ();
int[] tileSizes = new int[2];
tileSizes[0] = Math.min(tileSizeX, resDesc[this.nMipmapLevels - 1].sizeX);
tileSizes[1] = Math.min(tileSizeY, resDesc[this.nMipmapLevels - 1].sizeY);
tileSizes[0] = Math.min(tileSizeX, sizes[0]);
tileSizes[1] = Math.min(tileSizeY, sizes[1]);
imageSize.put(level, sizes);
tileSize.put(level, tileSizes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void run() {
password = "";
logger.info("Session active : " + omeroSession.getGateway().isConnected());
omeroSession.getSecurityContext().setServerInformation(new ServerInformation(host));
success = true;
success = omeroSession.getGateway().isConnected();
} catch (Exception e) {
error = e;
logger.error(e.getMessage());
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/ch/epfl/biop/SimpleIJLaunch.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public class SimpleIJLaunch {
static public void main(String... args) {
// Arrange
// create the ImageJ application context with all available services

//DebugTools.enableLogging("INFO");
final ImageJ ij = new ImageJ();
DebugTools.enableLogging("DEBUG");
//DebugTools.enableLogging("INFO");
//SwingUtilities.invokeAndWait(() ->);
//System.out.println("bf version = "+VersionUtils.getVersion(ZeissCZIReader.class));
ij.ui().showUI();
Expand Down

0 comments on commit 17fb9ee

Please sign in to comment.