Esempio n. 1
0
 public static ZipEntry findZipEntry(ZipFile zip, String name) throws IOException {
   Enumeration entries = zip.entries();
   while (entries.hasMoreElements()) {
     ZipEntry entry = (ZipEntry) entries.nextElement();
     LOGGER.fine(("findZipEntry(): entry=" + entry.getName()));
     if (entry.getName().equals(name)) return entry;
   }
   return null;
 }
Esempio n. 2
0
  private Version checkLibraryVersion(String libname, String libpath, boolean useLoadLibrary) {
    if (!useLoadLibrary && !fileExists(libpath)) {
      Logger.finer("File " + libpath + " not found");
      return null;
    }

    Version version = LibVersionChecker.getVersion(libname, libpath, useLoadLibrary);
    if (version == null) {
      Logger.fine("Cannot load or get library version: " + libpath);
    }

    return version;
  }
Esempio n. 3
0
  /** POST implementation. Calls download() to process the response. */
  static int post(String spec, String request, FileCreator creator, String libname) {
    Logger.fine(spec + "?" + request + " -> " + libname + "...");

    try {
      InputStream in = getPostInputStream(spec, request);

      if (download(in, creator, libname)) {
        return 200;
      } else {
        return -3;
      }
    } catch (HttpException e) {
      return e.getResponseCode();
    }
  }
Esempio n. 4
0
 static Bits detectJVM() {
   final String keys[] = {
     "sun.arch.data.model", "com.ibm.vm.bitmode", "os.arch",
   };
   for (String key : keys) {
     String property = System.getProperty(key);
     Logger.finest(key + "=" + property);
     if (property != null) {
       int errCode = (property.indexOf("64") >= 0) ? 64 : 32;
       Logger.finest(errCode + "-bit JVM");
       return errCode == 64 ? Bits.b64 : Bits.b32;
     }
   }
   Logger.fine("Unknown-bit JVM");
   return Bits.bUNKNOWN;
 }
Esempio n. 5
0
  public static String[] processArgs(String args[]) throws Exception {
    // Check for arguments which matter to us, and strip them.
    LOGGER.fine("processArgs(" + Arrays.asList(args) + ")");
    ArrayList list = new ArrayList();
    for (int a = 0; a < args.length; a++) {
      String argument = args[a];
      if (argument.startsWith(A_HELP)) {
        int width = firstWidth(HELP_ARGUMENTS);
        // Width of first column

        System.out.println("One-Jar uses the following command-line arguments");
        for (int i = 0; i < HELP_ARGUMENTS.length; i++) {
          System.out.print(pad("    ", HELP_ARGUMENTS[i++], width + 1));
          System.out.println(wrap("    ", HELP_ARGUMENTS[i], width + 1));
        }
        System.out.println();

        width = firstWidth(HELP_PROPERTIES);
        System.out.println(
            "One-Jar uses the following VM properties (-D<property>=<true|false|string>)");
        for (int i = 0; i < HELP_PROPERTIES.length; i++) {
          System.out.print(pad("    ", HELP_PROPERTIES[i++], width + 1));
          System.out.println(wrap("    ", HELP_PROPERTIES[i], width + 1));
        }
        System.out.println();
        System.exit(0);
      } else if (argument.startsWith(A_VERSION)) {
        InputStream is = Boot.class.getResourceAsStream("/.version");
        if (is != null) {
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
          String version = br.readLine();
          br.close();
          System.out.println("One-JAR version " + version);
        } else {
          System.out.println(
              "Unable to determine One-JAR version (missing /.version resource in One-JAR archive)");
        }
        System.exit(0);
      } else {
        list.add(argument);
      }
    }
    return (String[]) list.toArray(new String[0]);
  }
Esempio n. 6
0
  private Version getLatestVersion(String libname) {
    if (latestVersions.containsKey(libname)) {
      return latestVersions.get(libname);
    }

    String v = properties.getLatestVersion(libname, CACHE_LATEST_VERSION_INTERVAL);
    if (v == null && AUTO_DOWNLOAD) {
      v = downloadManager.getLatestVersion(libname);
      if (v != null) {
        properties.setLatestVersion(libname, v);
      }
    } else {
      Logger.fine("Cached LatestVersion (" + libname + ") = " + v);
    }

    // we will cache whatever we have here, even null
    Version version = null;
    if (v != null) {
      version = new Version(v);
    }
    latestVersions.put(libname, version);

    return version;
  }
