View Javadoc

1   /*
2    * Copyright 2007-2008 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  package org.kuali.rice.core.resourceloader;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.kuali.rice.core.config.Config;
21  import org.kuali.rice.core.config.ConfigContext;
22  import org.kuali.rice.core.config.ConfigurationException;
23  
24  
25  
26  /**
27   * Creates resource loader for rice spring context.
28   * Uses config object to store QNames so everything is good with the current context classloader.
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class RiceResourceLoaderFactory {
33  
34  	private static final String RICE_ROOT_RESOURCE_LOADER_NAME = "RICE_ROOT_RESOURCE_LOADER";
35  	private static final String RICE_SPRING_RESOURCE_LOADER_NAME = "RICE_SPRING_RESOURCE_LOADER_NAME";
36  
37  	private static void initialize() {
38  		Config config = ConfigContext.getCurrentContextConfig();
39  		if (config.getServiceNamespace() == null) {
40  			throw new ConfigurationException("No service namespace available at this time");
41  		}
42  		if (getRootResourceLoaderName() == null) {
43  			setRootResourceLoaderName(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), RICE_ROOT_RESOURCE_LOADER_NAME));
44  		}
45  		if (getSpringResourceLoaderName() == null) {
46  			setSpringResourceLoaderName(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), RICE_SPRING_RESOURCE_LOADER_NAME));
47  		}
48  	}
49  
50  	public static ResourceLoader createRootRiceResourceLoader(String springFileLocations) {
51  		initialize();
52  		ResourceLoader rootResourceLoader = 
53  			new BaseResourceLoader((QName)ConfigContext.getCurrentContextConfig().getObject(RICE_ROOT_RESOURCE_LOADER_NAME),
54  			new SimpleServiceLocator());
55  		ResourceLoader springResourceLoader = 
56  			new SpringResourceLoader((QName)ConfigContext.getCurrentContextConfig().getObject(RICE_SPRING_RESOURCE_LOADER_NAME),
57  			springFileLocations.split(SpringLoader.SPRING_SEPARATOR_CHARACTER));
58  		rootResourceLoader.addResourceLoaderFirst(springResourceLoader);
59  		return rootResourceLoader;
60  	}
61  
62  	public static BaseResourceLoader getRootResourceLoader() {
63  		return (BaseResourceLoader)GlobalResourceLoader.getResourceLoader(getRootResourceLoaderName());
64  	}
65  
66  	public static SpringResourceLoader getSpringResourceLoader() {
67  		return (SpringResourceLoader)GlobalResourceLoader.getResourceLoader(getSpringResourceLoaderName());
68  	}
69  
70  	public static QName getRootResourceLoaderName() {
71  		return (QName)ConfigContext.getCurrentContextConfig().getObject(RICE_ROOT_RESOURCE_LOADER_NAME);
72  	}
73  
74  	public static void setRootResourceLoaderName(QName name) {
75  		ConfigContext.getCurrentContextConfig().putObject(RICE_ROOT_RESOURCE_LOADER_NAME, name);
76  	}
77  
78  	public static QName getSpringResourceLoaderName() {
79  		return (QName)ConfigContext.getCurrentContextConfig().getObject(RICE_SPRING_RESOURCE_LOADER_NAME);
80  	}
81  
82  	public static void setSpringResourceLoaderName(QName ksbRsourceLoaderName) {
83  		ConfigContext.getCurrentContextConfig().putObject(RICE_SPRING_RESOURCE_LOADER_NAME, ksbRsourceLoaderName);
84  	}
85  
86  }