| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.core.resourceloader; |
| 18 | |
|
| 19 | |
import java.util.HashMap; |
| 20 | |
import java.util.Map; |
| 21 | |
|
| 22 | |
import javax.xml.namespace.QName; |
| 23 | |
|
| 24 | |
import org.apache.commons.lang.StringUtils; |
| 25 | |
import org.kuali.rice.core.config.ConfigContext; |
| 26 | |
import org.kuali.rice.core.exception.RiceRemoteServiceConnectionException; |
| 27 | |
import org.kuali.rice.core.exception.RiceRuntimeException; |
| 28 | |
import org.kuali.rice.core.reflect.ObjectDefinition; |
| 29 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | public class GlobalResourceLoader { |
| 38 | |
|
| 39 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalResourceLoader.class); |
| 40 | |
|
| 41 | 0 | private static Map<ClassLoader, ResourceLoader> rootResourceLoaders = new HashMap<ClassLoader, ResourceLoader>(); |
| 42 | |
|
| 43 | |
private static boolean initializing; |
| 44 | |
|
| 45 | |
public static ResourceLoader getResourceLoader() { |
| 46 | 0 | ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader(); |
| 47 | 0 | return getResourceLoaderCheckParent(classLoader); |
| 48 | |
} |
| 49 | |
|
| 50 | |
private static ResourceLoader getResourceLoaderCheckParent(ClassLoader classLoader) { |
| 51 | 0 | ResourceLoader resourceLoader = getResourceLoader(classLoader); |
| 52 | 0 | if (resourceLoader != null && classLoader.getParent() != null) { |
| 53 | 0 | ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader.getParent()); |
| 54 | 0 | if (parentResourceLoader != null) { |
| 55 | 0 | resourceLoader = new ParentChildResourceLoader(parentResourceLoader, resourceLoader); |
| 56 | |
} |
| 57 | |
} |
| 58 | 0 | if (resourceLoader == null && classLoader.getParent() != null) { |
| 59 | 0 | resourceLoader = getResourceLoaderCheckParent(classLoader.getParent()); |
| 60 | |
} |
| 61 | 0 | return resourceLoader; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public static ResourceLoader getResourceLoader(ClassLoader classloader) { |
| 65 | 0 | return rootResourceLoaders.get(classloader); |
| 66 | |
} |
| 67 | |
|
| 68 | |
public synchronized static void start() throws Exception { |
| 69 | |
try { |
| 70 | 0 | initializing = true; |
| 71 | 0 | ResourceLoader internalResourceLoader = getResourceLoader(); |
| 72 | 0 | if (internalResourceLoader == null) { |
| 73 | 0 | throw new RiceRuntimeException("Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :" + Thread.currentThread().getContextClassLoader()); |
| 74 | |
} |
| 75 | 0 | internalResourceLoader.start(); |
| 76 | |
} finally { |
| 77 | 0 | initializing = false; |
| 78 | 0 | } |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
public synchronized static void addResourceLoader(ResourceLoader resourceLoader) { |
| 82 | 0 | initialize(); |
| 83 | 0 | LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader"); |
| 84 | 0 | if (resourceLoader == null) { |
| 85 | 0 | throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader."); |
| 86 | |
} |
| 87 | 0 | getResourceLoader().addResourceLoader(resourceLoader); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
public synchronized static void addResourceLoaderFirst(ResourceLoader resourceLoader) { |
| 91 | 0 | initialize(); |
| 92 | 0 | LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader"); |
| 93 | 0 | if (resourceLoader == null) { |
| 94 | 0 | throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader."); |
| 95 | |
} |
| 96 | 0 | getResourceLoader().addResourceLoaderFirst(resourceLoader); |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
protected static void initialize() { |
| 100 | 0 | if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) { |
| 101 | 0 | LOG.info("Creating CompositeResourceLoader in GlobalResourceLoader"); |
| 102 | 0 | rootResourceLoaders.put(ClassLoaderUtils.getDefaultClassLoader(), new ResourceLoaderContainer(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), ResourceLoader.ROOT_RESOURCE_LOADER_NAME))); |
| 103 | |
} |
| 104 | 0 | } |
| 105 | |
|
| 106 | |
public static ResourceLoader getResourceLoader(QName name) { |
| 107 | 0 | return getResourceLoader().getResourceLoader(name); |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public static void stop() throws Exception { |
| 117 | 0 | LOG.info("Stopping the GlobalResourceLoader..."); |
| 118 | 0 | if (getResourceLoader() != null) { |
| 119 | 0 | LOG.info("Destroying GlobalResourceLoader"); |
| 120 | 0 | getResourceLoader().stop(); |
| 121 | 0 | rootResourceLoaders.remove(ClassLoaderUtils.getDefaultClassLoader()); |
| 122 | |
} |
| 123 | 0 | LOG.info("...GlobalResourceLoader successfully stopped."); |
| 124 | 0 | } |
| 125 | |
|
| 126 | |
public static Object getService(QName serviceName) { |
| 127 | 0 | if (serviceName == null) { |
| 128 | 0 | throw new IllegalArgumentException("The service name must be non-null."); |
| 129 | |
} |
| 130 | 0 | LOG.debug("GlobalResourceLoader fetching service " + serviceName); |
| 131 | |
try { |
| 132 | 0 | return getResourceLoader().getService(serviceName); |
| 133 | 0 | } catch (RiceRemoteServiceConnectionException ex) { |
| 134 | 0 | LOG.warn(ex.getMessage()); |
| 135 | 0 | return null; |
| 136 | |
} |
| 137 | |
} |
| 138 | |
|
| 139 | |
public static Object getService(String localServiceName) { |
| 140 | 0 | if (StringUtils.isEmpty(localServiceName)) { |
| 141 | 0 | throw new IllegalArgumentException("The service name must be non-null."); |
| 142 | |
} |
| 143 | 0 | return getService(new QName(localServiceName)); |
| 144 | |
} |
| 145 | |
|
| 146 | |
public static Object getObject(ObjectDefinition objectDefinition) { |
| 147 | 0 | return getResourceLoader().getObject(objectDefinition); |
| 148 | |
} |
| 149 | |
|
| 150 | |
public static boolean isInitialized() { |
| 151 | 0 | return getResourceLoader() != null; |
| 152 | |
} |
| 153 | |
|
| 154 | |
public static void logContents() { |
| 155 | 0 | if (LOG.isInfoEnabled()) { |
| 156 | 0 | LOG.info(getResourceLoader().getContents("", false)); |
| 157 | |
} |
| 158 | 0 | } |
| 159 | |
|
| 160 | |
public static boolean isInitializing() { |
| 161 | 0 | return initializing; |
| 162 | |
} |
| 163 | |
|
| 164 | |
public static void setInitializing(boolean initializing) { |
| 165 | 0 | GlobalResourceLoader.initializing = initializing; |
| 166 | 0 | } |
| 167 | |
} |