View Javadoc

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.api.resourceloader;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.config.CoreConfigHelper;
21  import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException;
22  import org.kuali.rice.core.api.exception.RiceRuntimeException;
23  import org.kuali.rice.core.api.reflect.ObjectDefinition;
24  import org.kuali.rice.core.api.util.ClassLoaderUtils;
25  
26  import javax.xml.namespace.QName;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  /**
31   * Wrapper on all the Resource loaders.  This is what programmers typically use to get in the resource loading
32   * framework.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class GlobalResourceLoader {
37  
38  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalResourceLoader.class);
39  
40  	private static Map<ClassLoader, ResourceLoader> rootResourceLoaders = new HashMap<ClassLoader, ResourceLoader>();
41  
42  	private static boolean initializing;
43  
44  	public static synchronized ResourceLoader getResourceLoader() {
45  		ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader();
46  		return getResourceLoaderCheckParent(classLoader);
47  	}
48  
49  	private static synchronized ResourceLoader getResourceLoaderCheckParent(ClassLoader classLoader) {
50          ResourceLoader resourceLoader = getResourceLoader(classLoader);
51  
52          if (resourceLoader != null && classLoader.getParent() != null) {
53              ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
54  
55              if (parentResourceLoader != null) {
56                  resourceLoader = new ParentChildResourceLoader(parentResourceLoader, resourceLoader);
57  		    }
58  	    }
59  
60  	    if (resourceLoader == null && classLoader.getParent() != null) {
61  		    resourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
62  	    }
63  	    return resourceLoader;
64  	}
65  
66  	public static synchronized ResourceLoader getResourceLoader(ClassLoader classloader) {
67  		return rootResourceLoaders.get(classloader);
68  	}
69  
70  	public static synchronized void start() throws Exception {
71  		try {
72  			initializing = true;
73  			ResourceLoader internalResourceLoader = getResourceLoader();
74  			if (internalResourceLoader == null) {
75  				throw new RiceRuntimeException("Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :" + Thread.currentThread().getContextClassLoader());
76  			}
77  			internalResourceLoader.start();
78  		} finally {
79  			initializing = false;
80  		}
81  	}
82  
83  	public static synchronized void addResourceLoader(ResourceLoader resourceLoader) {
84  		initialize();
85  		if (resourceLoader == null) {
86  			throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
87  		}
88  		LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
89  		getResourceLoader().addResourceLoader(resourceLoader);
90  	}
91  
92  	public static synchronized void addResourceLoaderFirst(ResourceLoader resourceLoader) {
93  		initialize();
94  		if (resourceLoader == null) {
95  			throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
96  		}
97  		LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
98  		getResourceLoader().addResourceLoaderFirst(resourceLoader);
99  	}
100 
101 	protected static synchronized void initialize() {
102 		if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) {
103 			LOG.info("Creating CompositeResourceLoader in GlobalResourceLoader");
104 			rootResourceLoaders.put(ClassLoaderUtils.getDefaultClassLoader(), new ResourceLoaderContainer(new QName(CoreConfigHelper.getApplicationId(), ResourceLoader.ROOT_RESOURCE_LOADER_NAME)));
105 		}
106 	}
107 
108 	public static synchronized ResourceLoader getResourceLoader(QName name) {
109 		return getResourceLoader().getResourceLoader(name);
110 	}
111 
112 	/**
113 	 * Stop the resource loader for the current context classloader.  Don't stop or clear them all
114 	 * because the stop was issued from the context of a single classloader.
115 	 *
116 	 * @throws Exception
117 	 */
118 	public static synchronized void stop() throws Exception {
119 		LOG.info("Stopping the GlobalResourceLoader...");
120 		if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) != null) {
121 			LOG.info("Destroying GlobalResourceLoader");
122 			getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()).stop();
123 			rootResourceLoaders.remove(ClassLoaderUtils.getDefaultClassLoader());
124 		}
125 		LOG.info("...GlobalResourceLoader successfully stopped.");
126 	}
127 
128 	public static synchronized <T extends Object> T getService(QName serviceName) {
129 		if (serviceName == null) {
130 			throw new IllegalArgumentException("The service name must be non-null.");
131 		}
132 		LOG.debug("GlobalResourceLoader fetching service " + serviceName);
133 		try {
134 			return getResourceLoader().<T>getService(serviceName);
135 		} catch (RiceRemoteServiceConnectionException ex) {
136 			LOG.warn(ex.getMessage());
137 			return null;
138 		}
139 	}
140 
141 	public static synchronized <T extends Object> T getService(String localServiceName) {
142 		if (StringUtils.isEmpty(localServiceName)) {
143 			throw new IllegalArgumentException("The service name must be non-null.");
144 		}
145 		return GlobalResourceLoader.<T>getService(new QName(localServiceName));
146 	}
147 
148 	public static synchronized <T extends Object> T getObject(ObjectDefinition objectDefinition) {
149 		return getResourceLoader().<T>getObject(objectDefinition);
150 	}
151 
152 	public static synchronized boolean isInitialized() {
153 		return getResourceLoader() != null;
154 	}
155 
156 	public static synchronized void logContents() {
157 		if (LOG.isInfoEnabled()) {
158 			LOG.info(getResourceLoader().getContents("", false));
159 		}
160 	}
161 	
162 	public static synchronized void logAllContents() {
163 		if (LOG.isInfoEnabled()) {
164 			LOG.info("######################### Logging All Contents ###########################");
165 			for (ResourceLoader rl : rootResourceLoaders.values()) {
166 				LOG.info("Logging contents for ResourceLoader: " + rl.getName() + "\n" + rl.getContents("  ", true));
167 			}
168 			LOG.info("###################### Done Logging All Contents #########################");
169 		}
170 	}
171 
172 	public static synchronized boolean isInitializing() {
173 		return initializing;
174 	}
175 
176 	public static synchronized void setInitializing(boolean initializing) {
177 		GlobalResourceLoader.initializing = initializing;
178 	}
179 }