001    /**
002     * Copyright 2008-2012 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     */
016    package org.codehaus.mojo.wagon.shared;
017    
018    /*
019     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
020     * agreements. See the NOTICE file distributed with this work for additional information regarding
021     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
022     * "License"); you may not use this file except in compliance with the License. You may obtain a
023     * copy of the License at
024     *
025     * http://www.apache.org/licenses/LICENSE-2.0
026     *
027     * Unless required by applicable law or agreed to in writing, software distributed under the License
028     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
029     * or implied. See the License for the specific language governing permissions and limitations under
030     * the License.
031     */
032    
033    import java.io.File;
034    import java.io.IOException;
035    import java.util.List;
036    
037    import org.apache.commons.io.FileUtils;
038    import org.apache.maven.plugin.logging.Log;
039    import org.apache.maven.wagon.TransferFailedException;
040    import org.apache.maven.wagon.Wagon;
041    import org.apache.maven.wagon.WagonException;
042    import org.codehaus.plexus.util.StringUtils;
043    
044    /**
045     * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonDownload" role-hint="default"
046     */
047    
048    public class DefaultWagonDownload implements WagonDownload {
049    
050        @Override
051        public List<String> getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException {
052            logger.info("Scanning repository - " + wagon.getRepository().getUrl());
053    
054            WagonDirectoryScanner dirScan = new WagonDirectoryScanner();
055            dirScan.setLogger(logger);
056            dirScan.setWagon(wagon);
057            dirScan.setExcludes(fileSet.getExcludes());
058            dirScan.setIncludes(fileSet.getIncludes());
059            dirScan.setCaseSensitive(fileSet.isCaseSensitive());
060            dirScan.setDirectory(fileSet.getDirectory());
061            if (fileSet.isUseDefaultExcludes()) {
062                dirScan.addDefaultExcludes();
063            }
064    
065            dirScan.scan();
066    
067            return dirScan.getFilesIncluded();
068        }
069    
070        @Override
071        public void download(Wagon wagon, WagonFileSet remoteFileSet, Log logger, boolean skipExisting)
072                throws WagonException {
073            List<String> fileList = getFileList(wagon, remoteFileSet, logger);
074    
075            String url = wagon.getRepository().getUrl();
076            url = url.endsWith("/") ? url : url + "/";
077    
078            if (fileList.size() == 0) {
079                logger.info("Nothing to download.");
080                return;
081            } else {
082                logger.info("Located " + fileList.size() + " files to download");
083            }
084    
085            int count = 0;
086            for (String remoteFile : fileList) {
087                String index = StringUtils.leftPad((++count) + "", 5, " ");
088    
089                File destination = new File(remoteFileSet.getDownloadDirectory() + "/" + remoteFile);
090    
091                if (skipExisting && destination.exists()) {
092                    logger.info(index + " Skipping " + url + remoteFile + " - " + destination + " already exists");
093                    continue;
094                }
095    
096                if (!StringUtils.isBlank(remoteFileSet.getDirectory())) {
097                    remoteFile = remoteFileSet.getDirectory() + "/" + remoteFile;
098                }
099    
100                logger.info(index + " Downloading " + url + remoteFile + " to " + destination);
101                try {
102                    FileUtils.touch(destination);
103                } catch (IOException e) {
104                    throw new TransferFailedException("Unexpected IO error", e);
105                }
106    
107                wagon.get(remoteFile, destination);
108            }
109        }
110    
111        /**
112         *
113         * @param wagon
114         *            - a Wagon instance
115         * @param resource
116         *            - Remote resource to check
117         * @throws WagonException
118         */
119        @Override
120        public boolean exists(Wagon wagon, String resource) throws WagonException {
121            return wagon.resourceExists(resource);
122        }
123    
124    }