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; }
/** * 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; }