KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > LifecycleBean


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory;
18
19 import org.springframework.beans.BeansException;
20 import org.springframework.beans.factory.config.BeanPostProcessor;
21
22 /**
23  * Simple test of BeanFactory initialization and lifecycle callbacks.
24  *
25  * @author Rod Johnson
26  * @author Colin Sampaleanu
27  * @since 12.03.2003
28  */

29 public class LifecycleBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
30
31     protected String JavaDoc beanName;
32     
33     protected boolean initMethodDeclared = false;
34
35     protected BeanFactory owningFactory;
36
37     protected boolean postProcessedBeforeInit;
38
39     protected boolean inited;
40     protected boolean initedViaDeclaredInitMethod;
41
42     protected boolean postProcessedAfterInit;
43
44     protected boolean destroyed;
45     
46     public boolean isInitMethodDeclared() {
47         return initMethodDeclared;
48     }
49     public void setInitMethodDeclared(boolean initMethodDeclared) {
50         this.initMethodDeclared = initMethodDeclared;
51     }
52
53     public void setBeanName(String JavaDoc name) {
54         this.beanName = name;
55     }
56     public String JavaDoc getBeanName() {
57         return beanName;
58     }
59
60     public void setBeanFactory(BeanFactory beanFactory) {
61         this.owningFactory = beanFactory;
62     }
63
64     public void postProcessBeforeInit() {
65         if (this.inited || this.initedViaDeclaredInitMethod) {
66             throw new RuntimeException JavaDoc("Factory called postProcessBeforeInit after afterPropertiesSet");
67         }
68         if (this.postProcessedBeforeInit) {
69             throw new RuntimeException JavaDoc("Factory called postProcessBeforeInit twice");
70         }
71         this.postProcessedBeforeInit = true;
72     }
73
74     public void afterPropertiesSet() {
75         if (this.owningFactory == null) {
76             throw new RuntimeException JavaDoc("Factory didn't call setBeanFactory before afterPropertiesSet on lifecycle bean");
77         }
78         if (!this.postProcessedBeforeInit) {
79             throw new RuntimeException JavaDoc("Factory didn't call postProcessBeforeInit before afterPropertiesSet on lifecycle bean");
80         }
81         if (this.initedViaDeclaredInitMethod) {
82             throw new RuntimeException JavaDoc("Factory initialized via declared init method before initializing via afterPropertiesSet");
83         }
84         if (this.inited) {
85             throw new RuntimeException JavaDoc("Factory called afterPropertiesSet twice");
86         }
87         this.inited = true;
88     }
89     
90     public void declaredInitMethod() {
91         
92         if (!this.inited) {
93             throw new RuntimeException JavaDoc("Factory didn't call afterPropertiesSet before declared init method");
94         }
95         
96         if (this.initedViaDeclaredInitMethod) {
97             throw new RuntimeException JavaDoc("Factory called declared init method twice");
98         }
99         this.initedViaDeclaredInitMethod = true;
100     }
101
102     public void postProcessAfterInit() {
103         if (!this.inited) {
104             throw new RuntimeException JavaDoc("Factory called postProcessAfterInit before afterPropertiesSet");
105         }
106         if (this.initMethodDeclared && !this.initedViaDeclaredInitMethod) {
107             throw new RuntimeException JavaDoc("Factory called postProcessAfterInit before calling declared init method");
108         }
109         if (this.postProcessedAfterInit) {
110             throw new RuntimeException JavaDoc("Factory called postProcessAfterInit twice");
111         }
112         this.postProcessedAfterInit = true;
113     }
114
115     /**
116      * Dummy business method that will fail unless the factory
117      * managed the bean's lifecycle correctly
118      */

119     public void businessMethod() {
120         if (!this.inited || (this.initMethodDeclared && !this.initedViaDeclaredInitMethod)
121                 || !this.postProcessedAfterInit) {
122             throw new RuntimeException JavaDoc("Factory didn't initialize lifecycle object correctly");
123         }
124     }
125
126     public void destroy() {
127         if (this.destroyed) {
128             throw new IllegalStateException JavaDoc("Already destroyed");
129         }
130         this.destroyed = true;
131     }
132
133     public boolean isDestroyed() {
134         return destroyed;
135     }
136
137
138     public static class PostProcessor implements BeanPostProcessor {
139
140         public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc name) throws BeansException {
141             if (bean instanceof LifecycleBean) {
142                 ((LifecycleBean) bean).postProcessBeforeInit();
143             }
144             return bean;
145         }
146
147         public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc name) throws BeansException {
148             if (bean instanceof LifecycleBean) {
149                 ((LifecycleBean) bean).postProcessAfterInit();
150             }
151             return bean;
152         }
153     }
154 }
155
Popular Tags