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 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   * File copying executable that uses simple patterns to copy files from one directory to another
31   *
32   * @author andrewlubbers
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 }