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
.annotation
.Autowired
;
21 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
22 import org
.springframework
.beans
.factory
.xml
.XmlBeanDefinitionReader
;
23 import org
.springframework
.context
.ApplicationListener
;
24 import org
.springframework
.context
.support
.PropertySourcesPlaceholderConfigurer
;
25 import org
.springframework
.core
.io
.ClassPathResource
;
26 import org
.springframework
.core
.io
.Resource
;
28 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
29 import eu
.etaxonomy
.cdm
.common
.monitor
.NullProgressMonitor
;
30 import eu
.etaxonomy
.cdm
.common
.monitor
.SubProgressMonitor
;
31 import eu
.etaxonomy
.cdm
.remote
.ICdmRemoteSource
;
32 import eu
.etaxonomy
.taxeditor
.remoting
.session
.CdmEntitySessionManager
;
35 * CDM Application Controller class for remoting clients
37 * FIXME:Remoting extending {@link CdmApplicationController} is a temporary workaround.
38 * The {@link CdmApplicationController} should be split into a CdmApplicationControllerBase
39 * class with {@link CdmApplicationController} and this class as subclasses
42 public class CdmApplicationRemoteController
extends CdmApplicationController
{
44 private static final Logger logger
= Logger
.getLogger(CdmApplicationRemoteController
.class);
47 public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
=
48 new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
49 private final Resource applicationContextResource
;
50 private final IProgressMonitor progressMonitor
;
54 * Creates new instance of CdmApplicationRemoteController
56 * @param applicationContextResource
58 * @param omitTermLoading
59 * @param progressMonitor
63 public static CdmApplicationRemoteController
NewInstance(Resource applicationContextResource
,
64 ICdmRemoteSource remoteSource
,
65 boolean omitTermLoading
,
66 IProgressMonitor progressMonitor
,
67 List
<ApplicationListener
> listeners
) {
68 return new CdmApplicationRemoteController(applicationContextResource
,
76 * Creates new instance of CdmApplicationRemoteController
78 * @param applicationContextResource
80 * @param omitTermLoading
81 * @param progressMonitor
85 public static CdmApplicationRemoteController
NewInstance(ICdmRemoteSource remoteSource
,
86 boolean omitTermLoading
,
87 IProgressMonitor progressMonitor
,
88 List
<ApplicationListener
> listeners
) {
90 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
,
99 * Constructs CdmApplicationRemoteController
101 * @param applicationContextResource
102 * @param remoteSource
103 * @param omitTermLoading
104 * @param progressMonitor
107 private CdmApplicationRemoteController(Resource applicationContextResource
,
108 ICdmRemoteSource remoteSource
,
109 boolean omitTermLoading
,
110 IProgressMonitor progressMonitor
,
111 List
<ApplicationListener
> listeners
){
112 logger
.info("Start CdmApplicationRemoteController with remote source: " + remoteSource
.getName());
113 this.applicationContextResource
=
114 applicationContextResource
!= null ? applicationContextResource
: DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
;
115 this.progressMonitor
= progressMonitor
!= null ? progressMonitor
: new NullProgressMonitor();
117 setNewRemoteSource(remoteSource
, omitTermLoading
, listeners
);
125 * Creates and starts a new spring application context
127 * @param remoteSource object for connecting to an http-invoker server
128 * @param omitTermLoading
132 protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource
,
133 boolean omitTermLoading
,
134 List
<ApplicationListener
> listeners
){
136 logger
.info("Connecting to '" + remoteSource
.getName() + "'");
138 MonitoredGenericApplicationContext applicationContext
= new MonitoredGenericApplicationContext();
139 int refreshTasks
= 45;
140 int nTasks
= 5 + refreshTasks
;
142 progressMonitor
.beginTask("Connecting to '" + remoteSource
.getName() + "'", nTasks
);
144 progressMonitor
.subTask("Registering remote source.");
145 PropertySourcesPlaceholderConfigurer pspc
= new PropertySourcesPlaceholderConfigurer();
146 Properties properties
= new Properties();
147 properties
.setProperty("remoteServer", remoteSource
.getServer());
148 properties
.setProperty("remotePort", String
.valueOf(remoteSource
.getPort()));
149 properties
.setProperty("remoteContext", remoteSource
.getContextPath());
150 pspc
.setProperties(properties
);
151 applicationContext
.addBeanFactoryPostProcessor(pspc
);
152 applicationContext
.getEnvironment().setActiveProfiles("remoting");
153 progressMonitor
.worked(1);
155 XmlBeanDefinitionReader xmlReader
= new XmlBeanDefinitionReader(applicationContext
);
156 //xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
157 progressMonitor
.subTask("Registering resources.");
158 xmlReader
.loadBeanDefinitions(applicationContextResource
);
159 progressMonitor
.worked(1);
162 if (omitTermLoading
== true){
163 String initializerName
= "persistentTermInitializer";
164 BeanDefinition beanDef
= applicationContext
.getBeanDefinition(initializerName
);
165 MutablePropertyValues values
= beanDef
.getPropertyValues();
166 values
.addPropertyValue("omit", omitTermLoading
);
169 if (listeners
!= null){
170 for(ApplicationListener listener
: listeners
){
171 applicationContext
.addApplicationListener(listener
);
176 applicationContext
.refresh(new SubProgressMonitor(progressMonitor
, refreshTasks
));
177 applicationContext
.start();
179 progressMonitor
.subTask("Cleaning up.");
180 setApplicationContext(applicationContext
);
181 progressMonitor
.worked(1);
183 progressMonitor
.done();
188 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
191 protected void init(){
192 configuration
= (ICdmApplicationConfiguration
)applicationContext
.getBean("cdmApplicationRemoteConfiguration");
193 AbstractLazyInitializer
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
194 AbstractPersistentCollection
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
198 public CdmEntitySessionManager
getCdmEntitySessionManager() {
199 return ((CdmApplicationRemoteConfiguration
)configuration
).getCdmEntitySessionManager();