1. /*
  2. * Copyright 2002-2004 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. package org.springframework.beans.factory.access;
  17. import org.springframework.beans.BeansException;
  18. import org.springframework.beans.factory.BeanFactory;
  19. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  20. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  21. import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
  22. /**
  23. * One singleton to rule them all. Reads System properties, which
  24. * must contain the definition of a bootstrap bean factory using
  25. * the Properties syntax supported by PropertiesBeanDefinitionReader.
  26. *
  27. * <oo>The name of the bootstrap factory must be "bootstrapBeanFactory".
  28. *
  29. * Thus a typical definition might be:
  30. * <code>
  31. * bootstrapBeanFactory.class=com.mycompany.MyBeanFactory
  32. * </code>
  33. *
  34. * <p>Use as follows:
  35. * <code>
  36. * BeanFactory bf = BeanFactoryBootstrap.getInstance().getBeanFactory();
  37. * </code>
  38. *
  39. * @author Rod Johnson
  40. * @since December 2, 2002
  41. * @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
  42. * @version $Id: BeanFactoryBootstrap.java,v 1.3 2004/03/18 02:46:14 trisberg Exp $
  43. */
  44. public class BeanFactoryBootstrap {
  45. public static final String BEAN_FACTORY_BEAN_NAME = "bootstrapBeanFactory";
  46. private static BeanFactoryBootstrap instance;
  47. private static BeansException startupException;
  48. private static void initializeSingleton() {
  49. try {
  50. instance = new BeanFactoryBootstrap();
  51. }
  52. catch (BeansException ex) {
  53. startupException = ex;
  54. }
  55. }
  56. // Do initialization when this class is loaded to avoid
  57. // potential concurrency issues or the need to synchronize later
  58. static {
  59. initializeSingleton();
  60. }
  61. /**
  62. * Return the singleton instance of the bootstrap factory
  63. * @return BeanFactoryBootstrap
  64. * @throws org.springframework.beans.BeansException
  65. */
  66. public static BeanFactoryBootstrap getInstance() throws BeansException {
  67. if (startupException != null)
  68. throw startupException;
  69. // Really an assertion
  70. if (instance == null)
  71. throw new BootstrapException("Anomaly: instance and exception null", null);
  72. return instance;
  73. }
  74. /**
  75. * <b>For testing only. Cleans and reinitalizes the instance.
  76. * Do not use in a production application!</b>
  77. */
  78. protected static void reinitialize() {
  79. instance = null;
  80. startupException = null;
  81. initializeSingleton();
  82. }
  83. /** The Singleton instance */
  84. private BeanFactory bootstrapFactory;
  85. /**
  86. * Apply rules to load factory.
  87. */
  88. private BeanFactoryBootstrap() throws BeansException {
  89. DefaultListableBeanFactory startupFactory = new DefaultListableBeanFactory();
  90. PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(startupFactory);
  91. try {
  92. propReader.registerBeanDefinitions(System.getProperties());
  93. this.bootstrapFactory = (BeanFactory) startupFactory.getBean(BEAN_FACTORY_BEAN_NAME);
  94. }
  95. catch (ClassCastException ex) {
  96. throw new BootstrapException("Bootstrap bean factory class does not implement BeanFactory interface", ex);
  97. }
  98. catch (NoSuchBeanDefinitionException ex) {
  99. throw new BootstrapException("No bean named '" + BEAN_FACTORY_BEAN_NAME + "' in system properties: [" + startupFactory + "]", null);
  100. }
  101. catch (BeansException ex) {
  102. throw new BootstrapException("Failed to bootstrap bean factory", ex);
  103. }
  104. }
  105. /**
  106. * Return the BeanFactory managed by the Bootstrap.
  107. */
  108. public BeanFactory getBeanFactory() {
  109. return bootstrapFactory;
  110. }
  111. }