List of usage examples for com.google.common.io Closer rethrow
public RuntimeException rethrow(Throwable e) throws IOException
From source file:tachyon.shell.TfsShell.java
/** * Load a file or directory in Tachyon space, makes it resident in memory. * * @param filePath The TachyonURI path to load into Tachyon memory * @throws IOException/*from ww w. j a v a2s . c o m*/ */ public void load(TachyonURI filePath) throws IOException { try { TachyonFile fd = mTfs.open(filePath); FileInfo fInfo = mTfs.getInfo(fd); if (fInfo.isFolder) { List<FileInfo> files = mTfs.listStatus(fd); Collections.sort(files); for (FileInfo file : files) { TachyonURI newPath = new TachyonURI(file.getPath()); load(newPath); } } else { if (fInfo.getInMemoryPercentage() == 100) { // The file has already been fully loaded into Tachyon memory. return; } Closer closer = Closer.create(); try { InStreamOptions op = new InStreamOptions.Builder(mTachyonConf) .setTachyonStorageType(TachyonStorageType.STORE).build(); FileInStream in = closer.register(mTfs.getInStream(fd, op)); byte[] buf = new byte[8 * Constants.MB]; while (in.read(buf) != -1) { } } catch (Exception e) { throw closer.rethrow(e); } finally { closer.close(); } } System.out.println(filePath + " loaded"); } catch (TachyonException e) { throw new IOException(e.getMessage()); } }
From source file:org.jclouds.vsphere.compute.config.VSphereComputeServiceAdapter.java
@Override public void destroyNode(String vmName) { Closer closer = Closer.create(); VSphereServiceInstance instance = serviceInstance.get(); closer.register(instance);/*from w ww . java2s. co m*/ try { try { VirtualMachine virtualMachine = getVM(vmName, instance.getInstance().getRootFolder()); Task powerOffTask = virtualMachine.powerOffVM_Task(); if (powerOffTask.waitForTask().equals(Task.SUCCESS)) logger.debug(String.format("VM %s powered off", vmName)); else logger.debug(String.format("VM %s could not be powered off", vmName)); Task destroyTask = virtualMachine.destroy_Task(); if (destroyTask.waitForTask().equals(Task.SUCCESS)) logger.debug(String.format("VM %s destroyed", vmName)); else logger.debug(String.format("VM %s could not be destroyed", vmName)); } catch (Exception e) { logger.error("Can't destroy vm " + vmName, e); throw closer.rethrow(e); } finally { closer.close(); } } catch (IOException e) { logger.trace(e.getMessage(), e); } }
From source file:org.apache.gobblin.runtime.mapreduce.MRJobLauncher.java
/** * Prepare the job input./*w ww. j av a 2 s . c om*/ * @throws IOException */ private void prepareJobInput(List<WorkUnit> workUnits) throws IOException { Closer closer = Closer.create(); try { ParallelRunner parallelRunner = closer .register(new ParallelRunner(this.parallelRunnerThreads, this.fs)); int multiTaskIdSequence = 0; // Serialize each work unit into a file named after the task ID for (WorkUnit workUnit : workUnits) { String workUnitFileName; if (workUnit instanceof MultiWorkUnit) { workUnitFileName = JobLauncherUtils.newMultiTaskId(this.jobContext.getJobId(), multiTaskIdSequence++) + MULTI_WORK_UNIT_FILE_EXTENSION; } else { workUnitFileName = workUnit.getProp(ConfigurationKeys.TASK_ID_KEY) + WORK_UNIT_FILE_EXTENSION; } Path workUnitFile = new Path(this.jobInputPath, workUnitFileName); LOG.debug("Writing work unit file " + workUnitFileName); parallelRunner.serializeToFile(workUnit, workUnitFile); // Append the work unit file path to the job input file } } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:org.pantsbuild.tools.jar.JarBuilder.java
/** * As an optimization, use {@link JarEntryCopier} to copy one jar file to * another without decompressing and recompressing. * * @param writer target to copy JAR file entries to. * @param entries entries that came from a jar file *///ww w . j a v a2 s .c om private void copyJarFiles(JarWriter writer, Iterable<ReadableJarEntry> entries) throws IOException { // Walk the entries to bucketize by input jar file names Multimap<JarSource, ReadableJarEntry> jarEntries = HashMultimap.create(); for (ReadableJarEntry entry : entries) { Preconditions.checkState(entry.getSource() instanceof JarSource); jarEntries.put((JarSource) entry.getSource(), entry); } // Copy the data from each jar input file to the output for (JarSource source : jarEntries.keySet()) { Closer jarFileCloser = Closer.create(); try { final InputSupplier<JarFile> jarSupplier = jarFileCloser .register(new JarSupplier(new File(source.name()))); JarFile jarFile = jarSupplier.getInput(); for (ReadableJarEntry readableJarEntry : jarEntries.get(source)) { JarEntry jarEntry = readableJarEntry.getJarEntry(); String resource = jarEntry.getName(); writer.copy(resource, jarFile, jarEntry); } } catch (IOException ex) { throw jarFileCloser.rethrow(ex); } finally { jarFileCloser.close(); } } }
From source file:alluxio.cli.ConfigurationDocGenerator.java
/** * Writes description of property key to yml files. * * @param defaultKeys Collection which is from PropertyKey DEFAULT_KEYS_MAP.values() * @param filePath path for csv files/*from w w w. j av a2 s.co m*/ */ static void writeYMLFile(Collection<? extends PropertyKey> defaultKeys, String filePath) throws IOException { if (defaultKeys.size() == 0) { return; } FileWriter fileWriter; Closer closer = Closer.create(); String[] fileNames = { "user-configuration.yml", "master-configuration.yml", "worker-configuration.yml", "security-configuration.yml", "key-value-configuration.yml", "common-configuration.yml", "cluster-management-configuration.yml" }; try { // HashMap for FileWriter per each category Map<String, FileWriter> fileWriterMap = new HashMap<>(); for (String fileName : fileNames) { fileWriter = new FileWriter(PathUtils.concatPath(filePath, fileName)); //put fileWriter String key = fileName.substring(0, fileName.indexOf("configuration") - 1); fileWriterMap.put(key, fileWriter); //register file writer closer.register(fileWriter); } // Sort defaultKeys List<PropertyKey> dfkeys = new ArrayList<>(defaultKeys); Collections.sort(dfkeys); for (PropertyKey iteratorPK : dfkeys) { String pKey = iteratorPK.toString(); // Puts descriptions in single quotes to avoid having to escaping reserved characters. // Still needs to escape single quotes with double single quotes. String description = iteratorPK.getDescription().replace("'", "''"); // Write property key and default value to yml files if (iteratorPK.isIgnoredSiteProperty()) { description += " Note: This property must be specified as a JVM property; " + "it is not accepted in alluxio-site.properties."; } String keyValueStr = pKey + ":\n '" + description + "'\n"; if (pKey.startsWith("alluxio.user.")) { fileWriter = fileWriterMap.get("user"); } else if (pKey.startsWith("alluxio.master.")) { fileWriter = fileWriterMap.get("master"); } else if (pKey.startsWith("alluxio.worker.")) { fileWriter = fileWriterMap.get("worker"); } else if (pKey.startsWith("alluxio.security.")) { fileWriter = fileWriterMap.get("security"); } else if (pKey.startsWith("alluxio.keyvalue.")) { fileWriter = fileWriterMap.get("key-value"); } else if (pKey.startsWith("alluxio.integration.")) { fileWriter = fileWriterMap.get("cluster-management"); } else { fileWriter = fileWriterMap.get("common"); } fileWriter.append(keyValueStr); } LOG.info("YML files for description of Property Keys were created successfully."); } catch (Exception e) { throw closer.rethrow(e); } finally { try { closer.close(); } catch (IOException e) { LOG.error("Error while flushing/closing YML files for description of Property Keys " + "FileWriter", e); } } }
From source file:org.sonatype.nexus.timeline.feeds.sources.ErrorWarningFeedSource.java
/** * Extracts ERROR and WARN log lines from given log file. It returns ordered list (newest 1st, oldest last) of * found//from www.jav a2 s . c om * log lines, and that list is maximized to have {@code entriesToExtract} entries. * * @param logFile the log file to scan. * @param entriesToExtract The number how much "newest" entries should be collected. */ private List<FeedEvent> extractEntriesFromLogfile(final File logFile, final int entriesToExtract) throws IOException { final List<FeedEvent> entries = Lists.newArrayList(); Closer closer = Closer.create(); try { final BufferedReader reader = Files.newReader(logFile, Charset.forName("UTF-8")); String logLine = reader.readLine(); while (logLine != null) { if (logLine.contains(" WARN ") || logLine.contains(" ERROR ")) { // FIXME: Grab following stacktrace if any in log // if ( StringUtils.isNotEmpty( item.getStackTrace() ) ) // { // // we need <br/> and to display stack trace on RSS // String stackTrace = item.getStackTrace().replace( // (String) System.getProperties().get( "line.separator" ), // "<br/>" ); // stackTrace = stackTrace.replace( "\t", " " ); // contentValue.append( "<br/>" ).append( stackTrace ); // } final FeedEvent entry = new FeedEvent("LOG", // ignored "ERROR_WARNING", // ignored new Date(), // TODO: timestamp from log file? null, // author "system" null, // no link (maybe log config? or support?) Maps.<String, String>newHashMap()); if (logLine.contains(" ERROR ")) { entry.setTitle("Error"); } else if (logLine.contains(" WARN ")) { entry.setTitle("Warning"); } entry.setContent(logLine); entries.add(entry); if (entries.size() > entriesToExtract) { entries.remove(0); } } logLine = reader.readLine(); } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } return Lists.reverse(entries); }
From source file:alluxio.cli.ConfigurationDocGenerator.java
/** * Writes property key to csv files.//from w ww . j a v a 2 s . com * * @param defaultKeys Collection which is from PropertyKey DEFAULT_KEYS_MAP.values() * @param filePath path for csv files */ static void writeCSVFile(Collection<? extends PropertyKey> defaultKeys, String filePath) throws IOException { if (defaultKeys.size() == 0) { return; } FileWriter fileWriter; Closer closer = Closer.create(); String[] fileNames = { "user-configuration.csv", "master-configuration.csv", "worker-configuration.csv", "security-configuration.csv", "key-value-configuration.csv", "common-configuration.csv", "cluster-management-configuration.csv" }; try { // HashMap for FileWriter per each category Map<String, FileWriter> fileWriterMap = new HashMap<>(); for (String fileName : fileNames) { fileWriter = new FileWriter(PathUtils.concatPath(filePath, fileName)); // Write the CSV file header and line separator after the header fileWriter.append(CSV_FILE_HEADER + "\n"); //put fileWriter String key = fileName.substring(0, fileName.indexOf("configuration") - 1); fileWriterMap.put(key, fileWriter); //register file writer closer.register(fileWriter); } // Sort defaultKeys List<PropertyKey> dfkeys = new ArrayList<>(defaultKeys); Collections.sort(dfkeys); for (PropertyKey propertyKey : dfkeys) { String pKey = propertyKey.toString(); String defaultDescription; if (propertyKey.getDefaultSupplier().get() == null) { defaultDescription = ""; } else { defaultDescription = propertyKey.getDefaultSupplier().getDescription(); } // Quote the whole description to escape characters such as commas. defaultDescription = String.format("\"%s\"", defaultDescription); // Write property key and default value to CSV String keyValueStr = pKey + "," + defaultDescription + "\n"; if (pKey.startsWith("alluxio.user.")) { fileWriter = fileWriterMap.get("user"); } else if (pKey.startsWith("alluxio.master.")) { fileWriter = fileWriterMap.get("master"); } else if (pKey.startsWith("alluxio.worker.")) { fileWriter = fileWriterMap.get("worker"); } else if (pKey.startsWith("alluxio.security.")) { fileWriter = fileWriterMap.get("security"); } else if (pKey.startsWith("alluxio.keyvalue.")) { fileWriter = fileWriterMap.get("key-value"); } else if (pKey.startsWith("alluxio.integration")) { fileWriter = fileWriterMap.get("cluster-management"); } else { fileWriter = fileWriterMap.get("common"); } fileWriter.append(keyValueStr); } LOG.info("Property Key CSV files were created successfully."); } catch (Exception e) { throw closer.rethrow(e); } finally { try { closer.close(); } catch (IOException e) { LOG.error("Error while flushing/closing Property Key CSV FileWriter", e); } } }
From source file:com.googlecode.jmxtrans.model.output.LibratoWriter2.java
@Override public void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results) throws IOException { Closer closer = Closer.create(); try {//from w w w . j a va2 s. c o m JsonGenerator g = closer.register(jsonFactory.createGenerator(writer)); g.writeStartObject(); g.writeArrayFieldStart("counters"); g.writeEndArray(); String source = getSource(server); g.writeArrayFieldStart("gauges"); for (Result result : results) { Map<String, Object> resultValues = result.getValues(); if (resultValues != null) { for (Map.Entry<String, Object> values : resultValues.entrySet()) { if (isNumeric(values.getValue())) { g.writeStartObject(); g.writeStringField("name", KeyUtils.getKeyString(query, result, values, typeNames)); if (source != null && !source.isEmpty()) { g.writeStringField("source", source); } g.writeNumberField("measure_time", SECONDS.convert(result.getEpoch(), MILLISECONDS)); Object value = values.getValue(); if (value instanceof Integer) { g.writeNumberField("value", (Integer) value); } else if (value instanceof Long) { g.writeNumberField("value", (Long) value); } else if (value instanceof Float) { g.writeNumberField("value", (Float) value); } else if (value instanceof Double) { g.writeNumberField("value", (Double) value); } g.writeEndObject(); } } } } g.writeEndArray(); g.writeEndObject(); g.flush(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:se.sics.caracaldb.store.RangeReq.java
@Override public StorageResponse execute(Persistence store) throws IOException { TreeMap<Key, byte[]> results = new TreeMap<Key, byte[]>(); long lengthDiff = 0; long keyNumDiff = 0; Diff diff = null;//from www .j av a 2 s . co m Closer closer = Closer.create(); try { action.prepare(store); byte[] begin = range.begin.getArray(); for (StoreIterator it = closer.register(store.iterator(begin)); it.hasNext(); it.next()) { byte[] key = it.peekKey(); SortedMap<Integer, ByteArrayRef> oldVals = it.peekAllValues(); ByteArrayRef oldVal = null; for (Entry<Integer, ByteArrayRef> e : oldVals.entrySet()) { if (e.getKey() <= maxVersionId) { oldVal = e.getValue(); break; } } if (range.contains(key)) { Pair<Boolean, ByteArrayRef> res = transFilter.execute(oldVal); if (res.getValue0()) { if (limit.read(res.getValue1())) { results.put(new Key(key), res.getValue1().dereference()); long newSize = action.process(key, res.getValue1(), actionVersionId); if (oldVal != null) { if (newSize == 0) { lengthDiff -= oldVal.length; keyNumDiff--; } else if (newSize > 0) { lengthDiff -= oldVal.length - newSize; } } if (!limit.canRead()) { break; } } else { break; } } } else { //special case (a,b) and key is a if (Key.compare(begin, key) != 0) { break; // reached end of range } } } action.commit(); diff = new Diff(lengthDiff, keyNumDiff); } catch (Throwable e) { action.abort(); closer.rethrow(e); } finally { closer.close(); } return new RangeResp(this, results, !limit.canRead(), diff); }
From source file:org.sonatype.nexus.rest.feeds.sources.ErrorWarningFeedSource.java
/** * Extracts ERROR and WARN log lines from given log file. It returns ordered list (newest 1st, oldest last) of * found/* w ww .ja v a 2 s.c o m*/ * log lines, and that list is maximized to have {@code entriesToExtract} entries. * * @param logFile the log file to scan. * @param entriesToExtract The number how much "newest" entries should be collected. */ protected List<SyndEntry> extractEntriesFromLogfile(final File logFile, final int entriesToExtract) throws IOException { final List<SyndEntry> entries = Lists.newArrayList(); Closer closer = Closer.create(); try { final BufferedReader reader = Files.newReader(logFile, Charset.forName("UTF-8")); String logLine = reader.readLine(); while (logLine != null) { if (logLine.contains(" WARN ") || logLine.contains(" ERROR ")) { final SyndEntry entry = new SyndEntryImpl(); entry.setPublishedDate(new Date()); // FIXME: item.getEventDate(); entry.setAuthor(getNexusAuthor()); entry.setLink("/"); if (logLine.contains(" ERROR ")) { entry.setTitle("Error"); } else if (logLine.contains(" WARN ")) { entry.setTitle("Warning"); } final StringBuilder contentValue = new StringBuilder(); contentValue.append(logLine); // FIXME: Grab following stacktrace if any in log // if ( StringUtils.isNotEmpty( item.getStackTrace() ) ) // { // // we need <br/> and to display stack trace on RSS // String stackTrace = item.getStackTrace().replace( // (String) System.getProperties().get( "line.separator" ), // "<br/>" ); // stackTrace = stackTrace.replace( "\t", " " ); // contentValue.append( "<br/>" ).append( stackTrace ); // } SyndContent content = new SyndContentImpl(); content.setType(MediaType.TEXT_PLAIN.toString()); content.setValue(contentValue.toString()); entry.setDescription(content); entries.add(entry); if (entries.size() > entriesToExtract) { entries.remove(0); } } logLine = reader.readLine(); } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } return Lists.reverse(entries); }