Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConfigContext |
|
| 1.0;1 |
1 | /** | |
2 | * Copyright 2005-2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.core.api.config.property; | |
17 | ||
18 | import java.util.Collections; | |
19 | import java.util.Map; | |
20 | import java.util.Set; | |
21 | import java.util.concurrent.ConcurrentHashMap; | |
22 | ||
23 | /** | |
24 | * Singleton that holds references to global engine objects. | |
25 | * | |
26 | * | |
27 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
28 | */ | |
29 | public class ConfigContext { | |
30 | 0 | private static final ConcurrentHashMap<ClassLoader, Config> CONFIGS = new ConcurrentHashMap<ClassLoader, Config>(); |
31 | ||
32 | 0 | private ConfigContext() { |
33 | // nothing to do here | |
34 | 0 | } |
35 | ||
36 | /** | |
37 | * Perform a one-time initialization of the Config system. This should only be performed by the applicable LifeCycle | |
38 | * implementation. | |
39 | * @param rootCfg the root config | |
40 | */ | |
41 | public static void init(Config rootCfg) { | |
42 | 0 | init(Thread.currentThread().getContextClassLoader(), rootCfg); |
43 | 0 | } |
44 | ||
45 | /** | |
46 | * Initializes the ConfigContext with the given Config and binds it to the given ClassLoader. | |
47 | */ | |
48 | public static void init(ClassLoader classLoader, Config config) { | |
49 | 0 | CONFIGS.put(classLoader, config); |
50 | 0 | } |
51 | ||
52 | public static boolean isInitialized() { | |
53 | 0 | return !CONFIGS.isEmpty(); |
54 | } | |
55 | ||
56 | /** | |
57 | * Destroy method (mostly to aid testing, as core needs to be torn down appropriately). | |
58 | */ | |
59 | public static void destroy() { | |
60 | 0 | CONFIGS.clear(); |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * Utility method that all code should call to obtain its appropriate Config object. | |
65 | * The Config object which is associated with the caller's context classloader will be | |
66 | * returned, being created first if it does not yet exist. | |
67 | * @return the Config object which is associated with the caller's context classloader | |
68 | */ | |
69 | public static Config getCurrentContextConfig() { | |
70 | 0 | return getConfig(Thread.currentThread().getContextClassLoader()); |
71 | } | |
72 | ||
73 | /** | |
74 | * Returns the Config which is bound to the given class loader. If no configuration is bound to the given class | |
75 | * loader this method will return null; | |
76 | * | |
77 | * @param cl the classloader whose Config to return | |
78 | * @return the Config of a particular class loader, or null if no config is bound to the given class loader | |
79 | */ | |
80 | public static Config getConfig(ClassLoader cl) { | |
81 | 0 | return CONFIGS.get(cl); |
82 | } | |
83 | ||
84 | /** | |
85 | * @return an immutable view of the Configs entry set | |
86 | */ | |
87 | public static Set<Map.Entry<ClassLoader, Config>> getConfigs() { | |
88 | 0 | return Collections.unmodifiableSet(CONFIGS.entrySet()); |
89 | } | |
90 | ||
91 | /** | |
92 | * Overrides any existing Config for the classloader | |
93 | * @param cl the classloader whose Config should be overridden | |
94 | * @param config the config | |
95 | */ | |
96 | public static void overrideConfig(ClassLoader cl, Config config) { | |
97 | 0 | CONFIGS.put(cl, config); |
98 | 0 | } |
99 | } |