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