01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/component/tags/sakai_2-4-1/component-api/component/src/java/org/springframework/context/support/SakaiApplicationContext.java $
03: * $Id: SakaiApplicationContext.java 22830 2007-03-17 22:47:40Z ggolden@umich.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006, 2007 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.springframework.context.support;
21:
22: import org.springframework.beans.BeansException;
23: import org.springframework.beans.factory.FactoryBean;
24: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
25: import org.springframework.beans.factory.support.RootBeanDefinition;
26:
27: /**
28: * Spring extension that logs failure details when singleton preinstantion fails.
29: *
30: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
31: *
32: */
33: public class SakaiApplicationContext extends GenericApplicationContext {
34:
35: protected DefaultListableBeanFactory createBeanFactory() {
36: return new DefaultListableBeanFactory(
37: getInternalParentBeanFactory()) {
38: public void preInstantiateSingletons()
39: throws BeansException {
40: if (logger.isInfoEnabled()) {
41: logger
42: .info("Pre-instantiating singletons in factory ["
43: + this + "]");
44: }
45: String currentBeanName = null;
46: try {
47: for (int i = 0; i < getBeanDefinitionNames().length; i++) {
48: String beanName = getBeanDefinitionNames()[i];
49: currentBeanName = beanName;
50: if (!containsSingleton(beanName)
51: && containsBeanDefinition(beanName)) {
52: RootBeanDefinition bd = getMergedBeanDefinition(
53: beanName, false);
54: if (!bd.isAbstract() && bd.isSingleton()
55: && !bd.isLazyInit()) {
56: if (bd.hasBeanClass()
57: && FactoryBean.class
58: .isAssignableFrom(bd
59: .getBeanClass())) {
60: FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX
61: + beanName);
62: if (factory.isSingleton()) {
63: getBean(beanName);
64: }
65: } else {
66: getBean(beanName);
67: }
68: }
69: }
70: }
71: } catch (BeansException ex) {
72: // Destroy already created singletons to avoid dangling resources.
73: try {
74: logger
75: .error("Failed to preinstantiate "
76: + currentBeanName
77: + " as a singleton. Destroying all spring beans.");
78: ex.printStackTrace();
79: destroySingletons();
80: } catch (Throwable ex2) {
81: logger
82: .error(
83: "Pre-instantiating singletons failed, "
84: + "and couldn't destroy already created singletons",
85: ex2);
86: }
87: throw ex;
88: }
89: }
90: };
91: }
92: }
|