// If there is a <title> element on the start page, use that as our AU
 // name.
 String recomputeRegName() {
   if (!isStarted()) {
     // This can get invoked (seveeral times, mostly from logging) before
     // enough mechanism has started to make it possible to resolve the CuUrl
     // below.
     return null;
   }
   try {
     CachedUrl cu = makeCachedUrl(m_registryUrl);
     if (cu == null) return null;
     URL cuUrl = CuUrl.fromCu(cu);
     Parser parser = new Parser(cuUrl.toString());
     NodeList nodelst = parser.extractAllNodesThatMatch(new NodeClassFilter(TitleTag.class));
     Node nodes[] = nodelst.toNodeArray();
     recomputeRegName = false;
     if (nodes.length < 1) return null;
     // Get the first title found
     TitleTag tag = (TitleTag) nodes[0];
     if (tag == null) return null;
     return tag.getTitle();
   } catch (MalformedURLException e) {
     log.warning("recomputeRegName", e);
     return null;
   } catch (ParserException e) {
     if (e.getThrowable() instanceof FileNotFoundException) {
       log.warning("recomputeRegName: " + e.getThrowable().toString());
     } else {
       log.warning("recomputeRegName", e);
     }
     return null;
   }
 }
  /** Create LockssKeystores from config subtree below {@link #PARAM_KEYSTORE} */
  void configureKeyStores(Configuration config) {
    Configuration allKs = config.getConfigTree(PARAM_KEYSTORE);
    for (Iterator iter = allKs.nodeIterator(); iter.hasNext(); ) {
      String id = (String) iter.next();
      Configuration oneKs = allKs.getConfigTree(id);
      try {
        LockssKeyStore lk = createLockssKeyStore(oneKs);
        String name = lk.getName();
        if (name == null) {
          log.error("KeyStore definition missing name: " + oneKs);
          continue;
        }
        LockssKeyStore old = keystoreMap.get(name);
        if (old != null && !lk.equals(old)) {
          log.warning(
              "Keystore "
                  + name
                  + " redefined.  "
                  + "New definition may not take effect until daemon restart");
        }

        log.debug("Adding keystore " + name);
        keystoreMap.put(name, lk);

      } catch (Exception e) {
        log.error("Couldn't create keystore: " + oneKs, e);
      }
    }
  }
