1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.api.config.property; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
21 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
22 | |
|
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Collections; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
import java.util.Set; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class ConfigContext { |
37 | 0 | private static final Logger LOG = Logger.getLogger(ConfigContext.class); |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | private static final ContextualConfigLock initialized = new ContextualConfigLock("ConfigurationInitialized"); |
44 | 0 | private static final Map<ClassLoader, Config> CONFIGS = new HashMap<ClassLoader, Config>(); |
45 | |
|
46 | 0 | private ConfigContext() { |
47 | |
|
48 | 0 | } |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public synchronized static void init(Config rootCfg) { |
56 | 0 | init(Thread.currentThread().getContextClassLoader(), rootCfg); |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public synchronized static void init(ClassLoader classLoader, Config config) { |
63 | 0 | CONFIGS.put(classLoader, config); |
64 | 0 | initialized.fire(); |
65 | 0 | } |
66 | |
|
67 | |
public synchronized static boolean isInitialized() { |
68 | 0 | return initialized.hasFired(); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public synchronized static void destroy() { |
75 | 0 | if (!initialized.hasFired()) { |
76 | 0 | LOG.warn("Destroy on un-initialized ConfigContext was ignored."); |
77 | 0 | return; |
78 | |
} |
79 | 0 | CONFIGS.clear(); |
80 | 0 | initialized.reset(); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public static ContextualConfigLock getInitializedCondition() { |
88 | 0 | return initialized; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public synchronized static Config getCurrentContextConfig() { |
98 | 0 | return CONFIGS.get(Thread.currentThread().getContextClassLoader()); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public synchronized static Config getConfig(ClassLoader cl) { |
106 | 0 | return CONFIGS.get(cl); |
107 | |
} |
108 | |
|
109 | |
public synchronized static Object getObjectFromConfigHierarchy(String name) { |
110 | 0 | return getObjectFromClassLoader(name, ClassLoaderUtils.getDefaultClassLoader()); |
111 | |
} |
112 | |
|
113 | |
private static Object getObjectFromClassLoader(String name, ClassLoader classLoader) { |
114 | 0 | if (classLoader instanceof ConfigHolder) { |
115 | 0 | Object object = ((ConfigHolder)classLoader).getConfig().getObject(name); |
116 | 0 | if (object != null) { |
117 | 0 | return object; |
118 | |
} |
119 | 0 | } else { |
120 | 0 | Object object = getCurrentContextConfig().getObject(name); |
121 | 0 | if (object != null) { |
122 | 0 | return object; |
123 | |
} |
124 | 0 | return null; |
125 | |
} |
126 | 0 | return getObjectFromClassLoader(name, classLoader.getParent()); |
127 | |
} |
128 | |
|
129 | |
public static List<Object> getObjectsFromConfigHierarchy(String name) { |
130 | 0 | List<Object> objects = new ArrayList<Object>(); |
131 | 0 | getObjectFromClassLoader(name, ClassLoaderUtils.getDefaultClassLoader(), objects); |
132 | 0 | return objects; |
133 | |
} |
134 | |
|
135 | |
private static void getObjectFromClassLoader(String name, ClassLoader classLoader, List<Object> objects) { |
136 | 0 | if (classLoader instanceof ConfigHolder) { |
137 | 0 | Object object = ((ConfigHolder)classLoader).getConfig().getObject(name); |
138 | 0 | if (object != null) { |
139 | 0 | objects.add(object); |
140 | |
} |
141 | 0 | } else { |
142 | 0 | Object object = getCurrentContextConfig().getObject(name); |
143 | 0 | if (object != null) { |
144 | 0 | objects.add(object); |
145 | |
} |
146 | 0 | return; |
147 | |
} |
148 | 0 | getObjectFromClassLoader(name, classLoader.getParent(), objects); |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public static Set<Map.Entry<ClassLoader, Config>> getConfigs() { |
155 | 0 | return Collections.unmodifiableSet(CONFIGS.entrySet()); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public static void overrideConfig(ClassLoader cl, Config config) { |
164 | 0 | CONFIGS.put(cl, config); |
165 | 0 | } |
166 | |
} |