Exemplo n.º 1
0
 /**
  * Returns a shallow copy of this ConfigObject, keys and configuration entries are not cloned.
  *
  * @return a shallow copy of this ConfigObject
  */
 public ConfigObject clone() {
   try {
     ConfigObject clone = (ConfigObject) super.clone();
     clone.configFile = configFile;
     clone.delegateMap = (LinkedHashMap) delegateMap.clone();
     return clone;
   } catch (CloneNotSupportedException e) {
     throw new AssertionError();
   }
 }
Exemplo n.º 2
0
  @SuppressWarnings("unchecked")
  void loadConfig(String location) {
    URL url = null;
    boolean throwException = false;
    try {
      url = new URL(location);
    } catch (MalformedURLException e) {
      File testConfigFile = new File(location);
      if (!testConfigFile.exists()) throwException = true;
      else {
        try {
          url = testConfigFile.toURI().toURL();
        } catch (MalformedURLException e1) {
          throwException = true;
        }
      }
    }

    if (throwException) {
      throw new RuntimeException(
          "Cannot load ["
              + location
              + "], it is not found or your location of the file "
              + "is incorrect. You have declared that your test ["
              + testClassName
              + "] requires "
              + "a configuration file, but the file cannot be loaded. Check the setting of the "
              + "org.rioproject.test.config system property");
    }
    ConfigObject config = new ConfigSlurper().parse(url);
    Map<String, Object> configMap = config.flatten();

    if (hasConfigurationFor(component, configMap)) {
      groups = getString(configMap.get(component + ".groups"));
      if (groups != null) System.setProperty(Constants.GROUPS_PROPERTY_NAME, groups);
      locators = getString(configMap.get(component + ".locators"));
      if (locators != null) System.setProperty(Constants.LOCATOR_PROPERTY_NAME, locators);
      numCybernodes = (Integer) configMap.get(component + ".numCybernodes");
      numMonitors = (Integer) configMap.get(component + ".numMonitors");
      numLookups = (Integer) configMap.get(component + ".numLookups");
      opString = getString(configMap.get(component + ".opstring"));
      Boolean b = (Boolean) configMap.get(component + ".autoDeploy");
      autoDeploy = b != null && b;
      testManager = (TestManager) configMap.get(component + ".testManager");
      b = (Boolean) configMap.get(component + ".harvest");
      runHarvester = b != null && b;
      String sTimeout = getString(configMap.get(component + ".timeout"));
      timeout = sTimeout == null ? 0 : Long.parseLong(sTimeout);
      loggingSystem = (LoggingSystem) configMap.get(component + ".loggingSystem");
      if (loggingSystem == null) {
        loggingSystem = LoggingSystem.LOGBACK;
      }
    }
  }
 public void configChanged() {
   ConfigObject co = getConfig();
   // not thread safe
   flatConfig = co.flatten();
   final ArtefactHandler[] handlers = getArtefactHandlers();
   for (ArtefactHandler handler : handlers) {
     if (handler instanceof GrailsConfigurationAware) {
       ((GrailsConfigurationAware) handler).setConfiguration(co);
     }
   }
 }
 public void setConfig(ConfigObject config) {
   this.config = config;
   if (config == null) {
     flatConfig = Collections.emptyMap();
   } else {
     flatConfig = config.flatten();
   }
 }
  /**
   * @param co current config object which is queried
   * @param currentNamePart the queried property name
   * @return the value for given property name
   * @throws ObjectNotFoundException
   */
  private Object configObjectGet(ConfigObject co, String currentNamePart) {

    /*
     * get the property value
     */
    Object result = co.getProperty(currentNamePart);

    if (result instanceof ConfigObject) {
      // try if property value is empty
      ConfigObject coResult = (ConfigObject) result;
      if (coResult.size() == 0) {
        throw new ObjectNotFoundException();
      }
    } else {
      result = resolvePropObject(result);
    } // fi
    return result;
  }
Exemplo n.º 6
0
  @Override
  public ConfigObject getAsConfig(String configId) {
    if (configId == null) {
      if (this.configId == null) {
        throw new IllegalArgumentException("Can't derive configId (none was supplied)");
      } else {
        configId = this.configId;
      }
    }

    ConfigObject root = new ConfigObject();
    ConfigObject filterAliases = (ConfigObject) root.getProperty(FILTERS_CONFIG_KEY);
    if (noRegex) {
      ConfigObject filterConfig = (ConfigObject) filterAliases.getProperty(configId);
      filterConfig.put(FILTERS_CONFIG_VALUE_KEY, givenFilterPattern);
      filterConfig.put(FILTERS_CONFIG_REGEX_KEY, noRegex);
    } else {
      filterAliases.put(configId, givenFilterPattern);
    }
    return root;
  }
 /**
  * merge two config objects. If both of config objects contian the same property key, then the
  * value of <code>highPriorityCO</code> is propagated to the result.
  *
  * @param lowPriorityCO
  * @param highPriorityCO
  * @return the merged version of two config objects.
  */
 static ConfigObject mergeConfigObjects(ConfigObject lowPriorityCO, ConfigObject highPriorityCO) {
   return (ConfigObject) lowPriorityCO.merge(highPriorityCO);
 }
 @SuppressWarnings("unchecked")
 public static Object instantiateFromConfig(
     ConfigObject config, String configKey, String defaultClassName)
     throws InstantiationException, IllegalAccessException, ClassNotFoundException, LinkageError {
   return instantiateFromFlatConfig(config.flatten(), configKey, defaultClassName);
 }
Exemplo n.º 9
0
  private void writeConfig(
      String prefix, ConfigObject map, BufferedWriter out, int tab, boolean apply)
      throws IOException {
    String space = apply ? StringGroovyMethods.multiply(TAB_CHARACTER, tab) : "";

    for (Object o1 : map.keySet()) {
      String key = (String) o1;
      Object v = map.get(key);

      if (v instanceof ConfigObject) {
        ConfigObject value = (ConfigObject) v;

        if (!value.isEmpty()) {

          Object dotsInKeys = null;
          for (Object o : value.entrySet()) {
            Entry e = (Entry) o;
            String k = (String) e.getKey();
            if (k.indexOf('.') > -1) {
              dotsInKeys = e;
              break;
            }
          }

          int configSize = value.size();
          Object firstKey = value.keySet().iterator().next();
          Object firstValue = value.values().iterator().next();

          int firstSize;
          if (firstValue instanceof ConfigObject) {
            firstSize = ((ConfigObject) firstValue).size();
          } else {
            firstSize = 1;
          }

          if (configSize == 1 || DefaultGroovyMethods.asBoolean(dotsInKeys)) {
            if (firstSize == 1 && firstValue instanceof ConfigObject) {
              key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key;
              String writePrefix = prefix + key + "." + firstKey + ".";
              writeConfig(writePrefix, (ConfigObject) firstValue, out, tab, true);
            } else if (!DefaultGroovyMethods.asBoolean(dotsInKeys)
                && firstValue instanceof ConfigObject) {
              writeNode(key, space, tab, value, out);
            } else {
              for (Object j : value.keySet()) {
                Object v2 = value.get(j);
                Object k2 = ((String) j).indexOf('.') > -1 ? InvokerHelper.inspect(j) : j;
                if (v2 instanceof ConfigObject) {
                  key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key;
                  writeConfig(prefix + key, (ConfigObject) v2, out, tab, false);
                } else {
                  writeValue(key + "." + k2, space, prefix, v2, out);
                }
              }
            }
          } else {
            writeNode(key, space, tab, value, out);
          }
        }
      } else {
        writeValue(key, space, prefix, v, out);
      }
    }
  }