2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.api
.application
;
13 import java
.util
.List
;
14 import java
.util
.Properties
;
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
;
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
;
33 * CDM Application Controller class for remoting clients
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
40 public class CdmApplicationRemoteController
extends CdmApplicationController
{
42 private static final Logger logger
= Logger
.getLogger(CdmApplicationRemoteController
.class);
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
;
50 * Creates new instance of CdmApplicationRemoteController
52 * @param applicationContextResource
54 * @param omitTermLoading
55 * @param progressMonitor
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
,
72 * Creates new instance of CdmApplicationRemoteController
74 * @param applicationContextResource
76 * @param omitTermLoading
77 * @param progressMonitor
81 public static CdmApplicationRemoteController
NewInstance(ICdmRemoteSource remoteSource
,
82 boolean omitTermLoading
,
83 IProgressMonitor progressMonitor
,
84 List
<ApplicationListener
> listeners
) {
86 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
,
95 * Constructs CdmApplicationRemoteController
97 * @param applicationContextResource
99 * @param omitTermLoading
100 * @param progressMonitor
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();
113 setNewRemoteSource(remoteSource
, omitTermLoading
, listeners
);
121 * Creates and starts a new spring application context
123 * @param remoteSource object for connecting to an http-invoker server
124 * @param omitTermLoading
128 protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource
,
129 boolean omitTermLoading
,
130 List
<ApplicationListener
> listeners
){
132 logger
.info("Connecting to '" + remoteSource
.getName() + "'");
134 MonitoredGenericApplicationContext applicationContext
= new MonitoredGenericApplicationContext();
135 int refreshTasks
= 45;
136 int nTasks
= 5 + refreshTasks
;
138 progressMonitor
.beginTask("Connecting to '" + remoteSource
.getName() + "'", nTasks
);
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);
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);
157 if (omitTermLoading
== true){
158 String initializerName
= "persistentTermInitializer";
159 BeanDefinition beanDef
= applicationContext
.getBeanDefinition(initializerName
);
160 MutablePropertyValues values
= beanDef
.getPropertyValues();
161 values
.addPropertyValue("omit", omitTermLoading
);
164 if (listeners
!= null){
165 for(ApplicationListener listener
: listeners
){
166 applicationContext
.addApplicationListener(listener
);
171 applicationContext
.refresh(new SubProgressMonitor(progressMonitor
, refreshTasks
));
172 applicationContext
.start();
174 progressMonitor
.subTask("Cleaning up.");
175 setApplicationContext(applicationContext
);
176 progressMonitor
.worked(1);
178 progressMonitor
.done();
183 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
186 protected void init(){
187 configuration
= (ICdmApplicationConfiguration
)applicationContext
.getBean("cdmApplicationRemoteConfiguration");
188 AbstractLazyInitializer
.setConfiguration(this);
189 AbstractPersistentCollection
.setConfiguration(this);