Coverage Report - org.kuali.rice.core.config.ConfigContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigContext
0%
0/52
0%
0/16
1.933
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.config;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.apache.log4j.Logger;
 28  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 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 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 static void init(ClassLoader classLoader, Config config) {
 63  0
             CONFIGS.put(classLoader, config);
 64  0
             initialized.fire();
 65  0
     }
 66  
 
 67  
     /**
 68  
      * Destroy method (mostly to aid testing, as core needs to be torn down appropriately).
 69  
      */
 70  
     public static void destroy() {
 71  0
         if (!initialized.hasFired()) {
 72  0
             LOG.warn("Destroy on un-initialized ConfigContext was ignored.");
 73  0
             return;
 74  
         }
 75  0
         CONFIGS.clear();
 76  0
         initialized.reset();
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Returns the "root" Config object
 81  
      * @return the "root" Config object
 82  
      * @deprecated this really isn't working as intended at the moment but it still needs to work the concept of a root config
 83  
      *         may need to go away
 84  
      */
 85  
     public static Config getRootConfig() {
 86  0
             return getCurrentContextConfig();
 87  
     }
 88  
 
 89  
     /**
 90  
      * Returns the Condition that allows waiting on configuration to complete
 91  
      * @return the Condition that allows waiting on configuration to complete
 92  
      */
 93  
     public static ContextualConfigLock getInitializedCondition() {
 94  0
         return initialized;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Runs a series of validation checks against the core configuration to ensure that required properties
 99  
      * are present.  For now, this just validates the service namespace.
 100  
      */
 101  
     public static void validateCoreConfiguration() {
 102  0
             Config config = getRootConfig();
 103  0
             if (StringUtils.isEmpty(config.getServiceNamespace())) {
 104  0
                     throw new ConfigurationException("The " + Config.SERVICE_NAMESPACE + " configuration parameter is required.");
 105  
             }
 106  0
     }
 107  
 
 108  
 
 109  
 
 110  
     /**
 111  
      * Utility method that all code should call to obtain its appropriate Config object.
 112  
      * The Config object which is associated with the caller's context classloader will be
 113  
      * returned, being created first if it does not yet exist.
 114  
      * @return the Config object which is associated with the caller's context classloader
 115  
      */
 116  
     public synchronized static Config getCurrentContextConfig() {
 117  0
             return CONFIGS.get(Thread.currentThread().getContextClassLoader());
 118  
     }
 119  
 
 120  
     /**
 121  
      * @param cl the classloader whose Config to return
 122  
      * @return the Config of a particular class loader
 123  
      */
 124  
     public synchronized static Config getConfig(ClassLoader cl) {
 125  0
         return CONFIGS.get(cl);
 126  
     }
 127  
 
 128  
     public synchronized static Object getObjectFromConfigHierarchy(String name) {
 129  0
             return getObjectFromClassLoader(name, ClassLoaderUtils.getDefaultClassLoader());
 130  
     }
 131  
 
 132  
     private static Object getObjectFromClassLoader(String name, ClassLoader classLoader) {
 133  0
             if (classLoader instanceof ConfigHolder) {
 134  0
                     Object object = ((ConfigHolder)classLoader).getConfig().getObject(name);
 135  0
                     if (object != null) {
 136  0
                             return object;
 137  
                     }
 138  0
             } else {
 139  0
                     Object object = getRootConfig().getObject(name);
 140  0
                     if (object != null) {
 141  0
                             return object;
 142  
                     }
 143  0
                     return null;
 144  
             }
 145  0
             return getObjectFromClassLoader(name, classLoader.getParent());
 146  
     }
 147  
 
 148  
     public static List<Object> getObjectsFromConfigHierarchy(String name) {
 149  0
             List<Object> objects = new ArrayList<Object>();
 150  0
             getObjectFromClassLoader(name, ClassLoaderUtils.getDefaultClassLoader(), objects);
 151  0
             return objects;
 152  
     }
 153  
 
 154  
     private static void getObjectFromClassLoader(String name, ClassLoader classLoader, List<Object> objects) {
 155  0
             if (classLoader instanceof ConfigHolder) {
 156  0
                     Object object = ((ConfigHolder)classLoader).getConfig().getObject(name);
 157  0
                     if (object != null) {
 158  0
                             objects.add(object);
 159  
                     }
 160  0
             } else {
 161  0
                     Object object = getRootConfig().getObject(name);
 162  0
                     if (object != null) {
 163  0
                             objects.add(object);
 164  
                     }
 165  0
                     return;
 166  
             }
 167  0
             getObjectFromClassLoader(name, classLoader.getParent(), objects);
 168  0
     }
 169  
 
 170  
     /**
 171  
      * @return an immutable view of the Configs entry set
 172  
      */
 173  
     public static Set<Map.Entry<ClassLoader, Config>> getConfigs() {
 174  0
         return Collections.unmodifiableSet(CONFIGS.entrySet());
 175  
     }
 176  
 
 177  
     /**
 178  
      * Overrides any existing Config for the classloader
 179  
      * @param cl the classloader whose Config should be overridden
 180  
      * @param config the config
 181  
      */
 182  
     public static void overrideConfig(ClassLoader cl, Config config) {
 183  0
         CONFIGS.put(cl, config);
 184  0
     }
 185  
 }