001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.core.framework.resourceloader;
017    
018    import org.kuali.rice.core.api.config.CoreConfigHelper;
019    import org.kuali.rice.core.api.config.property.ConfigContext;
020    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
021    import org.kuali.rice.core.api.resourceloader.ResourceLoader;
022    
023    import javax.servlet.ServletContext;
024    import javax.xml.namespace.QName;
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.List;
028    
029    
030    
031    /**
032     * Creates resource loader for rice spring context.
033     * Uses config object to store QNames so everything is good with the current context classloader.
034     *
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    public class RiceResourceLoaderFactory {
038    
039            private static final String RICE_ROOT_RESOURCE_LOADER_NAME = "RICE_ROOT_RESOURCE_LOADER";
040            private static final String RICE_SPRING_RESOURCE_LOADER_NAME = "RICE_SPRING_RESOURCE_LOADER_NAME";
041            
042            public static ResourceLoader createRootRiceResourceLoader(ServletContext context, List<String> springFileLocations, String prefix) {
043                    //FIXME: RICE MODULARITY
044                    //hack to not break the hack in ResourceLoaderContainer.moveKSBLoadersDownHack();
045                    if ("KSB".equals(prefix.toUpperCase())) {
046                            prefix = "K~S~B";
047                    }
048                    
049                    String applicationId = CoreConfigHelper.getApplicationId();
050                    final QName root = new QName(applicationId, prefix.toUpperCase() + "_" + RICE_ROOT_RESOURCE_LOADER_NAME);
051                    
052                    if (getRootResourceLoaderNames() == null || !getRootResourceLoaderNames().contains(root)) {
053                            addRootResourceLoaderName(root);
054                    }
055                    
056                    final QName spring = new QName(applicationId, prefix.toUpperCase() + "_" + RICE_SPRING_RESOURCE_LOADER_NAME);
057                    if (getSpringResourceLoaderNames() == null || !getSpringResourceLoaderNames().contains(root)) {
058                            addSpringResourceLoaderName(spring);
059                    }
060                    
061                    final ResourceLoader rootResourceLoader = new BaseResourceLoader(root, new SimpleServiceLocator());
062                    
063                    final ResourceLoader springResourceLoader = new SpringResourceLoader(spring, springFileLocations, context);
064                    rootResourceLoader.addResourceLoaderFirst(springResourceLoader);
065                    
066                    return rootResourceLoader;
067            }
068    
069            public static Collection<BaseResourceLoader> getRootResourceLoaders() {
070                    final Collection<QName> names = getRootResourceLoaderNames();
071                    final Collection<BaseResourceLoader> loaders = new ArrayList<BaseResourceLoader>();
072                    for (QName name : names) {
073                            loaders.add((BaseResourceLoader) GlobalResourceLoader.getResourceLoader(name));
074                    }
075                    return loaders;
076            }
077    
078            public static Collection<SpringResourceLoader> getSpringResourceLoaders() {
079                    final Collection<QName> names = getSpringResourceLoaderNames();
080                    final Collection<SpringResourceLoader> loaders = new ArrayList<SpringResourceLoader>();
081                    for (QName name : names) {
082                            loaders.add((SpringResourceLoader) GlobalResourceLoader.getResourceLoader(name));
083                    }
084                    return loaders;
085            }
086    
087            @SuppressWarnings("unchecked")
088            private static Collection<QName> getRootResourceLoaderNames() {
089                    return (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_ROOT_RESOURCE_LOADER_NAME);
090            }
091    
092            private static void addRootResourceLoaderName(QName name) {
093                    @SuppressWarnings("unchecked")
094                    Collection<QName> names = (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_ROOT_RESOURCE_LOADER_NAME);
095                    if (names == null) {
096                            names = new ArrayList<QName>();
097                    }
098                    names.add(name);
099                    
100                    ConfigContext.getCurrentContextConfig().putObject(RICE_ROOT_RESOURCE_LOADER_NAME, names);
101            }
102    
103            @SuppressWarnings("unchecked")
104            private static Collection<QName> getSpringResourceLoaderNames() {
105                    return (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_SPRING_RESOURCE_LOADER_NAME);
106            }
107    
108            private static void addSpringResourceLoaderName(QName name) {
109                    @SuppressWarnings("unchecked")
110                    Collection<QName> names = (Collection<QName>) ConfigContext.getCurrentContextConfig().getObject(RICE_SPRING_RESOURCE_LOADER_NAME);
111                    if (names == null) {
112                            names = new ArrayList<QName>();
113                    }
114                    names.add(name);
115                    
116                    ConfigContext.getCurrentContextConfig().putObject(RICE_SPRING_RESOURCE_LOADER_NAME, names);
117            }
118    
119    }