d0681e20c296d8da69e6f39a9c0cf49d45aa15d8
[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 import java.util.Properties;
15
16 import org.apache.log4j.Logger;
17 import org.hibernate.collection.internal.AbstractPersistentCollection;
18 import org.hibernate.proxy.AbstractLazyInitializer;
19 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
20 import org.springframework.context.ApplicationListener;
21 import org.springframework.context.support.GenericApplicationContext;
22 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
23 import org.springframework.core.io.ClassPathResource;
24 import org.springframework.core.io.Resource;
25
26 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
27 import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
28 import eu.etaxonomy.taxeditor.remoting.cache.CdmModelCacher;
29 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
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
50
51 /**
52 * Creates new instance of CdmApplicationRemoteController
53 *
54 * @param applicationContextResource
55 * @param remoteSource
56 * @param omitTermLoading
57 * @param progressMonitor
58 * @param listeners
59 * @return
60 */
61 // public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
62 // ICdmRemoteSource remoteSource,
63 // IProgressMonitor progressMonitor,
64 // List<ApplicationListener> listeners) {
65 // return new CdmApplicationRemoteController(applicationContextResource,
66 // remoteSource,
67 // false,
68 // progressMonitor,
69 // listeners);
70 //
71 // }
72 /**
73 * Creates new instance of CdmApplicationRemoteController
74 *
75 * @param applicationContextResource
76 * @param remoteSource
77 * @param omitTermLoading
78 * @param progressMonitor
79 * @param listeners
80 * @return
81 */
82 public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
83 IProgressMonitor progressMonitor,
84 List<ApplicationListener> listeners) {
85
86 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
87 remoteSource,
88 false,
89 progressMonitor,
90 listeners);
91
92 }
93
94 public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
95 boolean validateXml,
96 IProgressMonitor progressMonitor,
97 List<ApplicationListener> listeners) {
98
99 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
100 remoteSource,
101 validateXml,
102 progressMonitor,
103 listeners);
104
105 }
106
107 /**
108 * Constructs CdmApplicationRemoteController
109 *
110 * @param applicationContextResource
111 * @param remoteSource
112 * @param omitTermLoading
113 * @param progressMonitor
114 * @param listeners
115 */
116 private CdmApplicationRemoteController(Resource applicationContextResource,
117 ICdmRemoteSource remoteSource,
118 boolean validateXml,
119 IProgressMonitor progressMonitor,
120 List<ApplicationListener> listeners){
121 logger.info("Start CdmApplicationRemoteController with remote source: " + remoteSource.getName());
122 this.applicationContextResource =
123 applicationContextResource != null ? applicationContextResource : DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE;
124 this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
125 setNewRemoteSource(remoteSource, validateXml, listeners);
126
127 }
128
129
130
131
132 /**
133 * Creates and starts a new spring application context
134 *
135 * @param remoteSource object for connecting to an http-invoker server
136 * @param omitTermLoading
137 * @param listeners
138 * @return
139 */
140 public boolean setNewRemoteSource(ICdmRemoteSource remoteSource,
141 boolean validateXml,
142 List<ApplicationListener> listeners){
143
144 logger.info("Connecting to '" + remoteSource.getName() + "'");
145
146 GenericApplicationContext applicationContext = new GenericApplicationContext();
147
148 int nTasks = 5;
149
150 progressMonitor.beginTask("Connecting to '" + remoteSource.getName() + "'", nTasks);
151
152 progressMonitor.subTask("Loading context beans ...");
153 PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
154 Properties properties = new Properties();
155 properties.setProperty("remoteServer", remoteSource.getServer());
156 properties.setProperty("remotePort", String.valueOf(remoteSource.getPort()));
157 properties.setProperty("remoteContext", remoteSource.getContextPath());
158 pspc.setProperties(properties);
159 applicationContext.addBeanFactoryPostProcessor(pspc);
160 applicationContext.getEnvironment().setActiveProfiles("remoting");
161 XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
162 if(!validateXml) {
163 xmlReader.setValidating(false);
164 }
165 xmlReader.loadBeanDefinitions(applicationContextResource);
166 if (listeners != null){
167 for(ApplicationListener listener : listeners){
168 applicationContext.addApplicationListener(listener);
169 }
170 }
171 progressMonitor.worked(1);
172
173 progressMonitor.subTask("Refreshing / Starting context ...");
174 applicationContext.refresh();
175 applicationContext.start();
176 progressMonitor.worked(1);
177
178 progressMonitor.subTask("Cleaning up ...");
179 setApplicationContext(applicationContext);
180 progressMonitor.worked(1);
181
182 return true;
183 }
184
185
186
187 /* (non-Javadoc)
188 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
189 */
190 @Override
191 protected void init(){
192 progressMonitor.subTask("Loading configuration ...");
193 configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationRemoteConfiguration");
194 AbstractLazyInitializer.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
195 AbstractPersistentCollection.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
196 progressMonitor.worked(1);
197
198 progressMonitor.subTask("Loading CDM config cache ...");
199 CdmModelCacher cmdmc = new CdmModelCacher();
200 cmdmc.cacheGetterFields();
201 progressMonitor.worked(1);
202
203 progressMonitor.done();
204 }
205
206 public ICdmEntitySessionManager getCdmEntitySessionManager() {
207 return ((CdmApplicationRemoteConfiguration)configuration).getCdmEntitySessionManager();
208 }
209 }