Package com.google.common.io

Examples of com.google.common.io.Closer


            System.out.println(getProductInfo());
        }
    }

    private static void backup(String[] args) throws IOException {
        Closer closer = Closer.create();
        String h = "backup { /path/to/oak/repository | mongodb://host:port/database } <path/to/backup>";
        try {
            NodeStore store = bootstrapNodeStore(args, closer, h);
            FileStoreBackup.backup(store, new File(args[1]));
        } catch (Throwable e) {
            throw closer.rethrow(e);
        } finally {
            closer.close();
        }
    }
View Full Code Here


            closer.close();
        }
    }

    private static void restore(String[] args) throws IOException {
        Closer closer = Closer.create();
        String h = "restore { /path/to/oak/repository | mongodb://host:port/database } <path/to/backup>";
        try {
            NodeStore store = bootstrapNodeStore(args, closer, h);
            FileStoreRestore.restore(new File(args[1]), store);
        } catch (Throwable e) {
            throw closer.rethrow(e);
        } finally {
            closer.close();
        }
    }
View Full Code Here

    }

    private static void recovery(String[] args) throws IOException {
        MapFactory.setInstance(new MapDBMapFactory());
        Closer closer = Closer.create();
        String h = "recovery mongodb://host:port/database { dryRun }";
        try {
            NodeStore store = bootstrapNodeStore(args, closer, h);
            if (!(store instanceof DocumentNodeStore)) {
                System.err.println("Recovery only available for DocumentNodeStore");
                System.exit(1);
            }
            DocumentNodeStore dns = (DocumentNodeStore) store;
            if (!(dns.getDocumentStore() instanceof MongoDocumentStore)) {
                System.err.println("Recovery only available for MongoDocumentStore");
                System.exit(1);
            }
            MongoDocumentStore docStore = (MongoDocumentStore) dns.getDocumentStore();
            LastRevRecoveryAgent agent = new LastRevRecoveryAgent(dns);
            MongoMissingLastRevSeeker seeker = new MongoMissingLastRevSeeker(docStore);
            CloseableIterable<NodeDocument> docs = seeker.getCandidates(0);
            closer.register(docs);
            boolean dryRun = Arrays.asList(args).contains("dryRun");
            agent.recover(docs.iterator(), dns.getClusterId(), dryRun);
        } catch (Throwable e) {
            throw closer.rethrow(e);
        } finally {
            closer.close();
        }
    }
View Full Code Here

     *          returns the number of the affected documents even if
     *          {@code dryRun} is set true and no document was changed.
     */
    public int recover(Iterator<NodeDocument> suspects,
                       int clusterId, boolean dryRun) {
        Closer closer = Closer.create();
        try {
            UnsavedModifications unsaved = new UnsavedModifications();
            closer.register(unsaved);
            UnsavedModifications unsavedParents = new UnsavedModifications();
            closer.register(unsavedParents);

            //Map of known last rev of checked paths
            UnsavedModifications knownLastRevs = new UnsavedModifications();
            closer.register(knownLastRevs);

            long count = 0;
            while (suspects.hasNext()) {
                NodeDocument doc = suspects.next();
                count++;
                if (count % 100000 == 0) {
                    log.info("Scanned {} suspects so far...", count);
                }

                Revision currentLastRev = doc.getLastRev().get(clusterId);
                if (currentLastRev != null) {
                    knownLastRevs.put(doc.getPath(), currentLastRev);
                }
                Revision lostLastRev = determineMissedLastRev(doc, clusterId);

                //1. Update lastRev for this doc
                if (lostLastRev != null) {
                    unsaved.put(doc.getPath(), lostLastRev);
                }

                Revision lastRevForParents = lostLastRev != null ? lostLastRev : currentLastRev;

                //If both currentLastRev and lostLastRev are null it means
                //that no change is done by suspect cluster on this document
                //so nothing needs to be updated. Probably it was only changed by
                //other cluster nodes. If this node is parent of any child node which
                //has been modified by cluster then that node roll up would
                //add this node path to unsaved

                //2. Update lastRev for parent paths aka rollup
                if (lastRevForParents != null) {
                    String path = doc.getPath();
                    while (true) {
                        if (PathUtils.denotesRoot(path)) {
                            break;
                        }
                        path = PathUtils.getParentPath(path);
                        unsavedParents.put(path, lastRevForParents);
                    }
                }
            }

            for (String parentPath : unsavedParents.getPaths()) {
                Revision calcLastRev = unsavedParents.get(parentPath);
                Revision knownLastRev = knownLastRevs.get(parentPath);

                //Copy the calcLastRev of parent only if they have changed
                //In many case it might happen that parent have consistent lastRev
                //This check ensures that unnecessary updates are not made
                if (knownLastRev == null
                        || calcLastRev.compareRevisionTime(knownLastRev) > 0) {
                    unsaved.put(parentPath, calcLastRev);
                }
            }

            //Note the size before persist as persist operation
            //would empty the internal state
            int size = unsaved.getPaths().size();
            String updates = unsaved.toString();

            if (dryRun) {
                log.info("Dry run of lastRev recovery identified [{}] documents for " +
                        "cluster node [{}]: {}", size, clusterId, updates);
            } else {
                //UnsavedModifications is designed to be used in concurrent
                //access mode. For recovery case there is no concurrent access
                //involve so just pass a new lock instance
                unsaved.persist(nodeStore, new ReentrantLock());

                log.info("Updated lastRev of [{}] documents while performing lastRev recovery for " +
                        "cluster node [{}]: {}", size, clusterId, updates);
            }

            return size;
        } finally {
            try {
                closer.close();
            } catch (IOException e) {
                log.warn("Error closing UnsavedModifications", e);
            }
        }
    }
