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; }
bean.set(p, value); } catch (Exception e) { Throwable root = e;
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); } } }
try { if("index".equals(bean.getName())) { bean.set(p, "index");
@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 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); }