Coverage Report - org.kuali.rice.core.impl.resourceloader.SpringResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringResourceLoader
0%
0/38
0%
0/14
2.25
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 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.kuali.rice.core.impl.resourceloader;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.api.config.ConfigurationException;
 21  
 import org.kuali.rice.core.framework.resourceloader.BaseResourceLoader;
 22  
 import org.springframework.context.ApplicationContext;
 23  
 import org.springframework.context.ConfigurableApplicationContext;
 24  
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 25  
 import org.springframework.web.context.support.XmlWebApplicationContext;
 26  
 
 27  
 import javax.servlet.ServletContext;
 28  
 import javax.xml.namespace.QName;
 29  
 import java.util.Collections;
 30  
 import java.util.List;
 31  
 
 32  
 /**
 33  
  * A simple {@link org.kuali.rice.core.api.resourceloader.ResourceLoader} which wraps a Spring {@link ConfigurableApplicationContext}.
 34  
  *
 35  
  * Starts and stops the {@link ConfigurableApplicationContext}.
 36  
  * 
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  
 public class SpringResourceLoader extends BaseResourceLoader {
 40  
 
 41  
         private SpringResourceLoader parentSpringResourceLoader;
 42  
         
 43  
         private ApplicationContext parentContext;
 44  
         private ConfigurableApplicationContext context;
 45  
         private ServletContext servletContextcontext;
 46  
 
 47  
         private final List<String> fileLocs;
 48  
 
 49  
         public SpringResourceLoader(QName name, String fileLoc, ServletContext context) {
 50  0
             this(name, Collections.singletonList(fileLoc), context);
 51  0
         }
 52  
 
 53  
         public SpringResourceLoader(QName name, List<String> fileLocs, ServletContext servletContextcontext) {
 54  0
                 super(name);
 55  0
                 this.fileLocs = fileLocs;
 56  0
                 this.servletContextcontext = servletContextcontext;
 57  0
         }
 58  
 
 59  
         @Override
 60  
         public Object getService(QName serviceName) {
 61  0
                     if (!isStarted()) {
 62  0
                         return null;
 63  
                     }
 64  0
                 if (this.getContext().containsBean(serviceName.toString())) {
 65  0
                         Object service = this.getContext().getBean(serviceName.toString());
 66  0
                         return postProcessService(serviceName, service);
 67  
                 }
 68  0
                 return super.getService(serviceName);
 69  
         }
 70  
 
 71  
         @Override
 72  
         public void start() throws Exception {
 73  0
                 if(!isStarted()){
 74  0
                         LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ","));
 75  0
                         if (parentSpringResourceLoader != null && parentContext != null) {
 76  0
                                 throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined.  Only one can be defined!");
 77  
                         }
 78  0
                         if (parentSpringResourceLoader != null) {
 79  0
                                 parentContext = parentSpringResourceLoader.getContext();
 80  
                         }
 81  
                         
 82  0
                         if (servletContextcontext != null) {
 83  0
                                 XmlWebApplicationContext lContext = new XmlWebApplicationContext();
 84  0
                                 lContext.setServletContext(servletContextcontext);
 85  0
                                 lContext.setParent(parentContext);
 86  0
                                 lContext.setConfigLocations(this.fileLocs.toArray(new String[] {}));
 87  0
                                 lContext.refresh();
 88  0
                                 context = lContext;
 89  0
                         } else {
 90  0
                                 this.context = new ClassPathXmlApplicationContext(this.fileLocs.toArray(new String[] {}), parentContext);
 91  
                         }
 92  
            
 93  0
                         super.start();
 94  
                 }
 95  0
         }
 96  
 
 97  
         @Override
 98  
         public void stop() throws Exception {
 99  0
                 LOG.info("Stoping Spring context " + StringUtils.join(this.fileLocs, ","));
 100  0
                 this.context.close();
 101  0
                 super.stop();
 102  0
         }
 103  
 
 104  
         public ConfigurableApplicationContext getContext() {
 105  0
                 return this.context;
 106  
         }
 107  
         
 108  
         public void setParentContext(ApplicationContext parentContext) {
 109  0
                 this.parentContext = parentContext;
 110  0
         }
 111  
 
 112  
         public void setParentSpringResourceLoader(
 113  
                         SpringResourceLoader parentSpringResourceLoader) {
 114  0
                 this.parentSpringResourceLoader = parentSpringResourceLoader;
 115  0
         }
 116  
 }