4e03ea4d8eb34722cecbeb385fa624023cc264ab
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / RemotingMonitoredListableBeanFactory.java
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 RemotingMonitoredListableBeanFactory extends DefaultListableBeanFactory {
31 @SuppressWarnings("unused")
32 private static final Logger logger = Logger.getLogger(RemotingMonitoredListableBeanFactory.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 Set<String> alreadyMonitoredBeans = new HashSet<String>();
39
40 public RemotingMonitoredListableBeanFactory(){
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 public void preInstantiateSingletons() throws BeansException {
59 isInitializingBeans = true;
60 checkMonitorCancelled(currentMonitor);
61 int countBeans = 0;
62 for (String beanName : getBeanDefinitionNames()) {
63 RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
64 if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit() && beansToMonitor.contains(beanName) ){
65 countBeans++;
66 }
67 }
68 String message = "preinstantiate singletons";
69 currentMonitor.beginTask(message, countBeans);
70 super.preInstantiateSingletons();
71 isInitializingBeans = false;
72 currentMonitor.done();
73 }
74
75 // protected <T> T doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly){
76 // boolean doMonitor = isInitializingBeans && !monitoredBeanNames.contains(name);
77 // if (doMonitor){
78 // String message = "Handle bean '%s'";
79 // message = String.format(message, name);
80 // currentMonitor.subTask(message);
81 // monitoredBeanNames.add(name);
82 // }
83 // T result = super.doGetBean(name, requiredType, args, typeCheckOnly);
84 // if (doMonitor){
85 // currentMonitor.worked(1);
86 // }
87 // return result;
88 // }
89
90 protected Object createBean(final String name, final RootBeanDefinition mbd, final Object[] args){
91 boolean doMonitor = isInitializingBeans && beansToMonitor.contains(name) && !alreadyMonitoredBeans.contains(name);
92 checkMonitorCancelled(currentMonitor);
93 if (doMonitor){
94 String message;
95 if (name.equals("sessionFactory")){
96 message = "Initializing persistence context ...";
97 }else if(name.equals("persistentTermInitializer")){
98 message = "Loading terms ...";
99 }else{
100 message = "Handling '%s'";
101 message = String.format(message, name);
102 }
103 currentMonitor.subTask(message);
104 alreadyMonitoredBeans.add(name);
105 }
106 Object result = super.createBean(name, mbd, args);
107 if (doMonitor){
108 checkMonitorCancelled(currentMonitor);
109 currentMonitor.worked(1);
110 }
111 return result;
112 }
113
114
115 /**
116 * @param mainMonitor the mainMonitor to set
117 */
118 public void setCurrentMonitor(IProgressMonitor monitor) {
119 this.currentMonitor = monitor;
120 }
121
122 private void checkMonitorCancelled(IProgressMonitor monitor) {
123 if (monitor != null && monitor.isCanceled()){
124 throw new CancellationException();
125 }
126 }
127
128 }