Example #1
0
  /**
   * load from the specified jar filled with help files in the [language] directory in the jar
   *
   * @param file the jar file
   */
  private void loadFromJar(File file) {
    if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) {
      try {
        int counter = 0;
        JarInputStream jis;
        JarEntry je;
        counter = 0;
        jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file)));
        je = jis.getNextJarEntry();

        while (je != null) {
          String mnemo = trimEntryName(je);
          if (je.getName().toLowerCase().matches(helproot + "/" + language + "/.*.htm")
              && !exists(mnemo)) {
            addToCache(jis, mnemo);
            counter++;
          }
          je = jis.getNextJarEntry();
        }
        jis.close();

        System.out.println(
            "+ " + String.valueOf(counter) + "\thelp text(s) from:\t" + file.getCanonicalPath());
      } catch (IOException ignored) {
      }
    }
  }
  private String listFiles(final File cache, List<File> toDelete) throws Exception {
    boolean stopServices = false;
    if (toDelete == null) {
      toDelete = new ArrayList<File>();
    } else {
      stopServices = true;
    }
    int count = 0;
    Formatter f = new Formatter();

    f.format(" - Cache:%n    * %s%n", cache.getCanonicalPath());
    f.format(" - Commands:%n");
    for (CommandData cdata : getCommands(new File(cache, COMMANDS))) {
      f.format("    * %s \t0 handle for \"%s\"%n", cdata.bin, cdata.name);
      toDelete.add(new File(cdata.bin));
      count++;
    }
    f.format(" - Services:%n");
    for (ServiceData sdata : getServices(new File(cache, SERVICE))) {
      if (sdata != null) {
        f.format("    * %s \t0 service directory for \"%s\"%n", sdata.sdir, sdata.name);
        toDelete.add(new File(sdata.sdir));
        File initd = platform.getInitd(sdata);
        if (initd != null && initd.exists()) {
          f.format("    * %s \t0 init.d file for \"%s\"%n", initd.getCanonicalPath(), sdata.name);
          toDelete.add(initd);
        }
        if (stopServices) {
          Service s = getService(sdata);
          try {
            s.stop();
          } catch (Exception e) {
          }
        }
        count++;
      }
    }
    f.format("%n");

    String result = (count > 0) ? f.toString() : null;
    f.close();
    return result;
  }
Example #3
0
  /**
   * scans for possible help directories (languages)
   *
   * @return a list of possible language directories
   */
  public List<String> getLanguages() {
    List<String> paths = new ArrayList<>();
    URL home = getClass().getResource("..");
    File local;
    if (home != null) {
      System.out.println("looks like you are not starting from a jar-package");
      try {
        local = new File(new File(home.getPath()).getParentFile().getParentFile(), helproot);
        System.out.println("local: " + local.getAbsolutePath());
        for (File s : local.listFiles()) {
          if (s.isDirectory() && (s.listFiles().length > 0)) {
            addToList(paths, s.getName());
          }
        }
      } catch (Exception e) {
        System.out.println("but not loading from the developer workbench?!");
      }

    } else {
      System.out.println("looks like you are starting from a jar-package");
      local = CommandLoader.getLocation();
      File helpDir = new File(local, helproot);
      if (helpDir.exists() && helpDir.isDirectory() && (helpDir.listFiles().length > 0)) {
        for (File s : helpDir.listFiles()) {
          if (s.isDirectory()) {
            addToList(paths, s.getName());
          }
        }
      }

      File jar = new File(CommandLoader.classPath());
      for (File file : local.listFiles()) {
        try {
          if (!jar.getCanonicalPath().equals(file.getCanonicalPath())) {
            searchJarPath(file, paths);
          }
        } catch (IOException ignored) {
        }
        searchJarPath(jar, paths);
      }
    }
    return paths;
  }