Esempio n. 7
0
  /** POST implementation. Returns response text */
  static String post(String spec, String request) throws HttpException {
    Logger.fine(spec + "?" + request + "...");

    ByteArrayOutputStream result = new ByteArrayOutputStream();

    InputStream is = getPostInputStream(spec, request);
    byte[] buffer = new byte[1024];
    int l = 0;
    try {
      while ((l = is.read(buffer)) != -1) {
        result.write(buffer, 0, l);
      }
      is.close();
    } catch (IOException e) {
      throw new HttpException(-3);
    }

    try {
      return result.toString("UTF-8");
    } catch (java.io.UnsupportedEncodingException e) {
      System.err.println(e);
      throw new HttpException(-4);
    }
  }
Esempio n. 8
0
 public static String getMyJarPath() {
   if (myJarPath != null) {
     return myJarPath;
   }
   myJarPath = System.getProperty(P_JARPATH);
   if (myJarPath == null) {
     try {
       // Hack to obtain the name of this jar file.
       String jarname = System.getProperty(P_JAVA_CLASS_PATH);
       // Open each Jar file looking for this class name.  This allows for
       // JVM's that place more than the jar file on the classpath.
       String jars[] = jarname.split(System.getProperty("path.separator"));
       for (int i = 0; i < jars.length; i++) {
         jarname = jars[i];
         LOGGER.fine("Checking " + jarname + " as One-Jar file");
         // Allow for URL based paths, as well as file-based paths.  File
         InputStream is = null;
         try {
           is = new URL(jarname).openStream();
         } catch (MalformedURLException mux) {
           // Try a local file.
           try {
             is = new FileInputStream(jarname);
           } catch (IOException iox) {
             // Ignore..., but it isn't good to have bad entries on the classpath.
             continue;
           }
         }
         ZipEntry entry =
             findJarEntry(
                 new JarInputStream(is), Boot.class.getName().replace('.', '/') + ".class");
         if (entry != null) {
           myJarPath = jarname;
           break;
         } else {
           // One more try as a Zip file: supports launch4j on Windows.
           entry =
               findZipEntry(
                   new ZipFile(jarname), Boot.class.getName().replace('.', '/') + ".class");
           if (entry != null) {
             myJarPath = jarname;
             break;
           }
         }
       }
     } catch (Exception x) {
       x.printStackTrace();
       LOGGER.warning(
           "jar="
               + myJarPath
               + " loaded from "
               + P_JAVA_CLASS_PATH
               + " ("
               + System.getProperty(P_JAVA_CLASS_PATH)
               + ")");
     }
   }
   if (myJarPath == null) {
     throw new IllegalArgumentException(
         "Unable to locate "
             + Boot.class.getName()
             + " in the java.class.path: consider using -D"
             + P_JARPATH
             + " to specify the one-jar filename.");
   }
   // Normalize those annoying DOS backslashes.
   myJarPath = myJarPath.replace('\\', '/');
   return myJarPath;
 }