Example #3
0
  LibManager(String[] libs, String[] versions) {
    if (versions == null || libs == null) {
      throw new RuntimeException("Neither libs nor versions can be null");
    }

    if (versions.length != libs.length) {
      throw new RuntimeException("Invalid library versions: must match number of libraries");
    }

    checkSystemProperties();
    locations = generateLocations();

    for (int i = 0; i < libs.length; ++i) {
      libraryVersions.put(libs[i], versions[i]);
    }

    properties = new LMProperties(detectJVM().intString(), libraryVersions);
    if (AUTO_DOWNLOAD) {
      downloadManager = new DownloadManager(properties);
    }

    if (!JUST_DO_SIMPLE_LOAD_LIBRARY && System.getProperty("vdb.deleteLibraries") != null) {
      /* make sure we have the latest version of ngs-sdk & ncbi-vdb dll-s */
      for (String libname : libs) {
        Logger.warning("Deleting all JNI libraries...");
        LibPathIterator.deleteLibraries(this, libname);
      }
    }
  }
 public boolean isMatch(Alert alert) {
   try {
     Object attr = alert.getAttribute(attribute);
     switch (relation) {
       case EQ:
         return equalObjects(attr, value);
       case NE:
         return !equalObjects(attr, value);
       case CONTAINS:
         {
           if (value instanceof Collection) {
             return ((Collection) value).contains(attr);
           }
           return false;
         }
       default:
         int cmp = ((Comparable) attr).compareTo(value);
         switch (relation) {
           case GT:
             return cmp > 0;
           case GE:
             return cmp >= 0;
           case LT:
             return cmp < 0;
           case LE:
             return cmp <= 0;
           default:
             return false;
         }
     }
   } catch (Exception e) {
     log.warning("AlertPredicate.isMatch", e);
     return false;
   }
 }
 void setConfig(Configuration config) {
   log.debug("config: " + config);
   proxyHost = config.get(PARAM_PROXY_HOST);
   proxyPort = config.getInt(PARAM_PROXY_PORT, DEFAULT_PROXY_PORT);
   if (StringUtil.isNullString(proxyHost) || proxyPort <= 0) {
     String http_proxy = System.getenv("http_proxy");
     if (!StringUtil.isNullString(http_proxy)) {
       try {
         HostPortParser hpp = new HostPortParser(http_proxy);
         proxyHost = hpp.getHost();
         proxyPort = hpp.getPort();
       } catch (HostPortParser.InvalidSpec e) {
         log.warning("Can't parse http_proxy environment var, ignoring: " + http_proxy + ": " + e);
       }
     }
   }
   if (StringUtil.isNullString(proxyHost) || proxyPort <= 0) {
     proxyHost = null;
   } else {
     log.info("Proxying through " + proxyHost + ":" + proxyPort);
   }
   userAgent = config.get(PARAM_USER_AGENT);
   if (StringUtil.isNullString(userAgent)) {
     userAgent = null;
   } else {
     log.debug("Setting User-Agent to " + userAgent);
   }
 }
 protected void initFeatureVersions() throws PluginException.InvalidDefinition {
   if (definitionMap.containsKey(KEY_PLUGIN_FEATURE_VERSION_MAP)) {
     Map<Plugin.Feature, String> map = new HashMap<Plugin.Feature, String>();
     Map<String, String> spec =
         (Map<String, String>) definitionMap.getMap(KEY_PLUGIN_FEATURE_VERSION_MAP);
     log.debug2("features: " + spec);
     for (Map.Entry<String, String> ent : spec.entrySet()) {
       try {
         // Prefix version string with feature name to create separate
         // namespace for each feature
         String key = ent.getKey();
         map.put(Plugin.Feature.valueOf(key), key + "_" + ent.getValue());
       } catch (RuntimeException e) {
         log.warning(
             getPluginName()
                 + " set unknown feature: "
                 + ent.getKey()
                 + " to version "
                 + ent.getValue(),
             e);
         throw new PluginException.InvalidDefinition("Unknown feature: " + ent.getKey(), e);
       }
     }
     featureVersion = map;
   } else {
     featureVersion = null;
   }
 }
Example #7
0
  /**
   * Handles a message. Knows type "collectiveUpdate-"+name only. It is the responsibility of the
   * owner to propagate messages of this type using this method.
   */
  public boolean handleMessage(Message m, Object o) {

    if (!shouldLive || !m.getType().equals("collectiveUpdate-" + name)) return false;

    final String logSender = observer + "#collectiveUpdate";

    Logger.debug(logSender, "Update from " + m.getSender());

    /**/
    if (!m.getRecipient().name.equals(contributor.getName()))
      Logger.warning(
          logSender,
          "Recipient and my contributor are not the same:\n"
              + "Recipient: "
              + m.getRecipient().name
              + "\n"
              + "Contributor: "
              + contributor.getName(),
          null);
    /**/

    Collective c = (Collective) o;

    // --- reset array representations
    cacheCollection = null;
    commandCollection = null;

    // --- remove possible garbage
    cache.remove(m.getRecipient().name);
    c.cache.remove(m.getRecipient().name);
    cache.remove(m.getSender().name);
    c.cache.remove(m.getSender().name);

    // --- sending our contributions
    if (contributor == null)
      Logger.warning(logSender, "Non-contributor observer is known by " + m.getSender(), null);
    updateLocalInfo();
    m.setReply(this);

    // --- update containers
    repairSenderAddress(c, m.getSender());
    merge(c);
    observer.collectiveUpdated((ContributionBox) cache.get(m.getSender().name));

    return true;
  }
 /**
  * Lookup the CU's archive file type in its AU's ArchiveFileTypes
  *
  * @return the file extension (including dot), or null if none found
  */
 public static String getArchiveExtension(CachedUrl cu) {
   ArchiveFileTypes aft = cu.getArchivalUnit().getArchiveFileTypes();
   if (aft == null) {
     return null;
   }
   try {
     return aft.getFromCu(cu);
   } catch (MalformedURLException e) {
     log.warning("isArchive(" + cu + ")", e);
     return null;
   }
 }
