@SuppressWarnings("unchecked") public Map<KeyType, String> getCollectMapping() { Map<KeyType, String> rawMap = (Map<KeyType, String>) getPd().getCollectMapping(); Map<KeyType, String> retValues = new HashMap<KeyType, String>(rawMap.size()); for(Map.Entry<KeyType, String> e: rawMap.entrySet()) { String value = jrds.Util.parseTemplate(e.getValue(), this); KeyType key = e.getKey(); if(key instanceof String) key = (KeyType) jrds.Util.parseTemplate((String) key, this); retValues.put(key, value); } return retValues; }
public Boolean configure() { if(cmd == null) return false; String cmdargs = getPd().getSpecific("arguments"); if(cmdargs != null && !cmdargs.trim().isEmpty()) { cmd = cmd + " " + Util.parseTemplate(cmdargs, this); } return true; }
protected Map<String, Set<String>> buildRequestInfo(Set<String> collectPaths) { Map<String, Set<String>> retValue = new HashMap<String, Set<String>>(); for(String path: collectPaths) { String[] parsed = jrds.Util.parseTemplate(path, this).split("\\?"); String rdn = null, field = null; if(parsed.length == 2) { rdn = parsed[0]; field = parsed[1]; } else if(parsed.length == 1) { rdn = "."; field = parsed[0]; } if(!retValue.containsKey(rdn)) { retValue.put(rdn, new HashSet<String>()); } retValue.get(rdn).add(field); } log(Level.TRACE, "Preparing a request info: %s", retValue); return retValue; }
@Override public Boolean configure() { collectKeys = new HashMap<String, String>(); for(Map.Entry<String, String> e: getPd().getCollectMapping().entrySet()) { String dsName = e.getValue(); String solved = jrds.Util.parseTemplate(e.getKey(), this); collectKeys.put(solved, dsName); } return super.configure(); }
@Override public void configure(PropertiesManager pm) { url = jrds.Util.parseTemplate(url, this, getLevel()); super.configure(pm); }
public LinkedList<String> getTree(GraphNode graph, String tabname) { List<?> elementsTree = trees.get(tabname); LinkedList<String> tree = new LinkedList<String>(); if(elementsTree == null) return tree; for(Object o: elementsTree) { if(o instanceof String) { String pathElem = jrds.Util.parseTemplate((String) o, graph.getProbe(), this, graph.getProbe().getHost()); tree.add(pathElem); } else if(o instanceof PathElement) tree.add(((PathElement) o).resolve(graph)); } return tree; }
/** * * @see jrds.ProbeConnected#configure() */ public Boolean configure(List<?> args) { if(super.configure()) { ProbeDesc<String> pd = getPd(); query = jrds.Util.parseTemplate(pd.getSpecific("query"), getHost(), args); keyColumn = jrds.Util.parseTemplate(pd.getSpecific("key"), getHost(), args); uptimeQuery = jrds.Util.parseTemplate(pd.getSpecific("uptimeQuery"), getHost(), args); uptimeRow = jrds.Util.parseTemplate(pd.getSpecific("uptimeRow"), getHost(), args); String indexTemplate = pd.getSpecific("index"); if(indexTemplate != null && !"".equals(indexTemplate)) index = jrds.Util.parseTemplate(indexTemplate, getHost(), args); setName(jrds.Util.parseTemplate(pd.getProbeName(), args)); return true; } return false; }
private Map<String, String> makeProperties(JrdsElement n, Object... o) { if(n == null) return Collections.emptyMap(); JrdsElement propElem = n.getElementbyName("properties"); if(propElem == null) return Collections.emptyMap(); Map<String, String> props = new HashMap<String, String>(); for(JrdsElement propNode: propElem.getChildElementsByName("entry")) { String key = propNode.getAttribute("key"); if(key != null) { String value = propNode.getTextContent(); value = Util.parseTemplate(value, o); logger.trace(Util.delayedFormatString("Adding propertie %s=%s", key, value)); props.put(key, value); } } logger.debug(Util.delayedFormatString("Properties map: %s", props)); return props; }
@Override protected boolean finishConfigure(List<Object> args) { log(Level.TRACE, "Configuring collect xpath with %s", args); CollectResolver<XPathExpression> cr = new XmlProvider.XmlResolver(); collectKeys = new HashMap<>(getPd().getCollectMapping().size()); for(Map.Entry<String, String> e: getPd().getCollectMapping().entrySet()) { String solved = Util.parseTemplate(String.format(e.getKey(), args != null ? args.toArray() : null), this, args); XPathExpression xpath = cr.resolve(solved); if (xpath == null) { log(Level.DEBUG, "unparsed xpath: %s", e.getKey()); continue; } else { collectKeys.put(xpath, solved); } } collectKeys = Collections.unmodifiableMap(collectKeys); log(Level.TRACE, "collect xpath mapping %s", collectKeys); return super.finishConfigure(args); }
public void configure() { setName(jrds.Util.parseTemplate(getPd().getProbeName(), this)); if(listener != null) { listener.register(this); } }
@Test public void testParseTemplate2() { System.setProperty("jrds.unittest", "true"); String parsed = Util.parseTemplate("${jrds.unittest}", System.getProperties()); Assert.assertEquals("true", parsed); }
@Test public void testParseTemplate3() { List<String> args = Arrays.asList("unittest1", "unittest2"); String parsed = Util.parseTemplate("${1} ${2}", args); Assert.assertEquals("unittest1 unittest2", parsed); }
private void setAttributes(Map<String, ProbeDesc.DefaultBean> defaultBeans, JrdsElement probeNode, Probe<?, ?> p, Object... context) throws InvocationTargetException { // Resolve the beans for(JrdsElement attrNode: probeNode.getChildElementsByName("attr")) { String name = attrNode.getAttribute("name"); GenericBean bean = p.getPd().getBean(name); if(bean == null) { // Context[0] should be the host logger.error(context[0] + "/" + p.getPd().getName() + ": unknown bean '" + name + "'"); continue; } String textValue = Util.parseTemplate(attrNode.getTextContent(), context); logger.trace(Util.delayedFormatString("Found attribute %s with value %s", name, textValue)); bean.set(p, textValue); if(defaultBeans.containsKey(name)) { defaultBeans.remove(name); } } }
@Test public void testParseTemplate4() { String parsed = Util.parseTemplate("%string"); Assert.assertEquals("%string", parsed); }
@Test @SuppressWarnings("unused") public void testParseTemplate6() throws Exception { Object o = new Object() { public String getOne() { return "one"; } public void setOne(String bean) { } public String getTwo() { return null; } public void setTwo(String bean) { } }; String parsed = Util.parseTemplate("${attr.one} ${attr.two} ${attr.two.signature} ${attr.three}", o); Assert.assertEquals("one ${attr.two} ${attr.two.signature} ", parsed); }
private void validateBean(HttpProbe<String> p) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Assert.assertEquals("invalid url bean", p.getUrl(), p.getPd().getBean("url").get(p)); Assert.assertEquals("invalid port bean", p.getPort(), p.getPd().getBean("port").get(p)); Assert.assertEquals("invalid file bean", p.getFile(), p.getPd().getBean("file").get(p)); Assert.assertEquals("invalid url bean template", p.getUrl().toString(), Util.parseTemplate("${attr.url}", p)); Assert.assertEquals("invalid port bean template", p.getPort().toString(), Util.parseTemplate("${attr.port}", p)); Assert.assertEquals("invalid file bean template", p.getFile(), Util.parseTemplate("${attr.file}", p)); }
@Test public void testParseTemplate1() { Probe<?, ?> p = new MokeProbe<String, Number>(); p.setHost(new HostStarter(new HostInfo("Moke"))); p.setLabel("label"); String parsed = Util.parseTemplate("'${host}' \"${probename}\" ${label}", p); Assert.assertEquals("'Moke' \"DummyProbe\" label", parsed); }
@Test public void testCustomBeans() throws Exception { PropertiesManager localpm = Tools.makePm(testFolder); ConfigObjectFactory conf = new ConfigObjectFactory(localpm, localpm.extensionClassLoader); JrdsDocument pddoc = Tools.parseRessource("beans.xml"); pddoc.getRootElement().getElementbyName("probeClass").setTextContent("jrds.mockobjects.MokeProbeBean"); conf.getNodeMap(ConfigType.PROBEDESC).put("name", pddoc); @SuppressWarnings("unchecked") ProbeDesc<String> pd = (ProbeDesc<String>) conf.setProbeDescMap().get("name"); Assert.assertNotNull("custom bean customattr1 not found", pd.getBean("customattr1")); Assert.assertNotNull("custom bean customattr2 not found", pd.getBean("customattr2")); @SuppressWarnings("unchecked") Probe<String, String> p = (Probe<String, String>) pd.getProbeClass().getConstructor().newInstance(); p.setPd(pd); pd.getBean("customattr1").set(p, "value1"); pd.getBean("customattr2").set(p, "value2"); Assert.assertEquals("value1", pd.getBean("customattr1").get(p)); Assert.assertEquals("value2", pd.getBean("customattr2").get(p)); Assert.assertEquals("value1", Util.parseTemplate("${attr.customattr1}", p)); Assert.assertEquals("value2", Util.parseTemplate("${attr.customattr2}", p)); } }
@Test public void testBean() throws IOException, InvocationTargetException, IllegalArgumentException, IllegalAccessException { RRDToolProbe p = new RRDToolProbe(); p.setHost(new HostStarter(new HostInfo("toto"))); ProbeDesc<String> pd = new ProbeDesc<String>(); pd.setProbeClass(p.getClass()); pd.setName("Rrdtool"); pd.setProbeName("rrdtool"); p.setPd(pd); p.setRrdfile(rrdfile); Assert.assertEquals("invalid rrdfile bean", p.getRrdfile(), pd.getBean("rrdfile").get(p)); Assert.assertEquals("invalid rrdfile bean template ", p.getRrdfile().toString(), Util.parseTemplate("${attr.rrdfile}", p)); }
@Test public void testParseTemplate5() throws Exception { Probe<String, Number> testProbe = GenerateProbe.fillProbe(new GenerateProbe.EmptyProbe(), testFolder, GenerateProbe.ChainedMap.start()); GenericBean b1 = new GenericBean.CustomBean("one"); b1.set(testProbe, "one"); testProbe.getPd().addBean(b1); GenericBean b2 = new GenericBean.CustomBean("two"); testProbe.getPd().addBean(b2); String parsed = Util.parseTemplate("${attr.one} ${attr.two} ${attr.two.signature} ${attr.three} ${probename}", testProbe); Assert.assertEquals("one ${attr.two} ${attr.two.signature} EmptyProbe", parsed); }