86228cbb59fe2dd1673509f7594fcffe9b93756b
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationRemoteController.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10
11 package eu.etaxonomy.cdm.api.application;
12
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16 import org.hibernate.collection.internal.AbstractPersistentCollection;
17 import org.hibernate.proxy.AbstractLazyInitializer;
18 import org.springframework.context.ApplicationListener;
19 import org.springframework.core.io.ClassPathResource;
20 import org.springframework.core.io.Resource;
21
22 import eu.etaxonomy.cdm.api.service.ITestService;
23 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
24 import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
25 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
26 import eu.etaxonomy.cdm.io.service.IIOService;
27 import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
28 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
29 import eu.etaxonomy.taxeditor.service.ICachedCommonService;
30 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
31
32 /**
33 * CDM Application Controller class for remoting clients
34 *
35 * FIXME:Remoting extending {@link CdmApplicationController} is a temporary workaround.
36 * The {@link CdmApplicationController} should be split into a CdmApplicationControllerBase
37 * class with {@link CdmApplicationController} and this class as subclasses
38 *
39 */
40 public class CdmApplicationRemoteController extends CdmApplicationController {
41
42 private static final Logger logger = Logger.getLogger(CdmApplicationRemoteController.class);
43
44
45 public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE =
46 new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
47 private final Resource applicationContextResource;
48 private final IProgressMonitor progressMonitor;
49 private ICdmRemoteSource remoteSource;
50
51
52 /**
53 * Creates new instance of CdmApplicationRemoteController
54 *
55 * @param applicationContextResource
56 * @param remoteSource
57 * @param omitTermLoading
58 * @param progressMonitor
59 * @param listeners
60 * @return
61 */
62 // public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
63 // ICdmRemoteSource remoteSource,
64 // IProgressMonitor progressMonitor,
65 // List<ApplicationListener> listeners) {
66 // return new CdmApplicationRemoteController(applicationContextResource,
67 // remoteSource,
68 // false,
69 // progressMonitor,
70 // listeners);
71 //
72 // }
73 /**
74 * Creates new instance of CdmApplicationRemoteController
75 *
76 * @param applicationContextResource
77 * @param remoteSource
78 * @param omitTermLoading
79 * @param progressMonitor
80 * @param listeners
81 * @return
82 */
83 public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
84 IProgressMonitor progressMonitor,
85 List<ApplicationListener> listeners) {
86
87 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
88 remoteSource,
89 false,
90 progressMonitor,
91 listeners);
92
93 }
94
95 public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
96 boolean validateXml,
97 IProgressMonitor progressMonitor,
98 List<ApplicationListener> listeners) {
99
100 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
101 remoteSource,
102 validateXml,
103 progressMonitor,
104 listeners);
105
106 }
107
108 /**
109 * Constructs CdmApplicationRemoteController
110 *
111 * @param applicationContextResource
112 * @param remoteSource
113 * @param omitTermLoading
114 * @param progressMonitor
115 * @param listeners
116 */
117 private CdmApplicationRemoteController(Resource applicationContextResource,
118 ICdmRemoteSource remoteSource,
119 boolean validateXml,
120 IProgressMonitor progressMonitor,
121 List<ApplicationListener> listeners){
122 logger.info("Start CdmApplicationRemoteController with remote source: " + remoteSource.getName());
123 this.applicationContextResource =
124 applicationContextResource != null ? applicationContextResource : DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE;
125 this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
126 this.remoteSource = remoteSource;
127 setNewRemoteSource(remoteSource, validateXml, listeners);
128
129 }
130
131
132
133
134 /**
135 * Creates and starts a new spring application context
136 *
137 * @param remoteSource object for connecting to an http-invoker server
138 * @param omitTermLoading
139 * @param listeners
140 * @return
141 */
142 public boolean setNewRemoteSource(ICdmRemoteSource remoteSource,
143 boolean validateXml,
144 List<ApplicationListener> listeners){
145
146 logger.info("Connecting to '" + remoteSource.getName() + "'");
147
148 int nTasks = 2;
149
150 progressMonitor.beginTask("Connecting to '" + remoteSource.getName() + "'", nTasks);
151
152 progressMonitor.subTask("Initialising CDM Model Cache ...");
153 CdmRemoteCacheManager.getInstance();
154 progressMonitor.worked(1);
155 progressMonitor.subTask("Starting application context ...");
156 init();
157 progressMonitor.worked(1);
158 progressMonitor.done();
159 return true;
160 }
161
162
163
164 /* (non-Javadoc)
165 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
166 */
167 @Override
168 protected void init(){
169 configuration = new CdmApplicationRemoteConfiguration(remoteSource);
170 ((CdmApplicationRemoteConfiguration)configuration).setApplicationContext(applicationContext);
171 AbstractLazyInitializer.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
172 AbstractPersistentCollection.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
173 }
174
175 public ICdmEntitySessionManager getCdmEntitySessionManager() {
176 return ((CdmApplicationRemoteConfiguration)configuration).getCdmEntitySessionManager();
177 }
178
179 public IIOService getIOService(){
180 return ((CdmApplicationRemoteConfiguration)configuration).getIOService();
181 }
182
183 public ITestService getTestService(){
184 return ((CdmApplicationRemoteConfiguration)configuration).getTestService();
185 }
186
187 public ICachedCommonService getCachedCommonService(){
188 return ((CdmApplicationRemoteConfiguration)configuration).getCachedCommonService();
189 }
190
191 public IEditGeoService getEditGeoService() {
192 return ((CdmApplicationRemoteConfiguration)configuration).getEditGeoService();
193 }
194 }