Example #9
0
  /**
   * Gibt den Jahrgang der Klasse zurück.
   *
   * @return int-Wert, der den Jahrgang der Klasse angibt oder "0" bei Fehler
   */
  public int getJahrgang() {
    int jahrgang = 0;

    if (isValid()) {
      try {
        jahrgang = Integer.parseInt(name.substring(0, 1));
      } catch (Exception e) {
        Logger.warning(this, "Konnte den Jahrgang nicht bestimmen für " + this);
      }
    }

    return jahrgang;
  }
Example #10
0
  private void checkSystemProperties() {
    String loadLibraryProperty = System.getProperty("vdb.System.loadLibrary");
    if (loadLibraryProperty != null && loadLibraryProperty.equals("1")) {
      Logger.warning("Smart DLL search and library download was disabled");
      JUST_DO_SIMPLE_LOAD_LIBRARY = true;
      AUTO_DOWNLOAD = false;
      SEARCH_FOR_LIBRARY = false;
      return;
    }

    String noLibraryDownload = System.getProperty("vdb.System.noLibraryDownload");
    if (noLibraryDownload != null && noLibraryDownload.equals("1")) {
      Logger.warning("DLL download was disabled");
      AUTO_DOWNLOAD = false;
    }

    String noLibrarySearch = System.getProperty("vdb.System.noLibrarySearch");
    if (noLibrarySearch != null && noLibrarySearch.equals("1")) {
      Logger.warning("Search of installed DLL was disabled");
      SEARCH_FOR_LIBRARY = false;
    }
  }
Example #11
0
 protected String getHostName() {
   String res = ConfigManager.getPlatformHostname();
   if (res == null) {
     try {
       InetAddress inet = InetAddress.getLocalHost();
       return inet.getHostName();
     } catch (UnknownHostException e) {
       log.warning("Can't get hostname", e);
       return "unknown";
     }
   }
   return res;
 }
 static Properties getAuIdProperties(String location) {
   File propFile = new File(location + File.separator + AU_ID_FILE);
   try {
     InputStream is = new BufferedInputStream(new FileInputStream(propFile));
     Properties idProps = new Properties();
     idProps.load(is);
     is.close();
     return idProps;
   } catch (Exception e) {
     logger.warning("Error loading au id from " + propFile.getPath() + ".");
     return null;
   }
 }
 static String canonRoot(String root) {
   synchronized (canonicalRoots) {
     String canon = (String) canonicalRoots.get(root);
     if (canon == null) {
       try {
         canon = new File(root).getCanonicalPath();
         canonicalRoots.put(root, canon);
       } catch (IOException e) {
         logger.warning("Can't canonicalize: " + root, e);
         return root;
       }
     }
     return canon;
   }
 }
Example #14
0
  /**
   * Repairing address of sender (might contain only name). This sets the right address of
   * myContribution in c, and puts the contribution into c.cache. sender is the address of the peer
   * to our best knowledge. However the atual address of the contributor might be different due to
   * forwarding and other nuances. The authoritive name is in c.myContribution. The authoritive host
   * and port are in sender.
   */
  private static void repairSenderAddress(Collective c, Address sender) {

    if (c.myContribution != null) {
      c.cache.put(
          c.myContribution.contributor.name,
          new ContributionBox(
              new Address(sender.getHost(), sender.port, c.myContribution.contributor.name),
              c.myContribution.timeStamp,
              c.myContribution.contribution));
    } else
      Logger.warning(
          "Collective#repairSenderAddress()",
          "Update doesn't contain contribution of sender " + sender,
          null);
  }
