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.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.commons.lang3.StringUtils;
024import org.codehaus.plexus.util.FileUtils;
025import org.kuali.common.util.Assert;
026import org.kuali.common.util.CollectionUtils;
027import org.kuali.common.util.FileSystemUtils;
028import org.kuali.common.util.LocationUtils;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * @deprecated
034 */
035@Deprecated
036public class CopyFilePatternsExecutable implements Executable {
037
038        private static final Logger logger = LoggerFactory.getLogger(CopyFilePatternsExecutable.class);
039
040        public static final List<String> DEFAULT_INCLUDES = FileSystemUtils.DEFAULT_RECURSIVE_INCLUDES;
041        public static final List<String> DEFAULT_EXCLUDES = new ArrayList<String>();
042
043        List<String> includes = DEFAULT_INCLUDES;
044        List<String> excludes = DEFAULT_EXCLUDES;
045
046        File srcDir;
047        File dstDir;
048        File relativeDir;
049        boolean skip;
050
051        @Override
052        public void execute() {
053
054                if (skip) {
055                        return;
056                }
057
058                // Make sure we are configured correctly
059                Assert.notNull(srcDir, "srcDir is null");
060                Assert.notNull(dstDir, "dstDir is null");
061
062                // Source directory has to exist already. We'll create destination directory if necessary
063                Assert.isExistingDir(srcDir, "srcDir is not an existing directory");
064
065                try {
066                        // Null safe conversion of the lists to CSV
067                        String includesCSV = StringUtils.trimToNull(CollectionUtils.getCSV(includes));
068                        String excludesCSV = StringUtils.trimToNull(CollectionUtils.getCSV(excludes));
069
070                        // Make sure we can create the destination directory
071                        FileUtils.forceMkdir(dstDir);
072
073                        // Show what we are up to
074                        logCopy();
075
076                        // Copy files from src to dst
077                        FileUtils.copyDirectory(srcDir, dstDir, includesCSV, excludesCSV);
078                } catch (IOException e) {
079                        throw new IllegalStateException("Unexpected IO error", e);
080                }
081
082        }
083
084        protected void logCopy() {
085                String path = FileSystemUtils.getRelativePathQuietly(relativeDir, dstDir);
086                Object[] args = { path, org.kuali.common.util.LoggerUtils.getLogMsg(includes, excludes) };
087                logger.debug("srcDir - [{}]", LocationUtils.getCanonicalPath(srcDir));
088                logger.info("Copying to - [{}] - {}", args);
089        }
090
091        public List<String> getIncludes() {
092                return includes;
093        }
094
095        public void setIncludes(List<String> includes) {
096                this.includes = includes;
097        }
098
099        public List<String> getExcludes() {
100                return excludes;
101        }
102
103        public void setExcludes(List<String> excludes) {
104                this.excludes = excludes;
105        }
106
107        public File getSrcDir() {
108                return srcDir;
109        }
110
111        public void setSrcDir(File srcDir) {
112                this.srcDir = srcDir;
113        }
114
115        public File getDstDir() {
116                return dstDir;
117        }
118
119        public void setDstDir(File dstDir) {
120                this.dstDir = dstDir;
121        }
122
123        public File getRelativeDir() {
124                return relativeDir;
125        }
126
127        public void setRelativeDir(File relativeDir) {
128                this.relativeDir = relativeDir;
129        }
130
131        public boolean isSkip() {
132                return skip;
133        }
134
135        public void setSkip(boolean skip) {
136                this.skip = skip;
137        }
138}