private void setupAppenderIfRequired(LogEvent event) { if (appenderControl.get() == null) { if (node.getType().getElementName().equalsIgnoreCase("appender")) { for (final Node cnode : node.getChildren()) { final Node appNode = new Node(cnode); config.createConfiguration(appNode, event); if (appNode.getObject() instanceof Appender) { final Appender app = appNode.getObject(); app.start(); if (!(app instanceof RandomAccessFileAppender)) {
PluginType<NullAppender> nullAppenderType = new PluginType<>(nullAppenderEntry, NullAppender.class, "appender"); Node nullAppenderChildNode = new Node(null, "test-null-appender", nullAppenderType); defaultRouteEntry.setClassName(Route.class.getName()); PluginType<Route> defaultRouteType = new PluginType<>(defaultRouteEntry, Route.class, ""); Node defaultRouteNode = new Node(null, "test-route-default", defaultRouteType); defaultRouteNode.getChildren().add(nullAppenderChildNode); queryIdRouteEntry.setClassName(Route.class.getName()); PluginType<Route> queryIdRouteType = new PluginType<>(queryIdRouteEntry, Route.class, ""); Node queryIdRouteNode = new Node(null, "test-route-mdc", queryIdRouteType); HushableRandomAccessFileAppender.class, "appender"); Node queryIdAppenderNode = new Node(queryIdRouteNode, "test-query-file-appender", queryIdAppenderType); queryIdAppenderNode.getAttributes().put("fileName", logLocation + "/${ctx:sessionId}/${ctx:queryId}.test"); queryIdAppenderNode.getAttributes().put("name", "test-query-file-appender"); queryIdRouteNode.getChildren().add(queryIdAppenderNode); PluginType<TestFilter> filterType = new PluginType<>(filterEntry, TestFilter.class, ""); Node filterNode = new Node(queryIdAppenderNode, "test-filter", filterType); queryIdAppenderNode.getChildren().add(filterNode);
@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 void updateFilterNode(final Node target, final Node targetChildNode, final Node sourceChildNode, final PluginManager pluginManager) { if (CompositeFilter.class.isAssignableFrom(targetChildNode.getType().getPluginClass())) { final Node node = new Node(targetChildNode, sourceChildNode.getName(), sourceChildNode.getType()); node.getChildren().addAll(sourceChildNode.getChildren()); node.getAttributes().putAll(sourceChildNode.getAttributes()); targetChildNode.getChildren().add(node); } else { final PluginType pluginType = pluginManager.getPluginType(FILTERS); final Node filtersNode = new Node(targetChildNode, FILTERS, pluginType); final Node node = new Node(filtersNode, sourceChildNode.getName(), sourceChildNode.getType()); node.getAttributes().putAll(sourceChildNode.getAttributes()); final List<Node> children = filtersNode.getChildren(); children.add(targetChildNode); children.add(node); final List<Node> nodes = target.getChildren(); nodes.remove(targetChildNode); nodes.add(filtersNode); } }
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); 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)); if (text.length() > 0 || (!node.hasChildren() && !node.isRoot())) { node.setValue(text);
public Node(final Node node) { this.parent = node.parent; this.name = node.name; this.type = node.type; this.attributes.putAll(node.getAttributes()); this.value = node.getValue(); for (final Node child : node.getChildren()) { this.children.add(new Node(child)); } this.object = node.object; }
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; } }
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; } }
@Test public void testInvalidIpAddress() throws Exception { node.getAttributes().put("host", "256.256.256.256"); node.getAttributes().put("port", "1"); final HostAndPort plugin = buildPlugin(); assertNull("Expected null, but got: " + plugin, plugin); }
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())) { final Object childObject = child.getObject(); if (childObject == null) { LOGGER.error("Null object returned for {} in {}.", child.getName(), node.getName()); continue; return childObject; log.append(child.toString()); values.add(childObject); node.getChildren().removeAll(used); 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();
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 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())) { final List<Node> itemChildren = item.getChildren(); while (itemIter.hasNext()) { final Map.Entry<String, JsonNode> itemEntry = itemIter.next(); 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;
@Override public void createConfiguration(final Node node, final LogEvent event) { final PluginType<?> type = node.getType(); if (type != null && type.isDeferChildren()) { node.setObject(createPluginObject(type, node, event)); } else { for (final Node child : node.getChildren()) { createConfiguration(child, event); } if (type == null) { if (node.getParent() != null) { LOGGER.error("Unable to locate plugin for {}", node.getName()); } } else { node.setObject(createPluginObject(type, node, event)); } } }
@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); }
protected void doConfigure() { preConfigure(rootNode); configurationScheduler.start(); if (rootNode.hasChildren() && rootNode.getChildren().get(0).getName().equalsIgnoreCase("Properties")) { final Node first = rootNode.getChildren().get(0); createConfiguration(first, null); if (first.getObject() != null) { subst.setVariableResolver((StrLookup) first.getObject()); for (final Node child : rootNode.getChildren()) { if (child.getName().equalsIgnoreCase("Properties")) { if (tempLookup == subst.getVariableResolver()) { LOGGER.error("Properties declaration must be the first element in the configuration"); if (child.getObject() == null) { continue; if (child.getName().equalsIgnoreCase("Scripts")) { for (final AbstractScript script : child.getObject(AbstractScript[].class)) { if (script instanceof ScriptRef) { LOGGER.error("Script reference to {} not added. Scripts definition cannot contain script references", } else if (child.getName().equalsIgnoreCase("Appenders")) { appenders = child.getObject(); } else if (child.isInstanceOf(Filter.class)) { addFilter(child.getObject(Filter.class)); } else if (child.getName().equalsIgnoreCase("Loggers")) { final Loggers l = child.getObject(); loggerConfigs = l.getMap(); setLoggers = true;
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; }
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()); } } } }
private static Map<String, ?> createPluginMap(final Node node) { final Map<String, Object> map = new LinkedHashMap<>(); for (final Node child : node.getChildren()) { final Object object = child.getObject(); map.put(child.getName(), object); } return map; }
private static Collection<?> createPluginCollection(final Node node) { final List<Node> children = node.getChildren(); final Collection<Object> list = new ArrayList<>(children.size()); for (final Node child : children) { final Object object = child.getObject(); list.add(object); } return list; }
@Override public Object visit(final Configuration configuration, final Node node, final LogEvent event, final StringBuilder log) { final String name = this.annotation.value(); final String elementValue = node.getValue(); final String attributeValue = node.getAttributes().get("value"); String rawValue = null; // if neither is specified, return null (LOG4J2-1313) if (Strings.isNotEmpty(elementValue)) { if (Strings.isNotEmpty(attributeValue)) { LOGGER.error("Configuration contains {} with both attribute value ({}) AND element" + " value ({}). Please specify only one value. Using the element value.", node.getName(), attributeValue, elementValue); } rawValue = elementValue; } else { rawValue = removeAttributeValue(node.getAttributes(), "value"); } final String value = this.substitutor.replace(event, rawValue); StringBuilders.appendKeyDqValue(log, name, value); return value; } }
private void checkForRemainingAttributes() { final Map<String, String> attrs = node.getAttributes(); if (!attrs.isEmpty()) { final StringBuilder sb = new StringBuilder(); for (final String key : attrs.keySet()) { if (sb.length() == 0) { sb.append(node.getName()); sb.append(" contains "); if (attrs.size() == 1) { sb.append("an invalid element or attribute "); } else { sb.append("invalid attributes "); } } else { sb.append(", "); } StringBuilders.appendDqValue(sb, key); } LOGGER.error(sb.toString()); } }