001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.runonce; 017 018import java.io.File; 019import java.util.Properties; 020 021import org.apache.commons.lang3.StringUtils; 022import org.kuali.common.util.Assert; 023import org.kuali.common.util.PropertyUtils; 024import org.kuali.common.util.file.CanonicalFile; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028/** 029 * This implementation has issues. Don't use it. 030 * 031 * @deprecated Use org.kuali.common.util.runonce.smart.PropertiesFileStateManager instead 032 */ 033@Deprecated 034public class PropertiesFileStateManager implements RunOnceStateManager { 035 036 private static final Logger logger = LoggerFactory.getLogger(PropertiesFileStateManager.class); 037 038 public PropertiesFileStateManager(File propertiesFile, String encoding, String persistentPropertyKey) { 039 Assert.noNulls(propertiesFile); 040 Assert.noBlanks(persistentPropertyKey, encoding); 041 this.propertiesFile = new CanonicalFile(propertiesFile); 042 this.encoding = encoding; 043 this.persistentPropertyKey = persistentPropertyKey; 044 } 045 046 private final String persistentPropertyKey; 047 private final File propertiesFile; 048 private final String encoding; 049 050 private Properties properties; 051 private boolean runonce; 052 private boolean initialized; 053 054 @Override 055 public synchronized void initialize() { 056 Assert.isFalse(initialized, "Already initialized"); 057 this.properties = getProperties(); 058 this.runonce = getRunOnce(); 059 this.initialized = true; 060 } 061 062 @Override 063 public synchronized boolean isRunOnce() { 064 Assert.isTrue(initialized, "Not initialized"); 065 return runonce; 066 } 067 068 @Override 069 public synchronized void persistState(RunOnceState state) { 070 Assert.isTrue(initialized, "Not initialized"); 071 Assert.noNulls(state); 072 properties.setProperty(persistentPropertyKey, state.name()); 073 PropertyUtils.store(properties, propertiesFile, encoding); 074 } 075 076 public String getPersistentPropertyKey() { 077 return persistentPropertyKey; 078 } 079 080 public File getPropertiesFile() { 081 return propertiesFile; 082 } 083 084 protected Properties getProperties() { 085 if (propertiesFile.exists()) { 086 return PropertyUtils.load(propertiesFile, encoding); 087 } else { 088 logger.info("Skipping execution. File does not exist - [{}]", propertiesFile); 089 return PropertyUtils.EMPTY; 090 } 091 } 092 093 protected boolean getRunOnce() { 094 if (properties == PropertyUtils.EMPTY) { 095 return false; 096 } else { 097 // Log a message indicating we found the properties file and are going to inspect its contents 098 logger.info("Examining run once property [{}] in [{}]", persistentPropertyKey, propertiesFile); 099 String value = properties.getProperty(persistentPropertyKey); 100 boolean runonce = StringUtils.equalsIgnoreCase(Boolean.TRUE.toString(), value); 101 if (!runonce) { 102 logger.info("Skipping execution - [{}={}]", persistentPropertyKey, value); 103 } 104 return runonce; 105 } 106 } 107 108}