1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.wagon.shared;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
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 }