001/**
002 * Copyright 2010-2014 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 java.io.File;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.kuali.common.util.Assert;
023import org.kuali.common.util.CollectionUtils;
024import org.kuali.common.util.LocationUtils;
025import org.kuali.common.util.SimpleScanner;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * File copying executable that uses simple patterns to copy files from one directory to another
031 * 
032 * @author andrewlubbers
033 * 
034 * @deprecated
035 */
036@Deprecated
037public class FileCopyExecutable implements Executable {
038
039        private static final Logger logger = LoggerFactory.getLogger(FileCopyExecutable.class);
040
041        private String sourceDir;
042        private String filePatterns;
043        private String destinationDir;
044
045        @Override
046        public void execute() {
047                Assert.isTrue(LocationUtils.exists(sourceDir));
048                Assert.notNull(destinationDir);
049                Assert.notNull(filePatterns);
050
051                String sourceDirPath = LocationUtils.getCanonicalPath(new File(sourceDir));
052
053                logger.info("Starting File Copy");
054                logger.info("From: " + sourceDirPath);
055                logger.info("To: " + destinationDir);
056                logger.info("Using patterns: " + filePatterns);
057
058                List<String> patterns = CollectionUtils.getTrimmedListFromCSV(filePatterns);
059
060                SimpleScanner scanner = new SimpleScanner();
061                scanner.setBasedir(sourceDir);
062                scanner.setIncludes(patterns.toArray(new String[patterns.size()]));
063
064                List<File> sourceFiles = scanner.getFiles();
065                logger.info("Found " + sourceFiles.size() + " matching source files.");
066
067                List<String> sourceLocations = new ArrayList<String>(sourceFiles.size());
068                List<File> destinationFiles = new ArrayList<File>(sourceFiles.size());
069                for (File f : sourceFiles) {
070                        String sourcePath = LocationUtils.getCanonicalPath(f);
071                        sourceLocations.add(sourcePath);
072
073                        String destinationPath = sourcePath.replace(sourceDirPath, (destinationDir + File.separator));
074                        destinationFiles.add(new File(destinationPath));
075                }
076
077                LocationUtils.copyLocationsToFiles(sourceLocations, destinationFiles);
078
079                logger.info("File Copy Complete");
080        }
081
082        public String getDestinationDir() {
083                return destinationDir;
084        }
085
086        public void setDestinationDir(String destinationDir) {
087                this.destinationDir = destinationDir;
088        }
089
090        public String getFilePatterns() {
091                return filePatterns;
092        }
093
094        public void setFilePatterns(String filePatterns) {
095                this.filePatterns = filePatterns;
096        }
097
098        public String getSourceDir() {
099                return sourceDir;
100        }
101
102        public void setSourceDir(String sourceDir) {
103                this.sourceDir = sourceDir;
104        }
105}