001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.execute; 017 018import java.io.File; 019import java.util.List; 020import java.util.Properties; 021 022import org.kuali.common.util.Encodings; 023import org.kuali.common.util.LocationUtils; 024import org.kuali.common.util.PropertyUtils; 025import org.springframework.util.Assert; 026 027public class StorePropertiesExecutable implements Executable { 028 029 String encoding = Encodings.UTF8; 030 boolean skip; 031 boolean skipIfExists = true; 032 boolean skipIfEqual = true; 033 Properties properties; 034 File outputFile; 035 List<String> includes; 036 List<String> excludes; 037 038 @Override 039 public void execute() { 040 // Nothing to do in this case 041 if (skip) { 042 return; 043 } 044 045 // Make sure we have an output file 046 Assert.notNull(outputFile, "outputFile is null"); 047 048 // May not need to go any further 049 if (LocationUtils.exists(outputFile) && skipIfExists) { 050 return; 051 } 052 053 // Make sure we have some properties to work with 054 Assert.notNull(properties, "properties is null"); 055 056 // Clone the properties they passed us 057 Properties duplicate = PropertyUtils.duplicate(properties); 058 059 // Trim out unwanted properties 060 PropertyUtils.trim(duplicate, includes, excludes); 061 062 // Might not need to store them 063 if (!isStoreProperties(outputFile, skipIfEqual, duplicate)) { 064 return; 065 } 066 067 // Persist them to the file system 068 store(duplicate, outputFile, encoding); 069 } 070 071 protected boolean isStoreProperties(File outputFile, boolean skipIfEqual, Properties properties) { 072 // Always return true if the file does not exist 073 if (!LocationUtils.exists(outputFile)) { 074 return true; 075 } 076 077 // The file might exist and contain the exact same properties, but it doesn't matter 078 if (!skipIfEqual) { 079 return true; 080 } 081 082 // Load the existing properties 083 Properties loaded = PropertyUtils.loadSilently(outputFile); 084 085 // Compare the loaded properties with the properties we have 086 boolean equal = PropertyUtils.equals(loaded, properties); 087 088 // If they are not equal to each other, we need to store them 089 boolean storeProperties = !equal; 090 091 // Return our value 092 return storeProperties; 093 } 094 095 protected void store(Properties properties, File outputFile, String encoding) { 096 PropertyUtils.store(properties, outputFile, encoding, null, true); 097 } 098 099 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}