001package org.kuali.common.util.runonce;
002
003import java.io.File;
004import java.util.Properties;
005
006import org.apache.commons.lang3.StringUtils;
007import org.kuali.common.util.Assert;
008import org.kuali.common.util.PropertyUtils;
009import org.kuali.common.util.file.CanonicalFile;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * This implementation has issues. Don't use it.
015 * 
016 * @deprecated Use org.kuali.common.util.runonce.smart.PropertiesFileStateManager instead
017 */
018@Deprecated
019public class PropertiesFileStateManager implements RunOnceStateManager {
020
021        private static final Logger logger = LoggerFactory.getLogger(PropertiesFileStateManager.class);
022
023        public PropertiesFileStateManager(File propertiesFile, String encoding, String persistentPropertyKey) {
024                Assert.noNulls(propertiesFile);
025                Assert.noBlanks(persistentPropertyKey, encoding);
026                this.propertiesFile = new CanonicalFile(propertiesFile);
027                this.encoding = encoding;
028                this.persistentPropertyKey = persistentPropertyKey;
029        }
030
031        private final String persistentPropertyKey;
032        private final File propertiesFile;
033        private final String encoding;
034
035        private Properties properties;
036        private boolean runonce;
037        private boolean initialized;
038
039        @Override
040        public synchronized void initialize() {
041                Assert.isFalse(initialized, "Already initialized");
042                this.properties = getProperties();
043                this.runonce = getRunOnce();
044                this.initialized = true;
045        }
046
047        @Override
048        public synchronized boolean isRunOnce() {
049                Assert.isTrue(initialized, "Not initialized");
050                return runonce;
051        }
052
053        @Override
054        public synchronized void persistState(RunOnceState state) {
055                Assert.isTrue(initialized, "Not initialized");
056                Assert.noNulls(state);
057                properties.setProperty(persistentPropertyKey, state.name());
058                PropertyUtils.store(properties, propertiesFile, encoding);
059        }
060
061        public String getPersistentPropertyKey() {
062                return persistentPropertyKey;
063        }
064
065        public File getPropertiesFile() {
066                return propertiesFile;
067        }
068
069        protected Properties getProperties() {
070                if (propertiesFile.exists()) {
071                        return PropertyUtils.load(propertiesFile, encoding);
072                } else {
073                        logger.info("Skipping execution. File does not exist - [{}]", propertiesFile);
074                        return PropertyUtils.EMPTY;
075                }
076        }
077
078        protected boolean getRunOnce() {
079                if (properties == PropertyUtils.EMPTY) {
080                        return false;
081                } else {
082                        // Log a message indicating we found the properties file and are going to inspect its contents
083                        logger.info("Examining run once property [{}] in [{}]", persistentPropertyKey, propertiesFile);
084                        String value = properties.getProperty(persistentPropertyKey);
085                        boolean runonce = StringUtils.equalsIgnoreCase(Boolean.TRUE.toString(), value);
086                        if (!runonce) {
087                                logger.info("Skipping execution - [{}={}]", persistentPropertyKey, value);
088                        }
089                        return runonce;
090                }
091        }
092
093}