Esempio n. 9
0
  private LibSearchResult searchLibrary(String libname, Version requiredVersion) {
    LibSearchResult searchResult = new LibSearchResult();

    for (Location l : locations) {
      Logger.info("Checking " + libname + " from " + l + "...");

      List<String> pathsToCheck = new ArrayList<String>();
      boolean useLoadLibrary = false;
      boolean searchEvenAfterFound = !JUST_DO_SIMPLE_LOAD_LIBRARY;
      switch (l) {
        case LIBPATH:
          {
            pathsToCheck.add(libname);
            String libnameWithDataModel = libnameWithDataModel(libname);
            if (libnameWithDataModel != null) {
              pathsToCheck.add(libnameWithDataModel);
            }
            useLoadLibrary = true;
            break;
          }
        case CACHE:
          {
            String filename = properties.get(libname);
            if (filename == null) {
              continue;
            }

            // when search is enabled, we might skip it if cache has information about previously
            // loaded library and last search was less than SEARCH_LIB_FREQUENCY_INTERVAL ago
            Date lastSearchDate = properties.getLastSeach(libname);

            searchEvenAfterFound =
                lastSearchDate == null
                    || (new Date().getTime() - lastSearchDate.getTime()
                        > SEARCH_LIB_FREQUENCY_INTERVAL);

            pathsToCheck.add(filename);
            // this is kind of hack, but it does not require different checks between win/unix
            // we say that library was loaded from LIBPATH location when its path starts from
            // library simple name
            useLoadLibrary = filename.startsWith(libname);
            break;
          }
        case DOWNLOAD:
          {
            Logger.info("Downloading " + libname + " from NCBI...");
            LibDownloadResult downloadResult;
            if (!mocksEnabled) {
              downloadResult = download(libname);
            } else if (mockDownloadStatus == null) {
              throw new RuntimeException("mockDownloadStatus must be set when mocks enabled");
            } else {
              downloadResult = new LibDownloadResult();
              downloadResult.status = mockDownloadStatus;
              downloadResult.savedPath = "/some/path/" + libname;
            }
            if (downloadResult.status != DownloadManager.DownloadResult.SUCCESS) {
              Logger.warning("Failed to download " + libname + " from NCBI");
              if (downloadResult.status == DownloadManager.DownloadResult.UNSUPPORTED_OS) {
                searchResult.failCause = new UnsupportedArchCause();
              } else {
                searchResult.failCause = new ConnectionProblemCause();
              }
              continue;
            }
            Logger.info("Downloaded " + libname + " from NCBI");
            Logger.fine("Checking " + libname + " library...");

            pathsToCheck.add(downloadResult.savedPath);
            break;
          }
        default:
          {
            String name[] = mapLibraryName(libname);
            Logger.finest("System.mapLibraryName(" + libname + ") = " + name[0]);

            LibPathIterator it = new LibPathIterator(l, name);
            while (true) {
              String filename = it.nextName();
              if (filename == null) {
                break;
              }

              pathsToCheck.add(filename);
            }
            break;
          }
      }

      boolean foundInLocation = false;
      for (String path : pathsToCheck) {
        Version v;
        if (!mocksEnabled) {
          v = checkLibraryVersion(libname, path, useLoadLibrary);
        } else if (mockLocationVersions == null) {
          throw new RuntimeException("mockLocationVersions must be set when mocks enabled");
        } else {
          v = mockLocationVersions.get(l);
        }
        if (v == null) {
          continue;
        }

        foundInLocation = true;

        boolean versionFits = v.isCompatible(requiredVersion) && v.compareTo(requiredVersion) >= 0;
        // replace a found version if either:
        // a) none was previously found
        // b) found version which fits requirements and it is higher than previously found
        // c) found version version which fits requirements while previously found one does not
        if (searchResult.path == null
            || (versionFits && (v.compareTo(searchResult.version) > 0)
                || !searchResult.versionFits)) {
          searchResult.versionFits = versionFits;
          searchResult.location = l;
          searchResult.version = v;
          searchResult.path = path;
        }

        if (searchResult.versionFits && !searchEvenAfterFound) {
          break;
        }
      }

      if (l == Location.DOWNLOAD) {
        // when we downloaded something that either can't be loaded or does not fit our requirements
        // or we just overwrote our best found library and cannot load it
        if (!searchResult.versionFits
            || (!foundInLocation && searchResult.path.equals(pathsToCheck.get(0)))) {
          searchResult.versionFits = false;
          searchResult.location = l;
          searchResult.version = null;
          searchResult.path = pathsToCheck.get(0);
        }
      }

      if (searchResult.version != null && searchResult.version.isCompatible(requiredVersion)) {
        Version latestVersion = getLatestVersion(libname);
        // if we don't know the latest version, then we might stop the search if
        // found version is okay and searchEvenAfterFound == false
        if (latestVersion == null
            && searchResult.version.compareTo(requiredVersion) >= 0
            && !searchEvenAfterFound) {
          break;
        }
        // if we know the latest version, then we should search until find it
        if (latestVersion != null && searchResult.version.compareTo(requiredVersion) >= 0) {
          break;
        }
      }
    }

    boolean downloadEnabled = Arrays.asList(locations).contains(Location.DOWNLOAD);

    if (searchResult.failCause == null && !downloadEnabled) {
      searchResult.failCause = new DownloadDisabledCause();
    }

    return searchResult;
  }
