Use wizard to create new specimen
[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.MutablePropertyValues;
20 import org.springframework.beans.factory.config.BeanDefinition;
21 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
22 import org.springframework.context.ApplicationListener;
23 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
24 import org.springframework.core.io.ClassPathResource;
25 import org.springframework.core.io.Resource;
26
27 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
28 import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
29 import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
30 import eu.etaxonomy.cdm.remote.ICdmRemoteSource;
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 public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE =
45 new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
46 private final Resource applicationContextResource;
47 private final IProgressMonitor progressMonitor;
48
49 /**
50 * Creates new instance of CdmApplicationRemoteController
51 *
52 * @param applicationContextResource
53 * @param remoteSource
54 * @param omitTermLoading
55 * @param progressMonitor
56 * @param listeners
57 * @return
58 */
59 public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
60 ICdmRemoteSource remoteSource,
61 boolean omitTermLoading,
62 IProgressMonitor progressMonitor,
63 List<ApplicationListener> listeners) {
64 return new CdmApplicationRemoteController(applicationContextResource,
65 remoteSource,
66 omitTermLoading,
67 progressMonitor,
68 listeners);
69
70 }
71 /**
72 * Creates new instance of CdmApplicationRemoteController
73 *
74 * @param applicationContextResource
75 * @param remoteSource
76 * @param omitTermLoading
77 * @param progressMonitor
78 * @param listeners
79 * @return
80 */
81 public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
82 boolean omitTermLoading,
83 IProgressMonitor progressMonitor,
84 List<ApplicationListener> listeners) {
85
86 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
87 remoteSource,
88 omitTermLoading,
89 progressMonitor,
90 listeners);
91
92 }
93
94 /**
95 * Constructs CdmApplicationRemoteController
96 *
97 * @param applicationContextResource
98 * @param remoteSource
99 * @param omitTermLoading
100 * @param progressMonitor
101 * @param listeners
102 */
103 private CdmApplicationRemoteController(Resource applicationContextResource,
104 ICdmRemoteSource remoteSource,
105 boolean omitTermLoading,
106 IProgressMonitor progressMonitor,
107 List<ApplicationListener> listeners){
108 logger.info("Start CdmApplicationRemoteController with remote source: " + remoteSource.getName());
109 this.applicationContextResource =
110 applicationContextResource != null ? applicationContextResource : DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE;
111 this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
112
113 setNewRemoteSource(remoteSource, omitTermLoading, listeners);
114
115 }
116
117
118
119
120 /**
121 * Creates and starts a new spring application context
122 *
123 * @param remoteSource object for connecting to an http-invoker server
124 * @param omitTermLoading
125 * @param listeners
126 * @return
127 */
128 protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource,
129 boolean omitTermLoading,
130 List<ApplicationListener> listeners){
131
132 logger.info("Connecting to '" + remoteSource.getName() + "'");
133
134 MonitoredGenericApplicationContext applicationContext = new MonitoredGenericApplicationContext();
135 int refreshTasks = 45;
136 int nTasks = 5 + refreshTasks;
137
138 progressMonitor.beginTask("Connecting to '" + remoteSource.getName() + "'", nTasks);
139
140 progressMonitor.subTask("Registering remote source.");
141 PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
142 Properties properties = new Properties();
143 properties.setProperty("remoteServer", remoteSource.getServer());
144 properties.setProperty("remotePort", String.valueOf(remoteSource.getPort()));
145 properties.setProperty("remoteContext", remoteSource.getContextPath());
146 pspc.setProperties(properties);
147 applicationContext.addBeanFactoryPostProcessor(pspc);
148 progressMonitor.worked(1);
149
150 XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
151 //xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
152 progressMonitor.subTask("Registering resources.");
153 xmlReader.loadBeanDefinitions(applicationContextResource);
154 progressMonitor.worked(1);
155
156 //omitTerms
157 if (omitTermLoading == true){
158 String initializerName = "persistentTermInitializer";
159 BeanDefinition beanDef = applicationContext.getBeanDefinition(initializerName);
160 MutablePropertyValues values = beanDef.getPropertyValues();
161 values.addPropertyValue("omit", omitTermLoading);
162 }
163
164 if (listeners != null){
165 for(ApplicationListener listener : listeners){
166 applicationContext.addApplicationListener(listener);
167 }
168 }
169
170
171 applicationContext.refresh(new SubProgressMonitor(progressMonitor, refreshTasks));
172 applicationContext.start();
173
174 progressMonitor.subTask("Cleaning up.");
175 setApplicationContext(applicationContext);
176 progressMonitor.worked(1);
177
178 progressMonitor.done();
179 return true;
180 }
181
182 /* (non-Javadoc)
183 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
184 */
185 @Override
186 protected void init(){
187 configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationRemoteConfiguration");
188 AbstractLazyInitializer.setConfiguration(this);
189 AbstractPersistentCollection.setConfiguration(this);
190
191 }
192
193 }