1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.api.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.api.config.CoreConfigHelper; |
26 | |
import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException; |
27 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
28 | |
import org.kuali.rice.core.api.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 synchronized ResourceLoader getResourceLoader() { |
46 | 0 | ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader(); |
47 | 0 | return getResourceLoaderCheckParent(classLoader); |
48 | |
} |
49 | |
|
50 | |
private static synchronized ResourceLoader getResourceLoaderCheckParent(ClassLoader classLoader) { |
51 | 0 | ResourceLoader resourceLoader = getResourceLoader(classLoader); |
52 | |
|
53 | 0 | if (resourceLoader != null && classLoader.getParent() != null) { |
54 | 0 | ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader.getParent()); |
55 | |
|
56 | 0 | if (parentResourceLoader != null) { |
57 | 0 | resourceLoader = new ParentChildResourceLoader(parentResourceLoader, resourceLoader); |
58 | |
} |
59 | |
} |
60 | |
|
61 | 0 | if (resourceLoader == null && classLoader.getParent() != null) { |
62 | 0 | resourceLoader = getResourceLoaderCheckParent(classLoader.getParent()); |
63 | |
} |
64 | 0 | return resourceLoader; |
65 | |
} |
66 | |
|
67 | |
public static synchronized ResourceLoader getResourceLoader(ClassLoader classloader) { |
68 | 0 | return rootResourceLoaders.get(classloader); |
69 | |
} |
70 | |
|
71 | |
public static synchronized void start() throws Exception { |
72 | |
try { |
73 | 0 | initializing = true; |
74 | 0 | ResourceLoader internalResourceLoader = getResourceLoader(); |
75 | 0 | if (internalResourceLoader == null) { |
76 | 0 | throw new RiceRuntimeException("Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :" + Thread.currentThread().getContextClassLoader()); |
77 | |
} |
78 | 0 | internalResourceLoader.start(); |
79 | |
} finally { |
80 | 0 | initializing = false; |
81 | 0 | } |
82 | 0 | } |
83 | |
|
84 | |
public static synchronized void addResourceLoader(ResourceLoader resourceLoader) { |
85 | 0 | initialize(); |
86 | 0 | if (resourceLoader == null) { |
87 | 0 | throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader."); |
88 | |
} |
89 | 0 | LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader"); |
90 | 0 | getResourceLoader().addResourceLoader(resourceLoader); |
91 | 0 | } |
92 | |
|
93 | |
public static synchronized void addResourceLoaderFirst(ResourceLoader resourceLoader) { |
94 | 0 | initialize(); |
95 | 0 | if (resourceLoader == null) { |
96 | 0 | throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader."); |
97 | |
} |
98 | 0 | LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader"); |
99 | 0 | getResourceLoader().addResourceLoaderFirst(resourceLoader); |
100 | 0 | } |
101 | |
|
102 | |
protected static synchronized void initialize() { |
103 | 0 | if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) { |
104 | 0 | LOG.info("Creating CompositeResourceLoader in GlobalResourceLoader"); |
105 | 0 | rootResourceLoaders.put(ClassLoaderUtils.getDefaultClassLoader(), new ResourceLoaderContainer(new QName(CoreConfigHelper.getApplicationId(), ResourceLoader.ROOT_RESOURCE_LOADER_NAME))); |
106 | |
} |
107 | 0 | } |
108 | |
|
109 | |
public static synchronized ResourceLoader getResourceLoader(QName name) { |
110 | 0 | return getResourceLoader().getResourceLoader(name); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public static synchronized void stop() throws Exception { |
120 | 0 | LOG.info("Stopping the GlobalResourceLoader..."); |
121 | 0 | if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) != null) { |
122 | 0 | LOG.info("Destroying GlobalResourceLoader"); |
123 | 0 | getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()).stop(); |
124 | 0 | rootResourceLoaders.remove(ClassLoaderUtils.getDefaultClassLoader()); |
125 | |
} |
126 | 0 | LOG.info("...GlobalResourceLoader successfully stopped."); |
127 | 0 | } |
128 | |
|
129 | |
public static synchronized <T extends Object> T getService(QName serviceName) { |
130 | 0 | if (serviceName == null) { |
131 | 0 | throw new IllegalArgumentException("The service name must be non-null."); |
132 | |
} |
133 | 0 | LOG.debug("GlobalResourceLoader fetching service " + serviceName); |
134 | |
try { |
135 | 0 | return getResourceLoader().<T>getService(serviceName); |
136 | 0 | } catch (RiceRemoteServiceConnectionException ex) { |
137 | 0 | LOG.warn(ex.getMessage()); |
138 | 0 | return null; |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
public static synchronized <T extends Object> T getService(String localServiceName) { |
143 | 0 | if (StringUtils.isEmpty(localServiceName)) { |
144 | 0 | throw new IllegalArgumentException("The service name must be non-null."); |
145 | |
} |
146 | 0 | return GlobalResourceLoader.<T>getService(new QName(localServiceName)); |
147 | |
} |
148 | |
|
149 | |
public static synchronized <T extends Object> T getObject(ObjectDefinition objectDefinition) { |
150 | 0 | return getResourceLoader().<T>getObject(objectDefinition); |
151 | |
} |
152 | |
|
153 | |
public static synchronized boolean isInitialized() { |
154 | 0 | return getResourceLoader() != null; |
155 | |
} |
156 | |
|
157 | |
public static synchronized void logContents() { |
158 | 0 | if (LOG.isInfoEnabled()) { |
159 | 0 | LOG.info(getResourceLoader().getContents("", false)); |
160 | |
} |
161 | 0 | } |
162 | |
|
163 | |
public static synchronized void logAllContents() { |
164 | 0 | if (LOG.isInfoEnabled()) { |
165 | 0 | LOG.info("######################### Logging All Contents ###########################"); |
166 | 0 | for (ResourceLoader rl : rootResourceLoaders.values()) { |
167 | 0 | LOG.info("Logging contents for ResourceLoader: " + rl.getName() + "\n" + rl.getContents(" ", true)); |
168 | |
} |
169 | 0 | LOG.info("###################### Done Logging All Contents #########################"); |
170 | |
} |
171 | 0 | } |
172 | |
|
173 | |
public static synchronized boolean isInitializing() { |
174 | 0 | return initializing; |
175 | |
} |
176 | |
|
177 | |
public static synchronized void setInitializing(boolean initializing) { |
178 | 0 | GlobalResourceLoader.initializing = initializing; |
179 | 0 | } |
180 | |
} |