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
.common
.monitor
.IProgressMonitor
;
27 import eu
.etaxonomy
.cdm
.common
.monitor
.NullProgressMonitor
;
28 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
29 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
30 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
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);
45 public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
=
46 new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
47 private final Resource applicationContextResource
;
48 private final IProgressMonitor progressMonitor
;
52 * Creates new instance of CdmApplicationRemoteController
54 * @param applicationContextResource
56 * @param omitTermLoading
57 * @param progressMonitor
61 // public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
62 // ICdmRemoteSource remoteSource,
63 // IProgressMonitor progressMonitor,
64 // List<ApplicationListener> listeners) {
65 // return new CdmApplicationRemoteController(applicationContextResource,
73 * Creates new instance of CdmApplicationRemoteController
75 * @param applicationContextResource
77 * @param omitTermLoading
78 * @param progressMonitor
82 public static CdmApplicationRemoteController
NewInstance(ICdmRemoteSource remoteSource
,
83 IProgressMonitor progressMonitor
,
84 List
<ApplicationListener
> listeners
) {
86 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
,
94 public static CdmApplicationRemoteController
NewInstance(ICdmRemoteSource remoteSource
,
96 IProgressMonitor progressMonitor
,
97 List
<ApplicationListener
> listeners
) {
99 return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
,
108 * Constructs CdmApplicationRemoteController
110 * @param applicationContextResource
111 * @param remoteSource
112 * @param omitTermLoading
113 * @param progressMonitor
116 private CdmApplicationRemoteController(Resource applicationContextResource
,
117 ICdmRemoteSource remoteSource
,
119 IProgressMonitor progressMonitor
,
120 List
<ApplicationListener
> listeners
){
121 logger
.info("Start CdmApplicationRemoteController with remote source: " + remoteSource
.getName());
122 this.applicationContextResource
=
123 applicationContextResource
!= null ? applicationContextResource
: DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE
;
124 this.progressMonitor
= progressMonitor
!= null ? progressMonitor
: new NullProgressMonitor();
125 setNewRemoteSource(remoteSource
, validateXml
, listeners
);
133 * Creates and starts a new spring application context
135 * @param remoteSource object for connecting to an http-invoker server
136 * @param omitTermLoading
140 public boolean setNewRemoteSource(ICdmRemoteSource remoteSource
,
142 List
<ApplicationListener
> listeners
){
144 logger
.info("Connecting to '" + remoteSource
.getName() + "'");
146 GenericApplicationContext applicationContext
= new GenericApplicationContext();
150 progressMonitor
.beginTask("Connecting to '" + remoteSource
.getName() + "'", nTasks
);
152 progressMonitor
.subTask("Loading context beans ...");
153 // initialising the cdm model cache
154 CdmRemoteCacheManager
.getInstance();
156 PropertySourcesPlaceholderConfigurer pspc
= new PropertySourcesPlaceholderConfigurer();
157 Properties properties
= new Properties();
158 properties
.setProperty("remoteServer", remoteSource
.getServer());
159 properties
.setProperty("remotePort", String
.valueOf(remoteSource
.getPort()));
160 properties
.setProperty("remoteContext", remoteSource
.getContextPath());
161 pspc
.setProperties(properties
);
162 applicationContext
.addBeanFactoryPostProcessor(pspc
);
163 applicationContext
.getEnvironment().setActiveProfiles("remoting");
164 XmlBeanDefinitionReader xmlReader
= new XmlBeanDefinitionReader(applicationContext
);
166 xmlReader
.setValidating(false);
168 xmlReader
.loadBeanDefinitions(applicationContextResource
);
169 if (listeners
!= null){
170 for(ApplicationListener listener
: listeners
){
171 applicationContext
.addApplicationListener(listener
);
174 progressMonitor
.worked(1);
176 progressMonitor
.subTask("Starting context ...");
177 applicationContext
.refresh();
178 applicationContext
.start();
179 setApplicationContext(applicationContext
);
180 progressMonitor
.worked(1);
181 progressMonitor
.done();
188 * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
191 protected void init(){
193 // retrieving the application configuration
194 configuration
= (ICdmApplicationConfiguration
)applicationContext
.getBean("cdmApplicationRemoteConfiguration");
195 AbstractLazyInitializer
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
196 AbstractPersistentCollection
.setConfiguration((CdmApplicationRemoteConfiguration
)configuration
);
202 public ICdmEntitySessionManager
getCdmEntitySessionManager() {
203 return ((CdmApplicationRemoteConfiguration
)configuration
).getCdmEntitySessionManager();