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.FileInputStream;
35  import java.io.FileOutputStream;
36  import java.io.IOException;
37  import java.util.zip.ZipEntry;
38  import java.util.zip.ZipOutputStream;
39  
40  import org.apache.maven.plugin.logging.Log;
41  import org.apache.maven.shared.model.fileset.FileSet;
42  import org.apache.maven.shared.model.fileset.util.FileSetManager;
43  import org.apache.maven.wagon.CommandExecutor;
44  import org.apache.maven.wagon.UnsupportedProtocolException;
45  import org.apache.maven.wagon.Wagon;
46  import org.apache.maven.wagon.WagonException;
47  import org.codehaus.plexus.util.IOUtil;
48  import org.codehaus.plexus.util.StringUtils;
49  
50  /**
51   * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonUpload" role-hint="default"
52   */
53  
54  public class DefaultWagonUpload implements WagonUpload {
55      public void upload(Wagon wagon, FileSet fileset, Log logger) throws WagonException {
56  
57          FileSetManager fileSetManager = new FileSetManager(logger, logger.isDebugEnabled());
58  
59          String[] files = fileSetManager.getIncludedFiles(fileset);
60  
61          String url = wagon.getRepository().getUrl() + "/";
62  
63          if (files.length == 0) {
64              logger.info("Nothing to upload.");
65              return;
66          }
67  
68          for (int i = 0; i < files.length; ++i) {
69              String relativeDestPath = StringUtils.replace(files[i], "\\", "/");
70  
71              if (!StringUtils.isBlank(fileset.getOutputDirectory())) {
72                  relativeDestPath = fileset.getOutputDirectory() + "/" + relativeDestPath;
73              }
74  
75              File source = new File(fileset.getDirectory(), files[i]);
76  
77              logger.info("Uploading " + source + " to " + url + relativeDestPath + " ...");
78  
79              wagon.put(source, relativeDestPath);
80          }
81  
82      }
83  
84      @Override
85      public void upload(Wagon wagon, FileSet fileset, boolean optimize, Log logger) throws WagonException, IOException {
86          if (!optimize) {
87              upload(wagon, fileset, logger);
88              return;
89          }
90  
91          if (!(wagon instanceof CommandExecutor)) {
92              throw new UnsupportedProtocolException("Wagon " + wagon.getRepository().getProtocol()
93                      + " does not support optimize upload");
94          }
95  
96          logger.info("Uploading " + fileset);
97  
98          File zipFile;
99          zipFile = File.createTempFile("wagon", ".zip");
100 
101         try {
102             FileSetManager fileSetManager = new FileSetManager(logger, logger.isDebugEnabled());
103             String[] files = fileSetManager.getIncludedFiles(fileset);
104 
105             if (files.length == 0) {
106                 logger.info("Nothing to upload.");
107                 return;
108             }
109 
110             logger.info("Creating " + zipFile + " ...");
111             createZip(files, zipFile, fileset.getDirectory());
112 
113             String remoteFile = zipFile.getName();
114             String remoteDir = fileset.getOutputDirectory();
115             if (!StringUtils.isBlank(remoteDir)) {
116                 remoteFile = remoteDir + "/" + remoteFile;
117             }
118 
119             logger.info("Uploading " + zipFile + " to " + wagon.getRepository().getUrl() + "/" + remoteFile + " ...");
120             wagon.put(zipFile, remoteFile);
121 
122             // We use the super quiet option here as all the noise seems to kill/stall the connection
123             String command = "unzip -o -qq -d " + remoteDir + " " + remoteFile;
124 
125             try {
126                 logger.info("Remote: " + command);
127                 ((CommandExecutor) wagon).executeCommand(command);
128             } finally {
129                 command = "rm -f " + remoteFile;
130                 logger.info("Remote: " + command);
131 
132                 ((CommandExecutor) wagon).executeCommand(command);
133             }
134 
135         } finally {
136             zipFile.delete();
137         }
138 
139     }
140 
141     private static void createZip(String[] files, File zipName, String basedir) throws IOException {
142         ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
143 
144         try {
145             for (int i = 0; i < files.length; i++) {
146                 String file = files[i];
147 
148                 file = file.replace('\\', '/');
149 
150                 writeZipEntry(zos, new File(basedir, file), file);
151             }
152         } finally {
153             IOUtil.close(zos);
154         }
155     }
156 
157     private static void writeZipEntry(ZipOutputStream jar, File source, String entryName) throws IOException {
158         byte[] buffer = new byte[1024];
159 
160         int bytesRead;
161 
162         FileInputStream is = new FileInputStream(source);
163 
164         try {
165             ZipEntry entry = new ZipEntry(entryName);
166 
167             jar.putNextEntry(entry);
168 
169             while ((bytesRead = is.read(buffer)) != -1) {
170                 jar.write(buffer, 0, bytesRead);
171             }
172         } finally {
173             is.close();
174         }
175     }
176 
177 }