1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.api.application;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.concurrent.CancellationException;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.beans.BeansException;
|
20
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
21
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
22
|
|
23
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
24
|
|
25
|
/**
|
26
|
* @author a.mueller
|
27
|
* @date 29.09.2011
|
28
|
*
|
29
|
*/
|
30
|
public class MonitoredListableBeanFactory extends DefaultListableBeanFactory {
|
31
|
@SuppressWarnings("unused")
|
32
|
private static final Logger logger = Logger.getLogger(MonitoredListableBeanFactory.class);
|
33
|
|
34
|
private boolean isInitializingBeans = false;
|
35
|
private IProgressMonitor currentMonitor;
|
36
|
|
37
|
private static List<String> beansToMonitor = Arrays.asList("sessionFactory","defaultBeanInitializer","persistentTermInitializer");
|
38
|
private final Set<String> alreadyMonitoredBeans = new HashSet<String>();
|
39
|
|
40
|
public MonitoredListableBeanFactory(){
|
41
|
}
|
42
|
|
43
|
// @Override
|
44
|
// protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
|
45
|
// if (registeredBeanNames.contains(beanName)){
|
46
|
// return super.getMergedLocalBeanDefinition(beanName);
|
47
|
//
|
48
|
// }
|
49
|
//// String message = "Handle bean '%s'";
|
50
|
//// message = String.format(message, beanName);
|
51
|
//// currentMonitor.subTask(message);
|
52
|
// RootBeanDefinition result = super.getMergedLocalBeanDefinition(beanName);
|
53
|
//// currentMonitor.worked(1);
|
54
|
//// registeredBeanNames.add(beanName);
|
55
|
// return result;
|
56
|
// }
|
57
|
|
58
|
@Override
|
59
|
public void preInstantiateSingletons() throws BeansException {
|
60
|
isInitializingBeans = true;
|
61
|
checkMonitorCancelled(currentMonitor);
|
62
|
int countBeans = 0;
|
63
|
for (String beanName : getBeanDefinitionNames()) {
|
64
|
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
|
65
|
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit() && beansToMonitor.contains(beanName) ){
|
66
|
countBeans++;
|
67
|
}
|
68
|
}
|
69
|
String message = "preinstantiate singletons";
|
70
|
currentMonitor.beginTask(message, countBeans);
|
71
|
super.preInstantiateSingletons();
|
72
|
isInitializingBeans = false;
|
73
|
currentMonitor.done();
|
74
|
}
|
75
|
|
76
|
// protected <T> T doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly){
|
77
|
// boolean doMonitor = isInitializingBeans && !monitoredBeanNames.contains(name);
|
78
|
// if (doMonitor){
|
79
|
// String message = "Handle bean '%s'";
|
80
|
// message = String.format(message, name);
|
81
|
// currentMonitor.subTask(message);
|
82
|
// monitoredBeanNames.add(name);
|
83
|
// }
|
84
|
// T result = super.doGetBean(name, requiredType, args, typeCheckOnly);
|
85
|
// if (doMonitor){
|
86
|
// currentMonitor.worked(1);
|
87
|
// }
|
88
|
// return result;
|
89
|
// }
|
90
|
|
91
|
@Override
|
92
|
protected Object createBean(final String name, final RootBeanDefinition mbd, final Object[] args){
|
93
|
boolean doMonitor = isInitializingBeans && beansToMonitor.contains(name) && !alreadyMonitoredBeans.contains(name);
|
94
|
checkMonitorCancelled(currentMonitor);
|
95
|
if (doMonitor){
|
96
|
String message;
|
97
|
if (name.equals("sessionFactory")){
|
98
|
message = "Initializing persistence context ...";
|
99
|
}else if(name.equals("persistentTermInitializer")){
|
100
|
message = "Loading terms ...";
|
101
|
}else{
|
102
|
message = "Handling '%s'";
|
103
|
message = String.format(message, name);
|
104
|
}
|
105
|
currentMonitor.subTask(message);
|
106
|
alreadyMonitoredBeans.add(name);
|
107
|
}
|
108
|
Object result = super.createBean(name, mbd, args);
|
109
|
if (doMonitor){
|
110
|
checkMonitorCancelled(currentMonitor);
|
111
|
currentMonitor.worked(1);
|
112
|
}
|
113
|
return result;
|
114
|
}
|
115
|
|
116
|
|
117
|
/**
|
118
|
* @param mainMonitor the mainMonitor to set
|
119
|
*/
|
120
|
public void setCurrentMonitor(IProgressMonitor monitor) {
|
121
|
this.currentMonitor = monitor;
|
122
|
}
|
123
|
|
124
|
private void checkMonitorCancelled(IProgressMonitor monitor) {
|
125
|
if (monitor != null && monitor.isCanceled()){
|
126
|
throw new CancellationException();
|
127
|
}
|
128
|
}
|
129
|
|
130
|
}
|