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.List;
20 import java.util.Properties;
21
22 import org.kuali.common.util.Encodings;
23 import org.kuali.common.util.LocationUtils;
24 import org.kuali.common.util.PropertyUtils;
25 import org.springframework.util.Assert;
26
27 public class StorePropertiesExecutable implements Executable {
28
29 String encoding = Encodings.UTF8;
30 boolean skip;
31 boolean skipIfExists = true;
32 boolean skipIfEqual = true;
33 Properties properties;
34 File outputFile;
35 List<String> includes;
36 List<String> excludes;
37
38 @Override
39 public void execute() {
40
41 if (skip) {
42 return;
43 }
44
45
46 Assert.notNull(outputFile, "outputFile is null");
47
48
49 if (LocationUtils.exists(outputFile) && skipIfExists) {
50 return;
51 }
52
53
54 Assert.notNull(properties, "properties is null");
55
56
57 Properties duplicate = PropertyUtils.duplicate(properties);
58
59
60 PropertyUtils.trim(duplicate, includes, excludes);
61
62
63 if (!isStoreProperties(outputFile, skipIfEqual, duplicate)) {
64 return;
65 }
66
67
68 store(duplicate, outputFile, encoding);
69 }
70
71 protected boolean isStoreProperties(File outputFile, boolean skipIfEqual, Properties properties) {
72
73 if (!LocationUtils.exists(outputFile)) {
74 return true;
75 }
76
77
78 if (!skipIfEqual) {
79 return true;
80 }
81
82
83 Properties loaded = PropertyUtils.loadSilently(outputFile);
84
85
86 boolean equal = PropertyUtils.equals(loaded, properties);
87
88
89 boolean storeProperties = !equal;
90
91
92 return storeProperties;
93 }
94
95 protected void store(Properties properties, File outputFile, String encoding) {
96 PropertyUtils.store(properties, outputFile, encoding, null, true);
97 }
98
99 public Properties getProperties() {
100 return properties;
101 }
102
103 public void setProperties(Properties properties) {
104 this.properties = properties;
105 }
106
107 public File getOutputFile() {
108 return outputFile;
109 }
110
111 public void setOutputFile(File outputFile) {
112 this.outputFile = outputFile;
113 }
114
115 public List<String> getIncludes() {
116 return includes;
117 }
118
119 public void setIncludes(List<String> includes) {
120 this.includes = includes;
121 }
122
123 public List<String> getExcludes() {
124 return excludes;
125 }
126
127 public void setExcludes(List<String> excludes) {
128 this.excludes = excludes;
129 }
130
131 public String getEncoding() {
132 return encoding;
133 }
134
135 public void setEncoding(String encoding) {
136 this.encoding = encoding;
137 }
138
139 public boolean isSkip() {
140 return skip;
141 }
142
143 public void setSkip(boolean skip) {
144 this.skip = skip;
145 }
146
147 public boolean isSkipIfExists() {
148 return skipIfExists;
149 }
150
151 public void setSkipIfExists(boolean skipIfExists) {
152 this.skipIfExists = skipIfExists;
153 }
154
155 public boolean isSkipIfEqual() {
156 return skipIfEqual;
157 }
158
159 public void setSkipIfEqual(boolean skipIfEqual) {
160 this.skipIfEqual = skipIfEqual;
161 }
162
163 }