List of usage examples for org.apache.commons.io LineIterator hasNext
public boolean hasNext()
Reader
has more lines. From source file:org.openmrs.module.emrmonitor.metric.ConfigurableMetricProducer.java
/** * If multiple lines of output are returned and each is in the format of key=value, then the key will be considered part of the metric, and the value the value * Otherwise, the full contents of output will be the value of a single metric *///from w ww . j a v a 2s . com protected void handleShellScript(Map<String, String> metrics, String namespace, File f) throws IOException { Process process = Runtime.getRuntime().exec(f.getAbsolutePath()); StringBuilder singleValueMetric = new StringBuilder(); Map<String, String> keyValueMetrics = new LinkedHashMap<String, String>(); LineIterator successIterator = null; try { successIterator = IOUtils.lineIterator(process.getInputStream(), "UTF-8"); while (successIterator.hasNext()) { String line = successIterator.nextLine(); String[] split = StringUtils.splitByWholeSeparatorPreserveAllTokens(line, "=", 2); if (split.length == 2) { keyValueMetrics.put(namespace + "." + split[0], split[1]); } else { singleValueMetric.append(line).append(System.getProperty("line.separator")); } } if (singleValueMetric.length() > 0) { metrics.put(namespace, singleValueMetric.toString()); } else { metrics.putAll(keyValueMetrics); } } finally { successIterator.close(); } StringBuilder error = new StringBuilder(); LineIterator errorIterator = null; try { errorIterator = IOUtils.lineIterator(process.getErrorStream(), "UTF-8"); while (errorIterator.hasNext()) { String line = errorIterator.nextLine(); error.append(System.getProperty("line.separator")).append(line); } } finally { errorIterator.close(); } if (error.length() > 0) { throw new RuntimeException( "An error occurred while executing shell script " + f.getName() + ": " + error); } }
From source file:org.openmrs.module.pihmalawi.sql.MysqlRunner.java
/** * Executes a Sql Script// ww w . ja v a2 s .c o m */ public static MysqlResult executeSql(String sql, Map<String, Object> parameterValues) { log.info("Executing SQL..."); File toExecute = null; try { // Writing SQL to temporary file for execution toExecute = File.createTempFile("mysqlrunner", ".sql"); StringBuilder sqlToWrite = new StringBuilder(); if (parameterValues != null) { for (String paramName : parameterValues.keySet()) { Object paramValue = parameterValues.get(paramName); sqlToWrite.append("set @").append(paramName); sqlToWrite.append("=").append(getParameterAssignmentString(paramValue)).append(";"); sqlToWrite.append(System.getProperty("line.separator")); } } sqlToWrite.append(sql); FileUtils.writeStringToFile(toExecute, sqlToWrite.toString()); log.debug("Wrote SQL file for execution: " + toExecute.getAbsolutePath()); log.debug("Contents:\n" + sqlToWrite); // Constructing command line elements to execute List<String> commands = new ArrayList<String>(); commands.add("mysql"); commands.add("-u" + Context.getRuntimeProperties().getProperty("connection.username")); commands.add("-p" + Context.getRuntimeProperties().getProperty("connection.password")); commands.add("-esource " + toExecute.getAbsolutePath()); commands.add(DatabaseUpdater.getConnection().getCatalog()); // Database Name log.debug("Constructed command to execute: \n" + OpenmrsUtil.join(commands, " ")); Process process = Runtime.getRuntime().exec(commands.toArray(new String[] {})); MysqlResult result = new MysqlResult(); LineIterator successIterator = null; try { successIterator = IOUtils.lineIterator(process.getInputStream(), "UTF-8"); while (successIterator.hasNext()) { String line = successIterator.nextLine(); String[] elements = StringUtils.splitPreserveAllTokens(line, '\t'); if (result.getColumns().isEmpty()) { result.setColumns(Arrays.asList(elements)); } else { Map<String, String> row = new LinkedHashMap<String, String>(); for (int i = 0; i < result.getColumns().size(); i++) { String value = elements[i].trim(); if ("NULL".equals(value)) { value = null; } row.put(result.getColumns().get(i), value); } result.getData().add(row); } } } finally { successIterator.close(); } LineIterator errorIterator = null; try { errorIterator = IOUtils.lineIterator(process.getErrorStream(), "UTF-8"); while (errorIterator.hasNext()) { String line = errorIterator.nextLine(); if (!line.toLowerCase().startsWith("warning")) { result.getErrors().add(line); } } } finally { errorIterator.close(); } return result; } catch (Exception e) { throw new RuntimeException("An error occurred while executing a SQL file", e); } finally { FileUtils.deleteQuietly(toExecute); } }
From source file:org.opennms.upgrade.implementations.JettyConfigMigratorOffline.java
@Override public void execute() throws OnmsUpgradeException { String jettySSL = getMainProperties().getProperty("org.opennms.netmgt.jetty.https-port", null); String jettyAJP = getMainProperties().getProperty("org.opennms.netmgt.jetty.ajp-port", null); boolean sslWasFixed = false; boolean ajpWasFixed = false; try {/*from www. ja va2 s . c o m*/ log("SSL Enabled ? %s\n", jettySSL != null); log("AJP Enabled ? %s\n", jettyAJP != null); if (jettySSL != null || jettyAJP != null) { File jettyXmlExample = new File(getHomeDirectory(), "etc" + File.separator + "examples" + File.separator + "jetty.xml"); File jettyXml = new File(getHomeDirectory(), "etc" + File.separator + "jetty.xml"); if (!jettyXml.exists() && !jettyXmlExample.exists()) { throw new FileNotFoundException("The required file doesn't exist: " + jettyXmlExample); } if (!jettyXml.exists()) { log("Copying %s into %s\n", jettyXmlExample, jettyXml); FileUtils.copyFile(jettyXmlExample, jettyXml); } log("Creating %s\n", jettyXml); File tempFile = new File(jettyXml.getAbsoluteFile() + ".tmp"); FileWriter w = new FileWriter(tempFile); LineIterator it = FileUtils.lineIterator(jettyXmlExample); boolean startSsl = false; boolean startAjp = false; while (it.hasNext()) { String line = it.next(); if (startAjp) { if (line.matches("^\\s+[<][!]--\\s*$")) { continue; } if (line.matches("^\\s+--[>]\\s*$")) { startAjp = false; ajpWasFixed = true; continue; } } if (startSsl) { if (line.matches("^\\s+[<][!]--\\s*$")) { continue; } if (line.matches("^\\s+--[>]\\s*$")) { startSsl = false; sslWasFixed = true; continue; } } w.write(line + "\n"); if (startAjp == false && line.contains("<!-- Add AJP support -->") && jettyAJP != null) { startAjp = true; log("Enabling AjpConnector\n"); } if (startSsl == false && line.contains("<!-- Add HTTPS support -->") && jettySSL != null) { startSsl = true; log("Enabling SslSelectChannelConnector\n"); } } LineIterator.closeQuietly(it); w.close(); FileUtils.copyFile(tempFile, jettyXml); FileUtils.deleteQuietly(tempFile); } else { log("Neither SSL nor AJP are enabled.\n"); } } catch (Exception e) { throw new OnmsUpgradeException("Can't fix Jetty configuration because " + e.getMessage(), e); } if (jettyAJP != null && !ajpWasFixed) { throw new OnmsUpgradeException( "Can't enable APJ, please manually edit jetty.xml and uncomment the section where org.eclipse.jetty.ajp.Ajp13SocketConnector is defined."); } if (jettySSL != null && !sslWasFixed) { throw new OnmsUpgradeException( "Can't enable SSL, please manually edit jetty.xml and uncomment the section where org.eclipse.jetty.server.ssl.SslSelectChannelConnector is defined."); } }
From source file:org.opennms.upgrade.implementations.JmxRrdMigratorOffline.java
/** * Fixes a JMX configuration file.//from www.j a v a 2s. c o m * * @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.// w w w . ja v a 2s.c om * * @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.opens.referentiel.creator.CodeGeneratorMojo.java
/** * * @return//from w ww . j av a2 s.com */ 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.opensextant.examples.RegexTest.java
/** * The main method.//from w w w .j a v a 2 s . c o m * * @param args * the arguments */ public static void main(String[] args) { // file containing the tab separated test data String testFileName = args[0]; // the file containing the regex definintions String patternFileName = args[1]; // file into which we will write the results String resultFileName = args[2]; File testFile = new File(testFileName); URL patternFile = null; BufferedWriter resWriter = null; // setup the output File resFile = new File(resultFileName); try { resWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(resFile), "UTF-8")); } catch (UnsupportedEncodingException e) { LOGGER.error("Couldnt write to " + resFile.getName() + ":" + e.getMessage(), e); return; } catch (FileNotFoundException e) { LOGGER.error("Couldnt write to " + resFile.getName() + ":" + e.getMessage(), e); return; } // write the results header try { resWriter.write( "Entity Type\tPos\\Neg\tTest Input\tScore\tComplete\tCount\tTypes Found\tRules Matched\tAnnotations Found"); resWriter.newLine(); } catch (IOException e) { LOGGER.error("Couldnt write to " + resFile.getName() + ":" + e.getMessage(), e); } // get the pattern file as a URL try { patternFile = new File(patternFileName).toURI().toURL(); } catch (MalformedURLException e) { LOGGER.error("Couldn't use pattern file " + patternFileName + ":" + e.getMessage(), e); } // initialize the regex matcher RegexMatcher reger = new RegexMatcher(patternFile); LOGGER.info("Loaded " + reger.getRules().size() + " rules " + " for types " + reger.getTypes()); LOGGER.info("Writing results to " + resFile.getAbsolutePath()); // loop over the lines of the test file LineIterator iter = null; try { iter = FileUtils.lineIterator(testFile, "UTF-8"); } catch (IOException e) { LOGGER.error("Couldnt read from " + testFile.getName() + ":" + e.getMessage(), e); } int lineCount = 0; while (iter.hasNext()) { // get next line String line = iter.next(); // skip comments and blank lines if (line == null || line.startsWith("#") || line.trim().length() == 0) { continue; } lineCount++; // get the fields of the line String[] pieces = line.split("[\t\\s]+", 3); String entityType = pieces[0]; String posOrNeg = pieces[1]; String testText = pieces[2]; // send the test text to regex matcher List<RegexAnnotation> annos = reger.match(testText); // examine the results and return a line to be sent to the results // file String results = score(entityType, posOrNeg, testText, annos); annos.clear(); try { // write the original line and the results to the results file resWriter.write(line + "\t" + results); resWriter.newLine(); } catch (IOException e) { LOGGER.error("Couldn't write to " + resFile.getName() + ":" + e.getMessage(), e); } } iter.close(); LOGGER.info("Tagged and scored " + lineCount + " test lines"); // cleanup try { resWriter.flush(); resWriter.close(); } catch (IOException e) { LOGGER.error("Couldn't close " + resFile.getName() + ":" + e.getMessage(), e); } }
From source file:org.opensextant.matching.DataLoader.java
private static String flatten(String currentPath) { File topDir = new File(currentPath).getParentFile(); File input = new File(currentPath); Map<File, String> index = new HashMap<File, String>(); // read the index file into the index Map // loop over the lines of the index file LineIterator indexIter = null; try {/*from w w w . j av a2 s .c o m*/ indexIter = FileUtils.lineIterator(input, "UTF-8"); } catch (IOException e) { LOGGER.error("Couldnt read from " + input.getName() + ":", e); return null; } if (indexIter != null) { while (indexIter.hasNext()) { // get next line String line = indexIter.next(); String[] pieces = line.split(":"); File subFile = new File(topDir, pieces[0]); String tmpVal = pieces[1]; if (pieces.length >= 3) { tmpVal = tmpVal + ":" + pieces[2]; } index.put(subFile, tmpVal); } } File tmp = null; try { tmp = File.createTempFile("vocab", "txt"); } catch (IOException e) { LOGGER.error("Could not create temp file when flattening vocab:", e); return null; } // loop over the files mentioned in the index and write to temp file int indexID = 0; for (File in : index.keySet()) { String[] catAndTax = index.get(in).split(":"); String cat = catAndTax[0]; String tax = ""; if (catAndTax.length > 1) { tax = catAndTax[1]; } else { tax = "NONE"; } // loop over the lines of the subfiles file // write the new flat contents to the temp file LineIterator contentIter = null; try { contentIter = FileUtils.lineIterator(in, "UTF-8"); } catch (IOException e) { LOGGER.error("Couldnt read from " + in.getName(), e); return null; } if (contentIter != null) { while (contentIter.hasNext()) { // get next line String line = contentIter.next(); // concat the pieces String out = indexID + "\t" + line + "\t" + cat + "\t" + tax + "\n"; // write all pieces to temp try { FileUtils.writeStringToFile(tmp, out, "UTF-8", true); } catch (IOException e) { LOGGER.error("Could not write to temp file when flattening vocab:", e); } indexID++; } } } LOGGER.info("Flattened " + indexID + " vocabulary entries to temp file"); // return temp file path return tmp.getAbsolutePath(); }
From source file:org.opentestsystem.delivery.testreg.upload.TextFileUtils.java
public void processTextFile(final InputStream textFile, final String formatType, final LineMapper lineMapper) throws Exception { final String CHARSET = "UTF-8"; final TextFileProcessor<InputStream, String> textFileProcessor = new TextFileProcessor<InputStream, String>() { @Override//w ww . j a v a 2 s . c o m public String process(final InputStream input) throws Exception { LineIterator lineIterator = null; try { lineIterator = IOUtils.lineIterator(textFile, CHARSET); int lineNumber = 1; // First line starts at 1 while (lineIterator.hasNext()) { String line = lineIterator.next(); if (lineNumber == 1) { //worry about BOM chars (/ufeff) line = line.replaceAll("[\uFEFF]", ""); } if (!lineMapper.mapLine(line, lineNumber++, formatType)) { break; } } } catch (RuntimeException | IOException ex) { if (lineIterator != null) { lineIterator.close(); } IOUtils.closeQuietly(textFile); throw ex; } finally { if (lineIterator != null) { lineIterator.close(); } IOUtils.closeQuietly(textFile); } return null; } }; textFileProcessor.process(textFile); }
From source file:org.opoo.press.source.SourceParserImpl.java
@Override public Source parse(SourceEntry sourceEntry) throws NoFrontMatterException { List<String> metaLines = new ArrayList<String>(); List<String> contentLines = new ArrayList<String>(); InputStream stream = null;//w ww . java 2 s . c o m List<String> currentList = metaLines; try { stream = new FileInputStream(sourceEntry.getFile()); LineIterator iterator = IOUtils.lineIterator(stream, "UTF-8"); if (!iterator.hasNext()) { throw new RuntimeException("File not content: " + sourceEntry.getFile()); } String line = iterator.next(); if (!isFrontMatterStartLine(line, sourceEntry)) { log.debug("Maybe a static file: " + sourceEntry.getFile()); throw new NoFrontMatterException(sourceEntry); } boolean hasFrontMatterEndLine = false; //process headers while (iterator.hasNext()) { line = iterator.next(); if (isFrontMatterEndLine(line)) { hasFrontMatterEndLine = true; currentList = contentLines; continue; } currentList.add(line); } if (!hasFrontMatterEndLine) { log.debug("Maybe a static file: " + sourceEntry.getFile()); throw new NoFrontMatterException(sourceEntry); } } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(stream); } StringWriter metaWriter = new StringWriter(); StringWriter contentWriter = new StringWriter(); try { IOUtils.writeLines(metaLines, null, metaWriter); IOUtils.writeLines(contentLines, null, contentWriter); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(contentWriter); IOUtils.closeQuietly(metaWriter); } @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) yaml.load(metaWriter.toString()); String content = contentWriter.toString(); return new SimpleSource(sourceEntry, map, content); }