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