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 */
016package org.kuali.common.util.execute;
017
018import org.kuali.common.util.Assert;
019import org.kuali.common.util.CollectionUtils;
020import org.kuali.common.util.LocationUtils;
021import org.kuali.common.util.SimpleScanner;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import java.io.File;
026import java.util.ArrayList;
027import 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 */
034public 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}