1 package eu
.etaxonomy
.cdm
.api
.application
;
4 import java
.util
.Properties
;
6 import org
.apache
.log4j
.Logger
;
7 import org
.springframework
.beans
.MutablePropertyValues
;
8 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
9 import org
.springframework
.beans
.factory
.xml
.XmlBeanDefinitionReader
;
10 import org
.springframework
.context
.ApplicationListener
;
11 import org
.springframework
.context
.support
.PropertySourcesPlaceholderConfigurer
;
12 import org
.springframework
.core
.io
.ClassPathResource
;
13 import org
.springframework
.core
.io
.Resource
;
15 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
16 import eu
.etaxonomy
.cdm
.common
.monitor
.NullProgressMonitor
;
17 import eu
.etaxonomy
.cdm
.common
.monitor
.SubProgressMonitor
;
18 import eu
.etaxonomy
.cdm
.remote
.ICdmRemoteSource
;
20 public class CdmApplicationRemoteController
extends CdmApplicationController
{
22 private static final Logger logger
= Logger
.getLogger(CdmApplicationRemoteController
.class);
24 public static final String DEFAULT_APPLICATION_CONTEXT_RESOURCE
= "/eu/etaxonomy/cdm/remoteApplicationContext.xml";
25 private final Resource applicationContextResource
;
26 private final IProgressMonitor progressMonitor
;
28 public static CdmApplicationRemoteController
NewInstance(Resource applicationContextResource
,
29 ICdmRemoteSource remoteSource
,
30 boolean omitTermLoading
,
31 IProgressMonitor progressMonitor
,
32 List
<ApplicationListener
> listeners
) {
33 return new CdmApplicationRemoteController(applicationContextResource
,
41 private CdmApplicationRemoteController(Resource applicationContextResource
,
42 ICdmRemoteSource remoteSource
,
43 boolean omitTermLoading
,
44 IProgressMonitor progressMonitor
,
45 List
<ApplicationListener
> listeners
){
46 logger
.info("Start CdmApplicationRemoteController with remote source: " + remoteSource
.getName());
47 this.applicationContextResource
= applicationContextResource
!= null ? applicationContextResource
: getClasspathResource();
48 this.progressMonitor
= progressMonitor
!= null ? progressMonitor
: new NullProgressMonitor();
50 setNewRemoteSource(remoteSource
, omitTermLoading
, listeners
);
57 protected static ClassPathResource
getClasspathResource() {
58 return new ClassPathResource(DEFAULT_APPLICATION_CONTEXT_RESOURCE
);
61 * Sets the application context to a new spring ApplicationContext by using the according data source and initializes the Controller.
64 protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource
,
65 boolean omitTermLoading
,
66 List
<ApplicationListener
> listeners
){
68 logger
.info("Connecting to '" + remoteSource
.getName() + "'");
70 MonitoredGenericApplicationContext applicationContext
= new MonitoredGenericApplicationContext();
71 int refreshTasks
= 45;
72 int nTasks
= 5 + refreshTasks
;
74 progressMonitor
.beginTask("Connecting to '" + remoteSource
.getName() + "'", nTasks
);
76 progressMonitor
.subTask("Registering remote source.");
77 PropertySourcesPlaceholderConfigurer pspc
= new PropertySourcesPlaceholderConfigurer();
78 Properties properties
= new Properties();
79 properties
.setProperty("remoteServer", remoteSource
.getServer());
80 properties
.setProperty("remotePort", String
.valueOf(remoteSource
.getPort()));
81 properties
.setProperty("remoteContext", remoteSource
.getContextPath());
82 pspc
.setProperties(properties
);
83 applicationContext
.addBeanFactoryPostProcessor(pspc
);
84 progressMonitor
.worked(1);
86 // BeanDefinition remoteSourceBean = remoteSource.getRemoteSourceBean();
87 // remoteSourceBean.setAttribute("isLazy", false);
88 // applicationContext.registerBeanDefinition("dataSource", remoteSourceBean);
91 XmlBeanDefinitionReader xmlReader
= new XmlBeanDefinitionReader(applicationContext
);
92 progressMonitor
.subTask("Registering resources.");
93 xmlReader
.loadBeanDefinitions(applicationContextResource
);
94 progressMonitor
.worked(1);
97 if (omitTermLoading
== true){
98 String initializerName
= "persistentTermInitializer";
99 BeanDefinition beanDef
= applicationContext
.getBeanDefinition(initializerName
);
100 MutablePropertyValues values
= beanDef
.getPropertyValues();
101 values
.addPropertyValue("omit", omitTermLoading
);
104 if (listeners
!= null){
105 for(ApplicationListener listener
: listeners
){
106 applicationContext
.addApplicationListener(listener
);
111 applicationContext
.refresh(new SubProgressMonitor(progressMonitor
, refreshTasks
));
112 applicationContext
.start();
114 progressMonitor
.subTask("Cleaning up.");
115 setApplicationContext(applicationContext
);
116 progressMonitor
.worked(1);
118 progressMonitor
.done();
123 protected void init(){
124 logger
.debug("Init " + this.getClass().getName() + " ... ");
125 if (logger
.isDebugEnabled()){for (String beanName
: applicationContext
.getBeanDefinitionNames()){ logger
.debug(beanName
);}}
126 //TODO delete next row (was just for testing)
127 if (logger
.isInfoEnabled()){
128 logger
.info("Registered Beans: ");
129 String
[] beanNames
= applicationContext
.getBeanDefinitionNames();
130 for (String beanName
: beanNames
){
131 logger
.info(beanName
);
134 configuration
= (ICdmApplicationConfiguration
)applicationContext
.getBean("cdmApplicationRemoteConfiguration");