+
+ if (it == null)
+ {
+ LOG.error("cannot resolve {} as JSON (not found)!", parameter);
+ throw new RuntimeException("Template not found: " + parameter);
+ }
+ }
+
+ try
+ {
+ Map<String, Object> variables = getVariables(element);
+ if (merge)
+ {
+ while(it.hasNext())
+ {
+ Entry<String, Object> variable = it.next();
+ String key = variable.getKey();
+ Object value = variable.getValue();
+ Object existing = variables.get(key);
+ if (existing != null)
+ {
+ if (value instanceof String)
+ {
+ if (!(existing instanceof String))
+ {
+ LOG.error(
+ "cannot merge variable {} of type {} with a string",
+ key,
+ existing.getClass()
+ );
+ throw new RuntimeException(
+ "Type-Missmatch for variable " + key
+ );
+ }
+
+ String string = ((String)existing).concat((String) value);
+ LOG.info("appending variable to string {}", key);
+ element.setNodeLocalVariable(key, string);
+ }
+ else if (value instanceof Map)
+ {
+ if (!(existing instanceof Map))
+ {
+ LOG.error(
+ "cannot merge variable {} of type {} with a map",
+ key,
+ existing.getClass()
+ );
+ throw new RuntimeException(
+ "Type-Missmatch for variable " + key
+ );
+ }
+
+ Map map = ((Map)existing);
+ map.putAll((Map) value);
+ LOG.info("merging variable with map {}", key);
+ element.setNodeLocalVariable(key, map);
+ }
+ else if (value instanceof List)
+ {
+ if (!(existing instanceof List))
+ {
+ LOG.error(
+ "cannot merge variable {} of type {} with a list",
+ key,
+ existing.getClass()
+ );
+ throw new RuntimeException(
+ "Type-Missmatch for variable " + key
+ );
+ }
+
+ List list = ((List)existing);
+ list.addAll((List) value);
+ LOG.info("appending contents of variable to list {}", key);
+ element.setNodeLocalVariable(key, list);
+ }
+ else
+ {
+ LOG.error(
+ "variable {} is of unexpected type {}", key, value.getClass()
+ );
+ throw new RuntimeException(
+ "Found variable of unexpected type: " + key
+ );
+ }
+ }
+ else
+ {
+ LOG.info("adding new variable {}", key);
+ element.setNodeLocalVariable(key, value);
+ }
+ }
+ }
+ else
+ while(it.hasNext())
+ {
+ Entry<String, Object> variable = it.next();
+ String key = variable.getKey();
+ Object value = variable.getValue();
+ LOG.info("adding variable {}", key);
+ variables.put(key, value);
+ element.setNodeLocalVariable(key, value);
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ LOG.error("cannot parse {} as JSON: {}", parameter, e.getMessage());
+ throw new RuntimeException(e);