View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.core.resourceloader;
18  
19  import javax.xml.namespace.QName;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.kuali.rice.core.config.ConfigurationException;
24  import org.springframework.context.ApplicationContext;
25  import org.springframework.context.ConfigurableApplicationContext;
26  import org.springframework.context.support.ClassPathXmlApplicationContext;
27  
28  /**
29   * A simple {@link ResourceLoader} which wraps a Spring {@link ConfigurableApplicationContext}.
30   *
31   * Starts and stops the {@link ConfigurableApplicationContext}.
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class SpringResourceLoader extends BaseResourceLoader {
36  
37  	private static final Logger LOG = Logger.getLogger(SpringResourceLoader.class);
38  
39  	private SpringResourceLoader parentSpringResourceLoader;
40  	
41  	private ApplicationContext parentContext;
42  	private ConfigurableApplicationContext context;
43  
44  	private final String[] fileLocs;
45  
46  	public SpringResourceLoader(QName name, String fileLoc) {
47  	    this(name, new String[] { fileLoc });
48  	}
49  
50  	public SpringResourceLoader(QName name, String[] fileLocs) {
51  		super(name);
52  		this.fileLocs = fileLocs;
53  	}
54  
55  	public Object getService(QName serviceName) {
56  	    	if (!isStarted()) {
57  	    	    return null;
58  	    	}
59  		if (this.getContext().containsBean(serviceName.toString())) {
60  			Object service = this.getContext().getBean(serviceName.toString());
61  			return postProcessService(serviceName, service);
62  		}
63  		return super.getService(serviceName);
64  	}
65  
66  	@Override
67  	public void start() throws Exception {
68  		if(!isStarted()){
69  			LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ","));
70  			if (parentSpringResourceLoader != null && parentContext != null) {
71  				throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined.  Only one can be defined!");
72  			}
73  			if (parentSpringResourceLoader != null) {
74  				parentContext = parentSpringResourceLoader.getContext();
75  			}
76  			this.context = new ClassPathXmlApplicationContext(this.fileLocs, parentContext);
77  			super.start();
78  		}
79  	}
80  
81  	@Override
82  	public void stop() throws Exception {
83  		LOG.info("Stopping Spring context " + StringUtils.join(this.fileLocs, ","));
84  		this.context.close();
85  		super.stop();
86  	}
87  
88  	public ConfigurableApplicationContext getContext() {
89  		return this.context;
90  	}
91  	
92  	public void setParentContext(ApplicationContext parentContext) {
93  		this.parentContext = parentContext;
94  	}
95  
96  	public void setParentSpringResourceLoader(
97  			SpringResourceLoader parentSpringResourceLoader) {
98  		this.parentSpringResourceLoader = parentSpringResourceLoader;
99  	}
100 	
101 	
102 
103 }