Esempio n. 10
0
  /**
   * Loads the system library by finding it by iterating the location array. Try to download it from
   * NCBI if not found.
   *
   * <p>Will throw LibraryLoadError when failed.
   */
  void loadLibrary(String libname) {
    Version requiredVersion = getRequiredVersion(libname);
    boolean updateCache = Arrays.asList(locations).contains(Location.CACHE);

    Logger.fine("Searching for " + libname + " library...");
    try {
      LibSearchResult searchResult = searchLibrary(libname, requiredVersion);

      if (searchResult.path == null) {
        throw new LibraryNotFoundError(
            libname, "No installed library was found", searchResult.failCause);
      }

      Logger.fine("Found " + libname + " library");

      String libpath = searchResult.path;
      Logger.info("Loading " + libname + "...");
      try {
        if (!mocksEnabled) {
          if (libpath.startsWith(libname)) {
            System.loadLibrary(libpath);
          } else {
            System.load(libpath);
          }
        } else if (mockLoadException != null) {
          throw mockLoadException;
        }
      } catch (Throwable e) {
        if (searchResult.location != Location.DOWNLOAD) {
          throw new LibraryLoadError(
              libname, "Failed to load found library " + libpath, new JvmErrorCause(e));
        }

        throw new LibraryLoadError(
            libname,
            "No installed library was found and downloaded library '"
                + libpath
                + "' cannot be loaded",
            new JvmErrorCause(e),
            "Please install ngs and ncbi-vdb manually:"
                + " https://github.com/ncbi/ngs/wiki/Downloads"
                + " or write to \"[email protected]\" if problems persist");
      }
      Logger.fine("Loaded " + libname + " library");

      Logger.fine("Checking library " + libname + " version...");
      String v;
      if (!mocksEnabled) {
        v = LibVersionChecker.getLoadedVersion(libname);
      } else {
        v = mockLoadedLibraryVersion;
      }
      if (v == null) {
        throw new LibraryLoadError(
            libname, "Failed to retrieve loaded library's version", new InvalidLibraryCause());
      }
      Version loadedVersion = new Version(v);
      if (loadedVersion.compareTo(requiredVersion) < 0
          || !loadedVersion.isCompatible(requiredVersion)) {
        Logger.fine(
            "Library version is not compatible. Required: "
                + requiredVersion.toSimpleVersion()
                + " loaded: "
                + loadedVersion.toSimpleVersion());
        LibraryLoadCause failCause = searchResult.failCause;
        if (searchResult.location == Location.DOWNLOAD || failCause == null) {
          failCause =
              (loadedVersion.compareTo(requiredVersion) < 0)
                  ? new PrereleaseReqLibCause()
                  : new OutdatedJarCause();
        }
        throw new LibraryIncompatibleVersionError(
            libname, "Library is incompatible", libpath, failCause);
      }
      Logger.fine(
          "Library "
              + libname
              + " was loaded successfully."
              + " Version = "
              + loadedVersion.toSimpleVersion());

      if (updateCache) {
        properties.loaded(libname, searchResult.version.toSimpleVersion(), libpath);

        if (searchResult.location != Location.CACHE) {
          properties.setLastSearch(libname);
        }
      }
    } catch (LibraryLoadError e) {
      if (updateCache) {
        properties.notLoaded(libname);
      }
      Logger.warning("Loading of " + libname + " library failed");
      throw e;
    } finally {
      if (updateCache) {
        properties.store();
      }
    }
  }
Esempio n. 11
0
  /**
   * Creates a file by finding directory by iterating the location array and using libname to
   * generate the file name
   */
  public BufferedOutputStream create(String libname) {
    createdFileName = null;
    for (int i = 0; i < 2; ++i) {
      Location location = null;
      boolean model = true;
      switch (i) {
        case 0:
          location = Location.NCBI_HOME;
          model = false;
          break;
        case 1:
          break;
      }
      LibPathIterator it =
          new LibPathIterator(this, location, mapLibraryName(libname, model), true);

      while (true) {
        String pathname = it.nextName();
        if (pathname == null) {
          return null;
        }

        Logger.fine("Trying to create " + pathname + "...");
        File file = new File(pathname);
        try {
          pathname = file.getAbsolutePath();
        } catch (SecurityException e) {
          Logger.warning(pathname + " : cannot getAbsolutePath " + e);
          continue;
        }
        if (file.exists()) {
          String dathname = pathname + ".bak";
          File dest = new File(dathname);
          {
            String name = System.getProperty("os.name");
            if (name != null && name.startsWith("Win")) {
              /*    On Windows delete the file we are going to rename to.
              Otherwise renaming will fail. */
              if (dest.exists()) {
                Logger.fine("Trying to remove " + dathname + " ...");
                dest.delete();
              }
            }
          }
          Logger.finest("Trying to rename " + pathname + " to " + dathname + " ...");
          if (!file.renameTo(dest)) {
            Logger.warning(pathname + ".renameTo(" + dathname + ") failed");
          }
        }
        FileOutputStream s = null;
        try {
          s = new FileOutputStream(pathname);
        } catch (FileNotFoundException e) {
          /* e.message = pathname (Permission denied):
          could be because pathname is not writable
          or pathname not found and its directory is not writable */
          Logger.warning("Cannot open " + pathname);
          continue;
        }

        createdFileName = pathname;

        Logger.fine("Opened " + pathname);
        return new BufferedOutputStream(s, HttpManager.BUF_SZ);
      }
    }
    return null;
  }