|
13 | 13 | import omero.model.ExperimenterGroup;
|
14 | 14 | import org.slf4j.Logger;
|
15 | 15 | import org.slf4j.LoggerFactory;
|
16 |
| -import qupath.ext.omero.core.apis.ApisHandler; |
17 | 16 | import qupath.lib.color.ColorModelFactory;
|
18 | 17 | import qupath.lib.images.servers.ImageChannel;
|
19 | 18 | import qupath.lib.images.servers.PixelType;
|
@@ -47,41 +46,44 @@ class IceReader implements PixelAPIReader {
|
47 | 46 | /**
|
48 | 47 | * Creates a new Ice reader.
|
49 | 48 | *
|
50 |
| - * @param apisHandler the ApisHandler owning the image to open |
51 |
| - * @param sessionUuid the session UUID of the client connection |
| 49 | + * @param loginCredentials a list of credentials to use to connect to the server. Once a connection |
| 50 | + * is established, the remaining credentials are not used |
52 | 51 | * @param imageID the ID of the image to open
|
53 | 52 | * @param channels the channels of the image to open
|
54 | 53 | * @throws IOException when the reader creation fails
|
55 | 54 | */
|
56 |
| - public IceReader(ApisHandler apisHandler, String sessionUuid, long imageID, List<ImageChannel> channels) throws IOException { |
| 55 | + public IceReader(List<LoginCredentials> loginCredentials, long imageID, List<ImageChannel> channels) throws IOException { |
57 | 56 | try {
|
58 |
| - ExperimenterData user = connect(apisHandler, sessionUuid); |
59 |
| - |
60 |
| - context = new SecurityContext(user.getGroupId()); |
61 |
| - |
62 |
| - var imageData = getImage(imageID); |
63 |
| - if (imageData.isPresent()) { |
64 |
| - PixelsData pixelsData = imageData.get().getDefaultPixels(); |
65 |
| - |
66 |
| - reader = gateway.getPixelsStore(context); |
67 |
| - reader.setPixelsId(pixelsData.getId(), false); |
68 |
| - numberOfResolutionLevels = reader.getResolutionLevels(); |
69 |
| - nChannels = channels.size(); |
70 |
| - effectiveNChannels = pixelsData.getSizeC(); |
71 |
| - pixelType = switch (pixelsData.getPixelType()) { |
72 |
| - case PixelsData.INT8_TYPE -> PixelType.INT8; |
73 |
| - case PixelsData.UINT8_TYPE -> PixelType.UINT8; |
74 |
| - case PixelsData.INT16_TYPE -> PixelType.INT16; |
75 |
| - case PixelsData.UINT16_TYPE -> PixelType.UINT16; |
76 |
| - case PixelsData.UINT32_TYPE -> PixelType.UINT32; |
77 |
| - case PixelsData.INT32_TYPE -> PixelType.INT32; |
78 |
| - case PixelsData.FLOAT_TYPE -> PixelType.FLOAT32; |
79 |
| - case PixelsData.DOUBLE_TYPE -> PixelType.FLOAT64; |
80 |
| - default -> throw new IllegalArgumentException("Unsupported pixel type " + pixelsData.getPixelType()); |
81 |
| - }; |
82 |
| - colorModel = ColorModelFactory.createColorModel(pixelType, channels); |
| 57 | + Optional<ExperimenterData> user = connect(loginCredentials); |
| 58 | + if (user.isPresent()) { |
| 59 | + context = new SecurityContext(user.get().getGroupId()); |
| 60 | + |
| 61 | + var imageData = getImage(imageID); |
| 62 | + if (imageData.isPresent()) { |
| 63 | + PixelsData pixelsData = imageData.get().getDefaultPixels(); |
| 64 | + |
| 65 | + reader = gateway.getPixelsStore(context); |
| 66 | + reader.setPixelsId(pixelsData.getId(), false); |
| 67 | + numberOfResolutionLevels = reader.getResolutionLevels(); |
| 68 | + nChannels = channels.size(); |
| 69 | + effectiveNChannels = pixelsData.getSizeC(); |
| 70 | + pixelType = switch (pixelsData.getPixelType()) { |
| 71 | + case PixelsData.INT8_TYPE -> PixelType.INT8; |
| 72 | + case PixelsData.UINT8_TYPE -> PixelType.UINT8; |
| 73 | + case PixelsData.INT16_TYPE -> PixelType.INT16; |
| 74 | + case PixelsData.UINT16_TYPE -> PixelType.UINT16; |
| 75 | + case PixelsData.UINT32_TYPE -> PixelType.UINT32; |
| 76 | + case PixelsData.INT32_TYPE -> PixelType.INT32; |
| 77 | + case PixelsData.FLOAT_TYPE -> PixelType.FLOAT32; |
| 78 | + case PixelsData.DOUBLE_TYPE -> PixelType.FLOAT64; |
| 79 | + default -> throw new IllegalArgumentException("Unsupported pixel type " + pixelsData.getPixelType()); |
| 80 | + }; |
| 81 | + colorModel = ColorModelFactory.createColorModel(pixelType, channels); |
| 82 | + } else { |
| 83 | + throw new IOException("Couldn't find requested image of ID " + imageID); |
| 84 | + } |
83 | 85 | } else {
|
84 |
| - throw new IOException("Couldn't find requested image of ID " + imageID); |
| 86 | + throw new IOException("Could not connect to the ICE server"); |
85 | 87 | }
|
86 | 88 | } catch (DSOutOfServiceException | ExecutionException | ServerError e) {
|
87 | 89 | throw new IOException(e);
|
@@ -142,43 +144,19 @@ public String toString() {
|
142 | 144 | return String.format("Ice reader for %s", context.getServerInformation());
|
143 | 145 | }
|
144 | 146 |
|
145 |
| - /** |
146 |
| - * Attempt to create a connection with the server. The OMERO web host will be |
147 |
| - * used, and if not successful, the OMERO server host will be used (see |
148 |
| - * {@link ApisHandler#getServerURI()}). |
149 |
| - * |
150 |
| - * @param apisHandler the ApisHandler owning the image to open |
151 |
| - * @param sessionUuid the session UUID of the client connection |
152 |
| - * @return a valid connection |
153 |
| - * @throws DSOutOfServiceException when a connection cannot be established |
154 |
| - */ |
155 |
| - private ExperimenterData connect(ApisHandler apisHandler, String sessionUuid) throws DSOutOfServiceException { |
156 |
| - String firstURI = apisHandler.getWebServerURI().getHost(); |
157 |
| - String secondURI = apisHandler.getServerURI(); |
158 |
| - |
159 |
| - try { |
160 |
| - return gateway.connect(new LoginCredentials( |
161 |
| - sessionUuid, |
162 |
| - sessionUuid, |
163 |
| - firstURI, |
164 |
| - apisHandler.getServerPort() |
165 |
| - )); |
166 |
| - } catch (Exception e) { |
167 |
| - logger.warn(String.format( |
168 |
| - "Can't connect to %s:%d. Trying %s:%d...", |
169 |
| - firstURI, |
170 |
| - apisHandler.getServerPort(), |
171 |
| - secondURI, |
172 |
| - apisHandler.getServerPort() |
173 |
| - ), e); |
174 |
| - |
175 |
| - return gateway.connect(new LoginCredentials( |
176 |
| - sessionUuid, |
177 |
| - sessionUuid, |
178 |
| - secondURI, |
179 |
| - apisHandler.getServerPort() |
180 |
| - )); |
| 147 | + private Optional<ExperimenterData> connect(List<LoginCredentials> loginCredentials) { |
| 148 | + for (LoginCredentials loginCredential: loginCredentials) { |
| 149 | + try { |
| 150 | + return Optional.ofNullable(gateway.connect(loginCredential)); |
| 151 | + } catch (Exception e) { |
| 152 | + logger.info(String.format( |
| 153 | + "Can't connect to %s:%d", |
| 154 | + loginCredential.getServer().getHost(), |
| 155 | + loginCredential.getServer().getPort() |
| 156 | + ), e); |
| 157 | + } |
181 | 158 | }
|
| 159 | + return Optional.empty(); |
182 | 160 | }
|
183 | 161 |
|
184 | 162 | private Optional<ImageData> getImage(long imageID) throws ExecutionException, DSOutOfServiceException, ServerError {
|
|
0 commit comments