Example #15
0
  public static boolean isForwardingEnabled() {
    boolean forwarding = false;
    BufferedReader reader;
    String line;

    try {
      reader = new BufferedReader(new FileReader(IPV4_FORWARD_FILEPATH));
      line = reader.readLine().trim();
      forwarding = line.equals("1");

      reader.close();

    } catch (IOException e) {
      Logger.warning(e.toString());
    }

    return forwarding;
  }
 void checkParamAgreement(String key, PrintfContext context) {
   List<String> printfList = getElementList(key);
   if (printfList == null) {
     return;
   }
   for (String printf : printfList) {
     if (StringUtil.isNullString(printf)) {
       log.warning("Null printf string in " + key);
       continue;
     }
     PrintfUtil.PrintfData p_data = PrintfUtil.stringToPrintf(printf);
     Collection<String> p_args = p_data.getArguments();
     for (String arg : p_args) {
       ConfigParamDescr descr = findAuConfigDescr(arg);
       if (descr == null) {
         throw new PluginException.InvalidDefinition(
             "Not a declared parameter: " + arg + " in " + printf + " in " + getPluginName());
       }
       // ensure range and set params used only in legal context
       switch (context) {
         case Regexp:
         case Display:
           // everything is legal in a regexp or a display string
           break;
         case URL:
           // NUM_RANGE and SET legal because can enumerate.  Can't
           // enumerate RANGE
           switch (descr.getType()) {
             case ConfigParamDescr.TYPE_RANGE:
               throw new PluginException.InvalidDefinition(
                   "Range parameter ("
                       + arg
                       + ") used in illegal context in "
                       + getPluginName()
                       + ": "
                       + key
                       + ": "
                       + printf);
             default:
           }
       }
     }
   }
 }
 /*
  * This is comlicated. MOST AUs have articles that live below and issue level TOC
  * that is,
  * <blah>/<journal_id>/vol#/iss#/ is a toc with no relevant metadata
  * <blah>/<journal_id>/vol#/iss#/xxx is an article with metadata
  * (eg Economist Voice V1)
  * BUT
  * in some AUs there are issues with only 1 article, in which case
  * <blah>/<journal_id>/vol#/iss#/ is an abstract with metadata
  * (eg Rhodes Cook V4)
  * and a few AUs with a mixture
  * (eg Forum for Health Economics V5)
  * So to identify ALL articles, we'll also have to capture issue level items and then look
  * at the html and if it has article metadata in it, count it as an article.
  *
  */
 @Override
 protected ArticleFiles createArticleFiles(CachedUrl cu) {
   String url = cu.getUrl();
   Matcher mat = pattern.matcher(url);
   if (mat.find()) {
     // we matched, but could this pattern potentially be a toc?
     Matcher tocmat = TOC_pattern.matcher(url);
     // if we could be a TOC then we must have metadata to be considered an article
     if (tocmat.find()) {
       if (hasArticleMetadata(cu)) {
         return processUrl(cu, mat);
       }
     } else {
       // we're not a potential TOC, so treat this as an article without checking
       return processUrl(cu, mat);
     }
     return null; // this was a TOC, not an article
   }
   log.warning("Mismatch between article iterator factory and article iterator: " + url);
   return null;
 }
Example #18
0
  public boolean needed() {
    Logger.debug("Checking version file " + mVersionFile);

    File file = new File(mVersionFile);
    BufferedReader reader;
    String line;
    boolean needed = true;

    try {
      if (file.exists()) {
        reader = new BufferedReader(new FileReader(file));
        line = reader.readLine().trim();
        needed = !line.equals(mAppVersion);

        reader.close();
      }
    } catch (IOException e) {
      Logger.warning(e.toString());
    }

    return needed;
  }
Example #19
0
  /** check timeout site */
  private void check() {
    int size = mapTime.size();
    if (size == 0) return;

    ArrayList<SiteHost> dels = new ArrayList<SiteHost>(size);
    ArrayList<SiteHost> notifys = new ArrayList<SiteHost>(size);
    super.lockSingle();
    try {
      long nowTime = System.currentTimeMillis();
      for (SiteHost host : mapTime.keySet()) {
        Long value = mapTime.get(host);
        if (value == null) {
          dels.add(host);
          continue;
        }
        long time = value.longValue();
        if (nowTime - time >= deleteTime) {
          dels.add(host);
        } else if (nowTime - time >= refreshTimeout) {
          notifys.add(host);
        }
      }
    } catch (Throwable exp) {
      exp.printStackTrace();
    } finally {
      super.unlockSingle();
    }
    // remove timeout site
    for (SiteHost host : dels) {
      Logger.error("WorkPool.check, delete timeout site:%s", host);
      remove(host);
    }
    // notify site
    SiteHost listen = Launcher.getInstance().getLocalHost();
    for (SiteHost host : notifys) {
      Logger.warning("WorkPool.check, notify timeout site:%s", host);
      this.sendTimeout(host, listen, 2);
    }
  }
