List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:org.prebake.service.Main.java
public static final void main(String[] argv) { // The prebakery does not read stdin and neither should any execed process. Closeables.closeQuietly(System.in); final Logger logger = Logger.getLogger(Main.class.getName()); CommandLineArgs args = new CommandLineArgs(argv); if (!CommandLineArgs.setUpLogger(args, logger)) { System.out.println(USAGE); System.exit(0);/*from w ww .j av a2 s . com*/ } FileSystem fs = FileSystems.getDefault(); Config config; { MessageQueue mq = new MessageQueue(); config = new CommandLineConfig(fs, mq, args); if (mq.hasErrors()) { System.err.println(USAGE); System.err.println(); for (String msg : mq.getMessages()) { System.err.println(msg); } System.exit(-1); } } ImmutableMap<String, ?> env = CommonEnvironment.makeEnvironment(config.getClientRoot().getRoot(), getSystemPropertyMap()); ScheduledExecutorService execer = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(16)); OperatingSystem os = new RealOperatingSystem(fs, execer); final String token; { byte[] bytes = new byte[256]; new SecureRandom().nextBytes(bytes); token = new BigInteger(bytes).toString(Character.MAX_RADIX); } final Clock clock = SystemClock.instance(); final Path clientRoot = config.getClientRoot(); final LogHydra hydra = new LogHydra(clientRoot.resolve(FileNames.DIR).resolve(FileNames.LOGS), clock) { @Override protected void doInstall(OutputStream[] wrappedInheritedProcessStreams, Handler logHandler) { System.setOut(new PrintStream(wrappedInheritedProcessStreams[0], true)); System.setErr(new PrintStream(wrappedInheritedProcessStreams[1], true)); logger.addHandler(logHandler); } }; hydra.install(new FileOutputStream(FileDescriptor.out), new FileOutputStream(FileDescriptor.err)); final HighLevelLog highLevelLog = new HighLevelLog(clock); final Logs logs = new Logs(highLevelLog, logger, hydra); final Prebakery pb = new Prebakery(config, env, execer, os, logs) { @Override protected String makeToken() { return token; } @Override protected int openChannel(int portHint, final BlockingQueue<Commands> q) throws IOException { final ServerSocket ss = new ServerSocket(portHint); Thread th = new Thread(new Runnable() { public void run() { while (true) { try { boolean closeSock = true; Socket sock = ss.accept(); // TODO: move sock handling to a worker or use java.nio stuff. try { byte[] bytes = ByteStreams.toByteArray(sock.getInputStream()); sock.shutdownInput(); String commandText = new String(bytes, Charsets.UTF_8); try { q.put(Commands.fromJson(clientRoot, new JsonSource(new StringReader(commandText)), sock.getOutputStream())); // Closing sock is now the service's responsibility. closeSock = false; } catch (InterruptedException ex) { continue; } } finally { if (closeSock) { sock.close(); } } } catch (IOException ex) { logger.log(Level.WARNING, "Connection failed", ex); } } } }, Main.class.getName() + "#command_receiver"); th.setDaemon(true); th.start(); return ss.getLocalPort(); } @Override protected Environment createDbEnv(Path dir) { EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); return new Environment(new File(dir.toUri()), envConfig); } @Override protected Map<?, ?> getSysProps() { return Collections.unmodifiableMap(System.getProperties()); } @Override protected Map<String, String> getSysEnv() { return Collections.unmodifiableMap(new ProcessBuilder().environment()); } }; final Server server; if (config.getWwwPort() > 0) { server = new Server(config.getWwwPort()) { @Override public String toString() { return "[Prebake Web Server]"; } }; server.setSendServerVersion(false); server.setHandler(new AbstractHandler() { MainServlet servlet = new MainServlet(token, pb, TimeZone.getDefault()); public void handle(String tgt, Request r, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { try { servlet.service(req, resp); } catch (RuntimeException ex) { logger.log(Level.WARNING, "Web request failed", ex); throw ex; } } }); server.setStopAtShutdown(true); try { server.start(); } catch (Exception ex) { logger.log(Level.SEVERE, "Failed to start http server on port " + config.getWwwPort(), ex); } } else { server = null; } // If an interrupt signal is received, Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { pb.close(); try { if (server != null && !(server.isStopping() || server.isStopped())) { server.stop(); } } catch (Throwable th) { logger.log(Level.SEVERE, "Error shutting down http server", th); // Don't propagate since the process will soon be dead anyway. } } })); final boolean[] exitMutex = new boolean[1]; synchronized (exitMutex) { // Start the prebakery with a handler that will cause the main thread to // complete when it receives a shutdown command or is programatically // closed. pb.start(new Runnable() { public void run() { // When a shutdown command is received, signal the main thread so // it can complete. synchronized (exitMutex) { exitMutex[0] = true; exitMutex.notifyAll(); } } }); // The loop below gets findbugs to shut up about a wait outside a loop. while (!exitMutex[0]) { try { exitMutex.wait(); logger.log(Level.INFO, "Shut down cleanly"); } catch (InterruptedException ex) { // Just exit below if the main thread is interrupted. break; } } } System.exit(0); }
From source file:com.google.android.stardroid.data.AsciiProtoRewriter.java
public static void main(String[] args) throws IOException { if (args.length != 1 || !args[0].endsWith("_R.ascii")) { System.out.println("Usage: AsciiToBinaryProtoWriter <inputprefix>_R.ascii"); System.exit(1);/*www .j a va 2 s. co m*/ } String inputFile = args[0]; String outputFile = args[0].replaceAll("_R.ascii", ".ascii"); BufferedReader in = null; PrintWriter out = null; try { in = new BufferedReader(new FileReader(inputFile)); out = new PrintWriter(new FileWriter(outputFile)); String s; while ((s = in.readLine()) != null) { if (s.contains("REMOVE")) { try { s = s.replaceAll("REMOVE_", ""); String[] tokens = s.split("\\s+"); for (String token : tokens) { if (token.contains("R.string")) { int value = getString(token); s = s.replaceAll(token, "" + value); } } } catch (MissingStringException m) { System.out.println("Warning: " + m.getMessage() + ". Skipping..."); continue; } } out.println(s); } } finally { Closeables.closeQuietly(in); Closeables.close(out, false); } }
From source file:net.sourceforge.docfetcher.website.Website.java
public static void main(String[] args) throws Exception { for (File srcDir : Util.listFiles(websiteDir)) { if (!srcDir.isDirectory()) continue; if (srcDir.getName().equals("all")) continue; File dstDir = new File("dist/website/" + srcDir.getName()); dstDir.mkdirs();/*from ww w . j av a2 s. c o m*/ Util.deleteContents(dstDir); PegDownProcessor processor = new PegDownProcessor(Extensions.TABLES); File propsFile = new File(srcDir, "/page.properties"); PageProperties props = new PageProperties(propsFile); convertDir(processor, props, srcDir, dstDir); } // Deploy files in the website/all directory for (File file : Util.listFiles(new File(websiteDir + "/all"))) { File dstFile = new File("dist/website/all", file.getName()); Files.createParentDirs(dstFile); Files.copy(file, dstFile); } // Deploy PAD file File padFileSrc = new File(websiteDir, "docfetcher-pad-template.xml"); File padFileDst = new File("dist/website", "docfetcher-pad.xml"); String padContents = CharsetDetectorHelper.toString(padFileSrc); padContents = UtilGlobal.replace(padFileSrc.getPath(), padContents, "${version}", version); Files.write(padContents, padFileDst, Charsets.UTF_8); Util.println("File written: " + padFileDst.getPath()); // Deploy English Resource.properties; this is currently used for // GUI translation via transifex.com Properties prop = new Properties(); for (Msg msg : Msg.values()) prop.put(msg.name(), msg.get()); File propFile = new File("dist/website", "Resource.properties"); FileWriter w = new FileWriter(propFile); prop.store(w, ""); Closeables.closeQuietly(w); Util.println("File written: " + propFile.getPath()); // Deploy index.php file File indexPhpFile = new File("dist/website/index.php"); Files.copy(new File(websiteDir, "index.php"), indexPhpFile); Util.println("File written: " + indexPhpFile.getPath()); // Deploy help file for GUI translators File propHelpSrc = new File(websiteDir, "prop-help-template.html"); File propHelpDst = new File("dist/website", "properties-help.html"); StringBuilder sb = new StringBuilder(); for (Msg msg : Msg.values()) { sb.append("<tr align=\"left\">"); sb.append("<td>"); sb.append(escapeHtml(propHelpSrc.getPath(), msg.get())); sb.append("</td>"); sb.append(Util.LS); sb.append("<td>"); sb.append(msg.getComment()); sb.append("</td>"); sb.append("</tr>"); sb.append(Util.LS); } String propHelpContents = CharsetDetectorHelper.toString(propHelpSrc); propHelpContents = UtilGlobal.replace(propHelpSrc.getPath(), propHelpContents, "${contents}", sb.toString()); Files.write(propHelpContents, propHelpDst, Charsets.UTF_8); Util.println("File written: " + propHelpDst.getPath()); }
From source file:com.netflix.exhibitor.application.ExhibitorMain.java
public static void main(String[] args) throws Exception { ExhibitorCreator creator;/* w ww .ja va 2 s .c o m*/ try { creator = new ExhibitorCreator(args); } catch (ExhibitorCreatorExit exit) { if (exit.getError() != null) { System.err.println(exit.getError()); } exit.getCli().printHelp(); return; } SecurityArguments securityArguments = new SecurityArguments(creator.getSecurityFile(), creator.getRealmSpec(), creator.getRemoteAuthSpec()); ExhibitorMain exhibitorMain = new ExhibitorMain(creator.getBackupProvider(), creator.getConfigProvider(), creator.getBuilder(), creator.getHttpPort(), creator.getSecurityHandler(), securityArguments); setShutdown(exhibitorMain); exhibitorMain.start(); try { exhibitorMain.join(); } finally { exhibitorMain.close(); for (Closeable closeable : creator.getCloseables()) { Closeables.closeQuietly(closeable); } } }
From source file:druid.examples.flights.FlightsConverter.java
public static void main(String[] args) throws IOException { DateTimeZone.setDefault(DateTimeZone.UTC); ObjectMapper mapper = new DefaultObjectMapper(); File flightsDataDirectory = new File(args[0]); File flightsOutputDirectory = new File(args[1]); flightsOutputDirectory.mkdirs();/*from w w w. j a v a2 s .c o m*/ for (File flightsDataFile : flightsDataDirectory.listFiles()) { System.out.printf("Processing file[%s]%n", flightsDataFile); CSVParser parser = new CSVParser(); BufferedReader in = null; BufferedWriter out = null; try { in = new BufferedReader(new FileReader(flightsDataFile)); out = new BufferedWriter(new FileWriter( new File(flightsOutputDirectory, flightsDataFile.getName().replace("csv", "json")))); int count = 0; long time = System.currentTimeMillis(); parser.setFieldNames(in.readLine()); String line = null; while ((line = in.readLine()) != null) { if (++count % 100000 == 0) { System.out.printf("File[%s], processed %,d lines in %,d millis.%n", flightsDataFile.getName(), count, System.currentTimeMillis() - time); time = System.currentTimeMillis(); } Map<String, Object> event = parser.parse(line); int year = Integer.parseInt(event.get("Year").toString()); int month = Integer.parseInt(event.get("Month").toString()); int dayOfMonth = Integer.parseInt(event.get("DayofMonth").toString()); int departureTime = Integer.parseInt(event.get("CRSDepTime").toString()); int hourOfDay = departureTime / 100; final int minuteOfHour = departureTime % 100; DateTime timestamp = new DateTime(String.format("%4d-%02d-%02d", year, month, dayOfMonth)) .plus(new Period(hourOfDay, minuteOfHour, 0, 0)); event.put("timestamp", timestamp); for (String metricDimension : METRIC_DIMENSIONS) { String value = event.get(metricDimension).toString(); if (value.equals("NA")) { event.put(metricDimension, 0); } else { event.put(metricDimension, Integer.parseInt(value)); } } out.write(mapper.writeValueAsString(event)); out.write("\n"); } } finally { Closeables.closeQuietly(in); Closeables.closeQuietly(out); } } }
From source file:com.metamx.druid.utils.DruidSetup.java
public static void main(final String[] args) { CuratorFramework curator = null;// w w w . j a v a 2s.co m try { if (args.length < 2 || args.length > 3) { printUsage(); System.exit(1); } String cmd = args[0]; if ("dump".equals(cmd) && args.length == 3) { final String zkConnect = args[1]; curator = connectToZK(zkConnect); curator.start(); String zpathBase = args[2]; dumpFromZk(curator, zkConnect, zpathBase, System.out); } else if ("put".equals(cmd) && args.length == 3) { final String zkConnect = args[1]; curator = connectToZK(zkConnect); curator.start(); final String pfile = args[2]; putToZk(curator, pfile); } else { printUsage(); System.exit(1); } } finally { Closeables.closeQuietly(curator); } }
From source file:tiny.mdhbase.Client.java
/** * // www . j a va 2s .c o m * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Client client = new Client("Sample", 10); try { if (args.length == 0) { showHelp(); } else if (args[0].equals("put")) { int id; if (args.length < 4) { Random idGenerator = new Random(System.nanoTime()); id = idGenerator.nextInt(); } else { id = Integer.parseInt(args[3]); } int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); Point p = new Point(id, x, y); client.insert(p); } else if (args[0].equals("get")) { int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); Iterable<Point> points = client.get(x, y); for (Point point : points) { System.out.println(point); } } else if (args[0].equals("count")) { int xmin = Integer.parseInt(args[1]); int ymin = Integer.parseInt(args[2]); int xmax = Integer.parseInt(args[3]); int ymax = Integer.parseInt(args[4]); System.out.println(String.format("Query Region: [(%d,%d), (%d,%d)]", xmin, ymin, xmax, ymax)); Iterable<Point> points = client.rangeQuery(new Range(xmin, xmax), new Range(ymin, ymax)); System.out.println(String.format("%d hits", Iterables.size(points))); } else if (args[0].equals("index")) { HTable index = new HTable("Sample_index"); System.out.println("bucket name: size"); ResultScanner entries = index.getScanner(Index.FAMILY_INFO); for (Result entry : entries) { byte[] key = entry.getRow(); int prefixLength = Bytes.toInt(entry.getValue(Index.FAMILY_INFO, Index.COLUMN_PREFIX_LENGTH)); long bucketSize = Bytes.toLong(entry.getValue(Index.FAMILY_INFO, Index.COLUMN_BUCKET_SIZE)); System.out.println(String.format("%s: %d", Utils.toString(key, prefixLength), bucketSize)); } } else if (args[0].equals("drop")) { client.close(); HBaseAdmin admin = new HBaseAdmin(HBaseConfiguration.create()); admin.disableTable("Sample_index"); admin.deleteTable("Sample_index"); admin.disableTable("Sample"); admin.deleteTable("Sample"); admin.close(); } else { showHelp(); } } finally { Closeables.closeQuietly(client); } }
From source file:com.mdrsolutions.external.xml.XmlCalls.java
public static final String getXml(String filePath) { String xml = ""; try {// w w w.jav a 2 s . c o m InputStream is = SqlCalls.class.getResourceAsStream(filePath); xml = CharStreams.toString(new InputStreamReader(is)); if (null == xml || xml.isEmpty()) { Closeables.closeQuietly(is); throw new IOException("File path to XML file could not be read!"); } else { Closeables.closeQuietly(is); return xml; } } catch (IOException ex) { logger.error("Could not read the sql file specified!", ex); } return xml; }
From source file:org.trancecode.xml.Sax.java
public static void closeQuietly(final InputSource inputSource) { if (inputSource == null) { return;/*from w w w. j av a2 s. c o m*/ } Closeables.closeQuietly(inputSource.getByteStream()); Closeables.closeQuietly(inputSource.getCharacterStream()); }
From source file:net.awired.visuwall.plugin.jenkins.Downloadables.java
public static String getContent(URL url) throws IOException { InputStream stream = null;//ww w . j a va2 s . c om try { stream = url.openStream(); byte[] content = ByteStreams.toByteArray(stream); String xml = new String(content); return xml; } finally { Closeables.closeQuietly(stream); } }