2 * Copyright (C) 2009 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.
9 package eu
.etaxonomy
.cdm
.api
.application
;
11 import java
.util
.Arrays
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
15 import java
.util
.concurrent
.CancellationException
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.beans
.BeansException
;
19 import org
.springframework
.beans
.factory
.support
.DefaultListableBeanFactory
;
20 import org
.springframework
.beans
.factory
.support
.RootBeanDefinition
;
22 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
29 public class MonitoredListableBeanFactory
extends DefaultListableBeanFactory
{
30 @SuppressWarnings("unused")
31 private static final Logger logger
= Logger
.getLogger(MonitoredListableBeanFactory
.class);
33 private boolean isInitializingBeans
= false;
34 private IProgressMonitor currentMonitor
;
36 private static List
<String
> beansToMonitor
= Arrays
.asList("sessionFactory","defaultBeanInitializer","persistentTermInitializer");
37 private final Set
<String
> alreadyMonitoredBeans
= new HashSet
<>();
39 public MonitoredListableBeanFactory(){
43 // protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
44 // if (registeredBeanNames.contains(beanName)){
45 // return super.getMergedLocalBeanDefinition(beanName);
48 //// String message = "Handle bean '%s'";
49 //// message = String.format(message, beanName);
50 //// currentMonitor.subTask(message);
51 // RootBeanDefinition result = super.getMergedLocalBeanDefinition(beanName);
52 //// currentMonitor.worked(1);
53 //// registeredBeanNames.add(beanName);
58 public void preInstantiateSingletons() throws BeansException
{
59 isInitializingBeans
= true;
60 checkMonitorCancelled(currentMonitor
);
62 for (String beanName
: getBeanDefinitionNames()) {
63 RootBeanDefinition bd
= getMergedLocalBeanDefinition(beanName
);
64 if (!bd
.isAbstract() && bd
.isSingleton() && !bd
.isLazyInit() && beansToMonitor
.contains(beanName
) ){
68 String message
= "preinstantiate singletons";
69 currentMonitor
.beginTask(message
, countBeans
);
70 super.preInstantiateSingletons();
71 isInitializingBeans
= false;
72 currentMonitor
.done();
75 // protected <T> T doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly){
76 // boolean doMonitor = isInitializingBeans && !monitoredBeanNames.contains(name);
78 // String message = "Handle bean '%s'";
79 // message = String.format(message, name);
80 // currentMonitor.subTask(message);
81 // monitoredBeanNames.add(name);
83 // T result = super.doGetBean(name, requiredType, args, typeCheckOnly);
85 // currentMonitor.worked(1);
91 protected Object
createBean(final String name
, final RootBeanDefinition mbd
, final Object
[] args
){
92 boolean doMonitor
= isInitializingBeans
&& beansToMonitor
.contains(name
) && !alreadyMonitoredBeans
.contains(name
);
93 checkMonitorCancelled(currentMonitor
);
96 if (name
.equals("sessionFactory")){
97 message
= "Initializing persistence context ...";
98 }else if(name
.equals("persistentTermInitializer")){
99 message
= "Loading terms ...";
101 message
= "Handling '%s'";
102 message
= String
.format(message
, name
);
104 currentMonitor
.subTask(message
);
105 alreadyMonitoredBeans
.add(name
);
107 Object result
= super.createBean(name
, mbd
, args
);
109 checkMonitorCancelled(currentMonitor
);
110 currentMonitor
.worked(1);
117 * @param mainMonitor the mainMonitor to set
119 public void setCurrentMonitor(IProgressMonitor monitor
) {
120 this.currentMonitor
= monitor
;
123 private void checkMonitorCancelled(IProgressMonitor monitor
) {
124 if (monitor
!= null && monitor
.isCanceled()){
125 throw new CancellationException();