Search in sources :

Example 71 with Bag

use of gov.loc.repository.bagit.domain.Bag in project epadd by ePADD.

the class ArchiveReaderWriter method readArchiveIfPresent.

/**
 * VIP method. Should be the single place to load an archive from disk.
 * loads an archive from the given directory. always re-uses archive objects loaded from the same directory.
 * this is fine when:
 * - running single-user
 * - running discovery mode epadd, since a single archive should be loaded only once.
 * - even in a hosted mode with different archives in simultaneous play, where different people have their own userKeys and therefore different dirs.
 * It may NOT be fine if  multiple people are operating on their different copies of an archive loaded from the same place. Don't see a use-case for this right now.
 * if you don't like that, tough luck.
 * return the archive, or null if it doesn't exist.
 */
public static Archive readArchiveIfPresent(String baseDir, ModeConfig.Mode mode) {
    // check if a valid bag in basedir.
    Bag archiveBag = Archive.readArchiveBag(baseDir);
    if (archiveBag == null)
        return null;
    // else, the bag has been verified.. now load the content.
    String archiveFile = baseDir + File.separator + Archive.BAG_DATA_FOLDER + File.separator + Archive.SESSIONS_SUBDIR + File.separator + "default" + SESSION_SUFFIX;
    if (!new File(archiveFile).exists()) {
        return null;
    }
    try {
        // not a concern right now. it it does become one, locking a small per-dir object like archiveFile.intern(), along with a ConcurrenctHashMap might handle it.
        synchronized (globaldirToArchiveMap) {
            // the archive is wrapped inside a weak ref to allow the archive object to be collected if there are no references to it (usually the references
            // are in the user sessions).
            WeakReference<Archive> wra = getArchiveFromGlobalArchiveMap(baseDir);
            if (wra != null) {
                Archive a = wra.get();
                if (a != null) {
                    log.info("Great, could re-use loaded archive for dir: " + archiveFile + "; archive = " + a);
                    return a;
                }
            }
            log.info("Archive not already loaded, reading from dir: " + archiveFile);
            Map<String, Object> map = loadSessionAsMap(archiveFile, baseDir, true, mode);
            // read the session map, but only use archive
            Archive a = (Archive) map.get("archive");
            // could do more health checks on archive here
            if (a == null) {
                log.warn("Archive key is not present in archive file! The archive must be corrupted! directory:" + archiveFile);
                return null;
            }
            a.setBaseDir(baseDir);
            // no need to read archive authorized authorities, they will be loaded on demand from the legacy authorities.ser file
            addToGlobalArchiveMap(baseDir, a);
            // check if the loaded archive satisfy the verification condtiions. Call verify method on archive.
            /* JSPHelper.log.info("After reading the archive checking if it is in good shape");
                a.Verify();*/
            // assign bag to archive object.
            a.setArchiveBag(archiveBag);
            if (mode != ModeConfig.Mode.DISCOVERY) {
                // now intialize the cache for lexicon
                JSPHelper.log.info("Computing summary of Lexicons");
                // To reduce the load time that is spent in this summary calculation we first check if there is a file named 'lexicon-summary'
                // in the basedir. If it is present then the summary datastructure is filled using that file. If not then the load
                long startTime = System.currentTimeMillis();
                Lexicon.fillL1_Summary_all(a, false);
                JSPHelper.log.info("Lexicons summary computed successfully in " + (System.currentTimeMillis() - startTime) + " milliseconds");
            }
            return a;
        }
    } catch (Exception e) {
        Util.print_exception("Error reading archive from dir: " + archiveFile, e, log);
        throw new RuntimeException(e);
    }
}
Also used : Bag(gov.loc.repository.bagit.domain.Bag) ParseException(org.apache.lucene.queryparser.classic.ParseException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

Bag (gov.loc.repository.bagit.domain.Bag)71 Test (org.junit.Test)67 Path (java.nio.file.Path)55 File (java.io.File)38 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)28 Version (gov.loc.repository.bagit.domain.Version)11 InputStream (java.io.InputStream)10 Manifest (gov.loc.repository.bagit.domain.Manifest)9 BagReader (gov.loc.repository.bagit.reader.BagReader)9 BagVerifier (gov.loc.repository.bagit.verify.BagVerifier)6 Metadata (gov.loc.repository.bagit.domain.Metadata)2 CorruptChecksumException (gov.loc.repository.bagit.exceptions.CorruptChecksumException)2 FileNotInPayloadDirectoryException (gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException)2 InvalidBagitFileFormatException (gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)2 MaliciousPathException (gov.loc.repository.bagit.exceptions.MaliciousPathException)2 MissingBagitFileException (gov.loc.repository.bagit.exceptions.MissingBagitFileException)2 MissingPayloadDirectoryException (gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException)2 MissingPayloadManifestException (gov.loc.repository.bagit.exceptions.MissingPayloadManifestException)2 UnparsableVersionException (gov.loc.repository.bagit.exceptions.UnparsableVersionException)2 UnsupportedAlgorithmException (gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException)2