Example #20
0
  protected CIProperties makeCIProperties(ArchiveRecordHeader elementHeader) throws IOException {
    CIProperties ret = new CIProperties();
    Set elementHeaderFieldKeys = elementHeader.getHeaderFieldKeys();
    for (Iterator i = elementHeaderFieldKeys.iterator(); i.hasNext(); ) {
      String key = (String) i.next();
      try {

        Object valueObject = elementHeader.getHeaderValue(key);

        if (valueObject == null) {
          logger.warning("Ignoring null value for key '" + key + "'.");
        } else {
          String value = valueObject.toString();
          logger.debug3(key + ": " + value);
          ret.put(key, value);
        }

      } catch (ClassCastException ex) {
        logger.error("makeCIProperties: " + key + " threw ", ex);
        throw new CacheException.ExploderException(ex);
      }
    }
    return (ret);
  }
 public void warning(String message, Throwable t) {
   logger.warning(message, t);
 }
Example #22
0
  /**
   * Get or create TdbTitle for the specified properties and TdbAu.
   *
   * @param props the properties
   * @param au the TdbAu
   * @return the corresponding TdbTitle
   */
  private TdbTitle getTdbTitle(Properties props, TdbAu au) {
    TdbTitle title = null;

    // get publisher name
    String publisherNameFromProps = getTdbPublisherName(props, au);

    // get the title name
    String titleNameFromProps = getTdbTitleName(props, au);

    // get the title ID
    String titleIdFromProps = getTdbTitleId(props, au);

    String titleId = titleIdFromProps;
    if (titleId == null) {
      // generate a titleId if one not specified, using the
      // hash code of the combined title name and publisher names
      int hash = (titleNameFromProps + publisherNameFromProps).hashCode();
      titleId = (hash < 0) ? ("id:1" + (-hash)) : ("id:0" + hash);
    }

    // get publisher specified by property name
    TdbPublisher publisher = tdbPublisherMap.get(publisherNameFromProps);
    if (publisher != null) {
      // find title from publisher
      title = publisher.getTdbTitleById(titleId);
      if (title != null) {
        // warn that title name is different
        if (!title.getName().equals(titleNameFromProps)) {
          logger.warning(
              "Title for au \""
                  + au.getName()
                  + "\": \""
                  + titleNameFromProps
                  + "\" is different than existing title \""
                  + title.getName()
                  + "\" for id "
                  + titleId
                  + " -- using existing title.");
        }
        return title;
      }
    }

    if (publisher == null) {
      // warn of missing publisher name
      if (publisherNameFromProps.startsWith(UNKNOWN_PUBLISHER_PREFIX)) {
        logger.warning(
            "Publisher missing for au \""
                + au.getName()
                + "\" -- using \""
                + publisherNameFromProps
                + "\"");
      }

      // create new publisher for specified publisher name
      publisher = new TdbPublisher(publisherNameFromProps);
      tdbPublisherMap.put(publisherNameFromProps, publisher);
    }

    // warn of missing title name and/or id
    if (titleNameFromProps.startsWith(UNKNOWN_TITLE_PREFIX)) {
      logger.warning(
          "Title missing for au \"" + au.getName() + "\" -- using \"" + titleNameFromProps + "\"");
    }
    if (titleIdFromProps == null) {
      logger.debug2("Title ID missing for au \"" + au.getName() + "\" -- using " + titleId);
    }

    // create title and add to publisher
    title = new TdbTitle(titleNameFromProps, titleId);
    try {
      publisher.addTdbTitle(title);
    } catch (TdbException ex) {
      // shouldn't happen: title already exists in publisher
      logger.error(ex.getMessage(), ex);
    }

    return title;
  }
 public void warning(String message) {
   logger.warning(message);
 }
