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