List of usage examples for java.nio.file FileSystems getDefault
public static FileSystem getDefault()
From source file:de.huberlin.cuneiform.compiler.local.LocalDispatcher.java
protected Set<JsonReportEntry> dispatch(Invocation invocation) throws IOException, InterruptedException, NotDerivableException, JSONException { File scriptFile;//from w w w. ja v a 2 s. com Process process; int exitValue; Set<JsonReportEntry> report; String line; String[] arg; String value; int i; StringBuffer buf; File location; File reportFile; StreamConsumer stdoutConsumer, errConsumer; ExecutorService executor; String signature; Path srcPath, destPath; File successMarker; if (invocation == null) throw new NullPointerException("Invocation must not be null."); if (!invocation.isReady()) throw new RuntimeException("Cannot dispatch invocation that is not ready."); location = new File(buildDir.getAbsolutePath() + "/" + invocation.getSignature()); successMarker = new File(location.getAbsolutePath() + "/" + SUCCESS_FILENAME); reportFile = new File(location.getAbsolutePath() + "/" + Invocation.REPORT_FILENAME); if (!successMarker.exists()) { if (location.exists()) FileUtils.deleteDirectory(location); if (!location.mkdirs()) throw new IOException("Could not create invocation location."); scriptFile = new File(location.getAbsolutePath() + "/" + SCRIPT_FILENAME); try (BufferedWriter writer = new BufferedWriter(new FileWriter(scriptFile, false))) { // write away script writer.write(invocation.toScript()); } scriptFile.setExecutable(true); for (String filename : invocation.getStageInList()) { if (filename.charAt(0) != '/' && filename.indexOf('_') >= 0) { signature = filename.substring(0, filename.indexOf('_')); srcPath = FileSystems.getDefault() .getPath(buildDir.getAbsolutePath() + "/" + signature + "/" + filename); destPath = FileSystems.getDefault() .getPath(buildDir.getAbsolutePath() + "/" + invocation.getSignature() + "/" + filename); Files.createSymbolicLink(destPath, srcPath); } } arg = new String[] { "/usr/bin/time", "-a", "-o", location.getAbsolutePath() + "/" + Invocation.REPORT_FILENAME, "-f", "{" + JsonReportEntry.ATT_TIMESTAMP + ":" + System.currentTimeMillis() + "," + JsonReportEntry.ATT_RUNID + ":\"" + invocation.getDagId() + "\"," + JsonReportEntry.ATT_TASKID + ":" + invocation.getTaskNodeId() + "," + JsonReportEntry.ATT_TASKNAME + ":\"" + invocation.getTaskName() + "\"," + JsonReportEntry.ATT_LANG + ":\"" + invocation.getLangLabel() + "\"," + JsonReportEntry.ATT_INVOCID + ":" + invocation.getSignature() + "," + JsonReportEntry.ATT_KEY + ":\"" + JsonReportEntry.KEY_INVOC_TIME + "\"," + JsonReportEntry.ATT_VALUE + ":" + "{\"realTime\":%e,\"userTime\":%U,\"sysTime\":%S," + "\"maxResidentSetSize\":%M,\"avgResidentSetSize\":%t," + "\"avgDataSize\":%D,\"avgStackSize\":%p,\"avgTextSize\":%X," + "\"nMajPageFault\":%F,\"nMinPageFault\":%R," + "\"nSwapOutMainMem\":%W,\"nForcedContextSwitch\":%c," + "\"nWaitContextSwitch\":%w,\"nIoRead\":%I,\"nIoWrite\":%O," + "\"nSocketRead\":%r,\"nSocketWrite\":%s,\"nSignal\":%k}}", scriptFile.getAbsolutePath() }; // run script process = Runtime.getRuntime().exec(arg, null, location); executor = Executors.newCachedThreadPool(); stdoutConsumer = new StreamConsumer(process.getInputStream()); executor.execute(stdoutConsumer); errConsumer = new StreamConsumer(process.getErrorStream()); executor.execute(errConsumer); executor.shutdown(); exitValue = process.waitFor(); if (!executor.awaitTermination(4, TimeUnit.SECONDS)) throw new RuntimeException("Consumer threads did not finish orderly."); try (BufferedWriter reportWriter = new BufferedWriter(new FileWriter(reportFile, true))) { if (exitValue != 0) { System.err.println("[script]"); try (BufferedReader reader = new BufferedReader(new StringReader(invocation.toScript()))) { i = 0; while ((line = reader.readLine()) != null) System.err.println(String.format("%02d %s", ++i, line)); } System.err.println("[out]"); try (BufferedReader reader = new BufferedReader( new StringReader(stdoutConsumer.getContent()))) { while ((line = reader.readLine()) != null) System.err.println(line); } System.err.println("[err]"); try (BufferedReader reader = new BufferedReader(new StringReader(errConsumer.getContent()))) { while ((line = reader.readLine()) != null) System.err.println(line); } System.err.println("[end]"); throw new RuntimeException("Invocation of task '" + invocation.getTaskName() + "' with signature " + invocation.getSignature() + " terminated with non-zero exit value. Exit value was " + exitValue + "."); } try (BufferedReader reader = new BufferedReader(new StringReader(stdoutConsumer.getContent()))) { buf = new StringBuffer(); while ((line = reader.readLine()) != null) buf.append(line.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\"")).append('\n'); value = buf.toString(); if (!value.isEmpty()) reportWriter.write(new JsonReportEntry(invocation, JsonReportEntry.KEY_INVOC_STDOUT, value) .toString()); } try (BufferedReader reader = new BufferedReader(new StringReader(errConsumer.getContent()))) { buf = new StringBuffer(); while ((line = reader.readLine()) != null) buf.append(line.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\"")).append('\n'); value = buf.toString(); if (!value.isEmpty()) reportWriter.write(new JsonReportEntry(invocation, JsonReportEntry.KEY_INVOC_STDERR, value) .toString()); } } } // gather report report = new HashSet<>(); try (BufferedReader reader = new BufferedReader(new FileReader(reportFile))) { while ((line = reader.readLine()) != null) { line = line.trim(); if (line.isEmpty()) continue; report.add(new JsonReportEntry(line)); } } invocation.evalReport(report); if (!successMarker.exists()) if (!successMarker.createNewFile()) throw new IOException("Could not create success marker."); return report; }
From source file:se.kth.climate.fast.netcdfparquet.Main.java
private static File[] findFiles(String[] fileNames) { ArrayList<File> files = new ArrayList<>(); for (String fileName : fileNames) { if (fileName.contains("*")) { String[] parts = fileName.split("\\*"); if (parts.length > 2) { LOG.error("Only a single file wildcard ist supported! (in {} -> {})", fileName, Arrays.toString(parts)); System.exit(1);// w w w .j a v a2 s. c om } Path filePath = FileSystems.getDefault().getPath(parts[0]); String filePrefix = filePath.getName(filePath.getNameCount() - 1).toString(); Path directoryPath = filePath.getParent(); if (directoryPath == null) { directoryPath = FileSystems.getDefault().getPath("."); } directoryPath = directoryPath.normalize(); File dir = directoryPath.toFile(); if (dir.exists() && dir.isDirectory() && dir.canRead()) { FileFilter fileFilter; if (parts.length == 1) { fileFilter = new WildcardFileFilter(filePrefix + "*"); } else { fileFilter = new WildcardFileFilter(filePrefix + "*" + parts[1]); } File[] matches = dir.listFiles(fileFilter); for (File f : matches) { files.add(f); } } else { LOG.error("Can't access {} properly!", dir); System.exit(1); } } else { files.add(new File(fileName)); } } return files.toArray(new File[0]); }
From source file:com.acmutv.ontoqa.tool.io.IOManagerTest.java
/** * Tests append to a local resource./*from w ww.j av a 2s.c o m*/ * @throws IOException when errors in I/O. */ @Test public void test_appendResource_local() throws IOException { String resource = IOManagerTest.class.getResource("/tool/sample-append.txt").getPath(); Path path = FileSystems.getDefault().getPath(resource).toAbsolutePath(); Files.write(path, "".getBytes()); IOManager.appendResource(resource, "1"); String actual1 = IOManager.readResource(resource); final String expected1 = "1"; Assert.assertEquals(expected1, actual1); IOManager.appendResource(resource, "1"); String actual2 = IOManager.readResource(resource); final String expected2 = "11"; Assert.assertEquals(expected2, actual2); Files.write(path, "".getBytes()); }
From source file:se.trixon.filebydate.Profile.java
public boolean isValid() { mValidationErrorBuilder = new StringBuilder(); if (mModeCopy == mModeMove) { addValidationError(mBundle.getString("invalid_command")); } else {// ww w. j a va 2 s . c o m updateCommand(); } try { mPathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + mFilePattern); } catch (Exception e) { addValidationError("invalid file pattern: " + mFilePattern); } try { mDateFormat = new SimpleDateFormat(mDatePattern, Options.getInstance().getLocale()); } catch (Exception e) { addValidationError(String.format(mBundle.getString("invalid_date_pattern"), mDatePattern)); } if (mDateSourceString != null) { try { mDateSource = DateSource.valueOf(mDateSourceString.toUpperCase()); } catch (Exception e) { addValidationError(String.format(mBundle.getString("invalid_date_source"), mDateSourceString)); } } if (mCaseBaseString != null) { mCaseBase = NameCase.getCase(mCaseBaseString); if (mCaseBase == null) { addValidationError(String.format(mBundle.getString("invalid_case_base"), mCaseBaseString)); } } if (mCaseExtString != null) { mCaseExt = NameCase.getCase(mCaseExtString); if (mCaseExt == null) { addValidationError(String.format(mBundle.getString("invalid_case_ext"), mCaseExtString)); } } if (mSourceDir == null || !mSourceDir.isDirectory()) { addValidationError(String.format(mBundle.getString("invalid_source_dir"), mSourceDir)); } if (mDestDir == null || !mDestDir.isDirectory()) { addValidationError(String.format(mBundle.getString("invalid_dest_dir"), mDestDir)); } return mValidationErrorBuilder.length() == 0; }
From source file:com.opentable.db.postgres.embedded.BundledPostgresBinaryResolver.java
/** * Unpack archive compressed by tar with bzip2 compression. By default system tar is used * (faster). If not found, then the java implementation takes place. * * @param tbzPath/*from w ww. j av a 2s . c o m*/ * The archive path. * @param targetDir * The directory to extract the content to. */ private static void extractTxz(String tbzPath, String targetDir) throws IOException { try (InputStream in = Files.newInputStream(Paths.get(tbzPath)); XZInputStream xzIn = new XZInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn)) { TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final String individualFile = entry.getName(); final File fsObject = new File(targetDir + "/" + individualFile); if (entry.isSymbolicLink() || entry.isLink()) { Path target = FileSystems.getDefault().getPath(entry.getLinkName()); Files.createSymbolicLink(fsObject.toPath(), target); } else if (entry.isFile()) { byte[] content = new byte[(int) entry.getSize()]; int read = tarIn.read(content, 0, content.length); Verify.verify(read != -1, "could not read %s", individualFile); mkdirs(fsObject.getParentFile()); try (OutputStream outputFile = new FileOutputStream(fsObject)) { IOUtils.write(content, outputFile); } } else if (entry.isDirectory()) { mkdirs(fsObject); } else { throw new UnsupportedOperationException( String.format("Unsupported entry found: %s", individualFile)); } if (individualFile.startsWith("bin/") || individualFile.startsWith("./bin/")) { fsObject.setExecutable(true); } } } }
From source file:org.eclipse.smarthome.core.transform.AbstractFileTransformationService.java
private void initializeWatchService() { try {/*from w w w .j av a2s .c om*/ watchService = FileSystems.getDefault().newWatchService(); watchSubDirectory(""); } catch (IOException e) { logger.error("Unable to start transformation directory monitoring"); } }
From source file:request.processing.ServletLoader.java
/** * Watch dir monitors the current directory for any new files and calls * loadJar on them.// ww w . ja v a 2 s. c om */ public static void watchDir() { WatchService watcher = null; try { watcher = FileSystems.getDefault().newWatchService(); WatchKey key = servletDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); servletDir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY); } catch (IOException x) { System.err.println(x); } while (true) { WatchKey key = null; try { key = watcher.take(); } catch (InterruptedException x) { System.err.println("Interrupted Exception"); x.printStackTrace(); } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); if (kind == StandardWatchEventKinds.OVERFLOW) { continue; } try { System.out.println("Directory Changed!"); Thread.sleep(1000); loadServletsAndConfig(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean valid = key.reset(); if (!valid) { break; } } } }
From source file:org.nlp4l.lucene.TermsExtractor.java
void init() throws IOException { Directory dir = FSDirectory.open(FileSystems.getDefault().getPath(config.getIndex())); reader = DirectoryReader.open(dir);//from w w w . j av a2 s . c om pw = outFile == null ? new PrintWriter(System.out) : new PrintWriter(outFile, "UTF-8"); try { // load CompoundNounScorer class Class<?> aClass = Class.forName(config.getScorer()); Constructor<?> aConstr = aClass.getConstructor(IndexReader.class, String.class, String.class, String.class, String.class); scorer = (CompoundNounScorer) aConstr.newInstance(reader, delimiter, fieldNameCn, fieldNameLn2, fieldNameRn2); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.springframework.cloud.gcp.stream.binder.pubsub.PubSubEmulator.java
private void startEmulator() throws IOException, InterruptedException { boolean configPresent = Files.exists(EMULATOR_CONFIG_PATH); WatchService watchService = null; if (configPresent) { watchService = FileSystems.getDefault().newWatchService(); EMULATOR_CONFIG_DIR.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); }/* ww w . j a va 2 s. co m*/ try { this.emulatorProcess = new ProcessBuilder("gcloud", "beta", "emulators", "pubsub", "start").start(); } catch (IOException ex) { fail("Gcloud not found; leaving host/port uninitialized."); } if (configPresent) { updateConfig(watchService); watchService.close(); } else { createConfig(); } }
From source file:com.talkdesk.geo.GeoCodeRepositoryBuilder.java
/** * Populate country data for the table ISO and lang / lat mapping * * @throws GeoResolverException//from w w w .ja va 2 s. c o m */ public void populateCountryData() throws GeoResolverException { try { if (connection == null) connection = connectToDatabase(); if (!new File(countryDataLocation).exists()) { log.error("No Data file found for countryData. please add to data/country.csv "); return; } Path file = FileSystems.getDefault().getPath(countryDataLocation); Charset charset = Charset.forName("UTF-8"); BufferedReader inputStream = Files.newBufferedReader(file, charset); String buffer; PreparedStatement preparedStatement; preparedStatement = connection.prepareStatement( "INSERT INTO countrycodes (COUNTRY_CODE , LATITUDE, LONGITUDE, NAME)" + " VALUES (?,?,?,?)"); while ((buffer = inputStream.readLine()) != null) { String[] values = buffer.split(","); preparedStatement.setString(1, values[0].trim()); preparedStatement.setFloat(2, Float.parseFloat(values[1].trim())); preparedStatement.setFloat(3, Float.parseFloat(values[2].trim())); preparedStatement.setString(4, values[3].trim()); preparedStatement.execute(); } } catch (SQLException e) { throw new GeoResolverException("Error while executing SQL query", e); } catch (IOException e) { throw new GeoResolverException("Error while accessing input file", e); } log.info("Finished populating Database for country data."); //should close all the connections for memory leaks. }