View Javadoc
1   /**
2    * Copyright 2010-2014 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.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  		// Nothing to do in this case
41  		if (skip) {
42  			return;
43  		}
44  
45  		// Make sure we have an output file
46  		Assert.notNull(outputFile, "outputFile is null");
47  
48  		// May not need to go any further
49  		if (LocationUtils.exists(outputFile) && skipIfExists) {
50  			return;
51  		}
52  
53  		// Make sure we have some properties to work with
54  		Assert.notNull(properties, "properties is null");
55  
56  		// Clone the properties they passed us
57  		Properties duplicate = PropertyUtils.duplicate(properties);
58  
59  		// Trim out unwanted properties
60  		PropertyUtils.trim(duplicate, includes, excludes);
61  
62  		// Might not need to store them
63  		if (!isStoreProperties(outputFile, skipIfEqual, duplicate)) {
64  			return;
65  		}
66  
67  		// Persist them to the file system
68  		store(duplicate, outputFile, encoding);
69  	}
70  
71  	protected boolean isStoreProperties(File outputFile, boolean skipIfEqual, Properties properties) {
72  		// Always return true if the file does not exist
73  		if (!LocationUtils.exists(outputFile)) {
74  			return true;
75  		}
76  
77  		// The file might exist and contain the exact same properties, but it doesn't matter
78  		if (!skipIfEqual) {
79  			return true;
80  		}
81  
82  		// Load the existing properties
83  		Properties loaded = PropertyUtils.loadSilently(outputFile);
84  
85  		// Compare the loaded properties with the properties we have
86  		boolean equal = PropertyUtils.equals(loaded, properties);
87  
88  		// If they are not equal to each other, we need to store them
89  		boolean storeProperties = !equal;
90  
91  		// Return our value
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 }