From 17fb9ee2073f58bc5b4e944d4e150e9470186e3a Mon Sep 17 00:00:00 2001 From: Nicolas Chiaruttini Date: Tue, 17 Sep 2024 17:04:58 +0200 Subject: [PATCH] Better handling of failed connections - 3 attempts then returns null 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 --- .../epfl/biop/bdv/img/omero/OmeroHelper.java | 49 ++++++++++++------- .../epfl/biop/bdv/img/omero/OmeroOpener.java | 20 ++++---- .../omero/command/OmeroConnectCommand.java | 2 +- .../java/ch/epfl/biop/SimpleIJLaunch.java | 3 +- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroHelper.java b/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroHelper.java index ded98e3..57cde76 100644 --- a/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroHelper.java +++ b/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroHelper.java @@ -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; @@ -53,6 +55,8 @@ public class OmeroHelper { + protected static final Logger logger = LoggerFactory.getLogger(OmeroHelper.class); + /** * OMERO connection with credentials and simpleLogger * @@ -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"; + } else { + OmeroConnectCommand.message_in = "Error:"+lastErrorMessage+"
Please re-enter your " + host + " credentials ("+iAttempt+"/"+nAttempts+"):"; + } + 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; } /** diff --git a/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroOpener.java b/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroOpener.java index 0d42363..faff946 100644 --- a/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroOpener.java +++ b/src/main/java/ch/epfl/biop/bdv/img/omero/OmeroOpener.java @@ -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(); @@ -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); } diff --git a/src/main/java/ch/epfl/biop/bdv/img/omero/command/OmeroConnectCommand.java b/src/main/java/ch/epfl/biop/bdv/img/omero/command/OmeroConnectCommand.java index 6dd369d..3ecc394 100644 --- a/src/main/java/ch/epfl/biop/bdv/img/omero/command/OmeroConnectCommand.java +++ b/src/main/java/ch/epfl/biop/bdv/img/omero/command/OmeroConnectCommand.java @@ -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()); diff --git a/src/test/java/ch/epfl/biop/SimpleIJLaunch.java b/src/test/java/ch/epfl/biop/SimpleIJLaunch.java index cbd97ca..d2ef39b 100644 --- a/src/test/java/ch/epfl/biop/SimpleIJLaunch.java +++ b/src/test/java/ch/epfl/biop/SimpleIJLaunch.java @@ -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();