001 /**
002 * Copyright 2011 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015
016 package org.kuali.common.util.execute;
017
018 import org.kuali.common.util.Assert;
019 import org.kuali.common.util.CollectionUtils;
020 import org.kuali.common.util.LocationUtils;
021 import org.kuali.common.util.SimpleScanner;
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024
025 import java.io.File;
026 import java.util.ArrayList;
027 import java.util.List;
028
029 /**
030 * File copying executable that uses simple patterns to copy files from one directory to another
031 *
032 * @author andrewlubbers
033 */
034 public class FileCopyExecutable implements Executable {
035
036 private static final Logger logger = LoggerFactory.getLogger(FileCopyExecutable.class);
037
038 private String sourceDir;
039 private String filePatterns;
040 private String destinationDir;
041
042 @Override
043 public void execute() {
044 Assert.isTrue(LocationUtils.exists(sourceDir));
045 Assert.notNull(destinationDir);
046 Assert.notNull(filePatterns);
047
048 String sourceDirPath = LocationUtils.getCanonicalPath(new File(sourceDir));
049
050 logger.info("Starting File Copy");
051 logger.info("From: " + sourceDirPath);
052 logger.info("To: " + destinationDir);
053 logger.info("Using patterns: " + filePatterns);
054
055 List<String> patterns = CollectionUtils.getTrimmedListFromCSV(filePatterns);
056
057 SimpleScanner scanner = new SimpleScanner();
058 scanner.setBasedir(sourceDir);
059 scanner.setIncludes(patterns.toArray(new String[patterns.size()]));
060
061 List<File> sourceFiles = scanner.getFiles();
062 logger.info("Found " + sourceFiles.size() + " matching source files.");
063
064
065 List<String> sourceLocations = new ArrayList<String>(sourceFiles.size());
066 List<File> destinationFiles = new ArrayList<File>(sourceFiles.size());
067 for(File f : sourceFiles) {
068 String sourcePath = LocationUtils.getCanonicalPath(f);
069 sourceLocations.add(sourcePath);
070
071 String destinationPath = sourcePath.replace(sourceDirPath, (destinationDir + File.separator));
072 destinationFiles.add(new File(destinationPath));
073 }
074
075 LocationUtils.copyLocationsToFiles(sourceLocations, destinationFiles);
076
077 logger.info("File Copy Complete");
078 }
079
080 public String getDestinationDir() {
081 return destinationDir;
082 }
083
084 public void setDestinationDir(String destinationDir) {
085 this.destinationDir = destinationDir;
086 }
087
088 public String getFilePatterns() {
089 return filePatterns;
090 }
091
092 public void setFilePatterns(String filePatterns) {
093 this.filePatterns = filePatterns;
094 }
095
096 public String getSourceDir() {
097 return sourceDir;
098 }
099
100 public void setSourceDir(String sourceDir) {
101 this.sourceDir = sourceDir;
102 }
103 }