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