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.CollectionUtils;
23  import org.kuali.common.util.ComparisonResults;
24  import org.kuali.common.util.LocationUtils;
25  import org.kuali.common.util.PropertyUtils;
26  import org.kuali.common.util.SimpleScanner;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.util.Assert;
30  
31  /**
32   * This executable compares the files found by the given pattern in two different locations and populates a properties
33   * object with information about the comparison
34   * 
35   * @author andrewlubbers
36   */
37  public class FileListComparisonExecutable implements Executable {
38  
39  	private static final Logger logger = LoggerFactory.getLogger(FileListComparisonExecutable.class);
40  
41  	private String newFilesBaseDir;
42  	private String filePattern;
43  	private String originalFilesBaseDir;
44  	private List<String> propertyNames;
45  	private Properties mavenProperties;
46  
47  	@Override
48  	public void execute() {
49  		Assert.notNull(mavenProperties);
50  		Assert.notNull(propertyNames);
51  		Assert.notNull(newFilesBaseDir);
52  		Assert.notNull(originalFilesBaseDir);
53  		Assert.notNull(filePattern);
54  
55  		logger.info("Starting File List Comparison");
56  		logger.info("Original files dir: " + originalFilesBaseDir);
57  		logger.info("New files dir: " + LocationUtils.getCanonicalPath(new File(newFilesBaseDir)));
58  		logger.info("File pattern: " + filePattern);
59  		logger.info("Property names: " + propertyNames);
60  
61  		List<String> filePatterns = CollectionUtils.getTrimmedListFromCSV(filePattern);
62  		String[] includesArray = filePatterns.toArray(new String[filePatterns.size()]);
63  
64  		SimpleScanner scanner = new SimpleScanner();
65  		scanner.setBasedir(newFilesBaseDir);
66  		scanner.setIncludes(includesArray);
67  
68  		List<String> newLocations = LocationUtils.getAbsolutePaths(scanner.getFiles());
69  		List<String> newFileNames = LocationUtils.getFilenames(newLocations);
70  
71  		scanner = new SimpleScanner();
72  		scanner.setBasedir(originalFilesBaseDir);
73  		scanner.setIncludes(includesArray);
74  
75  		List<String> originalLocations = LocationUtils.getAbsolutePaths(scanner.getFiles());
76  		List<String> originalFileNames = LocationUtils.getFilenames(originalLocations);
77  
78  		ComparisonResults comparison = LocationUtils.getLocationListComparison(newFileNames, originalFileNames);
79  
80  		logger.info("Added {} files.", comparison.getAdded().size());
81  		logger.info("Updated {} existing files.", comparison.getSame().size());
82  		logger.info("Deleted {} files.", comparison.getDeleted().size());
83  
84  		PropertyUtils.addListComparisonProperties(mavenProperties, comparison, propertyNames);
85  	}
86  
87  	public String getFilePattern() {
88  		return filePattern;
89  	}
90  
91  	public void setFilePattern(String filePattern) {
92  		this.filePattern = filePattern;
93  	}
94  
95  	public Properties getMavenProperties() {
96  		return mavenProperties;
97  	}
98  
99  	public void setMavenProperties(Properties mavenProperties) {
100 		this.mavenProperties = mavenProperties;
101 	}
102 
103 	public String getNewFilesBaseDir() {
104 		return newFilesBaseDir;
105 	}
106 
107 	public void setNewFilesBaseDir(String newFilesBaseDir) {
108 		this.newFilesBaseDir = newFilesBaseDir;
109 	}
110 
111 	public String getOriginalFilesBaseDir() {
112 		return originalFilesBaseDir;
113 	}
114 
115 	public void setOriginalFilesBaseDir(String originalFilesBaseDir) {
116 		this.originalFilesBaseDir = originalFilesBaseDir;
117 	}
118 
119 	public List<String> getPropertyNames() {
120 		return propertyNames;
121 	}
122 
123 	public void setPropertyNames(List<String> propertyNames) {
124 		this.propertyNames = propertyNames;
125 	}
126 }