2afb681918a286ced8b1350fe6157864674024c9
[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.springframework.beans.MutablePropertyValues;
18 import org.springframework.beans.factory.config.BeanDefinition;
19 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
20 import org.springframework.context.ApplicationListener;
21 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
22 import org.springframework.core.io.ClassPathResource;
23 import org.springframework.core.io.Resource;
24
25 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
26 import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
27 import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
28 import eu.etaxonomy.cdm.remote.ICdmRemoteSource;
29
30 /**
31 * CDM Application Controller class for remoting clients
32 * FIXME:Remoting extending {@link CdmApplicationController} is a temporary workaround.
33 * The {@link CdmApplicationController} should be split into a CdmApplicationControllerBase
34 * class with {@link CdmApplicationController} and this class as subclasses
35 *
36 */
37 public class CdmApplicationRemoteController extends CdmApplicationController {
38
39 private static final Logger logger = Logger.getLogger(CdmApplicationRemoteController.class);
40
41 public static final String DEFAULT_APPLICATION_CONTEXT_RESOURCE = "/eu/etaxonomy/cdm/remoteApplicationContext.xml";
42 private final Resource applicationContextResource;
43 private final IProgressMonitor progressMonitor;
44
45 /**
46 * Creates new instance of CdmApplicationRemoteController
47 *
48 * @param applicationContextResource
49 * @param remoteSource
50 * @param omitTermLoading
51 * @param progressMonitor
52 * @param listeners
53 * @return
54 */
55 public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
56 ICdmRemoteSource remoteSource,
57 boolean omitTermLoading,
58 IProgressMonitor progressMonitor,
59 List<ApplicationListener> listeners) {
60 return new CdmApplicationRemoteController(applicationContextResource,
61 remoteSource,
62 omitTermLoading,
63 progressMonitor,
64 listeners);
65
66 }
67
68 /**
69 * Constructs CdmApplicationRemoteController
70 *
71 * @param applicationContextResource
72 * @param remoteSource
73 * @param omitTermLoading
74 * @param progressMonitor
75 * @param listeners
76 */
77 private CdmApplicationRemoteController(Resource applicationContextResource,
78 ICdmRemoteSource remoteSource,
79 boolean omitTermLoading,
80 IProgressMonitor progressMonitor,
81 List<ApplicationListener> listeners){
82 logger.info("Start CdmApplicationRemoteController with remote source: " + remoteSource.getName());
83 this.applicationContextResource = applicationContextResource != null ? applicationContextResource : getClasspathResource();
84 this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
85
86 setNewRemoteSource(remoteSource, omitTermLoading, listeners);
87
88 }
89
90 /**
91 * Returns classpath resource for application context config file
92 *
93 * @return
94 */
95 protected static ClassPathResource getClasspathResource() {
96 return new ClassPathResource(DEFAULT_APPLICATION_CONTEXT_RESOURCE);
97 }
98
99
100 /**
101 * Creates and starts a new spring application context
102 *
103 * @param remoteSource object for connecting to an http-invoker server
104 * @param omitTermLoading
105 * @param listeners
106 * @return
107 */
108 protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource,
109 boolean omitTermLoading,
110 List<ApplicationListener> listeners){
111
112 logger.info("Connecting to '" + remoteSource.getName() + "'");
113
114 MonitoredGenericApplicationContext applicationContext = new MonitoredGenericApplicationContext();
115 int refreshTasks = 45;
116 int nTasks = 5 + refreshTasks;
117
118 progressMonitor.beginTask("Connecting to '" + remoteSource.getName() + "'", nTasks);
119
120 progressMonitor.subTask("Registering remote source.");
121 PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
122 Properties properties = new Properties();
123 properties.setProperty("remoteServer", remoteSource.getServer());
124 properties.setProperty("remotePort", String.valueOf(remoteSource.getPort()));
125 properties.setProperty("remoteContext", remoteSource.getContextPath());
126 pspc.setProperties(properties);
127 applicationContext.addBeanFactoryPostProcessor(pspc);
128 progressMonitor.worked(1);
129
130 XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
131 progressMonitor.subTask("Registering resources.");
132 xmlReader.loadBeanDefinitions(applicationContextResource);
133 progressMonitor.worked(1);
134
135 //omitTerms
136 if (omitTermLoading == true){
137 String initializerName = "persistentTermInitializer";
138 BeanDefinition beanDef = applicationContext.getBeanDefinition(initializerName);
139 MutablePropertyValues values = beanDef.getPropertyValues();
140 values.addPropertyValue("omit", omitTermLoading);
141 }
142
143 if (listeners != null){
144 for(ApplicationListener listener : listeners){
145 applicationContext.addApplicationListener(listener);
146 }
147 }
148
149
150 applicationContext.refresh(new SubProgressMonitor(progressMonitor, refreshTasks));
151 applicationContext.start();
152
153 progressMonitor.subTask("Cleaning up.");
154 setApplicationContext(applicationContext);
155 progressMonitor.worked(1);
156
157 progressMonitor.done();
158 return true;
159 }
160
161 /* (non-Javadoc)
162 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
163 */
164 @Override
165 protected void init(){
166 configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationRemoteConfiguration");
167
168 }
169
170 }