Example #4
0
 /**
  * load a specified directory filled with help files (outside a jar)
  *
  * @param local the directory
  */
 private void initFile(File local) {
   if (!local.exists()) {
     return;
   }
   int counter = 0;
   if (local.isDirectory()) {
     for (File file : local.listFiles()) {
       if (file.getName().toLowerCase().endsWith(".htm") && file.isFile()) {
         String mnemo = file.getName().toLowerCase().replace(".htm", "");
         if (!exists(mnemo)) {
           addToCache(file, mnemo);
           counter++;
         }
       }
     }
   }
   try {
     System.out.println(
         "+ " + String.valueOf(counter) + "\thelp text(s) from:\t" + local.getCanonicalPath());
   } catch (IOException ignored) {
   }
 }
  public String what(String key, boolean oneliner) throws Exception {
    byte[] sha;

    Matcher m = SHA_P.matcher(key);
    if (m.matches()) {
      sha = Hex.toByteArray(key);
    } else {
      m = URL_P.matcher(key);
      if (m.matches()) {
        URL url = new URL(key);
        sha = SHA1.digest(url.openStream()).digest();
      } else {
        File jarfile = new File(key);
        if (!jarfile.exists()) {
          reporter.error("File does not exist: %s", jarfile.getCanonicalPath());
        }
        sha = SHA1.digest(jarfile).digest();
      }
    }
    reporter.trace("sha %s", Hex.toHexString(sha));
    Revision revision = library.getRevision(sha);
    if (revision == null) {
      return null;
    }

    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    Justif justif = new Justif(120, 20, 70, 20, 75);
    DateFormat dateFormat = DateFormat.getDateInstance();

    try {
      if (oneliner) {
        f.format("%20s %s%n", Hex.toHexString(revision._id), createCoord(revision));
      } else {
        f.format("Artifact: %s%n", revision.artifactId);
        if (revision.organization != null && revision.organization.name != null) {
          f.format(" (%s)", revision.organization.name);
        }
        f.format("%n");
        f.format("Coordinates\t0: %s%n", createCoord(revision));
        f.format("Created\t0: %s%n", dateFormat.format(new Date(revision.created)));
        f.format("Size\t0: %d%n", revision.size);
        f.format("Sha\t0: %s%n", Hex.toHexString(revision._id));
        f.format("URL\t0: %s%n", createJpmLink(revision));
        f.format("%n");
        f.format("%s%n", revision.description);
        f.format("%n");
        f.format("Dependencies\t0:%n");
        boolean flag = false;
        Iterable<RevisionRef> closure = library.getClosure(revision._id, true);
        for (RevisionRef dep : closure) {
          f.format(
              " - %s \t2- %s \t3- %s%n",
              dep.name, createCoord(dep), dateFormat.format(new Date(dep.created)));
          flag = true;
        }
        if (!flag) {
          f.format("     None%n");
        }
        f.format("%n");
      }
      f.flush();
      justif.wrap(sb);
      return sb.toString();
    } finally {
      f.close();
    }
  }
  public CommandData parseCommandData(ArtifactData artifact) throws Exception {
    File source = new File(artifact.file);
    if (!source.isFile()) throw new FileNotFoundException();

    CommandData data = new CommandData();
    data.sha = artifact.sha;
    data.jpmRepoDir = repoDir.getCanonicalPath();
    JarFile jar = new JarFile(source);
    try {
      reporter.trace("Parsing %s", source);
      Manifest m = jar.getManifest();
      Attributes main = m.getMainAttributes();
      data.name = data.bsn = main.getValue("Bundle-SymbolicName");
      String version = main.getValue("Bundle-Version");
      if (version == null) data.version = Version.LOWEST;
      else data.version = new Version(version);

      data.main = main.getValue("Main-Class");
      data.description = main.getValue("Bundle-Description");
      data.title = main.getValue("JPM-Name");

      reporter.trace("name " + data.name + " " + data.main + " " + data.title);
      DependencyCollector path = new DependencyCollector(this);
      path.add(artifact);
      DependencyCollector bundles = new DependencyCollector(this);
      if (main.getValue("JPM-Classpath") != null) {
        Parameters requires = OSGiHeader.parseHeader(main.getValue("JPM-Classpath"));

        for (Map.Entry<String, Attrs> e : requires.entrySet()) {
          path.add(e.getKey(), e.getValue().get("name")); // coordinate
        }
      } else if (!artifact.local) { // No JPM-Classpath, falling back to
        // server's revision
        // Iterable<RevisionRef> closure =
        // library.getClosure(artifact.sha,
        // false);
        // System.out.println("getting closure " + artifact.url + " " +
        // Strings.join("\n",closure));

        // if (closure != null) {
        // for (RevisionRef ref : closure) {
        // path.add(Hex.toHexString(ref.revision));
        // }
        // }
      }

      if (main.getValue("JPM-Runbundles") != null) {
        Parameters jpmrunbundles = OSGiHeader.parseHeader(main.getValue("JPM-Runbundles"));

        for (Map.Entry<String, Attrs> e : jpmrunbundles.entrySet()) {
          bundles.add(e.getKey(), e.getValue().get("name"));
        }
      }

      reporter.trace("collect digests runpath");
      data.dependencies.addAll(path.getDigests());
      reporter.trace("collect digests bundles");
      data.runbundles.addAll(bundles.getDigests());

      Parameters command = OSGiHeader.parseHeader(main.getValue("JPM-Command"));
      if (command.size() > 1) reporter.error("Only one command can be specified");

      for (Map.Entry<String, Attrs> e : command.entrySet()) {
        data.name = e.getKey();

        Attrs attrs = e.getValue();

        if (attrs.containsKey("jvmargs")) data.jvmArgs = attrs.get("jvmargs");

        if (attrs.containsKey("title")) data.title = attrs.get("title");

        if (data.title != null) data.title = data.name;
      }
      return data;
    } finally {
      jar.close();
    }
  }
