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