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.ArrayList;
036    import java.util.List;
037    
038    import org.apache.commons.io.FileUtils;
039    import org.apache.maven.plugin.logging.Log;
040    import org.apache.maven.wagon.TransferFailedException;
041    import org.apache.maven.wagon.Wagon;
042    import org.apache.maven.wagon.WagonException;
043    import org.codehaus.plexus.util.StringUtils;
044    
045    /**
046     * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonDownload" role-hint="default"
047     */
048    
049    public class DefaultWagonDownload implements WagonDownload {
050    
051        @Override
052        public List<String> getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException {
053            logger.info("Scanning repository - " + wagon.getRepository().getUrl());
054    
055            WagonDirectoryScanner dirScan = new WagonDirectoryScanner();
056            dirScan.setLogger(logger);
057            dirScan.setWagon(wagon);
058            dirScan.setExcludes(fileSet.getExcludes());
059            dirScan.setIncludes(fileSet.getIncludes());
060            dirScan.setCaseSensitive(fileSet.isCaseSensitive());
061            dirScan.setDirectory(fileSet.getDirectory());
062            if (fileSet.isUseDefaultExcludes()) {
063                dirScan.addDefaultExcludes();
064            }
065    
066            long start = System.currentTimeMillis();
067            dirScan.scan();
068            long elapsed = System.currentTimeMillis() - start;
069            logger.info("Scan time: " + (elapsed / 1000) + "s");
070            logger.info("Files located: " + dirScan.getFilesIncluded().size());
071    
072            return dirScan.getFilesIncluded();
073        }
074    
075        @Override
076        public void download(Wagon wagon, WagonFileSet remoteFileSet, Log logger, boolean skipExisting)
077                throws WagonException {
078            List<String> fileList = getFileList(wagon, remoteFileSet, logger);
079    
080            String url = wagon.getRepository().getUrl();
081            url = url.endsWith("/") ? url : url + "/";
082    
083            if (fileList.size() == 0) {
084                logger.info("Nothing to download.");
085                return;
086            }
087    
088            int count = 0;
089            long start = System.currentTimeMillis();
090            List<String> skipped = new ArrayList<String>();
091            for (String remoteFile : fileList) {
092                String index = StringUtils.leftPad((++count) + "", 5, " ");
093    
094                File destination = new File(remoteFileSet.getDownloadDirectory() + "/" + remoteFile);
095    
096                if (skipExisting && destination.exists()) {
097                    String msg = index + " Skipping " + url + remoteFile + " - " + destination + " already exists";
098                    logger.debug(msg);
099                    skipped.add(msg);
100                    continue;
101                }
102    
103                if (!StringUtils.isBlank(remoteFileSet.getDirectory())) {
104                    remoteFile = remoteFileSet.getDirectory() + "/" + remoteFile;
105                }
106    
107                logger.info(index + " Downloading " + url + remoteFile + " to " + destination);
108                try {
109                    FileUtils.touch(destination);
110                } catch (IOException e) {
111                    throw new TransferFailedException("Unexpected IO error", e);
112                }
113    
114                wagon.get(remoteFile, destination);
115            }
116            long elapsed = System.currentTimeMillis() - start;
117            if (skipped.size() > 0) {
118                logger.info("Skipped " + skipped.size() + " resources that already existed on the local file system");
119            }
120            logger.info("Download time: " + (elapsed / 1000) + "s");
121        }
122    
123        /**
124         *
125         * @param wagon
126         *            - a Wagon instance
127         * @param resource
128         *            - Remote resource to check
129         * @throws WagonException
130         */
131        @Override
132        public boolean exists(Wagon wagon, String resource) throws WagonException {
133            return wagon.resourceExists(resource);
134        }
135    
136    }