View Full Code Here

  private SshCommandResult execute(Session session, String script)
      throws Exception {
    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    try {
      channel.setCommand(this.script);
      Closer closer = Closer.create();
      InputStream stdoutStream = channel.getInputStream();
      InputStream stderrStream = channel.getErrStream();
      Reader stdoutReader = closer.register(new InputStreamReader(
          stdoutStream, "UTF-8"));
      Reader stderrReader = closer.register(new InputStreamReader(
          stderrStream, "UTF-8"));
      try {
        // executes the command
        channel.connect();
        String stdout = CharStreams.toString(stdoutReader);
        String stderr = CharStreams.toString(stderrReader);
        int exitStatus = channel.getExitStatus();
        return new SshCommandResult(exitStatus, stdout, stderr);
      } finally {
        closer.close();
      }
    } finally {
      channel.disconnect();
    }
  }
View Full Code Here

        } catch (IllegalArgumentException e) {
            return null;
        }

        try {
            Closer closer = Closer.create();
            InputStream in = closer.register(url.openStream());
            try {
                Properties props = new Properties();
                props.load(in);

                return props.getProperty("version");
            } finally {
                closer.close();
            }
        } catch (IOException e) {
            return null;
        }
    }
View Full Code Here

      public void receiveQuit(QuitEvent e) {
        slave.end = System.currentTimeMillis();
      }     
    });

    Closer closer = Closer.create();
    closer.register(eventStream);
    closer.register(w);
    try {
      OutputStream sysout = closer.register(new BufferedOutputStream(new FileOutputStream(sysoutFile)));
      OutputStream syserr = closer.register(new BufferedOutputStream(new FileOutputStream(syserrFile)));
      RandomAccessFile streamsBuffer = closer.register(new RandomAccessFile(streamsBufferFile, "rw"));

      Execute execute = forkProcess(slave, eventBus, commandline, eventStream, sysout, syserr, streamsBuffer);
      log("Forked JVM J" + slave.id + " finished with exit code: " + execute.getExitValue(), Project.MSG_DEBUG);

      if (execute.isFailure()) {
        final int exitStatus = execute.getExitValue();
        switch (exitStatus) {
          case SlaveMain.ERR_NO_JUNIT:
            throw new BuildException("Forked JVM's classpath must include a junit4 JAR.");
          case SlaveMain.ERR_OLD_JUNIT:
            throw new BuildException("Forked JVM's classpath must use JUnit 4.10 or newer.");
          default:
            Closeables.close(sysout, false);
            Closeables.close(syserr, false);

            StringBuilder message = new StringBuilder();
            if (exitStatus == SlaveMain.ERR_OOM) {
              message.append("Forked JVM ran out of memory.");
            } else {
              message.append("Forked process returned with error code: ").append(exitStatus).append(".");
            }

            if (sysoutFile.length() > 0 || syserrFile.length() > 0) {
              if (exitStatus != SlaveMain.ERR_OOM) {
                message.append(" Very likely a JVM crash. ");
              }

              if (jvmOutputAction.contains(JvmOutputAction.PIPE)) {
                message.append(" Process output piped in logs above.");
              } else if (!jvmOutputAction.contains(JvmOutputAction.IGNORE)) {
                if (sysoutFile.length() > 0) {
                  message.append(" See process stdout at: " + sysoutFile.getAbsolutePath());
                }
                if (syserrFile.length() > 0) {
                  message.append(" See process stderr at: " + syserrFile.getAbsolutePath());
                }
              }
            }
            throw new BuildException(message.toString());
        }
      }
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      try {
        closer.close();
      } finally {
        Files.asByteSource(classNamesDynamic).copyTo(Files.asByteSink(classNamesFile, FileWriteMode.APPEND));
        classNamesDynamic.delete();
        streamsBufferFile.delete();
       
View Full Code Here

  public static void main(String[] args) throws Exception {
    File inputFile = new File(args[0]);

    Gson gson = Serializer.createGSon(DumpStreamsFromEventStream.class.getClassLoader());

    Closer closer = Closer.create();
    try {
      OutputStream sysout = new BufferedOutputStream(new FileOutputStream(new File(inputFile.getAbsolutePath() + ".sysout")));
      closer.register(sysout);

      OutputStream syserr = new BufferedOutputStream(new FileOutputStream(new File(inputFile.getAbsolutePath() + ".syserr")));
      closer.register(syserr);

      InputStream is = new BufferedInputStream(new FileInputStream(inputFile));
      closer.register(is);
      JsonReader input = new JsonReader(new InputStreamReader(is, Charsets.UTF_8));
      input.setLenient(true);

      JsonToken peek;
      while (true) {
        peek = input.peek();
       
        if (peek == JsonToken.END_DOCUMENT) {
          return;
        }
 
        input.beginArray();
        EventType type = EventType.valueOf(input.nextString());
        switch (type) {
          case APPEND_STDERR:
            ((IStreamEvent) gson.fromJson(input, type.eventClass)).copyTo(syserr);
            break;
 
          case APPEND_STDOUT:
            ((IStreamEvent) gson.fromJson(input, type.eventClass)).copyTo(sysout);
            break;
 
          default:
            input.skipValue();
        }
        input.endArray();
      }
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

  /**
   * Writes back hints file.
   */
  public static void writeHints(File file, Map<String,List<Long>> hints) throws IOException {
    Closer closer = Closer.create();
    try {
      BufferedWriter w = closer.register(Files.newWriter(file, Charsets.UTF_8));
      if (!(hints instanceof SortedMap)) {
        hints = new TreeMap<String,List<Long>>(hints);
      }
     
      Joiner joiner = Joiner.on(',');
      for (Map.Entry<String,List<Long>> e : hints.entrySet()) {
        w.write(e.getKey());
        w.write("=");
        joiner.appendTo(w, e.getValue());
        w.write("\n");
      }
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

      public void receiveQuit(QuitEvent e) {
        slave.end = System.currentTimeMillis();
      }     
    });

    Closer closer = Closer.create();
    closer.register(eventStream);
    closer.register(w);
    try {
      OutputStream sysout = closer.register(new BufferedOutputStream(new FileOutputStream(sysoutFile)));
      OutputStream syserr = closer.register(new BufferedOutputStream(new FileOutputStream(syserrFile)));
      RandomAccessFile streamsBuffer = closer.register(new RandomAccessFile(streamsBufferFile, "rw"));

      Execute execute = forkProcess(slave, eventBus, commandline, eventStream, sysout, syserr, streamsBuffer);
      log("Forked JVM J" + slave.id + " finished with exit code: " + execute.getExitValue(), Project.MSG_DEBUG);

      if (execute.isFailure()) {
        final int exitStatus = execute.getExitValue();
        switch (exitStatus) {
          case SlaveMain.ERR_NO_JUNIT:
            throw new BuildException("Forked JVM's classpath must include a junit4 JAR.");
          case SlaveMain.ERR_OLD_JUNIT:
            throw new BuildException("Forked JVM's classpath must use JUnit 4.10 or newer.");
          default:
            Closeables.close(sysout, false);
            Closeables.close(syserr, false);

            StringBuilder message = new StringBuilder();
            if (exitStatus == SlaveMain.ERR_OOM) {
              message.append("Forked JVM ran out of memory.");
            } else {
              message.append("Forked process returned with error code: ").append(exitStatus).append(".");
            }

            if (sysoutFile.length() > 0 || syserrFile.length() > 0) {
              if (exitStatus != SlaveMain.ERR_OOM) {
                message.append(" Very likely a JVM crash. ");
              }

              if (jvmOutputAction.contains(JvmOutputAction.PIPE)) {
                message.append(" Process output piped in logs above.");
              } else if (!jvmOutputAction.contains(JvmOutputAction.IGNORE)) {
                if (sysoutFile.length() > 0) {
                  message.append(" See process stdout at: " + sysoutFile.getAbsolutePath());
                }
                if (syserrFile.length() > 0) {
                  message.append(" See process stderr at: " + syserrFile.getAbsolutePath());
                }
              }
            }
            throw new BuildException(message.toString());
        }
      }
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      try {
        closer.close();
      } finally {
        Files.asByteSource(classNamesDynamic).copyTo(Files.asByteSink(classNamesFile, FileWriteMode.APPEND));
        classNamesDynamic.delete();
        streamsBufferFile.delete();
       
View Full Code Here

TOP

Related Classes of com.google.common.io.Closer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.