Esempio n. 1
0
  public static String getValue(String valueType, String folderName, String folderKey)
      throws NativeBuildException {
    Commandline cl = new Commandline();
    cl.setExecutable("reg");
    cl.createArg().setValue("query");
    cl.createArg().setValue(folderName);
    cl.createArg().setValue("/v");
    cl.createArg().setValue(folderKey);

    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

    try {
      int ok = CommandLineUtils.executeCommandLine(cl, stdout, stderr);

      if (ok != 0) {
        return null;
      }
    } catch (CommandLineException e) {
      throw new NativeBuildException(e.getMessage(), e);
    }

    String result = stdout.getOutput();

    int p = result.indexOf(valueType);

    if (p == -1) {
      return null;
    }

    return result.substring(p + valueType.length()).trim();
  }
  /** {@inheritDoc} */
  protected ScmResult executeAddCommand(
      ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean binary)
      throws ScmException {
    Commandline cl = new Commandline();
    cl.setExecutable(accurevExecutable);
    cl.setWorkingDirectory(fileSet.getBasedir().getPath());
    cl.addArguments(new String[] {"add"});
    ArrayList params = new ArrayList();
    AccuRevScmProvider.appendHostToParamsIfNeeded(
        (AccuRevScmProviderRepository) repository, params);
    cl.addArguments((String[]) params.toArray(new String[params.size()]));

    cl.addArguments(makeFileArgs(fileSet.getFileList()));

    final List filesAdded = new ArrayList();
    final CommandLineUtils.StringStreamConsumer stdout =
        new CommandLineUtils.StringStreamConsumer();
    try {
      if (0
          != CommandLineUtils.executeCommandLine(
              cl, new AddCommandStreamConsumer(stdout, filesAdded), stdout)) {
        return new AddScmResult(cl.toString(), null, stdout.getOutput(), false);
      }
    } catch (CommandLineException e) {
      throw new ScmRepositoryException("Cannot exeucute add command", e);
    }
    return new AddScmResult(cl.toString(), filesAdded);
  }
  /** {@inheritDoc} */
  protected ScmResult executeEditCommand(ScmProviderRepository repo, ScmFileSet files)
      throws ScmException {
    Commandline cl =
        createCommandLine((PerforceScmProviderRepository) repo, files.getBasedir(), files);
    PerforceEditConsumer consumer = new PerforceEditConsumer();
    try {
      if (getLogger().isDebugEnabled()) {
        getLogger().debug(PerforceScmProvider.clean("Executing " + cl.toString()));
      }

      CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
      int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, err);

      if (exitCode != 0) {
        String cmdLine = CommandLineUtils.toString(cl.getCommandline());

        StringBuffer msg = new StringBuffer("Exit code: " + exitCode + " - " + err.getOutput());
        msg.append('\n');
        msg.append("Command line was:" + cmdLine);

        throw new CommandLineException(msg.toString());
      }
    } catch (CommandLineException e) {
      if (getLogger().isErrorEnabled()) {
        getLogger().error("CommandLineException " + e.getMessage(), e);
      }
    }

    if (consumer.isSuccess()) {
      return new EditScmResult(cl.toString(), consumer.getEdits());
    }

    return new EditScmResult(
        cl.toString(), "Unable to edit file(s)", consumer.getErrorMessage(), false);
  }
  @Override
  public BlameScmResult executeBlameCommand(
      ScmProviderRepository repo, ScmFileSet workingDirectory, String filename)
      throws ScmException {
    Commandline cl = new Commandline();
    cl.setWorkingDirectory(workingDirectory.getBasedir());
    cl.setExecutable(EXECUTABLE);
    cl.createArg().setValue(filename);

    TfsBlameConsumer consumer = new TfsBlameConsumer(getLogger());
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

    try {
      int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, stderr);

      if (exitCode != 0) {
        return new BlameScmResult(
            cl.toString(),
            "The "
                + EXECUTABLE
                + " command failed. Did you install https://github.com/SonarCommunity/sonar-tfs ?",
            stderr.getOutput(),
            false);
      }
    } catch (CommandLineException ex) {
      throw new ScmException("Error while executing command.", ex);
    }

    return new BlameScmResult(cl.toString(), consumer.getLines());
  }
Esempio n. 5
0
  /** {@inheritDoc} */
  protected ExportScmResult executeExportCommand(
      ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, String outputDirectory)
      throws ScmException {

    if (outputDirectory == null) {
      outputDirectory = fileSet.getBasedir().getAbsolutePath();
    }

    SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;

    String url = repository.getUrl();

    if (version != null && StringUtils.isNotEmpty(version.getName())) {
      if (version instanceof ScmTag) {
        url = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
      } else if (version instanceof ScmBranch) {
        url = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
      }
    }

    url = SvnCommandUtils.fixUrl(url, repository.getUser());

    Commandline cl =
        createCommandLine(
            (SvnScmProviderRepository) repo, fileSet.getBasedir(), version, url, outputDirectory);

    SvnUpdateConsumer consumer = new SvnUpdateConsumer(getLogger(), fileSet.getBasedir());

    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

    if (getLogger().isInfoEnabled()) {
      getLogger().info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
      if (cl.getWorkingDirectory() != null) {
        getLogger().info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
      }
    }

    int exitCode;

    try {
      exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr, getLogger());
    } catch (CommandLineException ex) {
      throw new ScmException("Error while executing command.", ex);
    }

    if (exitCode != 0) {
      return new ExportScmResult(
          cl.toString(), "The svn command failed.", stderr.getOutput(), false);
    }

    return new ExportScmResultWithRevision(
        cl.toString(), consumer.getUpdatedFiles(), String.valueOf(consumer.getRevision()));
  }
