1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.application;
|
10
|
|
11
|
import java.util.concurrent.CancellationException;
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.springframework.beans.BeansException;
|
15
|
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
|
16
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
17
|
import org.springframework.context.support.GenericApplicationContext;
|
18
|
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
19
|
|
20
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
21
|
import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
|
22
|
|
23
|
/**
|
24
|
* {@link GenericApplicationContext Generic application context} which allows progress monitoring.
|
25
|
*
|
26
|
* @author a.mueller
|
27
|
* @date 29.09.2011
|
28
|
*
|
29
|
*/
|
30
|
public class MonitoredGenericApplicationContext extends GenericApplicationContext{
|
31
|
@SuppressWarnings("unused")
|
32
|
private static final Logger logger = Logger.getLogger(CdmApplicationController.class);
|
33
|
|
34
|
final int countInvokeBeanFactoryPostProcessors = 10;
|
35
|
final int countFinishBeanFactoryInitialization = 90;
|
36
|
private final int countTasks = countInvokeBeanFactoryPostProcessors + countFinishBeanFactoryInitialization;
|
37
|
private IProgressMonitor currentMonitor;
|
38
|
|
39
|
|
40
|
|
41
|
/**
|
42
|
* Constructor.
|
43
|
* @param progressMonitor
|
44
|
*/
|
45
|
public MonitoredGenericApplicationContext() {
|
46
|
// MonitoredListableBeanFactory beanFactory =
|
47
|
super(new MonitoredListableBeanFactory());
|
48
|
//taken from empty constructor of GenericApplicationContext
|
49
|
((MonitoredListableBeanFactory)getBeanFactory()).setSerializationId(getId());
|
50
|
((MonitoredListableBeanFactory)getBeanFactory()).setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
|
51
|
((MonitoredListableBeanFactory)getBeanFactory()).setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
|
52
|
}
|
53
|
|
54
|
|
55
|
public int countTasks(){
|
56
|
return countTasks;
|
57
|
}
|
58
|
|
59
|
@Override
|
60
|
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory){
|
61
|
String task = "Invoke bean factory post processors";
|
62
|
checkMonitorCancelled(currentMonitor);
|
63
|
currentMonitor.subTask(task);
|
64
|
super.invokeBeanFactoryPostProcessors(beanFactory);
|
65
|
currentMonitor.worked(countInvokeBeanFactoryPostProcessors);
|
66
|
checkMonitorCancelled(currentMonitor);
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory){
|
71
|
checkMonitorCancelled(currentMonitor);
|
72
|
String task = "Finish bean factory initialization";
|
73
|
currentMonitor.subTask(task);
|
74
|
IProgressMonitor subMonitor = new SubProgressMonitor(currentMonitor, countFinishBeanFactoryInitialization);
|
75
|
getMyBeanFactory().setCurrentMonitor(subMonitor);
|
76
|
super.finishBeanFactoryInitialization(beanFactory);
|
77
|
checkMonitorCancelled(currentMonitor);
|
78
|
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* @param progressMonitor the progressMonitor to set
|
83
|
*/
|
84
|
public void setCurrentMonitor(IProgressMonitor monitor) {
|
85
|
this.currentMonitor = monitor;
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
*
|
90
|
*/
|
91
|
public IProgressMonitor getCurrentMonitor() {
|
92
|
return currentMonitor;
|
93
|
}
|
94
|
|
95
|
public void refresh(IProgressMonitor monitor) throws BeansException, IllegalStateException {
|
96
|
checkMonitorCancelled(monitor);
|
97
|
String message = "Refresh application context. This might take a while ...";
|
98
|
currentMonitor = monitor;
|
99
|
beginTask(message, countTasks);
|
100
|
super.refresh();
|
101
|
taskDone();
|
102
|
checkMonitorCancelled(monitor);
|
103
|
}
|
104
|
|
105
|
|
106
|
/**
|
107
|
*
|
108
|
*/
|
109
|
private void taskDone() {
|
110
|
if (currentMonitor != null){
|
111
|
currentMonitor.done();
|
112
|
}
|
113
|
}
|
114
|
|
115
|
|
116
|
/**
|
117
|
* @param monitor
|
118
|
* @param message
|
119
|
*/
|
120
|
private void beginTask(String message, int countTasks) {
|
121
|
if (currentMonitor != null){
|
122
|
currentMonitor.beginTask(message, countTasks);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
|
127
|
private MonitoredListableBeanFactory getMyBeanFactory(){
|
128
|
return (MonitoredListableBeanFactory)getBeanFactory();
|
129
|
}
|
130
|
|
131
|
|
132
|
private void checkMonitorCancelled(IProgressMonitor monitor) {
|
133
|
if (monitor != null && monitor.isCanceled()){
|
134
|
throw new CancellationException();
|
135
|
}
|
136
|
}
|
137
|
}
|