public void importJar(JarFile jarfile) throws IOException { if(logger.isTraceEnabled()) logger.trace("Importing jar " + jarfile.getName()); for(JarEntry je: Collections.list(jarfile.entries())) { String name = je.getName(); if(!je.isDirectory() && name.endsWith(".xml") && (name.startsWith("desc/") || name.startsWith("graph/") || name.startsWith("probe/"))) { logger.trace("Will import jar entry " + je); importStream(jarfile.getInputStream(je), je + " in " + jarfile); } } }
public void importDir(File path) { logger.trace("Importing directory " + path); if(!path.isDirectory()) { logger.warn(path + " is not a directory"); return; } // listFiles can return null File[] foundFiles = path.listFiles(filter); if(foundFiles == null) { logger.error("Failed to import " + path); return; } for(File f: foundFiles) { if(f.isDirectory()) { importDir(f); } else { try { logger.trace("Will import " + f); importStream(new FileInputStream(f), f); } catch (IOException e) { logger.error("IO error with " + f + ": " + e); } } } }
public void importUrl(URI ressourceUri) { try { logger.debug("Importing " + ressourceUri); String protocol = ressourceUri.getScheme(); if("file".equals(protocol)) { String fileName = ressourceUri.getPath(); File imported = new File(ressourceUri); if(imported.isDirectory()) { importDir(imported); } else if(fileName.endsWith(".jar")) { importJar(new JarFile(imported)); } else { importStream(new FileInputStream(fileName), fileName); } } else if("jar".equals(protocol)) { URL ressourceUrl = ressourceUri.toURL(); JarURLConnection cnx = (JarURLConnection) ressourceUrl.openConnection(); importJar(cnx.getJarFile()); } else { logger.error("ressource " + ressourceUri + " can't be loaded"); } } catch (IOException e) { logger.error("Invalid URL " + ressourceUri + ": " + e); } }
@Test public void doLoadView() throws Exception { Loader l = new Loader(); l.importStream(getClass().getClassLoader().getResourceAsStream("view1.xml"), ""); l.done(); Map<String, JrdsDocument> rep = l.getRepository(ConfigType.FILTER); logger.trace(rep); Assert.assertTrue(rep.containsKey("Test view 1")); }
@Test public void doLoadProbeDesc() throws Exception { Loader l = new Loader(); l.importStream(getClass().getClassLoader().getResourceAsStream("fulldesc.xml"), ""); l.done(); Map<String, JrdsDocument> rep = l.getRepository(ConfigType.PROBEDESC); logger.trace(rep); Assert.assertTrue(rep.containsKey("name")); }
@Test public void doLoadTab() throws Exception { Loader l = new Loader(); l.importStream(getClass().getClassLoader().getResourceAsStream("goodtab.xml"), ""); l.done(); Map<String, JrdsDocument> rep = l.getRepository(ConfigType.TAB); logger.trace(rep); Assert.assertTrue(rep.containsKey("goodtab")); }
@Test public void doLoadGraph() throws Exception { Loader l = new Loader(); l.importStream(getClass().getClassLoader().getResourceAsStream("customgraph.xml"), ""); l.done(); Map<String, JrdsDocument> rep = l.getRepository(ConfigType.GRAPH); logger.trace(rep); Assert.assertTrue(rep.containsKey("name")); }
@Test public void doLoadHost() throws Exception { Loader l = new Loader(); l.importStream(getClass().getClassLoader().getResourceAsStream("host1.xml"), ""); l.done(); Assert.assertTrue(l.getRepository(ConfigType.HOSTS).containsKey("name")); }