View Javadoc

1   /**
2    * Copyright 2008-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.wagon.shared;
17  
18  /*
19   * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
20   * agreements. See the NOTICE file distributed with this work for additional information regarding
21   * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
22   * "License"); you may not use this file except in compliance with the License. You may obtain a
23   * copy of the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software distributed under the License
28   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29   * or implied. See the License for the specific language governing permissions and limitations under
30   * the License.
31   */
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import org.apache.commons.io.FileUtils;
39  import org.apache.maven.plugin.logging.Log;
40  import org.apache.maven.wagon.TransferFailedException;
41  import org.apache.maven.wagon.Wagon;
42  import org.apache.maven.wagon.WagonException;
43  import org.codehaus.plexus.util.StringUtils;
44  
45  /**
46   * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonDownload" role-hint="default"
47   */
48  
49  public class DefaultWagonDownload implements WagonDownload {
50  
51      @Override
52      public List<String> getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException {
53          logger.info("Scanning repository - " + wagon.getRepository().getUrl());
54  
55          WagonDirectoryScanner dirScan = new WagonDirectoryScanner();
56          dirScan.setLogger(logger);
57          dirScan.setWagon(wagon);
58          dirScan.setExcludes(fileSet.getExcludes());
59          dirScan.setIncludes(fileSet.getIncludes());
60          dirScan.setCaseSensitive(fileSet.isCaseSensitive());
61          dirScan.setDirectory(fileSet.getDirectory());
62          if (fileSet.isUseDefaultExcludes()) {
63              dirScan.addDefaultExcludes();
64          }
65  
66          long start = System.currentTimeMillis();
67          dirScan.scan();
68          long elapsed = System.currentTimeMillis() - start;
69          logger.info("Scan time: " + (elapsed / 1000) + "s");
70          logger.info("Files located: " + dirScan.getFilesIncluded().size());
71  
72          return dirScan.getFilesIncluded();
73      }
74  
75      @Override
76      public void download(Wagon wagon, WagonFileSet remoteFileSet, Log logger, boolean skipExisting)
77              throws WagonException {
78          List<String> fileList = getFileList(wagon, remoteFileSet, logger);
79  
80          String url = wagon.getRepository().getUrl();
81          url = url.endsWith("/") ? url : url + "/";
82  
83          if (fileList.size() == 0) {
84              logger.info("Nothing to download.");
85              return;
86          }
87  
88          int count = 0;
89          long start = System.currentTimeMillis();
90          List<String> skipped = new ArrayList<String>();
91          for (String remoteFile : fileList) {
92              String index = StringUtils.leftPad((++count) + "", 5, " ");
93  
94              File destination = new File(remoteFileSet.getDownloadDirectory() + "/" + remoteFile);
95  
96              if (skipExisting && destination.exists()) {
97                  String msg = index + " Skipping " + url + remoteFile + " - " + destination + " already exists";
98                  logger.debug(msg);
99                  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 exist 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 }