Skip to content

Commit e5cde2e

Browse files
committed
Use preference omero server address in ice reader
1 parent e83c7ff commit e5cde2e

File tree

2 files changed

+70
-67
lines changed

2 files changed

+70
-67
lines changed

src/main/java/qupath/ext/omero/core/pixelapis/ice/IceAPI.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.drew.lang.annotations.Nullable;
44
import javafx.beans.property.*;
55
import javafx.beans.value.ObservableBooleanValue;
6+
import omero.gateway.LoginCredentials;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
89
import qupath.ext.omero.core.ClientsPreferencesManager;
@@ -13,6 +14,7 @@
1314
import qupath.lib.images.servers.PixelType;
1415

1516
import java.io.IOException;
17+
import java.util.List;
1618

1719
/**
1820
* <p>
@@ -108,7 +110,30 @@ public PixelAPIReader createReader(long id, ImageServerMetadata metadata) throws
108110
throw new IllegalArgumentException("The provided image cannot be read by this API");
109111
}
110112

111-
return new IceReader(apisHandler, sessionUuid, id, metadata.getChannels());
113+
return new IceReader(
114+
List.of(
115+
new LoginCredentials(
116+
sessionUuid,
117+
sessionUuid,
118+
apisHandler.getWebServerURI().getHost(),
119+
apisHandler.getServerPort()
120+
),
121+
new LoginCredentials(
122+
sessionUuid,
123+
sessionUuid,
124+
apisHandler.getServerURI(),
125+
apisHandler.getServerPort()
126+
),
127+
new LoginCredentials(
128+
sessionUuid,
129+
sessionUuid,
130+
serverAddress.get(),
131+
apisHandler.getServerPort() //TODO: change
132+
)
133+
),
134+
id,
135+
metadata.getChannels()
136+
);
112137
}
113138

114139
@Override

src/main/java/qupath/ext/omero/core/pixelapis/ice/IceReader.java

Lines changed: 44 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import omero.model.ExperimenterGroup;
1414
import org.slf4j.Logger;
1515
import org.slf4j.LoggerFactory;
16-
import qupath.ext.omero.core.apis.ApisHandler;
1716
import qupath.lib.color.ColorModelFactory;
1817
import qupath.lib.images.servers.ImageChannel;
1918
import qupath.lib.images.servers.PixelType;
@@ -47,41 +46,44 @@ class IceReader implements PixelAPIReader {
4746
/**
4847
* Creates a new Ice reader.
4948
*
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
5251
* @param imageID the ID of the image to open
5352
* @param channels the channels of the image to open
5453
* @throws IOException when the reader creation fails
5554
*/
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 {
5756
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+
}
8385
} else {
84-
throw new IOException("Couldn't find requested image of ID " + imageID);
86+
throw new IOException("Could not connect to the ICE server");
8587
}
8688
} catch (DSOutOfServiceException | ExecutionException | ServerError e) {
8789
throw new IOException(e);
@@ -142,43 +144,19 @@ public String toString() {
142144
return String.format("Ice reader for %s", context.getServerInformation());
143145
}
144146

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+
}
181158
}
159+
return Optional.empty();
182160
}
183161

184162
private Optional<ImageData> getImage(long imageID) throws ExecutionException, DSOutOfServiceException, ServerError {

0 commit comments

Comments
 (0)