001 package org.codehaus.mojo.wagon.shared;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
005 * agreements. See the NOTICE file distributed with this work for additional information regarding
006 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance with the License. You may obtain a
008 * copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed under the License
013 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014 * or implied. See the License for the specific language governing permissions and limitations under
015 * the License.
016 */
017
018 import java.io.File;
019 import java.io.FileInputStream;
020 import java.io.FileOutputStream;
021 import java.io.IOException;
022 import java.util.zip.ZipEntry;
023 import java.util.zip.ZipOutputStream;
024
025 import org.apache.maven.plugin.logging.Log;
026 import org.apache.maven.shared.model.fileset.FileSet;
027 import org.apache.maven.shared.model.fileset.util.FileSetManager;
028 import org.apache.maven.wagon.CommandExecutor;
029 import org.apache.maven.wagon.UnsupportedProtocolException;
030 import org.apache.maven.wagon.Wagon;
031 import org.apache.maven.wagon.WagonException;
032 import org.codehaus.plexus.util.IOUtil;
033 import org.codehaus.plexus.util.StringUtils;
034
035 /**
036 * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonUpload" role-hint="default"
037 */
038
039 public class DefaultWagonUpload implements WagonUpload {
040 public void upload(Wagon wagon, FileSet fileset, Log logger) throws WagonException {
041
042 FileSetManager fileSetManager = new FileSetManager(logger, logger.isDebugEnabled());
043
044 String[] files = fileSetManager.getIncludedFiles(fileset);
045
046 String url = wagon.getRepository().getUrl() + "/";
047
048 if (files.length == 0) {
049 logger.info("Nothing to upload.");
050 return;
051 }
052
053 for (int i = 0; i < files.length; ++i) {
054 String relativeDestPath = StringUtils.replace(files[i], "\\", "/");
055
056 if (!StringUtils.isBlank(fileset.getOutputDirectory())) {
057 relativeDestPath = fileset.getOutputDirectory() + "/" + relativeDestPath;
058 }
059
060 File source = new File(fileset.getDirectory(), files[i]);
061
062 logger.info("Uploading " + source + " to " + url + relativeDestPath + " ...");
063
064 wagon.put(source, relativeDestPath);
065 }
066
067 }
068
069 @Override
070 public void upload(Wagon wagon, FileSet fileset, boolean optimize, Log logger) throws WagonException, IOException {
071 if (!optimize) {
072 upload(wagon, fileset, logger);
073 return;
074 }
075
076 if (!(wagon instanceof CommandExecutor)) {
077 throw new UnsupportedProtocolException("Wagon " + wagon.getRepository().getProtocol()
078 + " does not support optimize upload");
079 }
080
081 logger.info("Uploading " + fileset);
082
083 File zipFile;
084 zipFile = File.createTempFile("wagon", ".zip");
085
086 try {
087 FileSetManager fileSetManager = new FileSetManager(logger, logger.isDebugEnabled());
088 String[] files = fileSetManager.getIncludedFiles(fileset);
089
090 if (files.length == 0) {
091 logger.info("Nothing to upload.");
092 return;
093 }
094
095 logger.info("Creating " + zipFile + " ...");
096 createZip(files, zipFile, fileset.getDirectory());
097
098 String remoteFile = zipFile.getName();
099 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 }