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
.factory
.xml
.XmlBeanDefinitionReader
;
20 import org
.springframework
.context
.ApplicationListener
;
21 import org
.springframework
.context
.support
.GenericApplicationContext
;
22 import org
.springframework
.context
.support
.PropertySourcesPlaceholderConfigurer
;
23 import org
.springframework
.core
.io
.ClassPathResource
;
24 import org
.springframework
.core
.io
.Resource
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITestService
;
27 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
28 import eu
.etaxonomy
.cdm
.common
.monitor
.NullProgressMonitor
;
29 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
30 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
31 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
34 * CDM Application Controller class for remoting clients
36 * FIXME:Remoting extending {@link CdmApplicationController} is a temporary workaround.
37 * The {@link CdmApplicationController} should be split into a CdmApplicationControllerBase
38 * class with {@link CdmApplicationController} and this class as subclasses
41 public class CdmApplicationRemoteController
extends CdmApplicationController
{
43 private static final Logger logger
= Logger
.getLogger(CdmApplicationRemoteController
.class);
46 public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
=
47 new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
48 private final Resource applicationContextResource
;
49 private final IProgressMonitor progressMonitor
;
53 * Creates new instance of CdmApplicationRemoteController
55 * @param applicationContextResource
57 * @param omitTermLoading
58 * @param progressMonitor
62 // public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
63 // ICdmRemoteSource remoteSource,
64 // IProgressMonitor progressMonitor,
65 // List<ApplicationListener> listeners) {
66 // return new CdmApplicationRemoteController(applicationContextResource,
74 * Creates new instance of CdmApplicationRemoteController
76 * @param applicationContextResource
78 * @param omitTermLoading
79 * @param progressMonitor
83 public static CdmApplicationRemoteController
NewInstance(ICdmRemoteSource remoteSource
,
84 IProgressMonitor progressMonitor
,
85 List
<ApplicationListener
> listeners
) {
87 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
,
95 public static CdmApplicationRemoteController
NewInstance(ICdmRemoteSource remoteSource
,
97 IProgressMonitor progressMonitor
,
98 List
<ApplicationListener
> listeners
) {
100 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
,
109 * Constructs CdmApplicationRemoteController
111 * @param applicationContextResource
112 * @param remoteSource
113 * @param omitTermLoading
114 * @param progressMonitor
117 private CdmApplicationRemoteController(Resource applicationContextResource
,
118 ICdmRemoteSource remoteSource
,
120 IProgressMonitor progressMonitor
,
121 List
<ApplicationListener
> listeners
){
122 logger
.info("Start CdmApplicationRemoteController with remote source: " + remoteSource
.getName());
123 this.applicationContextResource
=
124 applicationContextResource
!= null ? applicationContextResource
: DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
;
125 this.progressMonitor
= progressMonitor
!= null ? progressMonitor
: new NullProgressMonitor();
126 setNewRemoteSource(remoteSource
, validateXml
, listeners
);
134 * Creates and starts a new spring application context
136 * @param remoteSource object for connecting to an http-invoker server
137 * @param omitTermLoading
141 public boolean setNewRemoteSource(ICdmRemoteSource remoteSource
,
143 List
<ApplicationListener
> listeners
){
145 logger
.info("Connecting to '" + remoteSource
.getName() + "'");
147 GenericApplicationContext applicationContext
= new GenericApplicationContext();
151 progressMonitor
.beginTask("Connecting to '" + remoteSource
.getName() + "'", nTasks
);
153 progressMonitor
.subTask("Loading context beans ...");
154 // initialising the cdm model cache
155 CdmRemoteCacheManager
.getInstance();
157 PropertySourcesPlaceholderConfigurer pspc
= new PropertySourcesPlaceholderConfigurer();
158 Properties properties
= new Properties();
159 properties
.setProperty("remoteServer", remoteSource
.getServer());
160 properties
.setProperty("remotePort", String
.valueOf(remoteSource
.getPort()));
161 properties
.setProperty("remoteContext", remoteSource
.getContextPath());
162 pspc
.setProperties(properties
);
163 applicationContext
.addBeanFactoryPostProcessor(pspc
);
164 applicationContext
.getEnvironment().setActiveProfiles("remoting");
165 XmlBeanDefinitionReader xmlReader
= new XmlBeanDefinitionReader(applicationContext
);
167 xmlReader
.setValidating(false);
169 xmlReader
.loadBeanDefinitions(applicationContextResource
);
170 if (listeners
!= null){
171 for(ApplicationListener listener
: listeners
){
172 applicationContext
.addApplicationListener(listener
);
175 progressMonitor
.worked(1);
177 progressMonitor
.subTask("Starting context ...");
178 applicationContext
.refresh();
179 applicationContext
.start();
180 setApplicationContext(applicationContext
);
181 progressMonitor
.worked(1);
182 progressMonitor
.done();
189 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
192 protected void init(){
194 // retrieving the application configuration
195 configuration
= (ICdmApplicationConfiguration
)applicationContext
.getBean("cdmApplicationRemoteConfiguration");
196 AbstractLazyInitializer
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
197 AbstractPersistentCollection
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
203 public ICdmEntitySessionManager
getCdmEntitySessionManager() {
204 return ((CdmApplicationRemoteConfiguration
)configuration
).getCdmEntitySessionManager();
208 public ITestService
getTestService(){
209 return (ITestService
) getBean("testService");