1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.execute;
17
18 import org.kuali.common.util.Assert;
19 import org.kuali.common.util.CollectionUtils;
20 import org.kuali.common.util.LocationUtils;
21 import org.kuali.common.util.SimpleScanner;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.List;
28
29
30
31
32
33
34 public class FileCopyExecutable implements Executable {
35
36 private static final Logger logger = LoggerFactory.getLogger(FileCopyExecutable.class);
37
38 private String sourceDir;
39 private String filePatterns;
40 private String destinationDir;
41
42 @Override
43 public void execute() {
44 Assert.isTrue(LocationUtils.exists(sourceDir));
45 Assert.notNull(destinationDir);
46 Assert.notNull(filePatterns);
47
48 String sourceDirPath = LocationUtils.getCanonicalPath(new File(sourceDir));
49
50 logger.info("Starting File Copy");
51 logger.info("From: " + sourceDirPath);
52 logger.info("To: " + destinationDir);
53 logger.info("Using patterns: " + filePatterns);
54
55 List<String> patterns = CollectionUtils.getTrimmedListFromCSV(filePatterns);
56
57 SimpleScanner scanner = new SimpleScanner();
58 scanner.setBasedir(sourceDir);
59 scanner.setIncludes(patterns.toArray(new String[patterns.size()]));
60
61 List<File> sourceFiles = scanner.getFiles();
62 logger.info("Found " + sourceFiles.size() + " matching source files.");
63
64
65 List<String> sourceLocations = new ArrayList<String>(sourceFiles.size());
66 List<File> destinationFiles = new ArrayList<File>(sourceFiles.size());
67 for(File f : sourceFiles) {
68 String sourcePath = LocationUtils.getCanonicalPath(f);
69 sourceLocations.add(sourcePath);
70
71 String destinationPath = sourcePath.replace(sourceDirPath, (destinationDir + File.separator));
72 destinationFiles.add(new File(destinationPath));
73 }
74
75 LocationUtils.copyLocationsToFiles(sourceLocations, destinationFiles);
76
77 logger.info("File Copy Complete");
78 }
79
80 public String getDestinationDir() {
81 return destinationDir;
82 }
83
84 public void setDestinationDir(String destinationDir) {
85 this.destinationDir = destinationDir;
86 }
87
88 public String getFilePatterns() {
89 return filePatterns;
90 }
91
92 public void setFilePatterns(String filePatterns) {
93 this.filePatterns = filePatterns;
94 }
95
96 public String getSourceDir() {
97 return sourceDir;
98 }
99
100 public void setSourceDir(String sourceDir) {
101 this.sourceDir = sourceDir;
102 }
103 }