List of usage examples for com.google.common.io Closer close
@Override public void close() throws IOException
From source file:io.macgyver.plugin.ci.jenkins.JenkinsApiProxy.java
protected void proxyResponse(Response southResponse, HttpServletRequest northRequest, HttpServletResponse northResponse) throws IOException { int statusCode = southResponse.code(); northResponse.setStatus(statusCode); Closer closer = Closer.create(); try {// w w w.j ava2 s. c om Headers h = southResponse.headers(); for (String name : h.names()) { if (name.toLowerCase().equals("content-encoding")) { // do nothing } else if (name.toLowerCase().equals("content-length")) { // do nothing } else { logger.info("Add header: {}:{}", name, h.get(name)); northResponse.addHeader(name, h.get(name)); } } ResponseBody body = southResponse.body(); logger.info("jenkins response: {}", northResponse.getStatus()); String contentType = southResponse.header("content-type"); if (contentType.contains("json")) { String val = rewriteResponseBody(body.string(), northRequest); logger.info("body: {}", val); JsonNode n = mapper.readTree(val); PrintWriter pw = northResponse.getWriter(); closer.register(pw); pw.print(n.toString()); } else if (contentType.contains("xml")) { PrintWriter pw = northResponse.getWriter(); closer.register(pw); String val = rewriteResponseBody(body.string(), northRequest); pw.print(val); } else { OutputStream os = northResponse.getOutputStream(); closer.register(os); InputStream is = body.byteStream(); closer.register(is); ByteStreams.copy(is, os); } } finally { closer.close(); } }
From source file:org.gbif.ipt.task.GenerateDCAT.java
/** * This method loads the DCAT settings from dcatsettings.properties. *///from w ww . ja v a2 s . c o m private Map<String, String> loadDCATSettings() { Map<String, String> loadedSettings = Maps.newHashMap(); Closer closer = Closer.create(); try { InputStream configStream = closer.register(streamUtils.classpathStream(DCAT_SETTINGS)); if (configStream == null) { LOG.error("Failed to load DCAT settings: " + DCAT_SETTINGS); } else { Properties properties = new Properties(); properties.load(configStream); for (Map.Entry<Object, Object> entry : properties.entrySet()) { String key = StringUtils.trim((String) entry.getKey()); String value = StringUtils.trim((String) entry.getValue()); if (key != null && value != null) { loadedSettings.put(key, value); } else { throw new InvalidConfigException(InvalidConfigException.TYPE.INVALID_PROPERTIES_FILE, "Invalid properties file: " + DCAT_SETTINGS); } } LOG.debug("Loaded static DCAT settings: " + loadedSettings.toString()); } } catch (Exception e) { LOG.error("Failed to load DCAT settings from: " + DCAT_SETTINGS, e); } finally { try { closer.close(); } catch (IOException e) { LOG.debug("Failed to close input stream on DCAT settings file: " + DCAT_SETTINGS, e); } } return loadedSettings; }
From source file:org.gbif.ipt.task.GenerateDCAT.java
/** * This method loads the DCAT prefixes from dcat.properties. *///from www. j av a2s . c o m private Map<String, String> loadDCATPrefixes() { HashMap<String, String> prefixes = new HashMap<String, String>(); Closer closer = Closer.create(); try { InputStreamUtils streamUtils = new InputStreamUtils(); InputStream configStream = streamUtils.classpathStream(PREFIXES_PROPERTIES); if (configStream == null) { LOG.error("Could not load DCAT prefixes from file: " + PREFIXES_PROPERTIES); } else { Properties properties = new Properties(); properties.load(configStream); for (Map.Entry<Object, Object> entry : properties.entrySet()) { String key = StringUtils.trim((String) entry.getKey()); String value = StringUtils.trim((String) entry.getValue()); if (key != null && value != null) { prefixes.put(key, value); } else { throw new InvalidConfigException(InvalidConfigException.TYPE.INVALID_PROPERTIES_FILE, "Invalid properties file: " + PREFIXES_PROPERTIES); } } LOG.debug("Loaded DCAT prefixes: " + prefixes.toString()); } } catch (Exception e) { e.printStackTrace(); } finally { try { closer.close(); } catch (IOException e) { LOG.error("Failed to close input stream on DCAT prefixes file: " + PREFIXES_PROPERTIES); } } return prefixes; }
From source file:tachyon.shell.TfsShell.java
/** * Copies a file specified by argv from the filesystem to the local filesystem. This is the * utility function.//from w w w .ja v a2 s . co m * * @param srcPath The source TachyonURI (has to be a file) * @param dstFile The destination file in the local filesystem * @throws IOException */ public void copyFileToLocal(TachyonURI srcPath, File dstFile) throws IOException { try { TachyonFile srcFd = mTfs.open(srcPath); File tmpDst = File.createTempFile("copyToLocal", null); tmpDst.deleteOnExit(); Closer closer = Closer.create(); try { InStreamOptions op = new InStreamOptions.Builder(mTachyonConf) .setTachyonStorageType(TachyonStorageType.NO_STORE).build(); FileInStream is = closer.register(mTfs.getInStream(srcFd, op)); FileOutputStream out = closer.register(new FileOutputStream(tmpDst)); byte[] buf = new byte[64 * Constants.MB]; int t = is.read(buf); while (t != -1) { out.write(buf, 0, t); t = is.read(buf); } if (!tmpDst.renameTo(dstFile)) { throw new IOException( "Failed to rename " + tmpDst.getPath() + " to destination " + dstFile.getPath()); } System.out.println("Copied " + srcPath + " to " + dstFile.getPath()); } finally { closer.close(); } } catch (TachyonException e) { throw new IOException(e.getMessage()); } }
From source file:com.hujiang.gradle.plugin.android.aspectjx.JarMerger.java
public void addJar(@NonNull File file, boolean removeEntryTimestamp) throws IOException { init();/* w w w . j ava 2 s. com*/ Closer localCloser = Closer.create(); try { FileInputStream fis = localCloser.register(new FileInputStream(file)); ZipInputStream zis = localCloser.register(new ZipInputStream(fis)); // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory()) { continue; } String name = entry.getName(); if (filter != null && !filter.checkEntry(name)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } if (removeEntryTimestamp) { newEntry.setTime(0); } // add the entry to the jar archive jarOutputStream.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, count); } // close the entries for this file jarOutputStream.closeEntry(); zis.closeEntry(); } } catch (IZipEntryFilter.ZipAbortException e) { throw new IOException(e); } finally { localCloser.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 ww w .ja v a 2 s .c om */ 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//w w w. j a va2 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: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. java2 s .co 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:alluxio.cli.ConfigurationDocGenerator.java
/** * Writes property key to csv files.//from w w w. j a v a2 s.co m * * @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: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 w w w . j a va 2s . 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); }