Exemple #1
0
 /**
  * Silently dump a file to the given destionation. All {@link IOException}s gets caught and
  * logged, but not re-thrown.
  *
  * @param out dump destination
  * @param file file to dump.
  * @param compressed if {@code true} the denoted file is assumed to be gzipped.
  * @return {@code true} on success (everything read and written).
  * @throws NullPointerException if a parameter is {@code null}.
  */
 public static boolean dump(Writer out, File file, boolean compressed) {
   if (!file.exists()) {
     return false;
   }
   FileInputStream fis = null;
   GZIPInputStream gis = null;
   Reader in = null;
   try {
     if (compressed) {
       fis = new FileInputStream(file);
       gis = new GZIPInputStream(fis);
       in = new InputStreamReader(gis);
     } else {
       in = new FileReader(file);
     }
     dump(out, in);
     return true;
   } catch (IOException e) {
     OpenGrokLogger.getLogger()
         .log(Level.WARNING, "An error occured while piping file " + file + ": ", e);
   } finally {
     IOUtils.close(in);
     IOUtils.close(gis);
     IOUtils.close(fis);
   }
   return false;
 }
Exemple #2
0
  /**
   * Do a best effort to clean up all resources allocated when populating a Lucene document. On
   * normal execution, these resources should be closed automatically by the index writer once it's
   * done with them, but we may not get that far if something fails.
   *
   * @param doc the document whose resources to clean up
   */
  private void cleanupResources(Document doc) {
    for (IndexableField f : doc) {
      // If the field takes input from a reader, close the reader.
      IOUtils.close(f.readerValue());

      // If the field takes input from a token stream, close the
      // token stream.
      if (f instanceof Field) {
        IOUtils.close(((Field) f).tokenStreamValue());
      }
    }
  }
  /** @param directory Directory where we list tags */
  @Override
  protected void buildTagList(File directory) {
    this.tagList = new TreeSet<TagEntry>();
    ArrayList<String> argv = new ArrayList<String>();
    ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
    argv.add(cmd);
    argv.add("tags");
    ProcessBuilder pb = new ProcessBuilder(argv);
    pb.directory(directory);
    Process process = null;
    BufferedReader in = null;

    try {
      process = pb.start();
      in = new BufferedReader(new InputStreamReader(process.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        String parts[] = line.split("  *");
        if (parts.length < 2) {
          throw new HistoryException("Tag line contains more than 2 columns: " + line);
        }
        // Grrr, how to parse tags with spaces inside?
        // This solution will loose multiple spaces;-/
        String tag = parts[0];
        for (int i = 1; i < parts.length - 1; ++i) {
          tag += " " + parts[i];
        }
        TagEntry tagEntry = new BazaarTagEntry(Integer.parseInt(parts[parts.length - 1]), tag);
        // Bazaar lists multiple tags on more lines. We need to merge those into single TagEntry
        TagEntry higher = this.tagList.ceiling(tagEntry);
        if (higher != null && higher.equals(tagEntry)) {
          // Found in the tree, merge tags
          this.tagList.remove(higher);
          tagEntry.setTags(higher.getTags() + ", " + tag);
        }
        this.tagList.add(tagEntry);
      }
    } catch (IOException e) {
      OpenGrokLogger.getLogger().log(Level.WARNING, "Failed to read tag list: {0}", e.getMessage());
      this.tagList = null;
    } catch (HistoryException e) {
      OpenGrokLogger.getLogger()
          .log(Level.WARNING, "Failed to parse tag list: {0}", e.getMessage());
      this.tagList = null;
    }

    IOUtils.close(in);
    if (process != null) {
      try {
        process.exitValue();
      } catch (IllegalThreadStateException e) {
        // the process is still running??? just kill it..
        process.destroy();
      }
    }
  }
  @Override
  public Annotation annotate(File file, String revision) throws IOException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = null;
    try {
      saxParser = factory.newSAXParser();
    } catch (Exception ex) {
      IOException err = new IOException("Failed to create SAX parser", ex);
      throw err;
    }

    ArrayList<String> argv = new ArrayList<String>();
    ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
    argv.add(cmd);
    argv.add("annotate");
    argv.add("--trust-server-cert");
    argv.add("--non-interactive");
    argv.add("--xml");
    if (revision != null) {
      argv.add("-r");
      argv.add(revision);
    }
    argv.add(escapeFileName(file.getName()));
    ProcessBuilder pb = new ProcessBuilder(argv);
    pb.directory(file.getParentFile());
    Process process = null;
    BufferedInputStream in = null;
    Annotation ret = null;
    try {
      process = pb.start();
      in = new BufferedInputStream(process.getInputStream());

      AnnotateHandler handler = new AnnotateHandler(file.getName());
      try {
        saxParser.parse(in, handler);
        ret = handler.annotation;
      } catch (Exception e) {
        OpenGrokLogger.getLogger()
            .log(Level.SEVERE, "An error occurred while parsing the xml output", e);
      }
    } finally {
      IOUtils.close(in);
      if (process != null) {
        try {
          process.exitValue();
        } catch (IllegalThreadStateException e) {
          // the process is still running??? just kill it..
          process.destroy();
        }
      }
    }
    return ret;
  }
