diff --git a/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java b/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java index 8e8377880b30abd115b75eab9104074b0f1d5504..6d4e8e7e2dc646549e318c12dae9d4328fb3ba4b 100644 --- a/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java +++ b/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java @@ -271,27 +271,36 @@ public class DefaultConfigurator implements Configurator { if (elem.getName().equalsIgnoreCase(FAKE_CONTAINER_ELEMENT)) { configureFirstLevel(elem); } else if (elem.getName().equals(Configurator.DEFAULT_TAG)) { - for (Iterator iter = elem - .elementIterator(); iter - .hasNext();) { - Element variable = (Element) iter.next(); - if (variable != null) { - String name = variable - .attributeValue(Configurator.VARIABLE_NAME_TAG); - String value = variable - .attributeValue(Configurator.VARIABLE_VALUE_TAG); - if (!variables.containsKey(name)) { - // set to default only if not set yet - variables.put(name, parseValue(value)); - } - } - } + configureDefaults(elem); } else { configureComponent(elem); } } } + /** + * Processes a Tag by setting the respective + * variables. + * + * @param defaultElement + */ + private void configureDefaults(Element elem) { + assert elem.getName().equals(Configurator.DEFAULT_TAG); + for (Iterator iter = elem.elementIterator(); iter.hasNext();) { + Element variable = (Element) iter.next(); + if (variable != null) { + String name = variable + .attributeValue(Configurator.VARIABLE_NAME_TAG); + String value = variable + .attributeValue(Configurator.VARIABLE_VALUE_TAG); + if (!variables.containsKey(name)) { + // set to default only if not set yet + variables.put(name, parseValue(value)); + } + } + } + } + /** * Create (if not existent yet) and configure a configurable component by * parsing the XML subtree. @@ -301,7 +310,7 @@ public class DefaultConfigurator implements Configurator { * @return configured component */ public Object configureComponent(Element elem) { - + String name = elem.getName(); if (Configurator.SPECIAL_IF_EQUAL_STR.equalsIgnoreCase(name)) { processIfEqualStr(elem, new ToConfigureCallback() { @@ -323,6 +332,18 @@ public class DefaultConfigurator implements Configurator { return null; } + /* + * FIXED (BR) Workaround to support DEFAULT-tags within IF-blocks (i.e., + * as non-first-level elements). The additional check for the CLASS-tag + * prevents setDefault setters with custom classes from stopping to + * work. + */ + if (Configurator.DEFAULT_TAG.equalsIgnoreCase(name) + && elem.attribute(CLASS_TAG) == null) { + configureDefaults(elem); + return null; + } + Monitor.log(DefaultConfigurator.class, Level.DEBUG, "Configure component " + name); @@ -343,8 +364,8 @@ public class DefaultConfigurator implements Configurator { if (component != null) { Monitor.log(DefaultConfigurator.class, Level.INFO, "Configure component " - + component.getClass().getSimpleName() + " with element " - + name); + + component.getClass().getSimpleName() + + " with element " + name); configureAttributes(component, elem, consAttrs); // configure subcomponents if (component instanceof Builder) { @@ -362,8 +383,8 @@ public class DefaultConfigurator implements Configurator { } } else { // component cannot be created and has not been registered - Monitor.log(DefaultConfigurator.class, Level.WARN, "Skip element " - + name); + Monitor.log(DefaultConfigurator.class, Level.WARN, + "Skip element " + name); } return component; } @@ -375,8 +396,7 @@ public class DefaultConfigurator implements Configurator { Object component; String className = getAttributeValue(elem.attribute(CLASS_TAG)); Monitor.log(DefaultConfigurator.class, Level.DEBUG, "Create component " - + className + " with element " - + elem.getName()); + + className + " with element " + elem.getName()); component = createInstance(className, getAttributeValue(elem.attribute(STATIC_CREATION_METHOD_TAG)), consAttrs, elem); @@ -442,29 +462,30 @@ public class DefaultConfigurator implements Configurator { for (int i = 0; i < methods.length; i++) { if (methodName.equals(methods[i].getName())) { match = methods[i]; - Monitor.log(DefaultConfigurator.class, Level.DEBUG, "Match " - + match); + Monitor.log(DefaultConfigurator.class, Level.DEBUG, + "Match " + match); break; } } if (match == null) { - Monitor.log(DefaultConfigurator.class, Level.WARN, "Cannot set " - + subcomponent + " as there is no method " - + methodName + " declared in " + component); - throw new ConfigurationException("Cannot set " + subcomponent - + " as there is no method " + methodName + " declared in " - + component); + Monitor.log(DefaultConfigurator.class, Level.WARN, + "Cannot set " + subcomponent + " as there is no method " + + methodName + " declared in " + component); + throw new ConfigurationException( + "Cannot set " + subcomponent + " as there is no method " + + methodName + " declared in " + component); } else { Class[] types = match.getParameterTypes(); - Monitor.log(DefaultConfigurator.class, Level.DEBUG, "Param types" - + Arrays.asList(types)); + Monitor.log(DefaultConfigurator.class, Level.DEBUG, + "Param types" + Arrays.asList(types)); if (types.length == 1) { try { match.invoke(component, types[0].cast(subcomponent)); } catch (Exception e) { - throw new ConfigurationException("Failed to configure " - + methodName + " in " + component + " with " - + subcomponent, e); + throw new ConfigurationException( + "Failed to configure " + methodName + " in " + + component + " with " + subcomponent, + e); } } else { throw new ConfigurationException("Wrong number of params for " @@ -507,11 +528,10 @@ public class DefaultConfigurator implements Configurator { } else { Monitor.log(DefaultConfigurator.class, Level.ERROR, - "Found two possible methods " - + method + " and " + methods[i]); + "Found two possible methods " + method + + " and " + methods[i]); throw new IllegalArgumentException( - "Cannot set property " - + name + "Cannot set property " + name + " as there are more than one matching methods in " + classToConfigure); } @@ -525,7 +545,8 @@ public class DefaultConfigurator implements Configurator { */ for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals("setProperty") - && methods[i].getParameterTypes().length == 2) { + && methods[i] + .getParameterTypes().length == 2) { method = methods[i]; } } @@ -541,8 +562,7 @@ public class DefaultConfigurator implements Configurator { * Still nothing? */ throw new IllegalArgumentException( - "Cannot set property " - + name + "Cannot set property " + name + " as there are no matching methods in class " + classToConfigure); } @@ -554,7 +574,8 @@ public class DefaultConfigurator implements Configurator { } catch (Exception e) { throw new ConfigurationException( "Failed to set the property " + name + " in " - + component, e); + + component, + e); } } } @@ -607,8 +628,8 @@ public class DefaultConfigurator implements Configurator { } param = lvals; } else { - throw new IllegalArgumentException("Parameter type " + typeClass - + " is not supported"); + throw new IllegalArgumentException( + "Parameter type " + typeClass + " is not supported"); } return param; } @@ -635,8 +656,8 @@ public class DefaultConfigurator implements Configurator { String value = attr.getValue(); value = parseValue(value); if (value == null) - throw new IllegalStateException("Variable " + attr.getValue() - + " has not been set"); + throw new IllegalStateException( + "Variable " + attr.getValue() + " has not been set"); return value; } @@ -738,12 +759,11 @@ public class DefaultConfigurator implements Configurator { break; } consArgs[i] = configureComponent(elem); - if (consArgs[i].getClass().isAssignableFrom( - types[i])) + if (consArgs[i].getClass() + .isAssignableFrom(types[i])) throw new ConfigurationException( "The type of the component configured for the parameter '" - + cArgs[i] - + "', type is " + + cArgs[i] + "', type is " + consArgs[i].getClass() .getSimpleName() + " and is not equal to the type " @@ -771,15 +791,16 @@ public class DefaultConfigurator implements Configurator { component = forName.newInstance(); } else - component = forName.getDeclaredMethod(staticMethod, - new Class[0]).invoke(null, new Object[0]); + component = forName + .getDeclaredMethod(staticMethod, new Class[0]) + .invoke(null, new Object[0]); componentList.add(component); return component; } catch (Exception e) { - throw new ConfigurationException("Failed to create configurable " - + className, e); + throw new ConfigurationException( + "Failed to create configurable " + className, e); } } @@ -791,8 +812,8 @@ public class DefaultConfigurator implements Configurator { */ public void setVariables(Map variables) { if (variables.size() != 0) { - Monitor.log(DefaultConfigurator.class, Level.WARN, "Set variables " - + variables); + Monitor.log(DefaultConfigurator.class, Level.WARN, + "Set variables " + variables); } this.variables.putAll(variables); } @@ -835,8 +856,8 @@ public class DefaultConfigurator implements Configurator { factor *= Time.SECOND; break; default: - throw new IllegalStateException("time unit " + unit - + " is not allowed"); + throw new IllegalStateException( + "time unit " + unit + " is not allowed"); } } } @@ -870,7 +891,7 @@ public class DefaultConfigurator implements Configurator { throw new IllegalStateException("Invalid memory unit."); } } - + // Convert type double dNum = Double.valueOf(number); if (targetClass.equals(Integer.class)) { @@ -907,9 +928,11 @@ public class DefaultConfigurator implements Configurator { String arg1p = parseValue(arg1); if (arg0p == null) - throw new RuntimeException("Variable " + arg0 + " not set or null."); + throw new RuntimeException( + "Variable " + arg0 + " not set or null."); if (arg1p == null) - throw new RuntimeException("Variable " + arg1 + " not set or null."); + throw new RuntimeException( + "Variable " + arg1 + " not set or null."); if (arg0p.equals(arg1p)) { @@ -917,7 +940,7 @@ public class DefaultConfigurator implements Configurator { if (!iter.hasNext()) Monitor.log(DefaultConfigurator.class, Level.WARN, "No component to configure in the ifEqualStr-clause (arg0=" - + arg0 + ", arg1=" + arg1 + ")."); + + arg0 + ", arg1=" + arg1 + ")."); else { while (iter.hasNext()) { Element child = (Element) iter.next(); @@ -947,9 +970,11 @@ public class DefaultConfigurator implements Configurator { String arg1p = parseValue(arg1); if (arg0p == null) - throw new RuntimeException("Variable " + arg0 + " not set or null."); + throw new RuntimeException( + "Variable " + arg0 + " not set or null."); if (arg1p == null) - throw new RuntimeException("Variable " + arg1 + " not set or null."); + throw new RuntimeException( + "Variable " + arg1 + " not set or null."); if (!arg0p.equals(arg1p)) { @@ -957,7 +982,7 @@ public class DefaultConfigurator implements Configurator { if (!iter.hasNext()) Monitor.log(DefaultConfigurator.class, Level.WARN, "No component to configure in the ifNotEqualStr-clause (arg0=" - + arg0 + ", arg1=" + arg1 + ")."); + + arg0 + ", arg1=" + arg1 + ")."); else { while (iter.hasNext()) { Element child = (Element) iter.next(); @@ -967,8 +992,6 @@ public class DefaultConfigurator implements Configurator { } } - - /** * Gets the parsed XML-Configuration File. So are the included tags * resolved. @@ -1006,16 +1029,16 @@ public class DefaultConfigurator implements Configurator { return "Cannot parse/read the configuration"; } - @Override - public List getConfigurable(Class type) { - List components = Lists.newArrayList(); - for (Object obj : configurables.values()) { - if (type.isAssignableFrom(obj.getClass())) { - components.add(obj); - } - } - return components; - } + @Override + public List getConfigurable(Class type) { + List components = Lists.newArrayList(); + for (Object obj : configurables.values()) { + if (type.isAssignableFrom(obj.getClass())) { + components.add(obj); + } + } + return components; + } protected interface ToConfigureCallback { public void run(Element elemToConfigure);