List of usage examples for org.apache.commons.io FileUtils lineIterator
public static LineIterator lineIterator(File file) throws IOException
File
using the default encoding for the VM. From source file:org.opennms.upgrade.implementations.JmxRrdMigratorOffline.java
/** * Fixes a JMX configuration file./*ww w . j a v a 2s . com*/ * * @param jmxConfigFile the JMX configuration file * @throws OnmsUpgradeException the OpenNMS upgrade exception */ private void fixJmxConfigurationFile(File jmxConfigFile) throws OnmsUpgradeException { try { log("Updating JMX metric definitions on %s\n", jmxConfigFile); zipFile(jmxConfigFile); backupFiles.add(new File(jmxConfigFile.getAbsolutePath() + ZIP_EXT)); File outputFile = new File(jmxConfigFile.getCanonicalFile() + ".temp"); FileWriter w = new FileWriter(outputFile); Pattern extRegex = Pattern.compile("import-mbeans[>](.+)[<]"); Pattern aliasRegex = Pattern.compile("alias=\"([^\"]+\\.[^\"]+)\""); List<File> externalFiles = new ArrayList<File>(); LineIterator it = FileUtils.lineIterator(jmxConfigFile); while (it.hasNext()) { String line = it.next(); Matcher m = extRegex.matcher(line); if (m.find()) { externalFiles.add(new File(jmxConfigFile.getParentFile(), m.group(1))); } m = aliasRegex.matcher(line); if (m.find()) { String badDs = m.group(1); String fixedDs = getFixedDsName(badDs); log(" Replacing bad alias %s with %s on %s\n", badDs, fixedDs, line.trim()); line = line.replaceAll(badDs, fixedDs); if (badMetrics.contains(badDs) == false) { badMetrics.add(badDs); } } w.write(line + "\n"); } LineIterator.closeQuietly(it); w.close(); FileUtils.deleteQuietly(jmxConfigFile); FileUtils.moveFile(outputFile, jmxConfigFile); if (!externalFiles.isEmpty()) { for (File configFile : externalFiles) { fixJmxConfigurationFile(configFile); } } } catch (Exception e) { throw new OnmsUpgradeException("Can't fix " + jmxConfigFile + " because " + e.getMessage(), e); } }
From source file:org.opennms.upgrade.implementations.JmxRrdMigratorOffline.java
/** * Fixes a JMX graph template file.//www.ja va 2 s .co m * * @param jmxTemplateFile the JMX template file * @throws OnmsUpgradeException the OpenNMS upgrade exception */ private void fixJmxGraphTemplateFile(File jmxTemplateFile) throws OnmsUpgradeException { try { log("Updating JMX graph templates on %s\n", jmxTemplateFile); zipFile(jmxTemplateFile); backupFiles.add(new File(jmxTemplateFile.getAbsolutePath() + ZIP_EXT)); File outputFile = new File(jmxTemplateFile.getCanonicalFile() + ".temp"); FileWriter w = new FileWriter(outputFile); Pattern defRegex = Pattern.compile("DEF:.+:(.+\\..+):"); Pattern colRegex = Pattern.compile("\\.columns=(.+)$"); Pattern incRegex = Pattern.compile("^include.directory=(.+)$"); List<File> externalFiles = new ArrayList<File>(); boolean override = false; LineIterator it = FileUtils.lineIterator(jmxTemplateFile); while (it.hasNext()) { String line = it.next(); Matcher m = incRegex.matcher(line); if (m.find()) { File includeDirectory = new File(jmxTemplateFile.getParentFile(), m.group(1)); if (includeDirectory.isDirectory()) { FilenameFilter propertyFilesFilter = new FilenameFilter() { @Override public boolean accept(File dir, String name) { return (name.endsWith(".properties")); } }; for (File file : includeDirectory.listFiles(propertyFilesFilter)) { externalFiles.add(file); } } } m = colRegex.matcher(line); if (m.find()) { String[] badColumns = m.group(1).split(",(\\s)?"); for (String badDs : badColumns) { String fixedDs = getFixedDsName(badDs); if (fixedDs.equals(badDs)) { continue; } if (badMetrics.contains(badDs)) { override = true; log(" Replacing bad data source %s with %s on %s\n", badDs, fixedDs, line); line = line.replaceAll(badDs, fixedDs); } else { log(" Warning: a bad data source not related with JMX has been found: %s (this won't be updated)\n", badDs); } } } m = defRegex.matcher(line); if (m.find()) { String badDs = m.group(1); if (badMetrics.contains(badDs)) { override = true; String fixedDs = getFixedDsName(badDs); log(" Replacing bad data source %s with %s on %s\n", badDs, fixedDs, line); line = line.replaceAll(badDs, fixedDs); } else { log(" Warning: a bad data source not related with JMX has been found: %s (this won't be updated)\n", badDs); } } w.write(line + "\n"); } LineIterator.closeQuietly(it); w.close(); if (override) { FileUtils.deleteQuietly(jmxTemplateFile); FileUtils.moveFile(outputFile, jmxTemplateFile); } else { FileUtils.deleteQuietly(outputFile); } if (!externalFiles.isEmpty()) { for (File configFile : externalFiles) { fixJmxGraphTemplateFile(configFile); } } } catch (Exception e) { throw new OnmsUpgradeException("Can't fix " + jmxTemplateFile + " because " + e.getMessage(), e); } }
From source file:org.opennms.upgrade.implementations.JmxRrdMigratorOfflineTest.java
/** * Executes the JMX Migrator./*from w w w .ja v a2 s . co m*/ * * @return the JMX Migrator object * @throws Exception the exception */ private JmxRrdMigratorOffline executeMigrator() throws Exception { JmxRrdMigratorOffline jmxMigrator = new JmxRrdMigratorOffline(); jmxMigrator.preExecute(); jmxMigrator.execute(); jmxMigrator.postExecute(); Assert.assertEquals(60, jmxMigrator.badMetrics.size()); // Verify graph templates File templates = new File("target/home/etc/snmp-graph.properties.d/jvm-graph.properties"); Pattern defRegex = Pattern.compile("DEF:.+:(.+\\..+):"); Pattern colRegex = Pattern.compile("\\.columns=(.+)$"); for (LineIterator it = FileUtils.lineIterator(templates); it.hasNext();) { String line = it.next(); Matcher m = defRegex.matcher(line); if (m.find()) { String ds = m.group(1); if (jmxMigrator.badMetrics.contains(ds)) { Assert.fail("Bad metric found"); } } m = colRegex.matcher(line); if (m.find()) { String[] badColumns = m.group(1).split(",(\\s)?"); if (jmxMigrator.badMetrics.containsAll(Arrays.asList(badColumns))) { Assert.fail("Bad metric found"); } } } // Verify metric definitions File metrics = new File("target/home/etc/jmx-datacollection-config.xml"); Pattern aliasRegex = Pattern.compile("alias=\"([^\"]+\\.[^\"]+)\""); for (LineIterator it = FileUtils.lineIterator(metrics); it.hasNext();) { String line = it.next(); Matcher m = aliasRegex.matcher(line); if (m.find()) { String ds = m.group(1); if (jmxMigrator.badMetrics.contains(ds)) { Assert.fail("Bad metric found"); } } } return jmxMigrator; }
From source file:org.opens.referentiel.creator.CodeGeneratorMojo.java
/** * * @return//w w w . j a v a 2s. c om */ private Iterable<CSVRecord> getCsv() { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator; try { lineIterator = FileUtils.lineIterator(dataFile); } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); lineIterator = null; } String[] csvHeaders = lineIterator.next().split(String.valueOf(delimiter)); isCriterionPresent = extractCriterionFromCsvHeader(csvHeaders); try { extractAvailableLangsFromCsvHeader(csvHeaders); } catch (I18NLanguageNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } // from here we just add each line to a build to re-create the csv content // without the first line. StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:org.sindice.siren.demo.bnb.BNBDemo.java
public void index() throws IOException { final SimpleIndexer indexer = new SimpleIndexer(indexDir); try {/*from w w w . j a v a 2 s .c o m*/ int counter = 0; final LineIterator it = FileUtils.lineIterator(BNB_PATH); while (it.hasNext()) { final String id = Integer.toString(counter++); final String content = (String) it.next(); logger.info("Indexing document {}", id); indexer.addDocument(id, content); } LineIterator.closeQuietly(it); logger.info("Commiting all pending documents"); indexer.commit(); } finally { logger.info("Closing index"); indexer.close(); } }
From source file:org.sipfoundry.sipxconfig.admin.X509CertificateUtils.java
/** * Creates a X509 certificate based on the provided file path. Strips any preamble information * from the original certificate (if any) * * @param path/*from ww w .j ava 2s .c om*/ * @return X509 Certificate or null if cannot create */ public static X509Certificate getX509Certificate(String path) { X509Certificate cert = null; LineIterator it = null; InputStream is = null; try { File sslCertFile = new File(path); it = FileUtils.lineIterator(sslCertFile); StringBuffer rep = new StringBuffer(); boolean shouldWrite = false; while (it.hasNext()) { String line = it.nextLine(); if (line.equals(BEGIN_LINE)) { shouldWrite = true; } if (shouldWrite) { rep.append(line); rep.append(NEW_LINE); } } is = new ByteArrayInputStream(rep.toString().getBytes()); CertificateFactory cf = CertificateFactory.getInstance(X509_TYPE); cert = (X509Certificate) cf.generateCertificate(is); } catch (Exception ex) { cert = null; } finally { LineIterator.closeQuietly(it); IOUtils.closeQuietly(is); } return cert; }
From source file:org.soaplab.clients.InputUtils.java
static byte[][] list2Files(String value) throws IOException { ArrayList<byte[]> result = new ArrayList<byte[]>(); File listFile = new File(value); String fullPath = FilenameUtils.getFullPath(listFile.getAbsolutePath()); LineIterator it = FileUtils.lineIterator(listFile); try {/*from w ww .j a v a 2 s.com*/ while (it.hasNext()) { String line = it.nextLine().trim(); if (StringUtils.isEmpty(line)) continue; // ignore blank lines if (line.startsWith("#")) continue; // ignore comments File aFile = new File(line); try { result.add(FileUtils.readFileToByteArray(aFile)); } catch (IOException e) { // try to add path of the list file if (aFile.isAbsolute()) { throw new IOException("Error when reading [" + line + "]: " + e.getMessage()); } else { File bFile = new File(fullPath, line); result.add(FileUtils.readFileToByteArray(bFile)); } } } } finally { LineIterator.closeQuietly(it); } return result.toArray(new byte[][] {}); }
From source file:org.sonar.plugins.xaml.XamlLineCouterSensor.java
private void addMeasures(SensorContext sensorContext, File file, org.sonar.api.resources.File xmlFile) { LineIterator iterator = null;/*from w w w . jav a 2s .c om*/ int numLines = 0; int numEmptyLines = 0; try { iterator = FileUtils.lineIterator(file); while (iterator.hasNext()) { String line = iterator.nextLine(); numLines++; if (StringUtils.isEmpty(line)) { numEmptyLines++; } } } catch (IOException e) { LOG.warn(e.getMessage()); } finally { LineIterator.closeQuietly(iterator); } try { Log.debug("Count comment in " + file.getPath()); int numCommentLines = new XamlLineCountParser().countLinesOfComment(FileUtils.openInputStream(file)); sensorContext.saveMeasure(xmlFile, CoreMetrics.LINES, (double) numLines); sensorContext.saveMeasure(xmlFile, CoreMetrics.COMMENT_LINES, (double) numCommentLines); sensorContext.saveMeasure(xmlFile, CoreMetrics.NCLOC, (double) numLines - numEmptyLines - numCommentLines); } catch (Exception e) { LOG.debug("Fail to count lines in " + file.getPath(), e); } LOG.debug("LineCountSensor: " + xmlFile.getKey() + ":" + numLines + "," + numEmptyLines + "," + 0); }
From source file:org.sonar.plugins.xml.LineCountSensor.java
private void addMeasures(SensorContext sensorContext, File file, org.sonar.api.resources.File xmlFile) { LineIterator iterator = null;//from w ww . ja v a 2 s .c o m int numLines = 0; int numEmptyLines = 0; try { iterator = FileUtils.lineIterator(file); while (iterator.hasNext()) { String line = iterator.nextLine(); numLines++; if (StringUtils.isEmpty(line)) { numEmptyLines++; } } } catch (IOException e) { LOG.warn(e.getMessage()); } finally { LineIterator.closeQuietly(iterator); } try { Log.debug("Count comment in " + file.getPath()); int numCommentLines = new LineCountParser().countLinesOfComment(FileUtils.openInputStream(file)); sensorContext.saveMeasure(xmlFile, CoreMetrics.LINES, (double) numLines); sensorContext.saveMeasure(xmlFile, CoreMetrics.COMMENT_LINES, (double) numCommentLines); sensorContext.saveMeasure(xmlFile, CoreMetrics.NCLOC, (double) numLines - numEmptyLines - numCommentLines); } catch (Exception e) { LOG.debug("Fail to count lines in " + file.getPath(), e); } LOG.debug("LineCountSensor: " + xmlFile.getKey() + ":" + numLines + "," + numEmptyLines + "," + 0); }
From source file:org.tanaguru.rules.doc.utils.updateAw22toRgaa30.CopyFiles.java
private Iterable<CSVRecord> getCsv(ResourceBundle resourceBundle) { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator;// w w w . j av a 2 s . com try { lineIterator = FileUtils.lineIterator(FileUtils.getFile(resourceBundle.getString("export.csvPath"))); } catch (IOException ex) { Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex); lineIterator = null; } String[] csvHeaders = lineIterator.next().split(String.valueOf(delimiter)); // from here we just add each line to a build to re-create the csv content // without the first line. StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex); return null; } }