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.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 synchronized 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 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  
      * Destroy method (mostly to aid testing, as core needs to be torn down appropriately).
 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  
      * Returns the Condition that allows waiting on configuration to complete
 84  
      * @return the Condition that allows waiting on configuration to complete
 85  
      */
 86  
     public static ContextualConfigLock getInitializedCondition() {
 87  0
         return initialized;
 88  
     }
 89  
 
 90  
     /**
 91  
      * Utility method that all code should call to obtain its appropriate Config object.
 92  
      * The Config object which is associated with the caller's context classloader will be
 93  
      * returned, being created first if it does not yet exist.
 94  
      * @return the Config object which is associated with the caller's context classloader
 95  
      */
 96  
     public synchronized static Config getCurrentContextConfig() {
 97  0
             return CONFIGS.get(Thread.currentThread().getContextClassLoader());
 98  
     }
 99  
 
 100  
     /**
 101  
      * @param cl the classloader whose Config to return
 102  
      * @return the Config of a particular class loader
 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  
      * @return an immutable view of the Configs entry set
 152  
      */
 153  
     public static Set<Map.Entry<ClassLoader, Config>> getConfigs() {
 154  0
         return Collections.unmodifiableSet(CONFIGS.entrySet());
 155  
     }
 156  
 
 157  
     /**
 158  
      * Overrides any existing Config for the classloader
 159  
      * @param cl the classloader whose Config should be overridden
 160  
      * @param config the config
 161  
      */
 162  
     public static void overrideConfig(ClassLoader cl, Config config) {
 163  0
         CONFIGS.put(cl, config);
 164  0
     }
 165  
 }