Example #24
0
 /** Clean up all resources used by this poll. Removes the poll directory. */
 public void closePoll() {
   if (pollDir != null && pollDir.isDirectory() && !FileUtil.delTree(pollDir))
     log.warning("Unable to delete poll state directory: " + pollDir);
 }
  /** Query the daemon for information required to set up this command */
  private boolean commandSetup() {

    Configuration configuration = null;
    Collection noEditKeys = null;
    String key;
    String value;

    /*
     * Configure a well known publication?
     */
    if ((value = getParameter(AP_E_PUBLICATION)) != null) {
      PluginProxy plugin = getTitlePlugin(value);

      /*
       * Set plugin and Title configuration information
       */
      if (plugin == null) {
        String message = "Unknown Publication:" + value;

        log.warning(message);
        return error(message);
      }

      setPlugin(plugin);
      setTitleConfig(plugin.getTitleConfig(value));

      configuration = getTitleConfig().getConfig();
      noEditKeys = getNoEditKeys();

    } else {
      /*
       * Lookup by Plugin or Class name - set the plugin
       *
       * NB: As of 23-Feb-04, this is not supported from AddAuPage.java.  See
       *     AddAuWithCompleteFunctionalityPage.java for full support.
       */
      if ((value = getParameter(AP_E_PLUGIN)) != null) {
        key = RemoteApi.pluginKeyFromId(value);

      } else if ((value = getParameter(AP_E_CLASSNAME)) != null) {
        key = RemoteApi.pluginKeyFromId(value);

      } else {
        return error("Supply a Publication, Plugin, or Class name");
      }

      if (StringUtil.isNullString(key)) {
        return error("Supply a valid Publication, Plugin, or Class name");
      }

      if (!pluginLoaded(key)) {
        return error("Plugin is not loaded: " + key);
      }

      setPlugin(getPluginProxy(key));
    }

    /*
     * Finally, return an XML rendition of the Plugin and AU key set up
     */
    generateSetupXml(configuration, noEditKeys);
    return true;
  }
Example #26
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;
  }
Example #27
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();
      }
    }
  }
Example #28
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;
 }
