Coverage Report - org.kuali.rice.core.resourceloader.SpringResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringResourceLoader
0%
0/30
0%
0/12
2.125
 
 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  0
         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  0
             this(name, new String[] { fileLoc });
 48  0
         }
 49  
 
 50  
         public SpringResourceLoader(QName name, String[] fileLocs) {
 51  0
                 super(name);
 52  0
                 this.fileLocs = fileLocs;
 53  0
         }
 54  
 
 55  
         public Object getService(QName serviceName) {
 56  0
                     if (!isStarted()) {
 57  0
                         return null;
 58  
                     }
 59  0
                 if (this.getContext().containsBean(serviceName.toString())) {
 60  0
                         Object service = this.getContext().getBean(serviceName.toString());
 61  0
                         return postProcessService(serviceName, service);
 62  
                 }
 63  0
                 return super.getService(serviceName);
 64  
         }
 65  
 
 66  
         @Override
 67  
         public void start() throws Exception {
 68  0
                 if(!isStarted()){
 69  0
                         LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ","));
 70  0
                         if (parentSpringResourceLoader != null && parentContext != null) {
 71  0
                                 throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined.  Only one can be defined!");
 72  
                         }
 73  0
                         if (parentSpringResourceLoader != null) {
 74  0
                                 parentContext = parentSpringResourceLoader.getContext();
 75  
                         }
 76  0
                         this.context = new ClassPathXmlApplicationContext(this.fileLocs, parentContext);
 77  0
                         super.start();
 78  
                 }
 79  0
         }
 80  
 
 81  
         @Override
 82  
         public void stop() throws Exception {
 83  0
                 LOG.info("Stopping Spring context " + StringUtils.join(this.fileLocs, ","));
 84  0
                 this.context.close();
 85  0
                 super.stop();
 86  0
         }
 87  
 
 88  
         public ConfigurableApplicationContext getContext() {
 89  0
                 return this.context;
 90  
         }
 91  
         
 92  
         public void setParentContext(ApplicationContext parentContext) {
 93  0
                 this.parentContext = parentContext;
 94  0
         }
 95  
 
 96  
         public void setParentSpringResourceLoader(
 97  
                         SpringResourceLoader parentSpringResourceLoader) {
 98  0
                 this.parentSpringResourceLoader = parentSpringResourceLoader;
 99  0
         }
 100  
         
 101  
         
 102  
 
 103  
 }