private boolean resolveDefaultBean(Probe<?, ?> p, List<Object> args, Map<String, String> properties, String beanName, String beanValue) { HostInfo host = p.getHost(); ProbeDesc<?> pd = p.getPd(); GenericBean bean = pd.getBean(beanName); String value;
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 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)); } }
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)); }
@SuppressWarnings("unchecked") @Test public void build3() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { HttpProbe<String> p = new HttpProbe<String>() { @Override protected Map<String, Number> parseStream(InputStream stream) { return null; } }; p.setHost(webserver); ProbeDesc<String> pd = new ProbeDesc<String>(); pd.setProbeClass((Class<? extends Probe<String, ?>>) p.getClass()); p.setPd(pd); p.setFile("/file"); p.setPort(81); Assert.assertTrue(p.configure()); Assert.assertEquals("http://" + HOST + ":81/file", p.getUrlAsString()); Assert.assertEquals("http://" + HOST + ":81/file", pd.getBean("url").get(p).toString()); validateBean(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)); }
Assert.assertEquals("localhost", p.getPd().getBean("hostInfo").get(p)); logger.trace(p.getName());