/**
   * Locates the executable for the jarsigner tool.
   *
   * @Copy&paste from org.apache.maven.plugins.jarsigner.AbstractJarsignerMojo
   * @return The executable of the jarsigner tool, never <code>null<code>.
   */
  private String getExecutable() {
    String command = "jarsigner" + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : "");

    String executable =
        findExecutable(
            command, System.getProperty("java.home"), new String[] {"../bin", "bin", "../sh"});

    if (executable == null) {
      try {
        Properties env = CommandLineUtils.getSystemEnvVars();

        String[] variables = {"JDK_HOME", "JAVA_HOME"};

        for (int i = 0; i < variables.length && executable == null; i++) {
          executable =
              findExecutable(command, env.getProperty(variables[i]), new String[] {"bin", "sh"});
        }
      } catch (IOException e) {
        if (getLog().isDebugEnabled()) {
          getLog()
              .warn("Failed to retrieve environment variables, cannot search for " + command, e);
        } else {
          getLog().warn("Failed to retrieve environment variables, cannot search for " + command);
        }
      }
    }

    if (executable == null) {
      executable = command;
    }

    return executable;
  }
Beispiel #2
0
    private void configureProperties(final MavenExecutionRequest request) {
      assert request != null;
      assert config != null;

      Properties sys = new Properties();
      sys.putAll(System.getProperties());

      Properties user = new Properties();
      user.putAll(config.getProperties());

      // Add the env vars to the property set, with the "env." prefix
      boolean caseSensitive = !Os.isFamily(Os.FAMILY_WINDOWS);
      for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
        String key =
            "env." + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH));
        sys.setProperty(key, entry.getValue());
      }

      request.setUserProperties(user);

      // HACK: Some bits of Maven still require using System.properties :-(
      sys.putAll(user);
      System.getProperties().putAll(user);

      request.setSystemProperties(sys);
    }
  public void testGetAttributesForThisTestClass() throws IOException {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
      System.out.println("WARNING: Unsupported OS, skipping test");
      return;
    }

    URL resource =
        Thread.currentThread()
            .getContextClassLoader()
            .getResource(getClass().getName().replace('.', '/') + ".class");

    if (resource == null) {
      throw new IllegalStateException(
          "SOMETHING IS VERY WRONG. CANNOT FIND THIS TEST CLASS IN THE CLASSLOADER.");
    }

    File f = new File(resource.getPath().replaceAll("%20", " "));

    Map attrs =
        PlexusIoResourceAttributeUtils.getFileAttributesByPath(
            f, new ConsoleLogger(Logger.LEVEL_INFO, "test"), Logger.LEVEL_DEBUG);

    PlexusIoResourceAttributes fileAttrs =
        (PlexusIoResourceAttributes) attrs.get(f.getAbsolutePath());

    System.out.println("Got attributes for: " + f.getAbsolutePath() + fileAttrs);

    assertNotNull(fileAttrs);
    assertTrue(fileAttrs.isOwnerReadable());
    assertEquals(System.getProperty("user.name"), fileAttrs.getUserName());
  }
  public void testFolderJava7() throws IOException, CommandLineException {

    if (Os.isFamily(Os.FAMILY_WINDOWS) || Os.isFamily(Os.FAMILY_WIN9X)) {
      return; // Nothing to do here.
    }

    URL resource =
        Thread.currentThread()
            .getContextClassLoader()
            .getResource(getClass().getName().replace('.', '/') + ".class");

    if (resource == null) {
      throw new IllegalStateException(
          "SOMETHING IS VERY WRONG. CANNOT FIND THIS TEST CLASS IN THE CLASSLOADER.");
    }

    File f = new File(resource.getPath().replaceAll("%20", " "));
    final File aDir = f.getParentFile().getParentFile().getParentFile();

    Commandline commandLine = new Commandline("chmod");
    commandLine.addArguments(new String[] {"763", f.getAbsolutePath()});

    CommandLineUtils.executeCommandLine(commandLine, null, null);
    Map attrs =
        PlexusIoResourceAttributeUtils.getFileAttributesByPath(
            aDir, new ConsoleLogger(Logger.LEVEL_INFO, "test"), Logger.LEVEL_DEBUG);

    PlexusIoResourceAttributes fileAttrs =
        (PlexusIoResourceAttributes) attrs.get(f.getAbsolutePath());

    assertTrue(fileAttrs.isGroupReadable());
    assertTrue(fileAttrs.isGroupWritable());
    assertFalse(fileAttrs.isGroupExecutable());

    assertTrue(fileAttrs.isOwnerExecutable());
    assertTrue(fileAttrs.isOwnerReadable());
    assertTrue(fileAttrs.isOwnerWritable());

    assertTrue(fileAttrs.isWorldExecutable());
    assertFalse(fileAttrs.isWorldReadable());
    assertTrue(fileAttrs.isWorldWritable());

    assertNotNull(fileAttrs);
  }
  static boolean isOsFamily(List<String> families, boolean defaultMatch) {
    if (families != null && !families.isEmpty()) {
      for (String family : families) {
        if (Os.isFamily(family)) {
          return true;
        }
      }

      return false;
    } else {
      return defaultMatch;
    }
  }
