View Javadoc

1   package org.codehaus.mojo.wagon.shared;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.util.zip.ZipEntry;
23  import java.util.zip.ZipOutputStream;
24  
25  import org.apache.maven.plugin.logging.Log;
26  import org.apache.maven.shared.model.fileset.FileSet;
27  import org.apache.maven.shared.model.fileset.util.FileSetManager;
28  import org.apache.maven.wagon.CommandExecutor;
29  import org.apache.maven.wagon.UnsupportedProtocolException;
30  import org.apache.maven.wagon.Wagon;
31  import org.apache.maven.wagon.WagonException;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  /**
36   * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonUpload" role-hint="default"
37   */
38  
39  public class DefaultWagonUpload implements WagonUpload {
40      public void upload(Wagon wagon, FileSet fileset, Log logger) throws WagonException {
41  
42          FileSetManager fileSetManager = new FileSetManager(logger, logger.isDebugEnabled());
43  
44          String[] files = fileSetManager.getIncludedFiles(fileset);
45  
46          String url = wagon.getRepository().getUrl() + "/";
47  
48          if (files.length == 0) {
49              logger.info("Nothing to upload.");
50              return;
51          }
52  
53          for (int i = 0; i < files.length; ++i) {
54              String relativeDestPath = StringUtils.replace(files[i], "\\", "/");
55  
56              if (!StringUtils.isBlank(fileset.getOutputDirectory())) {
57                  relativeDestPath = fileset.getOutputDirectory() + "/" + relativeDestPath;
58              }
59  
60              File source = new File(fileset.getDirectory(), files[i]);
61  
62              logger.info("Uploading " + source + " to " + url + relativeDestPath + " ...");
63  
64              wagon.put(source, relativeDestPath);
65          }
66  
67      }
68  
69      @Override
70      public void upload(Wagon wagon, FileSet fileset, boolean optimize, Log logger) throws WagonException, IOException {
71          if (!optimize) {
72              upload(wagon, fileset, logger);
73              return;
74          }
75  
76          if (!(wagon instanceof CommandExecutor)) {
77              throw new UnsupportedProtocolException("Wagon " + wagon.getRepository().getProtocol()
78                      + " does not support optimize upload");
79          }
80  
81          logger.info("Uploading " + fileset);
82  
83          File zipFile;
84          zipFile = File.createTempFile("wagon", ".zip");
85  
86          try {
87              FileSetManager fileSetManager = new FileSetManager(logger, logger.isDebugEnabled());
88              String[] files = fileSetManager.getIncludedFiles(fileset);
89  
90              if (files.length == 0) {
91                  logger.info("Nothing to upload.");
92                  return;
93              }
94  
95              logger.info("Creating " + zipFile + " ...");
96              createZip(files, zipFile, fileset.getDirectory());
97  
98              String remoteFile = zipFile.getName();
99              String remoteDir = fileset.getOutputDirectory();
100             if (!StringUtils.isBlank(remoteDir)) {
101                 remoteFile = remoteDir + "/" + remoteFile;
102             }
103 
104             logger.info("Uploading " + zipFile + " to " + wagon.getRepository().getUrl() + "/" + remoteFile + " ...");
105             wagon.put(zipFile, remoteFile);
106 
107             // We use the super quiet option here as all the noise seems to kill/stall the connection
108             String command = "unzip -o -qq -d " + remoteDir + " " + remoteFile;
109 
110             try {
111                 logger.info("Remote: " + command);
112                 ((CommandExecutor) wagon).executeCommand(command);
113             } finally {
114                 command = "rm -f " + remoteFile;
115                 logger.info("Remote: " + command);
116 
117                 ((CommandExecutor) wagon).executeCommand(command);
118             }
119 
120         } finally {
121             zipFile.delete();
122         }
123 
124     }
125 
126     private static void createZip(String[] files, File zipName, String basedir) throws IOException {
127         ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
128 
129         try {
130             for (int i = 0; i < files.length; i++) {
131                 String file = files[i];
132 
133                 file = file.replace('\\', '/');
134 
135                 writeZipEntry(zos, new File(basedir, file), file);
136             }
137         } finally {
138             IOUtil.close(zos);
139         }
140     }
141 
142     private static void writeZipEntry(ZipOutputStream jar, File source, String entryName) throws IOException {
143         byte[] buffer = new byte[1024];
144 
145         int bytesRead;
146 
147         FileInputStream is = new FileInputStream(source);
148 
149         try {
150             ZipEntry entry = new ZipEntry(entryName);
151 
152             jar.putNextEntry(entry);
153 
154             while ((bytesRead = is.read(buffer)) != -1) {
155                 jar.write(buffer, 0, bytesRead);
156             }
157         } finally {
158             is.close();
159         }
160     }
161 
162 }