KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > access > ContextJndiBeanFactoryLocator


1 /*
2  * Copyright 2002-2007 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.context.access;
18
19 import javax.naming.NamingException JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.access.BeanFactoryLocator;
23 import org.springframework.beans.factory.access.BeanFactoryReference;
24 import org.springframework.beans.factory.access.BootstrapException;
25 import org.springframework.context.ApplicationContext;
26 import org.springframework.context.support.ClassPathXmlApplicationContext;
27 import org.springframework.jndi.JndiLocatorSupport;
28 import org.springframework.util.StringUtils;
29
30 /**
31  * BeanFactoryLocator implementation that creates the BeanFactory from one or
32  * more classpath locations specified in a JNDI environment variable.
33  *
34  * <p>This default implementation creates a
35  * {@link org.springframework.context.support.ClassPathXmlApplicationContext}.
36  * Subclasses may override {@link #createBeanFactory} for custom instantiation.
37  *
38  * @author Colin Sampaleanu
39  * @author Juergen Hoeller
40  * @see #createBeanFactory
41  */

42 public class ContextJndiBeanFactoryLocator extends JndiLocatorSupport implements BeanFactoryLocator {
43
44     /**
45      * Any number of these characters are considered delimiters between
46      * multiple bean factory config paths in a single String value.
47      */

48     public static final String JavaDoc BEAN_FACTORY_PATH_DELIMITERS = ",; \t\n";
49
50
51     /**
52      * Load/use a bean factory, as specified by a factory key which is a JNDI
53      * address, of the form <code>java:comp/env/ejb/BeanFactoryPath</code>. The
54      * contents of this JNDI location must be a string containing one or more
55      * classpath resource names (separated by any of the delimiters '<code>,; \t\n</code>'
56      * if there is more than one. The resulting BeanFactory (or ApplicationContext)
57      * will be created from the combined resources.
58      * @see #createBeanFactory
59      */

60     public BeanFactoryReference useBeanFactory(String JavaDoc factoryKey) throws BeansException {
61         try {
62             String JavaDoc beanFactoryPath = (String JavaDoc) lookup(factoryKey, String JavaDoc.class);
63             if (logger.isTraceEnabled()) {
64                 logger.trace("Bean factory path from JNDI environment variable [" + factoryKey +
65                         "] is: " + beanFactoryPath);
66             }
67             String JavaDoc[] paths = StringUtils.tokenizeToStringArray(beanFactoryPath, BEAN_FACTORY_PATH_DELIMITERS);
68             return createBeanFactory(paths);
69         }
70         catch (NamingException JavaDoc ex) {
71             throw new BootstrapException("Define an environment variable [" + factoryKey + "] containing " +
72                     "the class path locations of XML bean definition files", ex);
73         }
74     }
75
76     /**
77      * Create the BeanFactory instance, given an array of class path resource Strings
78      * which should be combined. This is split out as a separate method so that
79      * subclasses can override the actual BeanFactory implementation class.
80      * <p>Delegates to <code>createApplicationContext</code> by default,
81      * wrapping the result in a ContextBeanFactoryReference.
82      * @param resources an array of Strings representing classpath resource names
83      * @return the created BeanFactory, wrapped in a BeanFactoryReference
84      * (for example, a ContextBeanFactoryReference wrapping an ApplicationContext)
85      * @throws BeansException if factory creation failed
86      * @see #createApplicationContext
87      * @see ContextBeanFactoryReference
88      */

89     protected BeanFactoryReference createBeanFactory(String JavaDoc[] resources) throws BeansException {
90         ApplicationContext ctx = createApplicationContext(resources);
91         return new ContextBeanFactoryReference(ctx);
92     }
93
94     /**
95      * Create the ApplicationContext instance, given an array of class path resource
96      * Strings which should be combined
97      * @param resources an array of Strings representing classpath resource names
98      * @return the created ApplicationContext
99      * @throws BeansException if context creation failed
100      */

101     protected ApplicationContext createApplicationContext(String JavaDoc[] resources) throws BeansException {
102         return new ClassPathXmlApplicationContext(resources);
103     }
104
105 }
106
Popular Tags