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.List;
36  
37  import org.apache.commons.io.FileUtils;
38  import org.apache.maven.plugin.logging.Log;
39  import org.apache.maven.wagon.TransferFailedException;
40  import org.apache.maven.wagon.Wagon;
41  import org.apache.maven.wagon.WagonException;
42  import org.codehaus.plexus.util.StringUtils;
43  
44  /**
45   * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonDownload" role-hint="default"
46   */
47  
48  public class DefaultWagonDownload implements WagonDownload {
49  
50      @Override
51      public List<String> getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException {
52          logger.info("Scanning repository - " + wagon.getRepository().getUrl());
53  
54          WagonDirectoryScanner dirScan = new WagonDirectoryScanner();
55          dirScan.setLogger(logger);
56          dirScan.setWagon(wagon);
57          dirScan.setExcludes(fileSet.getExcludes());
58          dirScan.setIncludes(fileSet.getIncludes());
59          dirScan.setCaseSensitive(fileSet.isCaseSensitive());
60          dirScan.setDirectory(fileSet.getDirectory());
61          if (fileSet.isUseDefaultExcludes()) {
62              dirScan.addDefaultExcludes();
63          }
64  
65          dirScan.scan();
66  
67          return dirScan.getFilesIncluded();
68      }
69  
70      @Override
71      public void download(Wagon wagon, WagonFileSet remoteFileSet, Log logger, boolean skipExisting)
72              throws WagonException {
73          List<String> fileList = getFileList(wagon, remoteFileSet, logger);
74  
75          String url = wagon.getRepository().getUrl();
76          url = url.endsWith("/") ? url : url + "/";
77  
78          if (fileList.size() == 0) {
79              logger.info("Nothing to download.");
80              return;
81          } else {
82              logger.info("Located " + fileList.size() + " files to download");
83          }
84  
85          int count = 0;
86          for (String remoteFile : fileList) {
87              String index = StringUtils.leftPad((++count) + "", 5, " ");
88  
89              File destination = new File(remoteFileSet.getDownloadDirectory() + "/" + remoteFile);
90  
91              if (skipExisting && destination.exists()) {
92                  logger.info(index + " Skipping " + url + remoteFile + " - " + destination + " already exists");
93                  continue;
94              }
95  
96              if (!StringUtils.isBlank(remoteFileSet.getDirectory())) {
97                  remoteFile = remoteFileSet.getDirectory() + "/" + remoteFile;
98              }
99  
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 }