Beispiel #6
0
  private String findExecutable(CompilerConfiguration config) {
    String executable = config.getExecutable();

    if (!StringUtils.isEmpty(executable)) {
      return executable;
    }

    if (Os.isFamily("windows")) {
      return "csc";
    }

    return "mcs";
  }
  public static String cryptPassword(Commandline cl) {
    String clString = cl.toString();

    int pos = clString.indexOf("--password");

    if (pos > 0) {
      String beforePassword = clString.substring(0, pos + "--password ".length());
      String afterPassword = clString.substring(pos + "--password ".length());
      afterPassword = afterPassword.substring(afterPassword.indexOf(' '));
      if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        clString = beforePassword + "*****" + afterPassword;
      } else {
        clString = beforePassword + "'*****'" + afterPassword;
      }
    }

    return clString;
  }
  public File resolveFile(File baseDir, final String path) throws IOException {
    // baseDir may be null
    // path may be null

    File userDir = getUserDir();

    if (baseDir == null) {
      baseDir = userDir;
    }

    File file;
    if (path == null) {
      file = baseDir;
    } else if (path.startsWith("~")) {
      File userHome = getUserHomeDir();
      file = new File(userHome.getPath() + path.substring(1));
    } else {
      file = new File(path);
    }

    // support paths like "<drive>:" and "/" on windows
    if (Os.isFamily(FAMILY_WINDOWS)) {
      if (path != null && path.equals("/")) {
        // Get the current canonical path to access drive root
        String tmp = new File(".").getCanonicalPath().substring(0, 2);
        return new File(tmp + "/").getCanonicalFile();
      }

      String tmp = file.getPath();
      if (tmp.length() == 2 && tmp.charAt(1) == ':') {
        // Have to append "/" on windows it seems to get the right root
        return new File(tmp + "/").getCanonicalFile();
      }
    }

    if (!file.isAbsolute()) {
      file = new File(baseDir, file.getPath());
    }

    return file.getCanonicalFile();
  }
  public static int executeCleanUp(
      File workinDirectory, StreamConsumer stdout, StreamConsumer stderr, ScmLogger logger)
      throws CommandLineException {
    Commandline cl = new Commandline();

    cl.setExecutable("svn");

    cl.setWorkingDirectory(workinDirectory.getAbsolutePath());

    if (logger != null) {
      if (logger.isInfoEnabled()) {
        logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));

        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
          logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
        }
      }
    }

    return CommandLineUtils.executeCommandLine(cl, stdout, stderr);
  }
  public void testCreateArchiveWithDetectedModes() throws Exception {

    String[] executablePaths = {"path/to/executable", "path/to/executable.bat"};

    String[] confPaths = {"path/to/etc/file", "path/to/etc/file2"};

    String[] logPaths = {"path/to/logs/log.txt"};

    int exeMode = 0777;
    int confMode = 0600;
    int logMode = 0640;

    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
      StackTraceElement e = new Throwable().getStackTrace()[0];
      System.out.println(
          "Cannot execute test: " + e.getMethodName() + " on " + System.getProperty("os.name"));
      return;
    }

    File tmpDir = null;
    try {
      tmpDir = File.createTempFile("zip-with-chmod.", ".dir");
      tmpDir.delete();

      tmpDir.mkdirs();

      for (String executablePath : executablePaths) {
        writeFile(tmpDir, executablePath, exeMode);
      }

      for (String confPath : confPaths) {
        writeFile(tmpDir, confPath, confMode);
      }

      for (String logPath : logPaths) {
        writeFile(tmpDir, logPath, logMode);
      }

      {
        Map<String, PlexusIoResourceAttributes> attributesByPath =
            PlexusIoResourceAttributeUtils.getFileAttributesByPath(tmpDir);
        for (String path : executablePaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + exeMode, exeMode, attrs.getOctalMode());
        }

        for (String path : confPaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + confMode,
              confMode,
              attrs.getOctalMode());
        }

        for (String path : logPaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + logMode, logMode, attrs.getOctalMode());
        }
      }

      File zipFile = getTestFile("target/output/zip-with-modes.zip");

      ZipArchiver archiver = getZipArchiver(zipFile);

      archiver.addDirectory(tmpDir);
      archiver.createArchive();

      assertTrue(zipFile.exists());

      File zipFile2 = getTestFile("target/output/zip-with-modes-L2.zip");

      archiver = getZipArchiver();
      archiver.setDestFile(zipFile2);

      archiver.addArchivedFileSet(zipFile);
      archiver.createArchive();

      ZipFile zf = new ZipFile(zipFile2);

      for (String path : executablePaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, mode);
      }

      for (String path : confPaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, mode);
      }

      for (String path : logPaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, mode);
      }
    } finally {
      if (tmpDir != null && tmpDir.exists()) {
        try {
          FileUtils.forceDelete(tmpDir);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }