Coverage Report - org.kuali.rice.core.impl.resourceloader.RiceResourceLoaderFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
RiceResourceLoaderFactory
0%
0/40
0%
0/20
2.571
 
 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.kuali.rice.core.api.config.ConfigurationException;
 20  
 import org.kuali.rice.core.api.config.property.Config;
 21  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 22  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 23  
 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
 24  
 import org.kuali.rice.core.framework.resourceloader.BaseResourceLoader;
 25  
 
 26  
 import javax.servlet.ServletContext;
 27  
 import javax.xml.namespace.QName;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collection;
 30  
 import java.util.List;
 31  
 
 32  
 
 33  
 
 34  
 /**
 35  
  * Creates resource loader for rice spring context.
 36  
  * Uses config object to store QNames so everything is good with the current context classloader.
 37  
  *
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  0
 public class RiceResourceLoaderFactory {
 41  
 
 42  
         private static final String RICE_ROOT_RESOURCE_LOADER_NAME = "RICE_ROOT_RESOURCE_LOADER";
 43  
         private static final String RICE_SPRING_RESOURCE_LOADER_NAME = "RICE_SPRING_RESOURCE_LOADER_NAME";
 44  
         
 45  
         public static ResourceLoader createRootRiceResourceLoader(ServletContext context, List<String> springFileLocations, String prefix) {
 46  
                 //FIXME: RICE MODULARITY
 47  
                 //hack to not break the hack in ResourceLoaderContainer.moveKSBLoadersDownHack();
 48  0
                 if ("KSB".equals(prefix.toUpperCase())) {
 49  0
                         prefix = "K~S~B";
 50  
                 }
 51  
                 
 52  0
                 final Config config = ConfigContext.getCurrentContextConfig();
 53  0
                 if (config.getServiceNamespace() == null) {
 54  0
                         throw new ConfigurationException("No service namespace available at this time");
 55  
                 }
 56  0
                 final QName root = new QName(config.getServiceNamespace(), prefix.toUpperCase() + "_" + RICE_ROOT_RESOURCE_LOADER_NAME);
 57  
                 
 58  0
                 if (getRootResourceLoaderNames() == null || !getRootResourceLoaderNames().contains(root)) {
 59  0
                         addRootResourceLoaderName(root);
 60  
                 }
 61  
                 
 62  0
                 final QName spring = new QName(config.getServiceNamespace(), prefix.toUpperCase() + "_" + RICE_SPRING_RESOURCE_LOADER_NAME);
 63  0
                 if (getSpringResourceLoaderNames() == null || !getSpringResourceLoaderNames().contains(root)) {
 64  0
                         addSpringResourceLoaderName(spring);
 65  
                 }
 66  
                 
 67  0
                 final ResourceLoader rootResourceLoader = new BaseResourceLoader(root, new SimpleServiceLocator());
 68  
                 
 69  0
                 final ResourceLoader springResourceLoader = new SpringResourceLoader(spring, springFileLocations, context);
 70  0
                 rootResourceLoader.addResourceLoaderFirst(springResourceLoader);
 71  
                 
 72  0
                 return rootResourceLoader;
 73  
         }
 74  
 
 75  
         public static Collection<BaseResourceLoader> getRootResourceLoaders() {
 76  0
                 final Collection<QName> names = getRootResourceLoaderNames();
 77  0
                 final Collection<BaseResourceLoader> loaders = new ArrayList<BaseResourceLoader>();
 78  0
                 for (QName name : names) {
 79  0
                         loaders.add((BaseResourceLoader) GlobalResourceLoader.getResourceLoader(name));
 80  
                 }
 81  0
                 return loaders;
 82  
         }
 83  
 
 84  
         public static Collection<SpringResourceLoader> getSpringResourceLoaders() {
 85  0
                 final Collection<QName> names = getSpringResourceLoaderNames();
 86  0
                 final Collection<SpringResourceLoader> loaders = new ArrayList<SpringResourceLoader>();
 87  0
                 for (QName name : names) {
 88  0
                         loaders.add((SpringResourceLoader) GlobalResourceLoader.getResourceLoader(name));
 89  
                 }
 90  0
                 return loaders;
 91  
         }
 92  
 
 93  
         @SuppressWarnings("unchecked")
 94  
         private static Collection<QName> getRootResourceLoaderNames() {
 95  0
                 return (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_ROOT_RESOURCE_LOADER_NAME);
 96  
         }
 97  
 
 98  
         private static void addRootResourceLoaderName(QName name) {
 99  
                 @SuppressWarnings("unchecked")
 100  0
                 Collection<QName> names = (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_ROOT_RESOURCE_LOADER_NAME);
 101  0
                 if (names == null) {
 102  0
                         names = new ArrayList<QName>();
 103  
                 }
 104  0
                 names.add(name);
 105  
                 
 106  0
                 ConfigContext.getCurrentContextConfig().putObject(RICE_ROOT_RESOURCE_LOADER_NAME, names);
 107  0
         }
 108  
 
 109  
         @SuppressWarnings("unchecked")
 110  
         private static Collection<QName> getSpringResourceLoaderNames() {
 111  0
                 return (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_SPRING_RESOURCE_LOADER_NAME);
 112  
         }
 113  
 
 114  
         private static void addSpringResourceLoaderName(QName name) {
 115  
                 @SuppressWarnings("unchecked")
 116  0
                 Collection<QName> names = (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_SPRING_RESOURCE_LOADER_NAME);
 117  0
                 if (names == null) {
 118  0
                         names = new ArrayList<QName>();
 119  
                 }
 120  0
                 names.add(name);
 121  
                 
 122  0
                 ConfigContext.getCurrentContextConfig().putObject(RICE_SPRING_RESOURCE_LOADER_NAME, names);
 123  0
         }
 124  
 
 125  
 }