Example #7
0
  private static String validateLogFile(String logFileName, String scriptName) {
    String strippedDownScriptName = null;

    if (scriptName != null) {
      File scriptFile = new File(scriptName);
      if (!scriptFile.isDirectory()) {
        String scriptFileAbsPath;
        try {
          scriptFileAbsPath = scriptFile.getCanonicalPath();
        } catch (IOException ioe) {
          throw new AssertionError(
              "Could not compute canonical path to the script file " + ioe.getMessage());
        }
        strippedDownScriptName = getFileFromCanonicalPath(scriptFileAbsPath);
      }
    }

    String defaultLogFileName =
        (strippedDownScriptName == null ? "pig_" : strippedDownScriptName)
            + new Date().getTime()
            + ".log";
    File logFile;

    if (logFileName != null) {
      logFile = new File(logFileName);

      // Check if the file name is a directory
      // append the default file name to the file
      if (logFile.isDirectory()) {
        if (logFile.canWrite()) {
          try {
            logFileName = logFile.getCanonicalPath() + File.separator + defaultLogFileName;
          } catch (IOException ioe) {
            throw new AssertionError(
                "Could not compute canonical path to the log file " + ioe.getMessage());
          }
          return logFileName;
        } else {
          throw new AssertionError(
              "Need write permission in the directory: " + logFileName + " to create log file.");
        }
      } else {
        // we have a relative path or an absolute path to the log file
        // check if we can write to the directory where this file is/will be stored

        if (logFile.exists()) {
          if (logFile.canWrite()) {
            try {
              logFileName = new File(logFileName).getCanonicalPath();
            } catch (IOException ioe) {
              throw new AssertionError(
                  "Could not compute canonical path to the log file " + ioe.getMessage());
            }
            return logFileName;
          } else {
            // do not have write permissions for the log file
            // bail out with an error message
            throw new AssertionError(
                "Cannot write to file: " + logFileName + ". Need write permission.");
          }
        } else {
          logFile = logFile.getParentFile();

          if (logFile != null) {
            // if the directory is writable we are good to go
            if (logFile.canWrite()) {
              try {
                logFileName = new File(logFileName).getCanonicalPath();
              } catch (IOException ioe) {
                throw new AssertionError(
                    "Could not compute canonical path to the log file " + ioe.getMessage());
              }
              return logFileName;
            } else {
              throw new AssertionError(
                  "Need write permission in the directory: " + logFile + " to create log file.");
            }
          } // end if logFile != null else is the default in fall through
        } // end else part of logFile.exists()
      } // end else part of logFile.isDirectory()
    } // end if logFileName != null

    // file name is null or its in the current working directory
    // revert to the current working directory
    String currDir = System.getProperty("user.dir");
    logFile = new File(currDir);
    logFileName =
        currDir + File.separator + (logFileName == null ? defaultLogFileName : logFileName);
    if (logFile.canWrite()) {
      return logFileName;
    }
    throw new RuntimeException("Cannot write to log file: " + logFileName);
  }