Example #29
0
  public static void run(String args[]) throws Exception {

    args = processArgs(args);

    initializeLogging();

    // Is the main class specified on the command line?  If so, boot it.
    // Otherwise, read the main class out of the manifest.
    String mainClass = null;

    initializeProperties();
    // Reinitialze Logging (property file could have other loglevel set)
    initializeLogging();

    if (Boolean.valueOf(System.getProperty(P_SHOW_PROPERTIES, "false")).booleanValue()) {
      // What are the system properties.
      Properties props = System.getProperties();
      String keys[] = props.keySet().toArray(new String[0]);
      Arrays.sort(keys);

      for (int i = 0; i < keys.length; i++) {
        String key = keys[i];
        System.out.println(key + "=" + props.get(key));
      }
    }

    // Process developer properties:
    if (mainClass == null) {
      mainClass = System.getProperty(P_MAIN_CLASS);
    }

    if (mainJar == null) {
      String app = System.getProperty(P_MAIN_APP);
      if (app != null) {
        mainJar = "main/" + app + ".jar";
      } else {
        mainJar = System.getProperty(P_MAIN_JAR, MAIN_JAR);
      }
    }

    // Pick some things out of the top-level JAR file.
    String jar = getMyJarPath();
    JarFile jarFile = new JarFile(jar);
    Manifest manifest = jarFile.getManifest();
    Attributes attributes = manifest.getMainAttributes();
    String bootLoaderName = attributes.getValue(ONE_JAR_CLASSLOADER);

    if (mainJar == null) {
      mainJar = attributes.getValue(ONE_JAR_DEFAULT_MAIN_JAR);
    }

    String mainargs = attributes.getValue(ONE_JAR_MAIN_ARGS);
    if (mainargs != null && args.length == 0) {
      // Replace the args with built-in.  Support escaped whitespace.
      args = mainargs.split("[^\\\\]\\s");
      for (int i = 0; i < args.length; i++) {
        args[i] = args[i].replaceAll("\\\\(\\s)", "$1");
      }
    }

    // If no main-class specified, check the manifest of the main jar for
    // a Boot-Class attribute.
    if (mainClass == null) {
      mainClass = attributes.getValue(ONE_JAR_MAIN_CLASS);
      if (mainClass == null) {
        mainClass = attributes.getValue(BOOT_CLASS);
        if (mainClass != null) {
          LOGGER.warning(
              "The manifest attribute "
                  + BOOT_CLASS
                  + " is deprecated in favor of the attribute "
                  + ONE_JAR_MAIN_CLASS);
        }
      }
    }

    if (mainClass == null) {
      // Still don't have one (default).  One final try: look for a jar file in a
      // main directory.  There should be only one, and it's manifest
      // Main-Class attribute is the main class.  The JarClassLoader will take
      // care of finding it.
      InputStream is = Boot.class.getResourceAsStream("/" + mainJar);
      if (is != null) {
        JarInputStream jis = new JarInputStream(is);
        Manifest mainmanifest = jis.getManifest();
        jis.close();
        mainClass = mainmanifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
      } else {
        // There is no main jar. Info unless mainJar is empty string.
        // The load(mainClass) will scan for main jars anyway.
        if (mainJar != null && !mainJar.isEmpty()) {
          LOGGER.info(
              "Unable to locate main jar '" + mainJar + "' in the JAR file " + getMyJarPath());
        }
      }
    }

    // Do we need to create a wrapping classloader?  Check for the
    // presence of a "wrap" directory at the top of the jar file.
    URL url = Boot.class.getResource(WRAP_JAR);

    if (url != null) {
      // Wrap class loaders.
      final JarClassLoader bootLoader = getBootLoader(bootLoaderName);
      bootLoader.load(null);

      // Read the "Wrap-Class-Loader" property from the wraploader jar file.
      // This is the class to use as a wrapping class-loader.
      InputStream is = Boot.class.getResourceAsStream(WRAP_JAR);
      if (is != null) {
        JarInputStream jis = new JarInputStream(is);
        final String wrapLoader = jis.getManifest().getMainAttributes().getValue(WRAP_CLASS_LOADER);
        jis.close();
        if (wrapLoader == null) {
          LOGGER.warning(
              url
                  + " did not contain a "
                  + WRAP_CLASS_LOADER
                  + " attribute, unable to load wrapping classloader");
        } else {
          LOGGER.info("using " + wrapLoader);
          JarClassLoader wrapped = getWrapLoader(bootLoader, wrapLoader);
          if (wrapped == null) {
            LOGGER.warning(
                "Unable to instantiate "
                    + wrapLoader
                    + " from "
                    + WRAP_DIR
                    + ": using default JarClassLoader");
            wrapped = getBootLoader(null);
          }
          setClassLoader(wrapped);
        }
      }
    } else {
      setClassLoader(getBootLoader(bootLoaderName, Boot.class.getClassLoader()));
      LOGGER.info("using JarClassLoader: " + getClassLoader().getClass().getName());
    }

    // Allow injection of the URL factory.
    String urlfactory = attributes.getValue(ONE_JAR_URL_FACTORY);
    if (urlfactory != null) {
      loader.setURLFactory(urlfactory);
    }

    mainClass = loader.load(mainClass);

    if (mainClass == null && !loader.isExpanded())
      throw new Exception(
          getMyJarName()
              + " main class was not found (fix: add main/main.jar with a Main-Class manifest attribute, or specify -D"
              + P_MAIN_CLASS
              + "=<your.class.name>), or use "
              + ONE_JAR_MAIN_CLASS
              + " in the manifest");

    if (mainClass != null) {
      // Guard against the main.jar pointing back to this
      // class, and causing an infinite recursion.
      String bootClass = Boot.class.getName();
      if (bootClass.equals(mainClass))
        throw new Exception(
            getMyJarName()
                + " main class ("
                + mainClass
                + ") would cause infinite recursion: check main.jar/META-INF/MANIFEST.MF/Main-Class attribute: "
                + mainClass);

      Class cls = loader.loadClass(mainClass);

      endTime = System.currentTimeMillis();
      showTime();

      Method main = cls.getMethod("main", new Class[] {String[].class});
      main.invoke(null, new Object[] {args});
    }
  }
Example #30
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;
  }