001    /**
002     * Copyright 2010-2013 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     */
016    package org.kuali.common.util.execute;
017    
018    import java.io.File;
019    import java.util.List;
020    import java.util.Properties;
021    
022    import org.kuali.common.util.LocationUtils;
023    import org.kuali.common.util.PropertyUtils;
024    import org.springframework.util.Assert;
025    
026    public class StorePropertiesExecutable implements Executable {
027    
028            String encoding = "UTF-8";
029            boolean skip;
030            boolean skipIfExists = true;
031            Properties properties;
032            File outputFile;
033            List<String> includes;
034            List<String> excludes;
035    
036            @Override
037            public void execute() {
038                    // Nothing to do in this case
039                    if (skip) {
040                            return;
041                    }
042    
043                    // Make sure we have an output file
044                    Assert.notNull(outputFile, "outputFile is null");
045    
046                    // Might not need to do anything
047                    boolean exists = LocationUtils.exists(outputFile);
048                    if (exists && skipIfExists) {
049                            return;
050                    }
051    
052                    // Make sure we have some properties to work with
053                    Assert.notNull(properties, "properties is null");
054    
055                    // Clone the properties they passed us
056                    Properties duplicate = PropertyUtils.duplicate(properties);
057    
058                    // Trim out unwanted properties
059                    PropertyUtils.trim(duplicate, includes, excludes);
060    
061                    // Persist them to the file system
062                    store(duplicate, outputFile, encoding);
063            }
064    
065            protected void store(Properties properties, File outputFile, String encoding) {
066                    PropertyUtils.store(properties, outputFile, encoding, null, true);
067            }
068    
069            public Properties getProperties() {
070                    return properties;
071            }
072    
073            public void setProperties(Properties properties) {
074                    this.properties = properties;
075            }
076    
077            public File getOutputFile() {
078                    return outputFile;
079            }
080    
081            public void setOutputFile(File outputFile) {
082                    this.outputFile = outputFile;
083            }
084    
085            public List<String> getIncludes() {
086                    return includes;
087            }
088    
089            public void setIncludes(List<String> includes) {
090                    this.includes = includes;
091            }
092    
093            public List<String> getExcludes() {
094                    return excludes;
095            }
096    
097            public void setExcludes(List<String> excludes) {
098                    this.excludes = excludes;
099            }
100    
101            public String getEncoding() {
102                    return encoding;
103            }
104    
105            public void setEncoding(String encoding) {
106                    this.encoding = encoding;
107            }
108    
109            public boolean isSkip() {
110                    return skip;
111            }
112    
113            public void setSkip(boolean skip) {
114                    this.skip = skip;
115            }
116    
117            public boolean isSkipIfExists() {
118                    return skipIfExists;
119            }
120    
121            public void setSkipIfExists(boolean skipIfExists) {
122                    this.skipIfExists = skipIfExists;
123            }
124    
125    }