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 javax.naming.NamingException;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.springframework.beans.BeansException;
  21. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  22. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
  23. import org.springframework.core.io.ClassPathResource;
  24. import org.springframework.jndi.JndiTemplate;
  25. import org.springframework.util.StringUtils;
  26. /**
  27. * BeanFactoryLocator implementation that creates the BeanFactory
  28. * from file locations specified as JNDI environment variable.
  29. *
  30. * <p>This default implementation creates a DefaultListableBeanFactory,
  31. * populated via an XmlBeanDefinitionReader. Subclasses may override
  32. * createFactory for custom instantiation.
  33. *
  34. * @author Rod Johnson
  35. * @author Colin Sampaleanu
  36. * @version $Revision: 1.3 $
  37. * @see org.springframework.beans.factory.support.DefaultListableBeanFactory
  38. */
  39. public class JndiBeanFactoryLocator implements BeanFactoryLocator {
  40. /**
  41. * Any number of these characters are considered delimiters
  42. * between multiple bean factory paths in a single-String value.
  43. */
  44. public static final String BEAN_FACTORY_PATH_DELIMITERS = ",; ";
  45. protected Log logger = LogFactory.getLog(getClass());
  46. /**
  47. * Load/use a bean factory, as specified by a factoryKey which is a JNDI address,
  48. * of the form <code>java:comp/env/ejb/BeanFactoryPath</code>.
  49. */
  50. public BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException {
  51. String beanFactoryPath = null;
  52. try {
  53. beanFactoryPath = (String) (new JndiTemplate()).lookup(factoryKey);
  54. logger.info("BeanFactoryPath from JNDI is [" + beanFactoryPath + "]");
  55. String[] paths = StringUtils.tokenizeToStringArray(beanFactoryPath,
  56. BEAN_FACTORY_PATH_DELIMITERS, true, true);
  57. return createBeanFactory(paths);
  58. }
  59. catch (NamingException ex) {
  60. throw new BootstrapException("Define an environment variable 'ejb/BeanFactoryPath' containing " +
  61. "the class path locations of XML bean definition files", ex);
  62. }
  63. }
  64. /**
  65. * Actually create the BeanFactory, given an array of classpath resource strings
  66. * which should be combined. This is split out as a separate method so that subclasses
  67. * can override the actual type uses (to be an ApplicationContext, for example).
  68. * @param resources an array of Strings representing classpath resource names
  69. * @return the created BeanFactory, wrapped in a BeanFactoryReference
  70. */
  71. protected BeanFactoryReference createBeanFactory(String[] resources) throws BeansException {
  72. DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
  73. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
  74. for (int i = 0; i < resources.length; ++i) {
  75. reader.loadBeanDefinitions(new ClassPathResource(resources[i]));
  76. }
  77. factory.preInstantiateSingletons();
  78. return new DefaultBeanFactoryReference(factory);
  79. }
  80. }