001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.kuali.rice.resourceloader;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.xml.namespace.QName;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.rice.core.Core;
026: import org.kuali.rice.definition.ObjectDefinition;
027: import org.kuali.rice.exceptions.RiceRuntimeException;
028: import org.kuali.rice.util.ClassLoaderUtils;
029:
030: /**
031: * Wrapper on all the Resource loaders. This is what programmers typically use to get in the resource loading
032: * framework.
033: *
034: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
035: */
036: public class GlobalResourceLoader {
037:
038: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
039: .getLogger(GlobalResourceLoader.class);
040:
041: private static Map<ClassLoader, ResourceLoader> rootResourceLoaders = new HashMap<ClassLoader, ResourceLoader>();
042:
043: private static boolean initializing;
044:
045: public static ResourceLoader getResourceLoader() {
046: ClassLoader classLoader = ClassLoaderUtils
047: .getDefaultClassLoader();
048: return getResourceLoaderCheckParent(classLoader);
049: }
050:
051: private static ResourceLoader getResourceLoaderCheckParent(
052: ClassLoader classLoader) {
053: ResourceLoader resourceLoader = getResourceLoader(classLoader);
054: if (resourceLoader != null && classLoader.getParent() != null) {
055: ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader
056: .getParent());
057: if (parentResourceLoader != null) {
058: resourceLoader = new ParentChildResourceLoader(
059: parentResourceLoader, resourceLoader);
060: }
061: }
062: if (resourceLoader == null && classLoader.getParent() != null) {
063: resourceLoader = getResourceLoaderCheckParent(classLoader
064: .getParent());
065: }
066: return resourceLoader;
067: }
068:
069: public static ResourceLoader getResourceLoader(
070: ClassLoader classloader) {
071: return rootResourceLoaders.get(classloader);
072: }
073:
074: public static void start() throws Exception {
075: try {
076: initializing = true;
077: ResourceLoader internalResourceLoader = getResourceLoader();
078: if (internalResourceLoader == null) {
079: throw new RiceRuntimeException(
080: "Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :"
081: + Thread.currentThread()
082: .getContextClassLoader());
083: }
084: internalResourceLoader.start();
085: } finally {
086: initializing = false;
087: }
088: }
089:
090: public static void addResourceLoader(ResourceLoader resourceLoader) {
091: initialize();
092: LOG.info("Adding ResourceLoader " + resourceLoader.getName()
093: + " to GlobalResourceLoader");
094: if (resourceLoader == null) {
095: throw new ResourceLoaderException(
096: "Attempted to add a null resource loader to the Global resource loader.");
097: }
098: getResourceLoader().addResourceLoader(resourceLoader);
099: }
100:
101: public static void addResourceLoaderFirst(
102: ResourceLoader resourceLoader) {
103: initialize();
104: LOG.info("Adding ResourceLoader " + resourceLoader.getName()
105: + " to GlobalResourceLoader");
106: if (resourceLoader == null) {
107: throw new ResourceLoaderException(
108: "Attempted to add a null resource loader to the Global resource loader.");
109: }
110: getResourceLoader().addResourceLoaderFirst(resourceLoader);
111: }
112:
113: protected static void initialize() {
114: if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) {
115: LOG
116: .info("Creating CompositeResourceLoader in GlobalResourceLoader");
117: rootResourceLoaders.put(ClassLoaderUtils
118: .getDefaultClassLoader(),
119: new ResourceLoaderContainer(new QName(Core
120: .getCurrentContextConfig()
121: .getMessageEntity(),
122: ResourceLoader.ROOT_RESOURCE_LOADER_NAME)));
123: }
124: }
125:
126: public static ResourceLoader getResourceLoader(QName name) {
127: return getResourceLoader().getResourceLoader(name);
128: }
129:
130: /**
131: * Stop the resource loader for the current context classloader. Don't stop or clear them all
132: * because the stop was issued from the context of a single classloader.
133: *
134: * @throws Exception
135: */
136: public static void stop() throws Exception {
137: LOG.debug("Destroy called on GlobalResourceLoader");
138: if (getResourceLoader() != null) {
139: LOG.info("Destroying GlobalResourceLoader");
140: getResourceLoader().stop();
141: rootResourceLoaders.remove(ClassLoaderUtils
142: .getDefaultClassLoader());
143: }
144: }
145:
146: public static Object getService(QName serviceName) {
147: if (serviceName == null) {
148: throw new IllegalArgumentException(
149: "The service name must be non-null.");
150: }
151: LOG.debug("GlobalResourceLoader fetching service "
152: + serviceName);
153: return getResourceLoader().getService(serviceName);
154: }
155:
156: public static Object getService(String localServiceName) {
157: if (StringUtils.isEmpty(localServiceName)) {
158: throw new IllegalArgumentException(
159: "The service name must be non-null.");
160: }
161: return getService(new QName(localServiceName));
162: }
163:
164: public static Object getObject(ObjectDefinition objectDefinition) {
165: return getResourceLoader().getObject(objectDefinition);
166: }
167:
168: public static boolean isInitialized() {
169: return getResourceLoader() != null;
170: }
171:
172: public static void logContents() {
173: if (LOG.isInfoEnabled()) {
174: LOG.info(getResourceLoader().getContents("", false));
175: }
176: }
177:
178: public static boolean isInitializing() {
179: return initializing;
180: }
181:
182: public static void setInitializing(boolean initializing) {
183: GlobalResourceLoader.initializing = initializing;
184: }
185: }
|