KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > spring > factory > NamedXmlApplicationContext


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.spring.factory;
24
25 import java.io.IOException JavaDoc;
26
27 import org.springframework.beans.BeansException;
28 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
29 import org.springframework.beans.factory.xml.ResourceEntityResolver;
30 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
31 import org.springframework.context.support.ClassPathXmlApplicationContext;
32 import org.springframework.core.io.Resource;
33
34 /**
35  * @author <a HREF="mailto:ales.justin@genera-lynx.com">Ales Justin</a>
36  */

37 public class NamedXmlApplicationContext extends ClassPathXmlApplicationContext
38       implements Nameable
39 {
40
41    private String JavaDoc defaultName;
42    private Resource resource;
43    private NamedXmlBeanDefinitionReader beanDefinitionReader;
44
45    /**
46     * @see org.springframework.beans.factory.xml.XmlBeanFactory
47     */

48    public NamedXmlApplicationContext(String JavaDoc defaultName, Resource resource) throws BeansException
49    {
50       this(defaultName, resource, true);
51    }
52
53    public NamedXmlApplicationContext(String JavaDoc defaultName, Resource resource, boolean refresh) throws BeansException
54    {
55       //loading config from Resource
56
super(new String JavaDoc[]{}, false);
57       this.defaultName = defaultName;
58       this.resource = resource;
59       if (refresh)
60       {
61          refresh();
62       }
63    }
64
65    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException JavaDoc
66    {
67       // Create a new XmlBeanDefinitionReader for the given BeanFactory.
68
beanDefinitionReader = new NamedXmlBeanDefinitionReader(beanFactory);
69
70       // Configure the bean definition reader with this context's
71
// resource loading environment.
72
beanDefinitionReader.setResourceLoader(this);
73       if (getClassLoader() != null)
74       {
75          beanDefinitionReader.setBeanClassLoader(getClassLoader());
76       }
77       beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
78
79       // Allow a subclass to provide custom initialization of the reader,
80
// then proceed with actually loading the bean definitions.
81
initBeanDefinitionReader(beanDefinitionReader);
82       loadBeanDefinitions(beanDefinitionReader);
83    }
84
85    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException JavaDoc
86    {
87       reader.loadBeanDefinitions(resource);
88    }
89
90    public String JavaDoc getName()
91    {
92       String JavaDoc name = beanDefinitionReader.getName() != null ? beanDefinitionReader.getName() : defaultName;
93       if (name == null)
94       {
95          throw new IllegalArgumentException JavaDoc("Bean factory JNDI name must be set!");
96       }
97       return name;
98    }
99
100 }
101
Popular Tags