Esempio n. 6
0
  /** {@inheritDoc} */
  protected ExportScmResult executeCvsCommand(Commandline cl) throws ScmException {
    CvsUpdateConsumer consumer = new CvsUpdateConsumer(getLogger());

    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

    int exitCode;

    try {
      exitCode = CommandLineUtils.executeCommandLine(cl, consumer, stderr);
    } catch (CommandLineException ex) {
      throw new ScmException("Error while executing command.", ex);
    }

    if (exitCode != 0) {
      return new ExportScmResult(
          cl.toString(), "The cvs command failed.", stderr.getOutput(), false);
    }

    return new ExportScmResult(cl.toString(), consumer.getUpdatedFiles());
  }
Esempio n. 7
0
  private static int checkIfCleanUpIsNeeded(
      int exitCode,
      Commandline cl,
      StreamConsumer consumer,
      CommandLineUtils.StringStreamConsumer stderr,
      ScmLogger logger)
      throws CommandLineException {
    if (exitCode != 0
        && stderr.getOutput() != null
        && stderr.getOutput().indexOf("'svn cleanup'") > 0
        && stderr.getOutput().indexOf("'svn help cleanup'") > 0) {
      if (logger.isInfoEnabled()) {
        logger.info(
            "Svn command failed due to some locks in working copy. We try to run a 'svn cleanup'.");
      }

      if (executeCleanUp(cl.getWorkingDirectory(), consumer, stderr, logger) == 0) {
        exitCode = CommandLineUtils.executeCommandLine(cl, consumer, stderr);
      }
    }
    return exitCode;
  }
Esempio n. 8
0
  private List compileOutOfProcess(File workingDirectory, String executable, String[] args)
      throws CompilerException {
    Commandline cli = new Commandline();

    cli.setWorkingDirectory(workingDirectory.getAbsolutePath());

    cli.setExecutable(executable);

    cli.addArguments(args);

    CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();

    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();

    int returnCode;

    List messages;

    try {
      returnCode = CommandLineUtils.executeCommandLine(cli, out, err);

      messages = parseModernStream(new BufferedReader(new StringReader(err.getOutput())));
    } catch (CommandLineException e) {
      throw new CompilerException("Error while executing the external compiler.", e);
    } catch (IOException e) {
      throw new CompilerException("Error while executing the external compiler.", e);
    }

    if (returnCode != 0 && messages.isEmpty()) {
      // TODO: exception?
      messages.add(
          new CompilerError(
              "Failure executing javac,  but could not parse the error:" + EOL + err.getOutput(),
              true));
    }

    return messages;
  }
Esempio n. 9
0
  private void runForked(Set<URI> classPath, Class<?> cls, String[] args)
      throws MojoExecutionException {
    getLog().info("Running wsdl2java in fork mode...");

    Commandline cmd = new Commandline();
    cmd.getShell().setQuotedArgumentsEnabled(true); // for JVM args
    cmd.setWorkingDirectory(project.getBuild().getDirectory());
    try {
      cmd.setExecutable(getJavaExecutable().getAbsolutePath());
    } catch (IOException e) {
      getLog().debug(e);
      throw new MojoExecutionException(e.getMessage(), e);
    }

    cmd.createArg().setLine(additionalJvmArgs);

    File file = null;
    try {
      // file = new File("/tmp/test.jar");
      file = FileUtils.createTempFile("cxf-codegen", ".jar");

      JarArchiver jar = new JarArchiver();
      jar.setDestFile(file.getAbsoluteFile());

      Manifest manifest = new Manifest();
      Attribute attr = new Attribute();
      attr.setName("Class-Path");
      StringBuilder b = new StringBuilder(8000);
      for (URI cp : classPath) {
        b.append(cp.toURL().toExternalForm()).append(' ');
      }
      attr.setValue(b.toString());
      manifest.getMainSection().addConfiguredAttribute(attr);

      attr = new Attribute();
      attr.setName("Main-Class");
      attr.setValue(cls.getName());
      manifest.getMainSection().addConfiguredAttribute(attr);

      jar.addConfiguredManifest(manifest);
      jar.createArchive();

      cmd.createArg().setValue("-jar");
      cmd.createArg().setValue(file.getAbsolutePath());

    } catch (Exception e1) {
      throw new MojoExecutionException("Could not create runtime jar", e1);
    }
    cmd.addArguments(args);

    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();

    int exitCode;
    try {
      exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
    } catch (CommandLineException e) {
      getLog().debug(e);
      throw new MojoExecutionException(e.getMessage(), e);
    }

    String output = StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim();

    String cmdLine = CommandLineUtils.toString(cmd.getCommandline());

    if (exitCode != 0) {
      if (StringUtils.isNotEmpty(output)) {
        getLog().info(output);
      }

      StringBuffer msg = new StringBuffer("\nExit code: ");
      msg.append(exitCode);
      if (StringUtils.isNotEmpty(err.getOutput())) {
        msg.append(" - ").append(err.getOutput());
      }
      msg.append('\n');
      msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');

      throw new MojoExecutionException(msg.toString());
    }

    if (file != null) {
      file.delete();
    }
    if (StringUtils.isNotEmpty(err.getOutput()) && err.getOutput().contains("WSDL2Java Error")) {
      StringBuffer msg = new StringBuffer();
      msg.append(err.getOutput());
      msg.append('\n');
      msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
      throw new MojoExecutionException(msg.toString());
    }
  }