Coverage Report - org.kuali.rice.core.api.config.property.ConfigContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigContext
0%
0/47
0%
0/14
1.923
 
 1  
 /*
 2  
  * Copyright 2006-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  
 
 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  
  * Singleton that holds references to global engine objects.
 31  
  *
 32  
  *
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  */
 35  
 public class ConfigContext {
 36  0
     private static final Logger LOG = Logger.getLogger(ConfigContext.class);
 37  
 
 38  
     /**
 39  
      * Concurrency utility which allows other, loosely coupled, components to wait for configuration initialization
 40  
      * to complete before proceeding (namely the SpringServiceLocator, before it initializes Spring)
 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  
         // nothing to do here
 47  0
     }
 48  
 
 49  
     /**
 50  
      * Perform a one-time initialization of the Config system.  This should only be performed by the applicable LifeCycle
 51  
      * implementation.
 52  
      * @param rootCfg the root config
 53  
      */
 54  
     public static void init(Config rootCfg) {
 55  0
             init(Thread.currentThread().getContextClassLoader(), rootCfg);
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Initializes the ConfigContext with the given Config and binds it to the given ClassLoader.
 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  
      * Destroy method (mostly to aid testing, as core needs to be torn down appropriately).
 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  
      * Returns the Condition that allows waiting on configuration to complete
 80  
      * @return the Condition that allows waiting on configuration to complete
 81  
      */
 82  
     public static ContextualConfigLock getInitializedCondition() {
 83  0
         return initialized;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Utility method that all code should call to obtain its appropriate Config object.
 88  
      * The Config object which is associated with the caller's context classloader will be
 89  
      * returned, being created first if it does not yet exist.
 90  
      * @return the Config object which is associated with the caller's context classloader
 91  
      */
 92  
     public synchronized static Config getCurrentContextConfig() {
 93  0
             return CONFIGS.get(Thread.currentThread().getContextClassLoader());
 94  
     }
 95  
 
 96  
     /**
 97  
      * @param cl the classloader whose Config to return
 98  
      * @return the Config of a particular class loader
 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  
      * @return an immutable view of the Configs entry set
 148  
      */
 149  
     public static Set<Map.Entry<ClassLoader, Config>> getConfigs() {
 150  0
         return Collections.unmodifiableSet(CONFIGS.entrySet());
 151  
     }
 152  
 
 153  
     /**
 154  
      * Overrides any existing Config for the classloader
 155  
      * @param cl the classloader whose Config should be overridden
 156  
      * @param config the config
 157  
      */
 158  
     public static void overrideConfig(ClassLoader cl, Config config) {
 159  0
         CONFIGS.put(cl, config);
 160  0
     }
 161  
 }