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
.api
.application
.CdmApplicationController
;
28 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
29 import eu
.etaxonomy
.cdm
.api
.application
.RemotingMonitoredGenericApplicationContext
;
30 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
31 import eu
.etaxonomy
.cdm
.common
.monitor
.NullProgressMonitor
;
32 import eu
.etaxonomy
.cdm
.common
.monitor
.SubProgressMonitor
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmModelCacher
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
35 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySessionManager
;
36 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
39 * CDM Application Controller class for remoting clients
41 * FIXME:Remoting extending {@link CdmApplicationController} is a temporary workaround.
42 * The {@link CdmApplicationController} should be split into a CdmApplicationControllerBase
43 * class with {@link CdmApplicationController} and this class as subclasses
46 public class CdmApplicationRemoteController
extends CdmApplicationController
{
48 private static final Logger logger
= Logger
.getLogger(CdmApplicationRemoteController
.class);
51 public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
=
52 new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
53 private final Resource applicationContextResource
;
54 private final IProgressMonitor progressMonitor
;
58 * Creates new instance of CdmApplicationRemoteController
60 * @param applicationContextResource
62 * @param omitTermLoading
63 * @param progressMonitor
67 public static CdmApplicationRemoteController
NewInstance(Resource applicationContextResource
,
68 ICdmRemoteSource remoteSource
,
69 boolean omitTermLoading
,
70 IProgressMonitor progressMonitor
,
71 List
<ApplicationListener
> listeners
) {
72 return new CdmApplicationRemoteController(applicationContextResource
,
80 * Creates new instance of CdmApplicationRemoteController
82 * @param applicationContextResource
84 * @param omitTermLoading
85 * @param progressMonitor
89 public static CdmApplicationRemoteController
NewInstance(ICdmRemoteSource remoteSource
,
90 boolean omitTermLoading
,
91 IProgressMonitor progressMonitor
,
92 List
<ApplicationListener
> listeners
) {
94 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
,
103 * Constructs CdmApplicationRemoteController
105 * @param applicationContextResource
106 * @param remoteSource
107 * @param omitTermLoading
108 * @param progressMonitor
111 private CdmApplicationRemoteController(Resource applicationContextResource
,
112 ICdmRemoteSource remoteSource
,
113 boolean omitTermLoading
,
114 IProgressMonitor progressMonitor
,
115 List
<ApplicationListener
> listeners
){
116 logger
.info("Start CdmApplicationRemoteController with remote source: " + remoteSource
.getName());
117 this.applicationContextResource
=
118 applicationContextResource
!= null ? applicationContextResource
: DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
;
119 this.progressMonitor
= progressMonitor
!= null ? progressMonitor
: new NullProgressMonitor();
121 setNewRemoteSource(remoteSource
, omitTermLoading
, listeners
);
129 * Creates and starts a new spring application context
131 * @param remoteSource object for connecting to an http-invoker server
132 * @param omitTermLoading
136 protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource
,
137 boolean omitTermLoading
,
138 List
<ApplicationListener
> listeners
){
140 logger
.info("Connecting to '" + remoteSource
.getName() + "'");
142 RemotingMonitoredGenericApplicationContext applicationContext
= new RemotingMonitoredGenericApplicationContext();
143 int refreshTasks
= 45;
144 int nTasks
= 5 + refreshTasks
;
146 progressMonitor
.beginTask("Connecting to '" + remoteSource
.getName() + "'", nTasks
);
148 progressMonitor
.subTask("Registering remote source.");
149 PropertySourcesPlaceholderConfigurer pspc
= new PropertySourcesPlaceholderConfigurer();
150 Properties properties
= new Properties();
151 properties
.setProperty("remoteServer", remoteSource
.getServer());
152 properties
.setProperty("remotePort", String
.valueOf(remoteSource
.getPort()));
153 properties
.setProperty("remoteContext", remoteSource
.getContextPath());
154 pspc
.setProperties(properties
);
155 applicationContext
.addBeanFactoryPostProcessor(pspc
);
156 applicationContext
.getEnvironment().setActiveProfiles("remoting");
157 progressMonitor
.worked(1);
159 XmlBeanDefinitionReader xmlReader
= new XmlBeanDefinitionReader(applicationContext
);
160 //xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
161 progressMonitor
.subTask("Registering resources.");
162 xmlReader
.loadBeanDefinitions(applicationContextResource
);
163 progressMonitor
.worked(1);
166 if (omitTermLoading
== true){
167 String initializerName
= "persistentTermInitializer";
168 BeanDefinition beanDef
= applicationContext
.getBeanDefinition(initializerName
);
169 MutablePropertyValues values
= beanDef
.getPropertyValues();
170 values
.addPropertyValue("omit", omitTermLoading
);
173 if (listeners
!= null){
174 for(ApplicationListener listener
: listeners
){
175 applicationContext
.addApplicationListener(listener
);
180 applicationContext
.refresh(new SubProgressMonitor(progressMonitor
, refreshTasks
));
181 applicationContext
.start();
183 progressMonitor
.subTask("Cleaning up.");
184 setApplicationContext(applicationContext
);
185 progressMonitor
.worked(1);
187 progressMonitor
.done();
192 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
195 protected void init(){
196 configuration
= (ICdmApplicationConfiguration
)applicationContext
.getBean("cdmApplicationRemoteConfiguration");
197 AbstractLazyInitializer
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
198 AbstractPersistentCollection
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
200 CdmModelCacher cmdmc
= new CdmModelCacher();
201 cmdmc
.cacheGetterFields();
205 public ICdmEntitySessionManager
getCdmEntitySessionManager() {
206 return ((CdmApplicationRemoteConfiguration
)configuration
).getCdmEntitySessionManager();