-
Notifications
You must be signed in to change notification settings - Fork 11
/
ServiceFactory.java
336 lines (290 loc) · 9.79 KB
/
ServiceFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* ome.system.ServiceFactory
*
* Copyright 2006 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.system;
import java.util.Properties;
import ome.api.IAdmin;
import ome.api.IAnalysis;
import ome.api.IConfig;
import ome.api.IContainer;
import ome.api.IDelete;
import ome.api.ILdap;
import ome.api.IMetadata;
import ome.api.IPixels;
import ome.api.IProjection;
import ome.api.IQuery;
import ome.api.IRenderingSettings;
import ome.api.IRepositoryInfo;
import ome.api.ISession;
import ome.api.IShare;
import ome.api.ITypes;
import ome.api.IUpdate;
import ome.api.JobHandle;
import ome.api.RawFileStore;
import ome.api.RawPixelsStore;
import ome.api.Search;
import ome.api.ServiceInterface;
import ome.api.ThumbnailStore;
import ome.conditions.ApiUsageException;
import ome.model.meta.Session;
import omeis.providers.re.RenderingEngine;
import org.springframework.beans.BeansException;
/**
* Entry point for all client calls. Provides methods to obtain proxies for all
* remote facades.
*
* @author Josh Moore, josh.moore at gmx.de
* @see OmeroContext
* @since 3.0
*/
public class ServiceFactory {
/**
* the {@link OmeroContext context instance} which this
* {@link ServiceFactory} uses to look up all of its state.
*/
protected OmeroContext ctx;
/**
* default constructor which obtains the global static
* {@link ome.system.OmeroContext#CLIENT_CONTEXT client context} from
* {@link ome.system.OmeroContext}. This can be done manually by calling
* {@link ome.system.OmeroContext#getClientContext()}
*
* @see OmeroContext#CLIENT_CONTEXT
* @see OmeroContext#getClientContext()
*/
public ServiceFactory() {
if (getDefaultContext() != null) {
this.ctx = OmeroContext.getInstance(getDefaultContext());
}
}
/**
* constructor which obtains a new (non-static)
* {@link ome.system.OmeroContext#CLIENT_CONTEXT client context}, passing
* in the {@link Properties} representation of the {@link Login} for
* configuration.
*
* @see Login#asProperties()
* @see #ServiceFactory(Properties)
*/
public ServiceFactory(Login login) {
this.ctx = OmeroContext.getClientContext(login.asProperties());
}
/**
* constructor which obtains a new (non-static)
* {@link ome.system.OmeroContext#CLIENT_CONTEXT client context}, passing
* in the {@link Properties} representation of the {@link Server} for
* configuration.
*
* @see Server#asProperties()
* @see #ServiceFactory(Properties)
*/
public ServiceFactory(Server server) {
this.ctx = OmeroContext.getClientContext(server.asProperties());
}
/**
* constructor which obtains a new (non-static)
* {@link ome.system.OmeroContext#CLIENT_CONTEXT client context}, passing
* in the {@link Properties} representation of both the {@link Server} and
* the {@link Login} for configuration.
*
* @see Login#asProperties()
* @see #ServiceFactory(Properties)
*/
public ServiceFactory(Server server, Login login) {
Properties s = server.asProperties();
Properties l = login.asProperties();
s.putAll(l);
this.ctx = OmeroContext.getClientContext(s);
}
/**
* constructor which obtains a new
* {@link ome.system.OmeroContext#CLIENT_CONTEXT client context}, passing
* in the provided properties for configuration.
*
* @see OmeroContext#getClientContext(Properties)
*/
public ServiceFactory(Properties properties) {
this.ctx = OmeroContext.getClientContext(properties);
}
/**
* constructor which uses the provided {@link OmeroContext} for all
* loookups.
*/
public ServiceFactory(OmeroContext context) {
this.ctx = context;
}
/**
* constructor which finds the global static {@link OmeroContext} with the
* given name.
*
* @see OmeroContext#CLIENT_CONTEXT
* @see OmeroContext#MANAGED_CONTEXT
*/
public ServiceFactory(String contextName) {
this.ctx = OmeroContext.getInstance(contextName);
}
// ~ Stateless services
// =========================================================================
public IAdmin getAdminService() {
return getServiceByClass(IAdmin.class);
}
public IAnalysis getAnalysisService() {
return getServiceByClass(IAnalysis.class);
}
public IConfig getConfigService() {
return getServiceByClass(IConfig.class);
}
public IContainer getContainerService() {
return getServiceByClass(IContainer.class);
}
public IDelete getDeleteService() {
return getServiceByClass(IDelete.class);
}
public ILdap getLdapService() {
return getServiceByClass(ILdap.class);
}
public IPixels getPixelsService() {
return getServiceByClass(IPixels.class);
}
public IProjection getProjectionService() {
return getServiceByClass(IProjection.class);
}
public IQuery getQueryService() {
return getServiceByClass(IQuery.class);
}
public IShare getShareService() {
return getServiceByClass(IShare.class);
}
public ITypes getTypesService() {
return getServiceByClass(ITypes.class);
}
public IUpdate getUpdateService() {
return getServiceByClass(IUpdate.class);
}
public IRenderingSettings getRenderingSettingsService() {
return getServiceByClass(IRenderingSettings.class);
}
public IRepositoryInfo getRepositoryInfoService() {
return getServiceByClass(IRepositoryInfo.class);
}
public IMetadata getMetadataService() {
return getServiceByClass(IMetadata.class);
}
// ~ Stateful services
// =========================================================================
/**
* create a new {@link JobHandle} proxy. This proxy will have to be
* initialized using {@link JobHandle#attach(long)} or
* {@link JobHandle#submit(ome.model.jobs.Job)}.
*/
public JobHandle createJobHandle() {
return getServiceByClass(JobHandle.class);
}
/**
* create a new {@link RawPixelsStore} proxy. This proxy will have to be
* initialized using {@link RawPixelsStore#setPixelsId(long, boolean)}
*/
public RawPixelsStore createRawPixelsStore() {
return getServiceByClass(RawPixelsStore.class);
}
/**
* create a new {@link RawFileStore} proxy. This proxy will have to be
* initialized using {@link RawFileStore#setFileId(long)}
*/
public RawFileStore createRawFileStore() {
return getServiceByClass(RawFileStore.class);
}
/**
* create a new {@link RenderingEngine} proxy. This proxy will have to be
* initialized using {@link RenderingEngine#lookupPixels(long)} and
* {@link RenderingEngine#load()}
*/
public RenderingEngine createRenderingEngine() {
return getServiceByClass(RenderingEngine.class);
}
/**
* create a new {@link Search} proxy.
*/
public Search createSearchService() {
return getServiceByClass(Search.class);
}
/**
* create a new {@link ThumbnailStore} proxy. This proxy will have to be
* initialized using {@link ThumbnailStore#setPixelsId(long)}
*/
public ThumbnailStore createThumbnailService() {
return getServiceByClass(ThumbnailStore.class);
}
// ~ Sessions
// =========================================================================
public ISession getSessionService() {
return getServiceByClass(ISession.class);
}
public Session getSession() throws ApiUsageException {
return getSessionInitializer().getSession();
}
public void setSession(Session session) throws ApiUsageException {
SessionInitializer si = getSessionInitializer();
si.setSession(session);
}
public void closeSession() throws ApiUsageException {
ISession is = getSessionService();
SessionInitializer si = getSessionInitializer();
if (si.hasSession()) {
Session s = si.getSession();
try {
is.closeSession(s);
} finally {
si.setSession(null);
}
}
}
protected SessionInitializer getSessionInitializer() {
SessionInitializer si;
try {
si = (SessionInitializer) this.ctx.getBean("init");
} catch (Exception e) {
throw new ApiUsageException("This ServiceFactory is not configured "
+ "for sessions");
}
return si;
}
// ~ Helpers
// =========================================================================
/**
* looks up services based on the current {@link #getPrefix() prefix} and
* the class name of the service type.
*/
public <T extends ServiceInterface> T getServiceByClass(Class<T> klass) {
try {
return klass.cast(this.ctx.getBean(getPrefix() + klass.getName()));
} catch (BeansException be) {
if (be.getCause() instanceof RuntimeException) {
throw (RuntimeException) be.getCause();
} else {
throw be;
}
}
}
/**
* used by {@link #getServiceByClass(Class)} to find the correct service
* proxy in the {@link #ctx}
*
* @return a {@link String}, usually "internal-" or "managed-"
*/
protected String getPrefix() {
return "managed-";
}
/**
* used when no {@link OmeroContext context} name is provided to the
* constructor. Subclasses can override to allow for easier creation.
*
* @return name of default context as found in beanRefContext.xml.
*/
protected String getDefaultContext() {
return OmeroContext.CLIENT_CONTEXT;
}
}