List of usage examples for org.apache.commons.io IOUtils readLines
public static List readLines(Reader input) throws IOException
Reader
as a list of Strings, one entry per line. From source file:edu.ucsd.crbs.cws.cluster.submission.TestJobCmdScriptCreatorImpl.java
@Test public void testCreateWithErrorEmailsSet() throws Exception { File tempDirectory = Folder.newFolder(); File outputsDir = new File(tempDirectory + File.separator + Constants.OUTPUTS_DIR_NAME); assertTrue(outputsDir.mkdirs());/*from w w w. ja va 2 s.c o m*/ JobEmailNotificationData emailNotifyData = createJobEmailNotificationData(); emailNotifyData.setErrorEmail("error@error.com"); JobBinaries jb = new JobBinaries(); jb.setKeplerScript("kepler.sh"); jb.setRegisterUpdateJar("register.jar"); jb.setRetryCount(1); JobCmdScriptCreatorImpl scriptCreator = new JobCmdScriptCreatorImpl("/workflowsdir", jb, emailNotifyData); Job j = new Job(); Workflow w = new Workflow(); w.setId(new Long(5)); j.setWorkflow(w); Parameter emailParam = new Parameter(); emailParam.setName(Constants.CWS_NOTIFYEMAIL); emailParam.setValue("bob@bob.com"); ArrayList<Parameter> params = new ArrayList<>(); params.add(emailParam); j.setParameters(params); String jobCmd = scriptCreator.create(tempDirectory.getAbsolutePath(), j, new Long(10)); assertTrue(jobCmd != null); assertTrue( jobCmd.equals(outputsDir.getAbsolutePath() + File.separator + JobCmdScriptCreatorImpl.JOB_CMD_SH)); File checkCmdFile = new File(jobCmd); assertTrue(checkCmdFile.canExecute()); List<String> lines = IOUtils.readLines(new FileReader(jobCmd)); assertTrue(lines != null); boolean emailFound = false; boolean errorEmailFound = false; for (String line : lines) { if (line.startsWith("EMAIL_ADDR=")) { assertTrue(line.equals("EMAIL_ADDR=\"bob@bob.com\"")); emailFound = true; } if (line.startsWith("ERROR_EMAIL_ADDR")) { assertTrue(line.equals("ERROR_EMAIL_ADDR=\"error@error.com\"")); errorEmailFound = true; } } assertTrue(emailFound); assertTrue(errorEmailFound); }
From source file:com.netflix.turbine.monitor.instance.InstanceMonitor.java
/** * Init resources required//from w ww. j a va2s . c o m * @throws Exception */ private void init() throws Exception { HttpGet httpget = new HttpGet(url); HttpResponse response = gatewayHttpClient.getHttpClient().execute(httpget); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); reader = new BufferedReader(new InputStreamReader(is)); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { // this is unexpected. We probably have the wrong endpoint. Print the response out for debugging and give up here. List<String> responseMessage = IOUtils.readLines(reader); logger.error("Could not initiate connection to host, giving up: " + responseMessage); throw new MisconfiguredHostException(responseMessage.toString()); } }
From source file:hadoop.UIUCWikifierAppHadoop.java
/** * Read in NER and NE default config files, write out new config files * with appropriate paths then save config file and return its location * @param pathToWikifierFiles/*from w w w. j ava 2 s . c om*/ * @return * @throws IOException * @throws FileNotFoundException */ private static String[] writeNewConfigFiles(String pathToWikifierFiles) throws FileNotFoundException, IOException { String[] configFiles = new String[3]; //read in old ner config parameters and change List<String> nerConfigLines = IOUtils .readLines(new FileInputStream(new File(pathToWikifierFiles + "/" + pathToDefaultNERConfigFile))); List<String> newNERConfigLines = new ArrayList<String>(); for (String l : nerConfigLines) { String[] values = l.split("\\t+"); StringBuilder newLine = new StringBuilder(); for (String value : values) { if (value.contains("/")) { newLine.append(pathToWikifierFiles + "/" + value); newLine.append("\t"); } else { newLine.append(value); newLine.append("\t"); } } newNERConfigLines.add(newLine.toString().trim()); } //write out new config parameters File newNERConfigFile = File.createTempFile("NER.config", ".tmp"); newNERConfigFile.deleteOnExit(); configFiles[0] = newNERConfigFile.getAbsolutePath(); BufferedWriter nerWriter = new BufferedWriter(new FileWriter(newNERConfigFile)); for (String l : newNERConfigLines) { System.out.println(l); nerWriter.write(l + "\n"); } nerWriter.close(); //read in old ne config parameters and change List<String> neConfigLines = IOUtils .readLines(new FileInputStream(new File(pathToWikifierFiles + "/" + pathToDefaultNEConfigFile))); List<String> newNEConfigLines = new ArrayList<String>(); for (String l : neConfigLines) { String[] values = l.split("="); String value = values[1]; if (value.contains("/")) { String[] paths = value.split("\\s+"); StringBuilder newValue = new StringBuilder(); for (String path : paths) { newValue.append(pathToWikifierFiles + "/" + path); newValue.append(" "); } StringBuilder newLine = new StringBuilder(); newLine.append(values[0]); newLine.append("="); newLine.append(newValue.toString().trim()); newNEConfigLines.add(newLine.toString()); } else { newNEConfigLines.add(l); } } //write out new config parameters File newNEConfigFile = File.createTempFile("config.txt", ".tmp"); newNEConfigFile.deleteOnExit(); configFiles[1] = newNEConfigFile.getAbsolutePath(); BufferedWriter neWriter = new BufferedWriter(new FileWriter(newNEConfigFile)); for (String l : newNEConfigLines) { neWriter.write(l + "\n"); } neWriter.close(); //read in old wordnet properties List<String> wordNetPropertiesLines = IOUtils .readLines(new FileInputStream(new File(pathToWikifierFiles + "/" + pathToDefaultJWNLConfigFile))); List<String> newWordNetPropertiesLines = new ArrayList<String>(); String replacementString = pathToWikifierFiles + "/data/WordNet/"; String stringToReplace = "data/WordNet/"; for (String l : wordNetPropertiesLines) { if (l.contains("dictionary_path")) { newWordNetPropertiesLines.add(l.replace(stringToReplace, replacementString)); } else { newWordNetPropertiesLines.add(l); } } File newWNConfigFile = File.createTempFile("jwnl_properties.xml", ".tmp"); newWNConfigFile.deleteOnExit(); configFiles[2] = newWNConfigFile.getAbsolutePath(); BufferedWriter wnWriter = new BufferedWriter(new FileWriter(newWNConfigFile)); for (String l : newWordNetPropertiesLines) { wnWriter.write(l + "\n"); } wnWriter.close(); return configFiles; }
From source file:com.nesscomputing.db.postgres.embedded.EmbeddedPostgreSQL.java
private static List<String> system(String... command) { try {/* w w w. j av a 2s . c om*/ final Process process = new ProcessBuilder(command).start(); Preconditions.checkState(0 == process.waitFor(), "Process %s failed\n%s", Arrays.asList(command), IOUtils.toString(process.getErrorStream())); try (InputStream stream = process.getInputStream()) { return IOUtils.readLines(stream); } } catch (final Exception e) { throw Throwables.propagate(e); } }
From source file:com.slimgears.slimrepo.core.internal.sql.PrototypeTest.java
private void assertSqlEquals(String resourceId) throws IOException { Assert.assertNotNull(sqlStatements); Assert.assertNotEquals(0, sqlStatements.size()); String actualSql = Joiner.on("\n").join(sqlStatements); String[] actualLines = actualSql.split("\n"); try (InputStream stream = getClass().getResourceAsStream("/sql/" + resourceId)) { List<String> expectedLines = IOUtils.readLines(stream); Assert.assertEquals(expectedLines.size(), actualLines.length); for (int i = 0; i < expectedLines.size(); ++i) { Assert.assertEquals("Line mismatch", expectedLines.get(i), actualLines[i]); }/*from w w w . j a v a2s. c om*/ } }
From source file:info.guardianproject.mrapp.server.YouTubeSubmit.java
private String uploadMetaDataToGetLocation(String uploadUrl, String slug, String contentType, long contentLength, boolean retry) throws IOException { HttpPost hPost = getGDataHttpPost(new URL(uploadUrl).getHost(), uploadUrl, slug); //provide information about the media that is being uploaded hPost.setHeader("X-Upload-Content-Type", contentType); hPost.setHeader("X-Upload-Content-Length", contentLength + ""); hPost.setHeader("X-Upload-Content-Encoding", "utf-8"); String atomData;/*w w w .jav a 2s .c o m*/ String category = DEFAULT_VIDEO_CATEGORY; tags = DEFAULT_VIDEO_TAGS; String template = Util.readFile(activity, R.raw.gdata).toString(); atomData = String.format(template, StringEscapeUtils.escapeHtml4(title), StringEscapeUtils.escapeHtml4(description), category, tags); StringEntity entity = new StringEntity(atomData, HTTP.UTF_8); hPost.setEntity(entity); HttpResponse hResp = httpClient.execute(hPost); int responseCode = hResp.getStatusLine().getStatusCode(); StringBuffer errMsg = new StringBuffer(); InputStream is = hResp.getEntity().getContent(); List<String> list = IOUtils.readLines(is); for (String line : list) { Log.d(LOG_TAG, "http resp line: " + line); errMsg.append(line).append("\n"); } if (responseCode < 200 || responseCode >= 300) { // The response code is 40X if ((responseCode + "").startsWith("4") && retry && accountYouTube != null) { //invalidate our old one, that is locally cached this.clientLoginToken = authorizer.getFreshAuthToken(accountYouTube.name, clientLoginToken); // Try again with fresh token return uploadMetaDataToGetLocation(uploadUrl, slug, contentType, contentLength, false); } else { throw new IOException( String.format(Locale.US, "response code='%s' (code %d)" + " for %s. Output=%s", hResp.getStatusLine().getReasonPhrase(), responseCode, hPost.getRequestLine().getUri(), errMsg.toString())); } } // return urlConnection.getHeaderField("Location"); return hResp.getFirstHeader("Location").getValue(); }
From source file:com.vaadin.integration.maven.wscdn.CvalChecker.java
String readKeyFromFile(URL url, int majorVersion) throws IOException { String majorVersionStr = String.valueOf(majorVersion); List<String> lines = IOUtils.readLines(url.openStream()); String defaultKey = null;/*from w w w . j a va2 s.co m*/ for (String line : lines) { String[] parts = line.split("\\s*=\\s*"); if (parts.length < 2) { defaultKey = parts[0].trim(); } if (parts[0].equals(majorVersionStr)) { return parts[1].trim(); } } return defaultKey; }
From source file:edu.isi.wings.portal.controllers.ComponentController.java
public boolean initializeComponentFiles(String cid, String lang) { try {//from www .j av a 2 s. c o m Component c = cc.getComponent(cid, true); String loc = c.getLocation(); if (loc == null) { loc = cc.getDefaultComponentLocation(cid); c.setLocation(loc); cc.setComponentLocation(cid, loc); cc.save(); } // Copy io.sh from resources ClassLoader classloader = Thread.currentThread().getContextClassLoader(); FileUtils.copyInputStreamToFile(classloader.getResourceAsStream("io.sh"), new File(loc + "/io.sh")); int numi = 0, nump = 0, numo = c.getOutputs().size(); for (ComponentRole r : c.getInputs()) { if (r.isParam()) nump++; else numi++; } String suffix = ""; for (int i = 1; i <= numi; i++) suffix += " $INPUTS" + i; for (int i = 1; i <= nump; i++) suffix += " $PARAMS" + i; for (int i = 1; i <= numo; i++) suffix += " $OUTPUTS" + i; String runscript = ""; String filename = null; for (String line : IOUtils.readLines(classloader.getResourceAsStream("run"))) { if (line.matches(".*io\\.sh.*")) { // Number of inputs and outputs line = ". $BASEDIR/io.sh " + numi + " " + nump + " " + numo + " \"$@\""; } else if (line.matches(".*generic_code.*")) { // Code invocation if (lang.equals("R")) { filename = c.getName() + ".R"; line = "Rscript --no-save --no-restore $BASEDIR/" + filename; } else if (lang.equals("PHP")) { filename = c.getName() + ".php"; line = "php $BASEDIR/" + filename; } else if (lang.equals("Python")) { filename = c.getName() + ".py"; line = "python $BASEDIR/" + filename; } else if (lang.equals("Perl")) { filename = c.getName() + ".pl"; line = "perl $BASEDIR/" + filename; } else if (lang.equals("Java")) { line = "# Relies on existence of " + c.getName() + ".class file in this directory\n"; line += "java -classpath $BASEDIR " + c.getName(); } // Add inputs, outputs as suffixes line += suffix; } runscript += line + "\n"; } File runFile = new File(loc + "/run"); FileUtils.writeStringToFile(runFile, runscript); runFile.setExecutable(true); if (filename != null) new File(loc + "/" + filename).createNewFile(); } catch (Exception e) { e.printStackTrace(); } finally { cc.end(); dc.end(); prov.end(); } return false; }
From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java
/** * Retrieves the list of packages to scan for the specified system property * @param sysProp The system property that overrides * @return the list of packages to scan * @throws IOException if we fail to load a system resource *///w ww. ja v a 2s .c om public String[] getBasePackagesToScan(String sysProp, String resource) throws IOException { String sysPropScanPackages = System.getProperty(sysProp); if (StringUtils.isNotBlank(sysPropScanPackages)) { // The system property is set, use it. return StringUtils.split(sysPropScanPackages, ','); } else { // Search the classpath for the properties file. If not, use default List<String> packages = new ArrayList<>(); InputStream resourceStream = null; try { if (resource != null) { resourceStream = this.getClass().getClassLoader().getResourceAsStream(resource); } if (resourceStream != null) { packages = IOUtils.readLines(resourceStream); } else { packages = Arrays.asList(DEFAULT_SCAN_PACKAGES); } } finally { IOUtils.closeQuietly(resourceStream); } return packages.toArray(new String[packages.size()]); } }
From source file:io.fabric8.maven.core.service.openshift.OpenshiftBuildServiceTest.java
@Test public void checkTarPackage() throws Exception { int nTries = 0; boolean bTestComplete = false; do {/*www. jav a2 s . c o m*/ try { nTries++; BuildService.BuildServiceConfig config = defaultConfig.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true); OpenShiftMockServer mockServer = collector.getMockServer(); OpenShiftClient client = mockServer.createOpenShiftClient(); final OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); ImageConfiguration imageWithEnv = new ImageConfiguration.Builder(image) .buildConfig(new BuildImageConfiguration.Builder(image.getBuildConfiguration()) .env(Collections.singletonMap("FOO", "BAR")).build()) .build(); service.createBuildArchive(imageWithEnv); final List<ArchiverCustomizer> customizer = new LinkedList<>(); new Verifications() { { archiveService.createDockerBuildArchive(withInstanceOf(ImageConfiguration.class), withInstanceOf(MojoParameters.class), withCapture(customizer)); assertTrue(customizer.size() == 1); } }; customizer.get(0).customize(tarArchiver); final List<File> file = new LinkedList<>(); new Verifications() { { String path; tarArchiver.addFile(withCapture(file), path = withCapture()); assertEquals(".s2i/environment", path); } }; assertEquals(1, file.size()); List<String> lines; try (FileReader reader = new FileReader(file.get(0))) { lines = IOUtils.readLines(reader); } assertTrue(lines.contains("FOO=BAR")); bTestComplete = true; } catch (Fabric8ServiceException exception) { Throwable rootCause = getRootCause(exception); logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage()); // Let's wait for a while, and then retry again if (rootCause != null && rootCause instanceof IOException) { continue; } } } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete); }