Exemple #5
0
 public void close() throws IOException {
   IOUtils.close(ctagsIn);
   if (ctags != null) {
     ctags.destroy();
   }
 }
Exemple #6
0
  /**
   * Get all data required to create a diff view wrt. to this request in one go.
   *
   * @return an instance with just enough information to render a sufficient view. If not all
   *     required parameters were given either they are supplemented with reasonable defaults if
   *     possible, otherwise the related field(s) are {@code null}. {@link DiffData#errorMsg} {@code
   *     != null} indicates, that an error occured and one should not try to render a view.
   */
  public DiffData getDiffData() {
    DiffData data = new DiffData();
    data.path = getPath().substring(0, path.lastIndexOf('/'));
    data.filename = Util.htmlize(getResourceFile().getName());

    String srcRoot = getSourceRootPath();
    String context = req.getContextPath();

    String[] filepath = new String[2];
    data.rev = new String[2];
    data.file = new String[2][];
    data.param = new String[2];

    /*
     * Basically the request URI looks like this:
     * http://$site/$webapp/diff/$resourceFile?r1=$fileA@$revA&r2=$fileB@$revB
     * The code below extracts file path and revision from the URI.
     */
    for (int i = 1; i <= 2; i++) {
      String[] tmp = null;
      String p = req.getParameter("r" + i);
      if (p != null) {
        tmp = p.split("@");
      }
      if (tmp != null && tmp.length == 2) {
        filepath[i - 1] = tmp[0];
        data.rev[i - 1] = tmp[1];
      }
    }
    if (data.rev[0] == null
        || data.rev[1] == null
        || data.rev[0].length() == 0
        || data.rev[1].length() == 0
        || data.rev[0].equals(data.rev[1])) {
      data.errorMsg =
          "Please pick two revisions to compare the changed "
              + "from the <a href=\""
              + context
              + Prefix.HIST_L
              + getUriEncodedPath()
              + "\">history</a>";
      return data;
    }
    data.genre = AnalyzerGuru.getGenre(getResourceFile().getName());

    if (data.genre == null || txtGenres.contains(data.genre)) {
      InputStream[] in = new InputStream[2];
      try {
        // Get input stream for both older and newer file.
        for (int i = 0; i < 2; i++) {
          File f = new File(srcRoot + filepath[i]);
          in[i] = HistoryGuru.getInstance().getRevision(f.getParent(), f.getName(), data.rev[i]);
          if (in[i] == null) {
            data.errorMsg =
                "Unable to get revision "
                    + Util.htmlize(data.rev[i])
                    + " for file: "
                    + Util.htmlize(getPath());
            return data;
          }
        }

        /*
         * If the genre of the older revision cannot be determined,
         * (this can happen if the file was empty), try with newer
         * version.
         */
        for (int i = 0; i < 2 && data.genre == null; i++) {
          try {
            data.genre = AnalyzerGuru.getGenre(in[i]);
          } catch (IOException e) {
            data.errorMsg = "Unable to determine the file type: " + Util.htmlize(e.getMessage());
          }
        }

        if (data.genre != Genre.PLAIN && data.genre != Genre.HTML) {
          return data;
        }

        ArrayList<String> lines = new ArrayList<>();
        Project p = getProject();
        for (int i = 0; i < 2; i++) {
          try (BufferedReader br =
              new BufferedReader(ExpandTabsReader.wrap(new InputStreamReader(in[i]), p))) {
            String line;
            while ((line = br.readLine()) != null) {
              lines.add(line);
            }
            data.file[i] = lines.toArray(new String[lines.size()]);
            lines.clear();
          }
          in[i] = null;
        }
      } catch (Exception e) {
        data.errorMsg = "Error reading revisions: " + Util.htmlize(e.getMessage());
      } finally {
        for (int i = 0; i < 2; i++) {
          IOUtils.close(in[i]);
        }
      }
      if (data.errorMsg != null) {
        return data;
      }
      try {
        data.revision = Diff.diff(data.file[0], data.file[1]);
      } catch (DifferentiationFailedException e) {
        data.errorMsg = "Unable to get diffs: " + Util.htmlize(e.getMessage());
      }
      for (int i = 0; i < 2; i++) {
        try {
          URI u = new URI(null, null, null, filepath[i] + "@" + data.rev[i], null);
          data.param[i] = u.getRawQuery();
        } catch (URISyntaxException e) {
          LOGGER.log(Level.WARNING, "Failed to create URI: ", e);
        }
      }
      data.full = fullDiff();
      data.type = getDiffType();
    }
    return data;
  }