/** * Create the Route. * @param appenderRef The Appender reference. * @param key The key. * @param node The Node. * @return A Route. */ @PluginFactory public static Route createRoute( @PluginAttribute("ref") final String appenderRef, @PluginAttribute("key") final String key, @PluginNode final Node node) { if (node != null && node.hasChildren()) { for (final Node child : node.getChildren()) { } if (appenderRef != null) { LOGGER.error("A route cannot be configured with an appender reference and an appender definition"); return null; } } else { if (appenderRef == null) { LOGGER.error("A route must specify an appender reference or an appender definition"); return null; } } return new Route(node, appenderRef, key); }
private Appender createAppender(final Route route, final LogEvent event) { final Node routeNode = route.getNode(); for (final Node node : routeNode.getChildren()) { if (node.getType().getElementName().equals("appender")) { final Node appNode = new Node(node); config.createConfiguration(appNode, event); if (appNode.getObject() instanceof Appender) { final Appender app = (Appender) appNode.getObject(); app.start(); return app; } LOGGER.error("Unable to create Appender of type " + node.getName()); return null; } } LOGGER.error("No Appender was configured for route " + route.getKey()); return null; }
@Override public void setup() { final Iterator<Map.Entry<String, JsonNode>> iter = root.fields(); final List<Node> children = rootNode.getChildren(); while (iter.hasNext()) { final Map.Entry<String, JsonNode> entry = iter.next(); final JsonNode n = entry.getValue(); if (n.isObject()) { LOGGER.debug("Processing node for object {}", entry.getKey()); children.add(constructNode(entry.getKey(), rootNode, n)); } else if (n.isArray()) { LOGGER.error("Arrays are not supported at the root configuration."); } } LOGGER.debug("Completed parsing configuration"); if (status.size() > 0) { for (final Status s : status) { LOGGER.error("Error processing element {}: {}", s.name, s.errorType); } } }
private String getType(final Element element) { if (strict) { final NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { final org.w3c.dom.Node w3cNode = attrs.item(i); if (w3cNode instanceof Attr) { final Attr attr = (Attr) w3cNode; if (attr.getName().equalsIgnoreCase("type")) { final String type = attr.getValue(); attrs.removeNamedItem(attr.getName()); return type; } } } } return element.getTagName(); }
private Map<String, String> processAttributes(final Node node, final Element element) { final NamedNodeMap attrs = element.getAttributes(); final Map<String, String> attributes = node.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { final org.w3c.dom.Node w3cNode = attrs.item(i); if (w3cNode instanceof Attr) { final Attr attr = (Attr) w3cNode; if (attr.getName().equals("xml:base")) { continue; } attributes.put(attr.getName(), attr.getValue()); } } return attributes; }
@Override public void setup() { final List<Node> children = rootNode.getChildren(); if (propertiesComponent.getComponents().size() > 0) { children.add(convertToNode(rootNode, propertiesComponent)); } if (scriptsComponent.getComponents().size() > 0) { children.add(convertToNode(rootNode, scriptsComponent)); } if (customLevelsComponent.getComponents().size() > 0) { children.add(convertToNode(rootNode, customLevelsComponent)); } children.add(convertToNode(rootNode, loggersComponent)); children.add(convertToNode(rootNode, appendersComponent)); if (filtersComponent.getComponents().size() > 0) { if (filtersComponent.getComponents().size() == 1) { children.add(convertToNode(rootNode, filtersComponent.getComponents().get(0))); } else { children.add(convertToNode(rootNode, filtersComponent)); } } rootComponent = null; }
/** * Create the Route. * @param appenderRef The Appender reference. * @param key The key. * @param node The Node. * @return A Route. */ @PluginFactory public static Route createRoute( @PluginAttribute("ref") final String appenderRef, @PluginAttribute("key") final String key, @PluginNode final Node node) { if (node != null && node.hasChildren()) { if (appenderRef != null) { LOGGER.error("A route cannot be configured with an appender reference and an appender definition"); return null; } } else { if (appenderRef == null) { LOGGER.error("A route must specify an appender reference or an appender definition"); return null; } } return new Route(node, appenderRef, key); }
private Appender createAppender(final Route route, final LogEvent event) { final Node routeNode = route.getNode(); for (final Node node : routeNode.getChildren()) { if (node.getType().getElementName().equals(Appender.ELEMENT_TYPE)) { final Node appNode = new Node(node); configuration.createConfiguration(appNode, event); if (appNode.getObject() instanceof Appender) { final Appender app = appNode.getObject(); app.start(); return app; } error("Unable to create Appender of type " + node.getName()); return null; } } error("No Appender was configured for route " + route.getKey()); return null; }
public Appender createAppender(final String appenderName, final String actualName) { final Node node = nodeMap.get(appenderName); if (node == null) { LOGGER.error("No node named {} in {}", appenderName, this); return null; } node.getAttributes().put("name", actualName); if (node.getType().getElementName().equals(Appender.ELEMENT_TYPE)) { final Node appNode = new Node(node); configuration.createConfiguration(appNode, null); if (appNode.getObject() instanceof Appender) { final Appender app = appNode.getObject(); app.start(); return app; } LOGGER.error("Unable to create Appender of type " + node.getName()); return null; } LOGGER.error("No Appender was configured for name {} " + appenderName); return null; }
private void processAttributes(final Node parent, final JsonNode node) { final Map<String, String> attrs = parent.getAttributes(); final Iterator<Map.Entry<String, JsonNode>> iter = node.fields(); while (iter.hasNext()) { final Map.Entry<String, JsonNode> entry = iter.next(); if (!entry.getKey().equalsIgnoreCase("type")) { final JsonNode n = entry.getValue(); if (n.isValueNode()) { attrs.put(entry.getKey(), n.asText()); } } } }
private void constructHierarchy(final Node node, final Element element) { processAttributes(node, element); final StringBuilder buffer = new StringBuilder(); final NodeList list = element.getChildNodes(); final List<Node> children = node.getChildren(); for (int i = 0; i < list.getLength(); i++) { final org.w3c.dom.Node w3cNode = list.item(i); if (w3cNode instanceof Element) { final Element child = (Element) w3cNode; final String name = getType(child); final PluginType<?> type = pluginManager.getPluginType(name); final Node childNode = new Node(node, name, type); constructHierarchy(childNode, child); if (type == null) { final String value = childNode.getValue(); if (!childNode.hasChildren() && value != null) { node.getAttributes().put(name, value); } else { status.add(new Status(name, element, ErrorType.CLASS_NOT_FOUND)); } } else { children.add(childNode); } } else if (w3cNode instanceof Text) { final Text data = (Text) w3cNode; buffer.append(data.getData()); } } final String text = buffer.toString().trim(); if (text.length() > 0 || (!node.hasChildren() && !node.isRoot())) { node.setValue(text); } }
private void printNodes(final String indent, final Node node, final StringBuilder sb) { sb.append(indent).append(node.getName()).append(" type: ").append(node.getType()).append("\n"); sb.append(indent).append(node.getAttributes().toString()).append("\n"); for (final Node child : node.getChildren()) { printNodes(indent + " ", child, sb); } }
/** * Merge the root properties. * @param rootNode The composite root node. * @param configuration The configuration to merge. */ @Override public void mergeRootProperties(final Node rootNode, final AbstractConfiguration configuration) { for (final Map.Entry<String, String> attribute : configuration.getRootNode().getAttributes().entrySet()) { boolean isFound = false; for (final Map.Entry<String, String> targetAttribute : rootNode.getAttributes().entrySet()) { if (targetAttribute.getKey().equalsIgnoreCase(attribute.getKey())) { if (attribute.getKey().equalsIgnoreCase(STATUS)) { final Level targetLevel = Level.getLevel(targetAttribute.getValue().toUpperCase()); final Level sourceLevel = Level.getLevel(attribute.getValue().toUpperCase()); if (targetLevel != null && sourceLevel != null) { if (sourceLevel.isLessSpecificThan(targetLevel)) { targetAttribute.setValue(attribute.getValue()); } } else if (sourceLevel != null) { targetAttribute.setValue(attribute.getValue()); } } else { if (attribute.getKey().equalsIgnoreCase("monitorInterval")) { final int sourceInterval = Integer.parseInt(attribute.getValue()); final int targetInterval = Integer.parseInt(targetAttribute.getValue()); if (targetInterval == 0 || sourceInterval < targetInterval) { targetAttribute.setValue(attribute.getValue()); } } else { targetAttribute.setValue(attribute.getValue()); } } isFound = true; } } if (!isFound) { rootNode.getAttributes().put(attribute.getKey(), attribute.getValue()); } } }
private Node getLoggerNode(final Node parentNode, final String name) { for (final Node node : parentNode.getChildren()) { final String nodeName = node.getAttributes().get(NAME); if (name == null && nodeName == null) { return node; } if (nodeName != null && nodeName.equals(name)) { return node; } } return null; }
protected Node convertToNode(final Node parent, final Component component) { final String name = component.getPluginType(); final PluginType<?> pluginType = pluginManager.getPluginType(name); final Node node = new Node(parent, name, pluginType); node.getAttributes().putAll(component.getAttributes()); node.setValue(component.getValue()); final List<Node> children = node.getChildren(); for (final Component child : component.getComponents()) { children.add(convertToNode(node, child)); } return node; }
private Node findNamedNode(final String name, final Iterable<Node> children) { for (final Node child : children) { final PluginType<?> childType = child.getType(); if (childType == null) { //System.out.println(); } if (name.equalsIgnoreCase(childType.getElementName()) || this.conversionType.isAssignableFrom(childType.getPluginClass())) { // FIXME: check child.getObject() for null? // doing so would be more consistent with the array version return child; } } return null; }
@Override public Object visit(final Configuration configuration, final Node node, final LogEvent event, final StringBuilder log) { if (this.conversionType.isInstance(node)) { log.append("Node=").append(node.getName()); return node; } LOGGER.warn("Variable annotated with @PluginNode is not compatible with the type {}.", node.getClass()); return null; }
@Override public Object visit(final Configuration configuration, final Node node, final LogEvent event, final StringBuilder log) { if (this.conversionType.isInstance(configuration)) { log.append("Configuration"); if (configuration.getName() != null) { log.append('(').append(configuration.getName()).append(')'); } return configuration; } LOGGER.warn("Variable annotated with @PluginConfiguration is not compatible with type {}.", configuration.getClass()); return null; }
private void verifyNodeChildrenUsed() { final List<Node> children = node.getChildren(); if (!(pluginType.isDeferChildren() || children.isEmpty())) { for (final Node child : children) { final String nodeType = node.getType().getElementName(); final String start = nodeType.equals(node.getName()) ? node.getName() : nodeType + ' ' + node.getName(); LOGGER.error("{} has no parameter that matches element {}", start, child.getName()); } } }
@Override public AppenderSet build() { if (configuration == null) { LOGGER.error("Configuration is missing from AppenderSet {}", this); return null; } if (node == null) { LOGGER.error("No node in AppenderSet {}", this); return null; } final List<Node> children = node.getChildren(); if (children == null) { LOGGER.error("No children node in AppenderSet {}", this); return null; } final Map<String, Node> map = new HashMap<>(children.size()); for (final Node childNode : children) { final String key = childNode.getAttributes().get("name"); if (key == null) { LOGGER.error("The attribute 'name' is missing from from the node {} in AppenderSet {}", childNode, children); } else { map.put(key, childNode); } } return new AppenderSet(configuration, map); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { final PluginManager manager = new PluginManager("Test"); manager.collectPlugins(); plugin = (PluginType<ValidatingPlugin>) manager.getPluginType("Validator"); assertNotNull("Rebuild this module to make sure annotaion processing kicks in.", plugin); node = new Node(null, "Validator", plugin); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { final PluginManager manager = new PluginManager("Test"); manager.collectPlugins(); plugin = (PluginType<ValidatingPluginWithGenericBuilder>) manager.getPluginType("ValidatingPluginWithGenericBuilder"); assertNotNull("Rebuild this module to make sure annotaion processing kicks in.", plugin); node = new Node(null, "Validator", plugin); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { final PluginManager manager = new PluginManager("Test"); manager.collectPlugins(); plugin = (PluginType<ValidatingPluginWithTypedBuilder>) manager .getPluginType("ValidatingPluginWithTypedBuilder"); assertNotNull("Rebuild this module to make sure annotaion processing kicks in.", plugin); node = new Node(null, "Validator", plugin); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { final PluginManager manager = new PluginManager("Test"); manager.collectPlugins(); plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort"); assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin); node = new Node(null, "HostAndPort", plugin); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { final PluginManager manager = new PluginManager("Test"); manager.collectPlugins(); plugin = (PluginType<PluginWithGenericSubclassFoo1Builder>) manager.getPluginType("PluginWithGenericSubclassFoo1Builder"); assertNotNull("Rebuild this module to make sure annotaion processing kicks in.", plugin); node = new Node(null, "Validator", plugin); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { final PluginManager manager = new PluginManager("Test"); manager.collectPlugins(); plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort"); assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin); node = new Node(null, "HostAndPort", plugin); node.getAttributes().put("host", "localhost"); }
private Route(final Node node, final String appenderRef, final String key) { this.node = node; this.appenderRef = appenderRef; this.key = key; }
private Node constructNode(final String name, final Node parent, final JsonNode jsonNode) { final PluginType<?> type = pluginManager.getPluginType(name); final Node node = new Node(parent, name, type); processAttributes(node, jsonNode); final Iterator<Map.Entry<String, JsonNode>> iter = jsonNode.fields(); final List<Node> children = node.getChildren(); while (iter.hasNext()) { final Map.Entry<String, JsonNode> entry = iter.next(); final JsonNode n = entry.getValue(); if (n.isArray() || n.isObject()) { if (type == null) { status.add(new Status(name, n, ErrorType.CLASS_NOT_FOUND)); } if (n.isArray()) { LOGGER.debug("Processing node for array {}", entry.getKey()); for (int i = 0; i < n.size(); ++i) { final String pluginType = getType(n.get(i), entry.getKey()); final PluginType<?> entryType = pluginManager.getPluginType(pluginType); final Node item = new Node(node, entry.getKey(), entryType); processAttributes(item, n.get(i)); if (pluginType.equals(entry.getKey())) { LOGGER.debug("Processing {}[{}]", entry.getKey(), i); } else { LOGGER.debug("Processing {} {}[{}]", pluginType, entry.getKey(), i); } final Iterator<Map.Entry<String, JsonNode>> itemIter = n.get(i).fields(); final List<Node> itemChildren = item.getChildren(); while (itemIter.hasNext()) { final Map.Entry<String, JsonNode> itemEntry = itemIter.next(); if (itemEntry.getValue().isObject()) { LOGGER.debug("Processing node for object {}", itemEntry.getKey()); itemChildren.add(constructNode(itemEntry.getKey(), item, itemEntry.getValue())); } else if (itemEntry.getValue().isArray()) { final JsonNode array = itemEntry.getValue(); final String entryName = itemEntry.getKey(); LOGGER.debug("Processing array for object {}", entryName); for (int j = 0; j < array.size(); ++j) { itemChildren.add(constructNode(entryName, item, array.get(j))); } } } children.add(item); } } else { LOGGER.debug("Processing node for object {}", entry.getKey()); children.add(constructNode(entry.getKey(), node, n)); } } else { LOGGER.debug("Node {} is of type {}", entry.getKey(), n.getNodeType()); } } String t; if (type == null) { t = "null"; } else { t = type.getElementName() + ':' + type.getPluginClass(); } final String p = node.getParent() == null ? "null" : node.getParent().getName() == null ? LoggerConfig.ROOT : node.getParent().getName(); LOGGER.debug("Returning {} with parent {} of type {}", node.getName(), p, t); return node; }
private boolean isFilterNode(final Node node) { return Filter.class.isAssignableFrom(node.getType().getPluginClass()); }
private boolean isSameName(final Node node1, final Node node2) { final String value = node1.getAttributes().get(NAME); return value != null && value.toLowerCase().equals(node2.getAttributes().get(NAME).toLowerCase()); }
private boolean isSameReference(final Node node1, final Node node2) { final String value = node1.getAttributes().get(REF); return value != null && value.toLowerCase().equals(node2.getAttributes().get(REF).toLowerCase()); }
@Override public Object visit(final Configuration configuration, final Node node, final LogEvent event, final StringBuilder log) { final String name = this.annotation.value(); if (this.conversionType.isArray()) { setConversionType(this.conversionType.getComponentType()); final List<Object> values = new ArrayList<>(); final Collection<Node> used = new ArrayList<>(); log.append("={"); boolean first = true; for (final Node child : node.getChildren()) { final PluginType<?> childType = child.getType(); if (name.equalsIgnoreCase(childType.getElementName()) || this.conversionType.isAssignableFrom(childType.getPluginClass())) { if (!first) { log.append(", "); } first = false; used.add(child); final Object childObject = child.getObject(); if (childObject == null) { LOGGER.error("Null object returned for {} in {}.", child.getName(), node.getName()); continue; } if (childObject.getClass().isArray()) { log.append(Arrays.toString((Object[]) childObject)).append('}'); return childObject; } log.append(child.toString()); values.add(childObject); } } log.append('}'); // note that we need to return an empty array instead of null if the types are correct if (!values.isEmpty() && !this.conversionType.isAssignableFrom(values.get(0).getClass())) { LOGGER.error("Attempted to assign attribute {} to list of type {} which is incompatible with {}.", name, values.get(0).getClass(), this.conversionType); return null; } node.getChildren().removeAll(used); // we need to use reflection here because values.toArray() will cause type errors at runtime final Object[] array = (Object[]) Array.newInstance(this.conversionType, values.size()); for (int i = 0; i < array.length; i++) { array[i] = values.get(i); } return array; } final Node namedNode = findNamedNode(name, node.getChildren()); if (namedNode == null) { log.append(name).append("=null"); return null; } log.append(namedNode.getName()).append('(').append(namedNode.toString()).append(')'); node.getChildren().remove(namedNode); return namedNode.getObject(); }
public Node getNode() { return node; }
public Builder withNode(@SuppressWarnings("hiding") final Node node) { this.node = node; return this; }
private AppenderSet(final Configuration configuration, final Map<String, Node> appenders) { this.configuration = configuration; this.nodeMap = appenders; }
/** * Returns the Dynamic Appender Node. * @return The Node. */ public Node getNode() { return node; }
/** * Merge the root node properties into the configuration. * @param rootNode The composite root node. * @param configuration The configuration to merge. */ void mergeRootProperties(Node rootNode, AbstractConfiguration configuration);
/** * Merge the soure node tree into the target node tree. * @param target The target Node tree. * @param source The source Node tree. */ void mergConfigurations(Node target, Node source, PluginManager pluginManager);
/** * Visits a Node to obtain a value for constructing a Plugin object. * * @param configuration the current Configuration. * @param node the current Node corresponding to the Plugin object being created. * @param event the current LogEvent that caused this Plugin object to be made (optional). * @param log the StringBuilder being used to build a debug message. * @return the converted value to be used for Plugin creation. */ Object visit(Configuration configuration, Node node, LogEvent event, StringBuilder log);