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.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 synchronized static void init(Config rootCfg) { |
55 | 0 | init(Thread.currentThread().getContextClassLoader(), rootCfg); |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public synchronized static void init(ClassLoader classLoader, Config config) { |
62 | 0 | CONFIGS.put(classLoader, config); |
63 | 0 | initialized.fire(); |
64 | 0 | } |
65 | |
|
66 | |
public synchronized static boolean isInitialized() { |
67 | 0 | return initialized.hasFired(); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public synchronized static void destroy() { |
74 | 0 | if (!initialized.hasFired()) { |
75 | 0 | LOG.warn("Destroy on un-initialized ConfigContext was ignored."); |
76 | 0 | return; |
77 | |
} |
78 | 0 | CONFIGS.clear(); |
79 | 0 | initialized.reset(); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public static ContextualConfigLock getInitializedCondition() { |
87 | 0 | return initialized; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public synchronized static Config getCurrentContextConfig() { |
97 | 0 | return CONFIGS.get(Thread.currentThread().getContextClassLoader()); |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public synchronized static Config getConfig(ClassLoader cl) { |
105 | 0 | return CONFIGS.get(cl); |
106 | |
} |
107 | |
|
108 | |
public synchronized static Object getObjectFromConfigHierarchy(String name) { |
109 | 0 | return getObjectFromClassLoader(name, ClassLoaderUtils.getDefaultClassLoader()); |
110 | |
} |
111 | |
|
112 | |
private static Object getObjectFromClassLoader(String name, ClassLoader classLoader) { |
113 | 0 | if (classLoader instanceof ConfigHolder) { |
114 | 0 | Object object = ((ConfigHolder)classLoader).getConfig().getObject(name); |
115 | 0 | if (object != null) { |
116 | 0 | return object; |
117 | |
} |
118 | 0 | } else { |
119 | 0 | Object object = getCurrentContextConfig().getObject(name); |
120 | 0 | if (object != null) { |
121 | 0 | return object; |
122 | |
} |
123 | 0 | return null; |
124 | |
} |
125 | 0 | return getObjectFromClassLoader(name, classLoader.getParent()); |
126 | |
} |
127 | |
|
128 | |
public static List<Object> getObjectsFromConfigHierarchy(String name) { |
129 | 0 | List<Object> objects = new ArrayList<Object>(); |
130 | 0 | getObjectFromClassLoader(name, ClassLoaderUtils.getDefaultClassLoader(), objects); |
131 | 0 | return objects; |
132 | |
} |
133 | |
|
134 | |
private static void getObjectFromClassLoader(String name, ClassLoader classLoader, List<Object> objects) { |
135 | 0 | if (classLoader instanceof ConfigHolder) { |
136 | 0 | Object object = ((ConfigHolder)classLoader).getConfig().getObject(name); |
137 | 0 | if (object != null) { |
138 | 0 | objects.add(object); |
139 | |
} |
140 | 0 | } else { |
141 | 0 | Object object = getCurrentContextConfig().getObject(name); |
142 | 0 | if (object != null) { |
143 | 0 | objects.add(object); |
144 | |
} |
145 | 0 | return; |
146 | |
} |
147 | 0 | getObjectFromClassLoader(name, classLoader.getParent(), objects); |
148 | 0 | } |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public static Set<Map.Entry<ClassLoader, Config>> getConfigs() { |
154 | 0 | return Collections.unmodifiableSet(CONFIGS.entrySet()); |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public static void overrideConfig(ClassLoader cl, Config config) { |
163 | 0 | CONFIGS.put(cl, config); |
164 | 0 | } |
165 | |
} |