001/*
002 * #%L
003 * Netarchivesuite - archive
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.archive.arcrepositoryadmin;
025
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import dk.netarkivet.common.exceptions.IllegalState;
030
031/**
032 * A version of AdminData that cannot be changed, but which allows synchronization through a file.
033 * <p>
034 * To avoid excessive reading of the admin data file and constant stat() calls, users of this are required call
035 * synchronize() before major chunks of use to ensure that the data are up to date.
036 * <p>
037 * Implementation note: Two alternative synch strategies are
038 * <p>
039 * 1) Recreate ReadOnlyAdminData before every use -- this requires reading the entire file again (millions of lines).<br>
040 * 2) Synchronize at every entry point (hasEntry, getState etc) -- this requires an expensive stat() call before every
041 * action, costly when iterating.
042 *
043 * @deprecated This class is only used by the deprecated class FileBasedActiveBitPreservation.
044 */
045@Deprecated
046public class ReadOnlyAdminData extends AdminData {
047
048    /** The log. */
049    private static final Logger log = LoggerFactory.getLogger(ReadOnlyAdminData.class);
050
051    /**
052     * The time the underlying file (adminDataFile) was last read in. If 0, we have never read admin data (the file
053     * doesn't exist).
054     */
055    protected long lastModified = 0;
056
057    /**
058     * @see AdminData#AdminData()
059     */
060    public ReadOnlyAdminData() {
061        super();
062    }
063
064    /**
065     * Returns _an_ instance if admin data. This is _not_ a singleton.
066     *
067     * @return An instance of ReadOnlyAdminData
068     */
069    public static ReadOnlyAdminData getInstance() {
070        return new ReadOnlyAdminData();
071    }
072
073    /**
074     * Read admin data. This should not be used, use synchronize instead, which only rereads when necessary.
075     */
076    protected void read() {
077        lastModified = adminDataFile.lastModified();
078        super.read();
079    }
080
081    /**
082     * Make sure that the internal admin data set is synchronized to the file.
083     */
084    public void synchronize() {
085        if (adminDataFile.lastModified() > lastModified) {
086            storeEntries.clear();
087            knownBitArchives.clear();
088            read();
089        }
090        if (lastModified == 0) {
091            String msg = "Admin data (file: " + adminDataFile.getAbsolutePath() + ") not created in time for reading.";
092            log.warn(msg);
093            throw new IllegalState(msg);
094        }
095    }
096
097}