001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.access;
018:
019: import javax.naming.NamingException;
020:
021: import org.springframework.beans.BeansException;
022: import org.springframework.beans.factory.access.BeanFactoryLocator;
023: import org.springframework.beans.factory.access.BeanFactoryReference;
024: import org.springframework.beans.factory.access.BootstrapException;
025: import org.springframework.context.ApplicationContext;
026: import org.springframework.context.support.ClassPathXmlApplicationContext;
027: import org.springframework.jndi.JndiLocatorSupport;
028: import org.springframework.util.StringUtils;
029:
030: /**
031: * BeanFactoryLocator implementation that creates the BeanFactory from one or
032: * more classpath locations specified in a JNDI environment variable.
033: *
034: * <p>This default implementation creates a
035: * {@link org.springframework.context.support.ClassPathXmlApplicationContext}.
036: * Subclasses may override {@link #createBeanFactory} for custom instantiation.
037: *
038: * @author Colin Sampaleanu
039: * @author Juergen Hoeller
040: * @see #createBeanFactory
041: */
042: public class ContextJndiBeanFactoryLocator extends JndiLocatorSupport
043: implements BeanFactoryLocator {
044:
045: /**
046: * Any number of these characters are considered delimiters between
047: * multiple bean factory config paths in a single String value.
048: */
049: public static final String BEAN_FACTORY_PATH_DELIMITERS = ",; \t\n";
050:
051: /**
052: * Load/use a bean factory, as specified by a factory key which is a JNDI
053: * address, of the form <code>java:comp/env/ejb/BeanFactoryPath</code>. The
054: * contents of this JNDI location must be a string containing one or more
055: * classpath resource names (separated by any of the delimiters '<code>,; \t\n</code>'
056: * if there is more than one. The resulting BeanFactory (or ApplicationContext)
057: * will be created from the combined resources.
058: * @see #createBeanFactory
059: */
060: public BeanFactoryReference useBeanFactory(String factoryKey)
061: throws BeansException {
062: try {
063: String beanFactoryPath = (String) lookup(factoryKey,
064: String.class);
065: if (logger.isTraceEnabled()) {
066: logger
067: .trace("Bean factory path from JNDI environment variable ["
068: + factoryKey
069: + "] is: "
070: + beanFactoryPath);
071: }
072: String[] paths = StringUtils.tokenizeToStringArray(
073: beanFactoryPath, BEAN_FACTORY_PATH_DELIMITERS);
074: return createBeanFactory(paths);
075: } catch (NamingException ex) {
076: throw new BootstrapException(
077: "Define an environment variable ["
078: + factoryKey
079: + "] containing "
080: + "the class path locations of XML bean definition files",
081: ex);
082: }
083: }
084:
085: /**
086: * Create the BeanFactory instance, given an array of class path resource Strings
087: * which should be combined. This is split out as a separate method so that
088: * subclasses can override the actual BeanFactory implementation class.
089: * <p>Delegates to <code>createApplicationContext</code> by default,
090: * wrapping the result in a ContextBeanFactoryReference.
091: * @param resources an array of Strings representing classpath resource names
092: * @return the created BeanFactory, wrapped in a BeanFactoryReference
093: * (for example, a ContextBeanFactoryReference wrapping an ApplicationContext)
094: * @throws BeansException if factory creation failed
095: * @see #createApplicationContext
096: * @see ContextBeanFactoryReference
097: */
098: protected BeanFactoryReference createBeanFactory(String[] resources)
099: throws BeansException {
100: ApplicationContext ctx = createApplicationContext(resources);
101: return new ContextBeanFactoryReference(ctx);
102: }
103:
104: /**
105: * Create the ApplicationContext instance, given an array of class path resource
106: * Strings which should be combined
107: * @param resources an array of Strings representing classpath resource names
108: * @return the created ApplicationContext
109: * @throws BeansException if context creation failed
110: */
111: protected ApplicationContext createApplicationContext(
112: String[] resources) throws BeansException {
113: return new ClassPathXmlApplicationContext(resources);
114: }
115:
116: }
|