6ad7ad4189f53e5b94c3da17450f2223fd1bfc20
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / RemotingMonitoredGenericApplicationContext.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.concurrent.CancellationException;
13
14 import org.apache.log4j.Logger;
15 import org.springframework.beans.BeansException;
16 import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
17 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
18 import org.springframework.context.support.GenericApplicationContext;
19 import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
20
21 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
22 import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
23
24 /**
25 * {@link GenericApplicationContext Generic application context} which allows progress monitoring.
26 * @author a.mueller
27 * @date 29.09.2011
28 *
29 */
30 public class RemotingMonitoredGenericApplicationContext 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 RemotingMonitoredGenericApplicationContext() {
46 // MonitoredListableBeanFactory beanFactory =
47 super(new RemotingMonitoredListableBeanFactory());
48 //taken from empty constructor of GenericApplicationContext
49 ((RemotingMonitoredListableBeanFactory)getBeanFactory()).setSerializationId(getId());
50 ((RemotingMonitoredListableBeanFactory)getBeanFactory()).setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
51 ((RemotingMonitoredListableBeanFactory)getBeanFactory()).setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
52 }
53
54
55 public int countTasks(){
56 return countTasks;
57 }
58
59 protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory){
60 String task = "Invoke bean factory post processors";
61 checkMonitorCancelled(currentMonitor);
62 currentMonitor.subTask(task);
63 super.invokeBeanFactoryPostProcessors(beanFactory);
64 currentMonitor.worked(countInvokeBeanFactoryPostProcessors);
65 checkMonitorCancelled(currentMonitor);
66 }
67
68 protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory){
69 checkMonitorCancelled(currentMonitor);
70 String task = "Finish bean factory initialization";
71 currentMonitor.subTask(task);
72 IProgressMonitor subMonitor = new SubProgressMonitor(currentMonitor, countFinishBeanFactoryInitialization);
73 getMyBeanFactory().setCurrentMonitor(subMonitor);
74 super.finishBeanFactoryInitialization(beanFactory);
75 checkMonitorCancelled(currentMonitor);
76
77 }
78
79 /**
80 * @param progressMonitor the progressMonitor to set
81 */
82 public void setCurrentMonitor(IProgressMonitor monitor) {
83 this.currentMonitor = monitor;
84 }
85
86 /**
87 *
88 */
89 public IProgressMonitor getCurrentMonitor() {
90 return currentMonitor;
91 }
92
93
94 /* (non-Javadoc)
95 * @see org.springframework.context.support.AbstractApplicationContext#refresh()
96 */
97 public void refresh(IProgressMonitor monitor) throws BeansException, IllegalStateException {
98 checkMonitorCancelled(monitor);
99 String message = "Refresh application context. This might take a while ...";
100 currentMonitor = monitor;
101 beginTask(message, countTasks);
102 super.refresh();
103 taskDone();
104 checkMonitorCancelled(monitor);
105 }
106
107
108 /**
109 *
110 */
111 private void taskDone() {
112 if (currentMonitor != null){
113 currentMonitor.done();
114 }
115 }
116
117
118 /**
119 * @param monitor
120 * @param message
121 */
122 private void beginTask(String message, int countTasks) {
123 if (currentMonitor != null){
124 currentMonitor.beginTask(message, countTasks);
125 }
126 }
127
128
129 private RemotingMonitoredListableBeanFactory getMyBeanFactory(){
130 return (RemotingMonitoredListableBeanFactory)getBeanFactory();
131 }
132
133
134 private void checkMonitorCancelled(IProgressMonitor monitor) {
135 if (monitor != null && monitor.isCanceled()){
136 throw new CancellationException();
137 }
138 }
139 }