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