1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
59 Properties properties = PropertyUtils.load(propertiesFile, encoding);
60
61
62 ExecutionMode mode = getExecutionMode(properties, property);
63
64
65 if (!isRunOnce(mode)) {
66 logger.info("Skipping execution - [{}={}]", property, mode);
67 return;
68 }
69
70
71 logger.info("{}={}", property, mode);
72
73
74 if (!isAlways(mode)) {
75 setState(properties, property, ExecutionMode.INPROGRESS);
76 }
77
78 try {
79
80 executable.execute();
81
82
83
84
85
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 }