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