View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.execute;
17  
18  import java.io.File;
19  import java.util.Properties;
20  
21  import org.kuali.common.util.LocationUtils;
22  import org.kuali.common.util.PropertyUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.util.Assert;
26  
27  /**
28   * @deprecated
29   */
30  @Deprecated
31  public class RunOnceExecutable implements Executable {
32  
33  	private static final Logger logger = LoggerFactory.getLogger(RunOnceExecutable.class);
34  
35  	Executable executable;
36  	File propertiesFile;
37  	String property;
38  	String encoding;
39  	boolean skip;
40  
41  	@Override
42  	public void execute() {
43  		if (skip) {
44  			logger.info("Skipping execution");
45  			return;
46  		}
47  		Assert.notNull(propertiesFile);
48  		Assert.notNull(property);
49  		Assert.notNull(executable);
50  
51  		if (!propertiesFile.exists()) {
52  			logger.info("Skipping execution. File does not exist - [{}]", LocationUtils.getCanonicalPath(propertiesFile));
53  			return;
54  		}
55  
56  		logger.info("Examining run once property [{}] in [{}]", property, LocationUtils.getCanonicalPath(propertiesFile));
57  
58  		// Load the properties
59  		Properties properties = PropertyUtils.load(propertiesFile, encoding);
60  
61  		// Translate the property value into an execution mode
62  		ExecutionMode mode = getExecutionMode(properties, property);
63  
64  		// Are we going to run once?
65  		if (!isRunOnce(mode)) {
66  			logger.info("Skipping execution - [{}={}]", property, mode);
67  			return;
68  		}
69  
70  		// Show the execution mode we are in
71  		logger.info("{}={}", property, mode);
72  
73  		// Make sure we have the ability to successfully store updated properties back to the file
74  		if (!isAlways(mode)) {
75  			setState(properties, property, ExecutionMode.INPROGRESS);
76  		}
77  
78  		try {
79  			// Invoke execute now that we have successfully transitioned things to INPROGRESS
80  			executable.execute();
81  			// There is always a chance that the executable finishes correctly and we encounter some kind of
82  			// issue just storing the properties back to the file. This should be pretty rare considering
83  			// we were able to successfully store the properties just prior to the executable commencing.
84  			// In any event, the executable won't run again in the normal use case because we can only get to this point
85  			// if the original execution mode was "TRUE" and we were able to successfully change it to "INPROGRESS"
86  			if (!isAlways(mode)) {
87  				setState(properties, property, ExecutionMode.COMPLETED);
88  			}
89  		} catch (Exception e) {
90  			if (!isAlways(mode)) {
91  				setState(properties, property, ExecutionMode.FAILED);
92  			}
93  			throw new IllegalStateException("Unexpected execution error", e);
94  		}
95  	}
96  
97  	protected boolean isAlways(ExecutionMode mode) {
98  		return ExecutionMode.ALWAYS.equals(mode);
99  	}
100 
101 	protected boolean isRunOnce(ExecutionMode mode) {
102 		if (ExecutionMode.RUNONCE.equals(mode)) {
103 			return true;
104 		}
105 		if (isAlways(mode)) {
106 			return true;
107 		}
108 		return ExecutionMode.TRUE.equals(mode);
109 	}
110 
111 	protected ExecutionMode getExecutionMode(Properties properties, String key) {
112 		String value = properties.getProperty(property);
113 		if (value == null) {
114 			return ExecutionMode.NULL;
115 		} else {
116 			return ExecutionMode.valueOf(value.toUpperCase());
117 		}
118 
119 	}
120 
121 	protected void setState(Properties properties, String key, ExecutionMode mode) {
122 		logger.info("{}={}", key, mode);
123 		properties.setProperty(property, mode.name());
124 		PropertyUtils.store(properties, propertiesFile, encoding);
125 	}
126 
127 	public Executable getExecutable() {
128 		return executable;
129 	}
130 
131 	public void setExecutable(Executable executable) {
132 		this.executable = executable;
133 	}
134 
135 	public File getPropertiesFile() {
136 		return propertiesFile;
137 	}
138 
139 	public void setPropertiesFile(File propertiesFile) {
140 		this.propertiesFile = propertiesFile;
141 	}
142 
143 	public String getProperty() {
144 		return property;
145 	}
146 
147 	public void setProperty(String property) {
148 		this.property = property;
149 	}
150 
151 	public String getEncoding() {
152 		return encoding;
153 	}
154 
155 	public void setEncoding(String encoding) {
156 		this.encoding = encoding;
157 	}
158 
159 	public boolean isSkip() {
160 		return skip;
161 	}
162 
163 	public void setSkip(boolean skip) {
164 		this.skip = skip;
165 	}
166 }