public void addBean(GenericBean bean) { beans.put(bean.getName(), bean); }
GenericBean bean = ((Probe<?, ?>) o).getPd().getBean(beanName); if(bean != null) { Object beanValue = bean.get(o); if (beanValue != null) { toAppend = stringSignature(beanValue.toString()); GenericBean bean = ((Probe<?, ?>) o).getPd().getBean(beanName); if(bean != null) { Object beanValue = bean.get(o); if(beanValue != null) { toAppend = beanValue.toString();
public Graph getGraph() { Class<Graph> gclass = gd.getGraphClass(); Graph g; Map<String, GenericBean> beansList; try { g = gclass.getConstructor(GraphNode.class).newInstance(this); beansList = ArgFactory.getBeanPropertiesMap(gclass, Graph.class); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { Util.log(this, logger, Level.ERROR, e, "Failed to build a graph instance %s", gclass.getCanonicalName()); return null; } // Resolve the beans for(Map.Entry<String, String> e: beans.entrySet()) { String name = Util.parseTemplate(e.getKey(), probe); String textValue = Util.parseTemplate(e.getValue(), probe); GenericBean bean = beansList.get(name); if(bean == null) { logger.error(String.format("Unknown bean for %s: %s", gd.getName(), name)); continue; } logger.trace(Util.delayedFormatString("Found attribute %s with value %s", name, textValue)); bean.set(g, textValue); } return g; }
@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)); } }
@Override public StoreType configure(Probe<?, ?> p, Map<String, String> properties) throws InvocationTargetException { StoreType s = create(p); for(GenericBean bean: ArgFactory.getBeanPropertiesMap(s.getClass(), AbstractStore.class).values()) { String beanName = bean.getName(); if(properties.containsKey(beanName)) { String beanValue = properties.get(beanName); ArgFactory.beanSetter(s, bean.getName(), beanValue); } } return s; }
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)); }
bean.set(p, value); } catch (Exception e) { Throwable root = e;
for(GenericBean bean: tryBeans) { String defaultValue = ""; ProbeDesc.DefaultBean currentAttribute = defaultBeans.get(bean.getName()); if(currentAttribute != null && !currentAttribute.delayed) { defaultValue = currentAttribute.value; System.out.println("| " + bean.getName() + " | " + defaultValue + " | | ");
@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); }
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 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); }
Assert.assertEquals("localhost", p.getPd().getBean("hostInfo").get(p)); logger.trace(p.getName());