Check all warnings in core and fix where applicable, also apply autoformatting on save.

This commit is contained in:
Hugo Trippaers 2013-09-21 16:15:36 +08:00
parent bb8b7340f3
commit 4945d465bf
21 changed files with 580 additions and 575 deletions

View File

@ -20,9 +20,6 @@ import java.util.List;
import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO;
import com.cloud.agent.api.to.DiskTO;
import com.cloud.agent.api.to.VolumeTO;
public class MigrateWithStorageAnswer extends Answer { public class MigrateWithStorageAnswer extends Answer {
List<VolumeObjectTO> volumeTos; List<VolumeObjectTO> volumeTos;

View File

@ -20,8 +20,6 @@ import java.util.List;
import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO;
import com.cloud.agent.api.to.VolumeTO;
public class MigrateWithStorageCompleteAnswer extends Answer { public class MigrateWithStorageCompleteAnswer extends Answer {
List<VolumeObjectTO> volumeTos; List<VolumeObjectTO> volumeTos;

View File

@ -16,7 +16,6 @@
// under the License. // under the License.
package com.cloud.agent.api; package com.cloud.agent.api;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachine;
public class RebootCommand extends Command { public class RebootCommand extends Command {

View File

@ -20,7 +20,6 @@ import java.util.List;
import com.cloud.agent.api.Answer; import com.cloud.agent.api.Answer;
import com.cloud.agent.api.to.LoadBalancerTO; import com.cloud.agent.api.to.LoadBalancerTO;
import com.cloud.agent.api.to.NicTO;
/** /**
* LoadBalancerConfigCommand sends the load balancer configuration * LoadBalancerConfigCommand sends the load balancer configuration

View File

@ -17,7 +17,6 @@
package com.cloud.agent.api.routing; package com.cloud.agent.api.routing;
import com.cloud.agent.api.to.LoadBalancerTO; import com.cloud.agent.api.to.LoadBalancerTO;
import com.cloud.agent.api.to.NicTO;
/** /**
* LoadBalancerConfigCommand sends the load balancer configuration * LoadBalancerConfigCommand sends the load balancer configuration

View File

@ -20,9 +20,7 @@ package com.cloud.agent.api.routing;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import com.cloud.agent.api.to.NetworkACLTO; import com.cloud.agent.api.to.NetworkACLTO;
import com.cloud.agent.api.to.NicTO; import com.cloud.agent.api.to.NicTO;

View File

@ -41,30 +41,30 @@ public class UploadCommand extends AbstractUploadCommand implements InternalIden
this.template = new TemplateTO(template); this.template = new TemplateTO(template);
this.url = url; this.url = url;
this.installPath = installPath; this.installPath = installPath;
this.checksum = template.getChecksum(); checksum = template.getChecksum();
this.id = template.getId(); id = template.getId();
this.templateSizeInBytes = sizeInBytes; templateSizeInBytes = sizeInBytes;
} }
public UploadCommand(String url, long id, long sizeInBytes, String installPath, Type type){ public UploadCommand(String url, long id, long sizeInBytes, String installPath, Type type){
this.template = null; template = null;
this.url = url; this.url = url;
this.installPath = installPath; this.installPath = installPath;
this.id = id; this.id = id;
this.type = type; this.type = type;
this.templateSizeInBytes = sizeInBytes; templateSizeInBytes = sizeInBytes;
} }
protected UploadCommand() { protected UploadCommand() {
} }
public UploadCommand(UploadCommand that) { public UploadCommand(UploadCommand that) {
this.template = that.template; template = that.template;
this.url = that.url; url = that.url;
this.installPath = that.installPath; installPath = that.installPath;
this.checksum = that.getChecksum(); checksum = that.getChecksum();
this.id = that.id; id = that.id;
} }
public String getDescription() { public String getDescription() {
@ -114,7 +114,8 @@ public class UploadCommand extends AbstractUploadCommand implements InternalIden
this.templateSizeInBytes = templateSizeInBytes; this.templateSizeInBytes = templateSizeInBytes;
} }
public long getId() { @Override
public long getId() {
return id; return id;
} }

View File

@ -108,7 +108,6 @@ public class VirtualRoutingResource implements Manager {
private String _firewallPath; private String _firewallPath;
private String _loadbPath; private String _loadbPath;
private String _dhcpEntryPath; private String _dhcpEntryPath;
private String _vmDataPath;
private String _publicEthIf; private String _publicEthIf;
private String _privateEthIf; private String _privateEthIf;
private String _bumpUpPriorityPath; private String _bumpUpPriorityPath;

View File

@ -31,14 +31,6 @@ import java.util.zip.GZIPOutputStream;
import org.apache.log4j.Level; import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.SecStorageFirewallCfgCommand.PortConfig;
import com.cloud.exception.UnsupportedVersionException;
import com.cloud.serializer.GsonHelper;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.CloudRuntimeException;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
@ -50,6 +42,15 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.SecStorageFirewallCfgCommand.PortConfig;
import com.cloud.exception.UnsupportedVersionException;
import com.cloud.serializer.GsonHelper;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.CloudRuntimeException;
/** /**
* Request is a simple wrapper around command and answer to add sequencing, * Request is a simple wrapper around command and answer to add sequencing,
* versioning, and flags. Note that the version here represents the changes * versioning, and flags. Note that the version here represents the changes
@ -107,7 +108,7 @@ public class Request {
protected long _agentId; protected long _agentId;
protected Command[] _cmds; protected Command[] _cmds;
protected String _content; protected String _content;
protected Request() { protected Request() {
} }
@ -158,14 +159,14 @@ public class Request {
} }
protected Request(final Request that, final Command[] cmds) { protected Request(final Request that, final Command[] cmds) {
this._ver = that._ver; _ver = that._ver;
this._seq = that._seq; _seq = that._seq;
setInSequence(that.executeInSequence()); setInSequence(that.executeInSequence());
setStopOnError(that.stopOnError()); setStopOnError(that.stopOnError());
this._cmds = cmds; _cmds = cmds;
this._mgmtId = that._mgmtId; _mgmtId = that._mgmtId;
this._via = that._via; _via = that._via;
this._agentId = that._agentId; _agentId = that._agentId;
setFromServer(!that.isFromServer()); setFromServer(!that.isFromServer());
} }
@ -287,7 +288,7 @@ public class Request {
retBuff.flip(); retBuff.flip();
return retBuff; return retBuff;
} }
public static ByteBuffer doCompress(ByteBuffer buffer, int length) { public static ByteBuffer doCompress(ByteBuffer buffer, int length) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(length); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(length);
byte[] array; byte[] array;
@ -307,11 +308,11 @@ public class Request {
} }
return ByteBuffer.wrap(byteOut.toByteArray()); return ByteBuffer.wrap(byteOut.toByteArray());
} }
public ByteBuffer[] toBytes() { public ByteBuffer[] toBytes() {
final ByteBuffer[] buffers = new ByteBuffer[2]; final ByteBuffer[] buffers = new ByteBuffer[2];
ByteBuffer tmp; ByteBuffer tmp;
if (_content == null) { if (_content == null) {
_content = s_gson.toJson(_cmds, _cmds.getClass()); _content = s_gson.toJson(_cmds, _cmds.getClass());
} }
@ -372,7 +373,7 @@ public class Request {
} }
} }
} }
@Override @Override
public String toString() { public String toString() {
return log("", true, Level.DEBUG); return log("", true, Level.DEBUG);
@ -447,7 +448,7 @@ public class Request {
if (version.ordinal() != Version.v1.ordinal() && version.ordinal() != Version.v3.ordinal()) { if (version.ordinal() != Version.v1.ordinal() && version.ordinal() != Version.v3.ordinal()) {
throw new UnsupportedVersionException("This version is no longer supported: " + version.toString(), UnsupportedVersionException.IncompatibleVersion); throw new UnsupportedVersionException("This version is no longer supported: " + version.toString(), UnsupportedVersionException.IncompatibleVersion);
} }
final byte reserved = buff.get(); // tossed away for now. buff.get();
final short flags = buff.getShort(); final short flags = buff.getShort();
final boolean isRequest = (flags & FLAG_REQUEST) > 0; final boolean isRequest = (flags & FLAG_REQUEST) > 0;
@ -456,7 +457,7 @@ public class Request {
final int size = buff.getInt(); final int size = buff.getInt();
final long mgmtId = buff.getLong(); final long mgmtId = buff.getLong();
final long agentId = buff.getLong(); final long agentId = buff.getLong();
long via; long via;
if (version.ordinal() == Version.v1.ordinal()) { if (version.ordinal() == Version.v1.ordinal()) {
via = buff.getLong(); via = buff.getLong();
@ -467,7 +468,7 @@ public class Request {
if ((flags & FLAG_COMPRESSED) != 0) { if ((flags & FLAG_COMPRESSED) != 0) {
buff = doDecompress(buff, size); buff = doDecompress(buff, size);
} }
byte[] command = null; byte[] command = null;
int offset = 0; int offset = 0;
if (buff.hasArray()) { if (buff.hasArray()) {
@ -519,7 +520,7 @@ public class Request {
public static long getViaAgentId(final byte[] bytes) { public static long getViaAgentId(final byte[] bytes) {
return NumbersUtil.bytesToLong(bytes, 32); return NumbersUtil.bytesToLong(bytes, 32);
} }
public static boolean fromServer(final byte[] bytes) { public static boolean fromServer(final byte[] bytes) {
return (bytes[3] & FLAG_FROM_SERVER) > 0; return (bytes[3] & FLAG_FROM_SERVER) > 0;
} }

View File

@ -18,15 +18,20 @@ package com.cloud.exception;
public class UsageServerException extends CloudException { public class UsageServerException extends CloudException {
public UsageServerException() { /**
*
} */
private static final long serialVersionUID = -8398313106067116466L;
public UsageServerException() {
}
public UsageServerException(String message) {
super(message);
}
public UsageServerException(String message) {
super(message);
}
} }

View File

@ -17,7 +17,6 @@
package com.cloud.storage; package com.cloud.storage;
import java.io.File; import java.io.File;
import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -41,7 +40,7 @@ public class JavaStorageLayer implements StorageLayer {
public JavaStorageLayer(boolean makeWorldWriteable) { public JavaStorageLayer(boolean makeWorldWriteable) {
this(); this();
this._makeWorldWriteable = makeWorldWriteable; _makeWorldWriteable = makeWorldWriteable;
} }
@Override @Override
@ -171,7 +170,7 @@ public class JavaStorageLayer implements StorageLayer {
File dir = new File(dirName); File dir = new File(dirName);
if (dir.exists()) { if (dir.exists()) {
String uniqDirName = dir.getAbsolutePath() + File.separator + UUID.randomUUID().toString(); String uniqDirName = dir.getAbsolutePath() + File.separator + UUID.randomUUID().toString();
if (this.mkdir(uniqDirName)) { if (mkdir(uniqDirName)) {
return new File(uniqDirName); return new File(uniqDirName);
} }
} }
@ -219,6 +218,7 @@ public class JavaStorageLayer implements StorageLayer {
return dirPaths; return dirPaths;
} }
@Override
public boolean setWorldReadableAndWriteable(File file) { public boolean setWorldReadableAndWriteable(File file) {
return (file.setReadable(true, false) && file.setWritable(true, false)); return (file.setReadable(true, false) && file.setWritable(true, false));
} }

View File

@ -21,8 +21,6 @@ import com.cloud.agent.api.storage.CopyVolumeAnswer;
import com.cloud.agent.api.storage.CopyVolumeCommand; import com.cloud.agent.api.storage.CopyVolumeCommand;
import com.cloud.agent.api.storage.CreateAnswer; import com.cloud.agent.api.storage.CreateAnswer;
import com.cloud.agent.api.storage.CreateCommand; import com.cloud.agent.api.storage.CreateCommand;
import com.cloud.agent.api.storage.CreateVolumeOVAAnswer;
import com.cloud.agent.api.storage.CreateVolumeOVACommand;
import com.cloud.agent.api.storage.DestroyCommand; import com.cloud.agent.api.storage.DestroyCommand;
import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer; import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;

View File

@ -30,203 +30,202 @@ import org.apache.log4j.Logger;
public class FtpTemplateUploader implements TemplateUploader { public class FtpTemplateUploader implements TemplateUploader {
public static final Logger s_logger = Logger.getLogger(FtpTemplateUploader.class.getName()); public static final Logger s_logger = Logger.getLogger(FtpTemplateUploader.class.getName());
public TemplateUploader.Status status = TemplateUploader.Status.NOT_STARTED; public TemplateUploader.Status status = TemplateUploader.Status.NOT_STARTED;
public String errorString = ""; public String errorString = "";
public long totalBytes = 0; public long totalBytes = 0;
public long entitySizeinBytes; public long entitySizeinBytes;
private String sourcePath; private String sourcePath;
private String ftpUrl; private String ftpUrl;
private UploadCompleteCallback completionCallback; private UploadCompleteCallback completionCallback;
private boolean resume;
private BufferedInputStream inputStream = null; private BufferedInputStream inputStream = null;
private BufferedOutputStream outputStream = null; private BufferedOutputStream outputStream = null;
private static final int CHUNK_SIZE = 1024*1024; //1M private static final int CHUNK_SIZE = 1024*1024; //1M
public FtpTemplateUploader(String sourcePath, String url, UploadCompleteCallback callback, long entitySizeinBytes){ public FtpTemplateUploader(String sourcePath, String url, UploadCompleteCallback callback, long entitySizeinBytes){
this.sourcePath = sourcePath; this.sourcePath = sourcePath;
this.ftpUrl = url; ftpUrl = url;
this.completionCallback = callback; completionCallback = callback;
this.entitySizeinBytes = entitySizeinBytes; this.entitySizeinBytes = entitySizeinBytes;
} }
public long upload(UploadCompleteCallback callback ) @Override
{ public long upload(UploadCompleteCallback callback )
{
switch (status) {
case ABORTED: switch (status) {
case UNRECOVERABLE_ERROR: case ABORTED:
case UPLOAD_FINISHED: case UNRECOVERABLE_ERROR:
return 0; case UPLOAD_FINISHED:
default: return 0;
default:
}
}
Date start = new Date();
new Date();
StringBuffer sb = new StringBuffer(ftpUrl);
// check for authentication else assume its anonymous access. StringBuffer sb = new StringBuffer(ftpUrl);
/* if (user != null && password != null) // check for authentication else assume its anonymous access.
/* if (user != null && password != null)
{ {
sb.append( user ); sb.append( user );
sb.append( ':' ); sb.append( ':' );
sb.append( password ); sb.append( password );
sb.append( '@' ); sb.append( '@' );
}*/ }*/
/* /*
* type ==> a=ASCII mode, i=image (binary) mode, d= file directory * type ==> a=ASCII mode, i=image (binary) mode, d= file directory
* listing * listing
*/ */
sb.append( ";type=i" ); sb.append( ";type=i" );
try try
{ {
URL url = new URL( sb.toString() ); URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection(); URLConnection urlc = url.openConnection();
File sourceFile = new File(sourcePath); File sourceFile = new File(sourcePath);
entitySizeinBytes = sourceFile.length(); entitySizeinBytes = sourceFile.length();
outputStream = new BufferedOutputStream( urlc.getOutputStream() ); outputStream = new BufferedOutputStream( urlc.getOutputStream() );
inputStream = new BufferedInputStream( new FileInputStream(sourceFile) ); inputStream = new BufferedInputStream( new FileInputStream(sourceFile) );
status = TemplateUploader.Status.IN_PROGRESS; status = TemplateUploader.Status.IN_PROGRESS;
int bytes = 0; int bytes = 0;
byte[] block = new byte[CHUNK_SIZE]; byte[] block = new byte[CHUNK_SIZE];
boolean done=false; boolean done=false;
while (!done && status != Status.ABORTED ) { while (!done && status != Status.ABORTED ) {
if ( (bytes = inputStream.read(block, 0, CHUNK_SIZE)) > -1) { if ( (bytes = inputStream.read(block, 0, CHUNK_SIZE)) > -1) {
outputStream.write(block,0, bytes); outputStream.write(block,0, bytes);
totalBytes += bytes; totalBytes += bytes;
} else { } else {
done = true; done = true;
} }
} }
status = TemplateUploader.Status.UPLOAD_FINISHED; status = TemplateUploader.Status.UPLOAD_FINISHED;
return totalBytes; return totalBytes;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
status = TemplateUploader.Status.UNRECOVERABLE_ERROR; status = TemplateUploader.Status.UNRECOVERABLE_ERROR;
errorString = e.getMessage(); errorString = e.getMessage();
s_logger.error(errorString); s_logger.error(errorString);
} catch (IOException e) { } catch (IOException e) {
status = TemplateUploader.Status.UNRECOVERABLE_ERROR; status = TemplateUploader.Status.UNRECOVERABLE_ERROR;
errorString = e.getMessage(); errorString = e.getMessage();
s_logger.error(errorString); s_logger.error(errorString);
} }
finally finally
{ {
try try
{ {
if (inputStream != null){ if (inputStream != null){
inputStream.close(); inputStream.close();
} }
if (outputStream != null){ if (outputStream != null){
outputStream.close(); outputStream.close();
} }
}catch (IOException ioe){ }catch (IOException ioe){
s_logger.error(" Caught exception while closing the resources" ); s_logger.error(" Caught exception while closing the resources" );
} }
if (callback != null) { if (callback != null) {
callback.uploadComplete(status); callback.uploadComplete(status);
} }
} }
return 0; return 0;
} }
@Override @Override
public void run() { public void run() {
try { try {
upload(completionCallback); upload(completionCallback);
} catch (Throwable t) { } catch (Throwable t) {
s_logger.warn("Caught exception during upload "+ t.getMessage(), t); s_logger.warn("Caught exception during upload "+ t.getMessage(), t);
errorString = "Failed to install: " + t.getMessage(); errorString = "Failed to install: " + t.getMessage();
status = TemplateUploader.Status.UNRECOVERABLE_ERROR; status = TemplateUploader.Status.UNRECOVERABLE_ERROR;
} }
}
@Override }
public Status getStatus() {
return status;
}
@Override @Override
public String getUploadError() { public Status getStatus() {
return errorString; return status;
} }
@Override @Override
public String getUploadLocalPath() { public String getUploadError() {
return sourcePath; return errorString;
} }
@Override @Override
public int getUploadPercent() { public String getUploadLocalPath() {
if (entitySizeinBytes == 0) { return sourcePath;
return 0; }
}
return (int)(100.0*totalBytes/entitySizeinBytes);
}
@Override @Override
public long getUploadTime() { public int getUploadPercent() {
// TODO if (entitySizeinBytes == 0) {
return 0; return 0;
} }
return (int)(100.0*totalBytes/entitySizeinBytes);
}
@Override @Override
public long getUploadedBytes() { public long getUploadTime() {
return totalBytes; // TODO
} return 0;
}
@Override @Override
public void setResume(boolean resume) { public long getUploadedBytes() {
this.resume = resume; return totalBytes;
}
}
@Override @Override
public void setStatus(Status status) { public void setResume(boolean resume) {
this.status = status;
}
@Override }
public void setUploadError(String string) {
errorString = string;
}
@Override @Override
public boolean stopUpload() { public void setStatus(Status status) {
switch (getStatus()) { this.status = status;
case IN_PROGRESS: }
try {
if(outputStream != null) {
outputStream.close();
}
if (inputStream != null){
inputStream.close();
}
} catch (IOException e) {
s_logger.error(" Caught exception while closing the resources" );
}
status = TemplateUploader.Status.ABORTED;
return true;
case UNKNOWN:
case NOT_STARTED:
case RECOVERABLE_ERROR:
case UNRECOVERABLE_ERROR:
case ABORTED:
status = TemplateUploader.Status.ABORTED;
case UPLOAD_FINISHED:
return true;
default: @Override
return true; public void setUploadError(String string) {
} errorString = string;
} }
@Override
public boolean stopUpload() {
switch (getStatus()) {
case IN_PROGRESS:
try {
if(outputStream != null) {
outputStream.close();
}
if (inputStream != null){
inputStream.close();
}
} catch (IOException e) {
s_logger.error(" Caught exception while closing the resources" );
}
status = TemplateUploader.Status.ABORTED;
return true;
case UNKNOWN:
case NOT_STARTED:
case RECOVERABLE_ERROR:
case UNRECOVERABLE_ERROR:
case ABORTED:
status = TemplateUploader.Status.ABORTED;
case UPLOAD_FINISHED:
return true;
default:
return true;
}
}
} }

View File

@ -22,14 +22,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Date; import java.util.Date;
import org.apache.cloudstack.storage.command.DownloadCommand.ResourceType;
import org.apache.commons.httpclient.ChunkedInputStream; import org.apache.commons.httpclient.ChunkedInputStream;
import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.Header;
@ -46,9 +41,10 @@ import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.cloudstack.storage.command.DownloadCommand.ResourceType;
import com.cloud.agent.api.storage.Proxy; import com.cloud.agent.api.storage.Proxy;
import com.cloud.storage.StorageLayer; import com.cloud.storage.StorageLayer;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
import com.cloud.utils.UriUtils; import com.cloud.utils.UriUtils;
@ -57,144 +53,145 @@ import com.cloud.utils.UriUtils;
* *
*/ */
public class HttpTemplateDownloader implements TemplateDownloader { public class HttpTemplateDownloader implements TemplateDownloader {
public static final Logger s_logger = Logger.getLogger(HttpTemplateDownloader.class.getName()); public static final Logger s_logger = Logger.getLogger(HttpTemplateDownloader.class.getName());
private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager(); private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager();
private static final int CHUNK_SIZE = 1024*1024; //1M private static final int CHUNK_SIZE = 1024*1024; //1M
private String downloadUrl; private String downloadUrl;
private String toFile; private String toFile;
public TemplateDownloader.Status status= TemplateDownloader.Status.NOT_STARTED; public TemplateDownloader.Status status= TemplateDownloader.Status.NOT_STARTED;
public String errorString = " "; public String errorString = " ";
private long remoteSize = 0; private long remoteSize = 0;
public long downloadTime = 0; public long downloadTime = 0;
public long totalBytes; public long totalBytes;
private final HttpClient client; private final HttpClient client;
private GetMethod request; private GetMethod request;
private boolean resume = false; private boolean resume = false;
private DownloadCompleteCallback completionCallback; private DownloadCompleteCallback completionCallback;
StorageLayer _storage; StorageLayer _storage;
boolean inited = true; boolean inited = true;
private String toDir; private String toDir;
private long MAX_TEMPLATE_SIZE_IN_BYTES; private long MAX_TEMPLATE_SIZE_IN_BYTES;
private ResourceType resourceType = ResourceType.TEMPLATE; private ResourceType resourceType = ResourceType.TEMPLATE;
private final HttpMethodRetryHandler myretryhandler; private final HttpMethodRetryHandler myretryhandler;
public HttpTemplateDownloader (StorageLayer storageLayer, String downloadUrl, String toDir, DownloadCompleteCallback callback, long maxTemplateSizeInBytes, String user, String password, Proxy proxy, ResourceType resourceType) { public HttpTemplateDownloader (StorageLayer storageLayer, String downloadUrl, String toDir, DownloadCompleteCallback callback, long maxTemplateSizeInBytes, String user, String password, Proxy proxy, ResourceType resourceType) {
this._storage = storageLayer; _storage = storageLayer;
this.downloadUrl = downloadUrl; this.downloadUrl = downloadUrl;
this.setToDir(toDir); setToDir(toDir);
this.status = TemplateDownloader.Status.NOT_STARTED; status = TemplateDownloader.Status.NOT_STARTED;
this.resourceType = resourceType; this.resourceType = resourceType;
this.MAX_TEMPLATE_SIZE_IN_BYTES = maxTemplateSizeInBytes; MAX_TEMPLATE_SIZE_IN_BYTES = maxTemplateSizeInBytes;
this.totalBytes = 0; totalBytes = 0;
this.client = new HttpClient(s_httpClientManager); client = new HttpClient(s_httpClientManager);
myretryhandler = new HttpMethodRetryHandler() { myretryhandler = new HttpMethodRetryHandler() {
public boolean retryMethod( @Override
final HttpMethod method, public boolean retryMethod(
final IOException exception, final HttpMethod method,
int executionCount) { final IOException exception,
if (executionCount >= 2) { int executionCount) {
// Do not retry if over max retry count if (executionCount >= 2) {
return false; // Do not retry if over max retry count
} return false;
if (exception instanceof NoHttpResponseException) { }
// Retry if the server dropped connection on us if (exception instanceof NoHttpResponseException) {
return true; // Retry if the server dropped connection on us
} return true;
if (!method.isRequestSent()) { }
// Retry if the request has not been sent fully or if (!method.isRequestSent()) {
// if it's OK to retry methods that have been sent // Retry if the request has not been sent fully or
return true; // if it's OK to retry methods that have been sent
} return true;
// otherwise do not retry }
return false; // otherwise do not retry
} return false;
}; }
};
try { try {
this.request = new GetMethod(downloadUrl); request = new GetMethod(downloadUrl);
this.request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler); request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
this.completionCallback = callback; completionCallback = callback;
//this.request.setFollowRedirects(false); //this.request.setFollowRedirects(false);
File f = File.createTempFile("dnld", "tmp_", new File(toDir)); File f = File.createTempFile("dnld", "tmp_", new File(toDir));
if (_storage != null) { if (_storage != null) {
_storage.setWorldReadableAndWriteable(f); _storage.setWorldReadableAndWriteable(f);
} }
toFile = f.getAbsolutePath(); toFile = f.getAbsolutePath();
Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl); Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl);
if (proxy != null) { if (proxy != null) {
client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort());
if (proxy.getUserName() != null) { if (proxy.getUserName() != null) {
Credentials proxyCreds = new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword()); Credentials proxyCreds = new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword());
client.getState().setProxyCredentials(AuthScope.ANY, proxyCreds); client.getState().setProxyCredentials(AuthScope.ANY, proxyCreds);
} }
} }
if ((user != null) && (password != null)) { if ((user != null) && (password != null)) {
client.getParams().setAuthenticationPreemptive(true); client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(user, password); Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
client.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds); client.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds);
s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second()); s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second());
} else { } else {
s_logger.info("No credentials configured for host=" + hostAndPort.first() + ":" + hostAndPort.second()); s_logger.info("No credentials configured for host=" + hostAndPort.first() + ":" + hostAndPort.second());
} }
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
errorString = iae.getMessage(); errorString = iae.getMessage();
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
inited = false; inited = false;
} catch (Exception ex){ } catch (Exception ex){
errorString = "Unable to start download -- check url? "; errorString = "Unable to start download -- check url? ";
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
s_logger.warn("Exception in constructor -- " + ex.toString()); s_logger.warn("Exception in constructor -- " + ex.toString());
} catch (Throwable th) { } catch (Throwable th) {
s_logger.warn("throwable caught ", th); s_logger.warn("throwable caught ", th);
} }
} }
@Override @Override
public long download(boolean resume, DownloadCompleteCallback callback) { public long download(boolean resume, DownloadCompleteCallback callback) {
switch (status) { switch (status) {
case ABORTED: case ABORTED:
case UNRECOVERABLE_ERROR: case UNRECOVERABLE_ERROR:
case DOWNLOAD_FINISHED: case DOWNLOAD_FINISHED:
return 0; return 0;
default: default:
} }
int bytes=0; int bytes=0;
File file = new File(toFile); File file = new File(toFile);
try { try {
long localFileSize = 0; long localFileSize = 0;
if (file.exists() && resume) { if (file.exists() && resume) {
localFileSize = file.length(); localFileSize = file.length();
s_logger.info("Resuming download to file (current size)=" + localFileSize); s_logger.info("Resuming download to file (current size)=" + localFileSize);
} }
Date start = new Date(); Date start = new Date();
int responseCode=0; int responseCode=0;
if (localFileSize > 0 ) { if (localFileSize > 0 ) {
// require partial content support for resume // require partial content support for resume
request.addRequestHeader("Range", "bytes=" + localFileSize + "-"); request.addRequestHeader("Range", "bytes=" + localFileSize + "-");
if (client.executeMethod(request) != HttpStatus.SC_PARTIAL_CONTENT) { if (client.executeMethod(request) != HttpStatus.SC_PARTIAL_CONTENT) {
errorString = "HTTP Server does not support partial get"; errorString = "HTTP Server does not support partial get";
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
return 0; return 0;
} }
} else if ((responseCode = client.executeMethod(request)) != HttpStatus.SC_OK) { } else if ((responseCode = client.executeMethod(request)) != HttpStatus.SC_OK) {
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
errorString = " HTTP Server returned " + responseCode + " (expected 200 OK) "; errorString = " HTTP Server returned " + responseCode + " (expected 200 OK) ";
return 0; //FIXME: retry? return 0; //FIXME: retry?
} }
@ -202,16 +199,16 @@ public class HttpTemplateDownloader implements TemplateDownloader {
boolean chunked = false; boolean chunked = false;
long remoteSize2 = 0; long remoteSize2 = 0;
if (contentLengthHeader == null) { if (contentLengthHeader == null) {
Header chunkedHeader = request.getResponseHeader("Transfer-Encoding"); Header chunkedHeader = request.getResponseHeader("Transfer-Encoding");
if (chunkedHeader == null || !"chunked".equalsIgnoreCase(chunkedHeader.getValue())) { if (chunkedHeader == null || !"chunked".equalsIgnoreCase(chunkedHeader.getValue())) {
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
errorString=" Failed to receive length of download "; errorString=" Failed to receive length of download ";
return 0; //FIXME: what status do we put here? Do we retry? return 0; //FIXME: what status do we put here? Do we retry?
} else if ("chunked".equalsIgnoreCase(chunkedHeader.getValue())){ } else if ("chunked".equalsIgnoreCase(chunkedHeader.getValue())){
chunked = true; chunked = true;
} }
} else { } else {
remoteSize2 = Long.parseLong(contentLengthHeader.getValue()); remoteSize2 = Long.parseLong(contentLengthHeader.getValue());
if ( remoteSize2 == 0 ) { if ( remoteSize2 == 0 ) {
status = TemplateDownloader.Status.DOWNLOAD_FINISHED; status = TemplateDownloader.Status.DOWNLOAD_FINISHED;
String downloaded = "(download complete remote=" + remoteSize + "bytes)"; String downloaded = "(download complete remote=" + remoteSize + "bytes)";
@ -222,22 +219,22 @@ public class HttpTemplateDownloader implements TemplateDownloader {
} }
if (remoteSize == 0) { if (remoteSize == 0) {
remoteSize = remoteSize2; remoteSize = remoteSize2;
} }
if (remoteSize > MAX_TEMPLATE_SIZE_IN_BYTES) { if (remoteSize > MAX_TEMPLATE_SIZE_IN_BYTES) {
s_logger.info("Remote size is too large: " + remoteSize + " , max=" + MAX_TEMPLATE_SIZE_IN_BYTES); s_logger.info("Remote size is too large: " + remoteSize + " , max=" + MAX_TEMPLATE_SIZE_IN_BYTES);
status = Status.UNRECOVERABLE_ERROR; status = Status.UNRECOVERABLE_ERROR;
errorString = "Download file size is too large"; errorString = "Download file size is too large";
return 0; return 0;
} }
if (remoteSize == 0) { if (remoteSize == 0) {
remoteSize = MAX_TEMPLATE_SIZE_IN_BYTES; remoteSize = MAX_TEMPLATE_SIZE_IN_BYTES;
} }
InputStream in = !chunked?new BufferedInputStream(request.getResponseBodyAsStream()) InputStream in = !chunked ? new BufferedInputStream(request.getResponseBodyAsStream()) : new ChunkedInputStream(
: new ChunkedInputStream(request.getResponseBodyAsStream()); request.getResponseBodyAsStream());
RandomAccessFile out = new RandomAccessFile(file, "rwd"); RandomAccessFile out = new RandomAccessFile(file, "rwd");
out.seek(localFileSize); out.seek(localFileSize);
@ -249,187 +246,193 @@ public class HttpTemplateDownloader implements TemplateDownloader {
boolean done=false; boolean done=false;
status = TemplateDownloader.Status.IN_PROGRESS; status = TemplateDownloader.Status.IN_PROGRESS;
while (!done && status != Status.ABORTED && offset <= remoteSize) { while (!done && status != Status.ABORTED && offset <= remoteSize) {
if ( (bytes = in.read(block, 0, CHUNK_SIZE)) > -1) { if ( (bytes = in.read(block, 0, CHUNK_SIZE)) > -1) {
out.write(block, 0, bytes); out.write(block, 0, bytes);
offset +=bytes; offset +=bytes;
out.seek(offset); out.seek(offset);
totalBytes += bytes; totalBytes += bytes;
} else { } else {
done = true; done = true;
} }
} }
Date finish = new Date(); Date finish = new Date();
String downloaded = "(incomplete download)"; String downloaded = "(incomplete download)";
if (totalBytes >= remoteSize) { if (totalBytes >= remoteSize) {
status = TemplateDownloader.Status.DOWNLOAD_FINISHED; status = TemplateDownloader.Status.DOWNLOAD_FINISHED;
downloaded = "(download complete remote=" + remoteSize + "bytes)"; downloaded = "(download complete remote=" + remoteSize + "bytes)";
} }
errorString = "Downloaded " + totalBytes + " bytes " + downloaded; errorString = "Downloaded " + totalBytes + " bytes " + downloaded;
downloadTime += finish.getTime() - start.getTime(); downloadTime += finish.getTime() - start.getTime();
in.close();
out.close(); out.close();
return totalBytes; return totalBytes;
}catch (HttpException hte) { }catch (HttpException hte) {
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
errorString = hte.getMessage(); errorString = hte.getMessage();
} catch (IOException ioe) { } catch (IOException ioe) {
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; //probably a file write error? status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; //probably a file write error?
errorString = ioe.getMessage(); errorString = ioe.getMessage();
} finally { } finally {
if (status == Status.UNRECOVERABLE_ERROR && file.exists() && !file.isDirectory()) { if (status == Status.UNRECOVERABLE_ERROR && file.exists() && !file.isDirectory()) {
file.delete(); file.delete();
}
request.releaseConnection();
if (callback != null) {
callback.downloadComplete(status);
} }
} request.releaseConnection();
return 0; if (callback != null) {
} callback.downloadComplete(status);
}
}
return 0;
}
public String getDownloadUrl() { public String getDownloadUrl() {
return downloadUrl; return downloadUrl;
} }
public String getToFile() { public String getToFile() {
File file = new File(toFile); File file = new File(toFile);
return file.getAbsolutePath(); return file.getAbsolutePath();
} }
public TemplateDownloader.Status getStatus() { @Override
return status; public TemplateDownloader.Status getStatus() {
} return status;
}
public long getDownloadTime() { @Override
return downloadTime; public long getDownloadTime() {
} return downloadTime;
}
public long getDownloadedBytes() { @Override
return totalBytes; public long getDownloadedBytes() {
} return totalBytes;
}
@Override @Override
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public boolean stopDownload() { public boolean stopDownload() {
switch (getStatus()) { switch (getStatus()) {
case IN_PROGRESS: case IN_PROGRESS:
if (request != null) { if (request != null) {
request.abort(); request.abort();
} }
status = TemplateDownloader.Status.ABORTED; status = TemplateDownloader.Status.ABORTED;
return true; return true;
case UNKNOWN: case UNKNOWN:
case NOT_STARTED: case NOT_STARTED:
case RECOVERABLE_ERROR: case RECOVERABLE_ERROR:
case UNRECOVERABLE_ERROR: case UNRECOVERABLE_ERROR:
case ABORTED: case ABORTED:
status = TemplateDownloader.Status.ABORTED; status = TemplateDownloader.Status.ABORTED;
case DOWNLOAD_FINISHED: case DOWNLOAD_FINISHED:
File f = new File(toFile); File f = new File(toFile);
if (f.exists()) { if (f.exists()) {
f.delete(); f.delete();
} }
return true; return true;
default: default:
return true; return true;
} }
} }
@Override @Override
public int getDownloadPercent() { public int getDownloadPercent() {
if (remoteSize == 0) { if (remoteSize == 0) {
return 0; return 0;
} }
return (int)(100.0*totalBytes/remoteSize); return (int)(100.0*totalBytes/remoteSize);
} }
@Override @Override
public void run() { public void run() {
try { try {
download(resume, completionCallback); download(resume, completionCallback);
} catch (Throwable t) { } catch (Throwable t) {
s_logger.warn("Caught exception during download "+ t.getMessage(), t); s_logger.warn("Caught exception during download "+ t.getMessage(), t);
errorString = "Failed to install: " + t.getMessage(); errorString = "Failed to install: " + t.getMessage();
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
} }
} }
@Override @Override
public void setStatus(TemplateDownloader.Status status) { public void setStatus(TemplateDownloader.Status status) {
this.status = status; this.status = status;
} }
public boolean isResume() { public boolean isResume() {
return resume; return resume;
} }
@Override @Override
public String getDownloadError() { public String getDownloadError() {
return errorString; return errorString;
} }
@Override @Override
public String getDownloadLocalPath() { public String getDownloadLocalPath() {
return getToFile(); return getToFile();
} }
public void setResume(boolean resume) { @Override
this.resume = resume; public void setResume(boolean resume) {
} this.resume = resume;
}
public void setToDir(String toDir) { public void setToDir(String toDir) {
this.toDir = toDir; this.toDir = toDir;
} }
public String getToDir() { public String getToDir() {
return toDir; return toDir;
} }
public long getMaxTemplateSizeInBytes() { @Override
return this.MAX_TEMPLATE_SIZE_IN_BYTES; public long getMaxTemplateSizeInBytes() {
} return MAX_TEMPLATE_SIZE_IN_BYTES;
}
public static void main(String[] args) { public static void main(String[] args) {
String url ="http:// dev.mysql.com/get/Downloads/MySQL-5.0/mysql-noinstall-5.0.77-win32.zip/from/http://mirror.services.wisc.edu/mysql/"; String url ="http:// dev.mysql.com/get/Downloads/MySQL-5.0/mysql-noinstall-5.0.77-win32.zip/from/http://mirror.services.wisc.edu/mysql/";
try { try {
URI uri = new java.net.URI(url); new java.net.URI(url);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
TemplateDownloader td = new HttpTemplateDownloader(null, url,"/tmp/mysql", null, TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES, null, null, null, null); TemplateDownloader td = new HttpTemplateDownloader(null, url,"/tmp/mysql", null, TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES, null, null, null, null);
long bytes = td.download(true, null); long bytes = td.download(true, null);
if (bytes > 0) { if (bytes > 0) {
System.out.println("Downloaded (" + bytes + " bytes)" + " in " + td.getDownloadTime()/1000 + " secs"); System.out.println("Downloaded (" + bytes + " bytes)" + " in " + td.getDownloadTime()/1000 + " secs");
} else { } else {
System.out.println("Failed download"); System.out.println("Failed download");
} }
} }
@Override @Override
public void setDownloadError(String error) { public void setDownloadError(String error) {
errorString = error; errorString = error;
} }
@Override @Override
public boolean isInited() { public boolean isInited() {
return inited; return inited;
} }
public ResourceType getResourceType() { public ResourceType getResourceType() {
return resourceType; return resourceType;
} }
} }

View File

@ -34,7 +34,7 @@ import com.cloud.storage.StorageLayer;
public class LocalTemplateDownloader extends TemplateDownloaderBase implements TemplateDownloader { public class LocalTemplateDownloader extends TemplateDownloaderBase implements TemplateDownloader {
public static final Logger s_logger = Logger.getLogger(LocalTemplateDownloader.class); public static final Logger s_logger = Logger.getLogger(LocalTemplateDownloader.class);
public LocalTemplateDownloader(StorageLayer storageLayer, String downloadUrl, String toDir, long maxTemplateSizeInBytes, DownloadCompleteCallback callback) { public LocalTemplateDownloader(StorageLayer storageLayer, String downloadUrl, String toDir, long maxTemplateSizeInBytes, DownloadCompleteCallback callback) {
super(storageLayer, downloadUrl, toDir, maxTemplateSizeInBytes, callback); super(storageLayer, downloadUrl, toDir, maxTemplateSizeInBytes, callback);
String filename = downloadUrl.substring(downloadUrl.lastIndexOf(File.separator)); String filename = downloadUrl.substring(downloadUrl.lastIndexOf(File.separator));
@ -44,14 +44,14 @@ public class LocalTemplateDownloader extends TemplateDownloaderBase implements T
@Override @Override
public long download(boolean resume, DownloadCompleteCallback callback) { public long download(boolean resume, DownloadCompleteCallback callback) {
if (_status == Status.ABORTED || if (_status == Status.ABORTED ||
_status == Status.UNRECOVERABLE_ERROR || _status == Status.UNRECOVERABLE_ERROR ||
_status == Status.DOWNLOAD_FINISHED) { _status == Status.DOWNLOAD_FINISHED) {
return 0; return 0;
} }
_start = System.currentTimeMillis(); _start = System.currentTimeMillis();
_resume = resume; _resume = resume;
File src; File src;
try { try {
src = new File(new URI(_downloadUrl)); src = new File(new URI(_downloadUrl));
@ -61,18 +61,20 @@ public class LocalTemplateDownloader extends TemplateDownloaderBase implements T
return 0; return 0;
} }
File dst = new File(_toFile); File dst = new File(_toFile);
FileChannel fic = null; FileChannel fic = null;
FileChannel foc = null; FileChannel foc = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try { try {
if (_storage != null) { if (_storage != null) {
dst.createNewFile(); dst.createNewFile();
_storage.setWorldReadableAndWriteable(dst); _storage.setWorldReadableAndWriteable(dst);
} }
ByteBuffer buffer = ByteBuffer.allocate(1024 * 512); ByteBuffer buffer = ByteBuffer.allocate(1024 * 512);
FileInputStream fis;
try { try {
fis = new FileInputStream(src); fis = new FileInputStream(src);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -81,7 +83,6 @@ public class LocalTemplateDownloader extends TemplateDownloaderBase implements T
return -1; return -1;
} }
fic = fis.getChannel(); fic = fis.getChannel();
FileOutputStream fos;
try { try {
fos = new FileOutputStream(dst); fos = new FileOutputStream(dst);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -89,11 +90,11 @@ public class LocalTemplateDownloader extends TemplateDownloaderBase implements T
return -1; return -1;
} }
foc = fos.getChannel(); foc = fos.getChannel();
_remoteSize = src.length(); _remoteSize = src.length();
this._totalBytes = 0; _totalBytes = 0;
_status = TemplateDownloader.Status.IN_PROGRESS; _status = TemplateDownloader.Status.IN_PROGRESS;
try { try {
while (_status != Status.ABORTED && fic.read(buffer) != -1) { while (_status != Status.ABORTED && fic.read(buffer) != -1) {
buffer.flip(); buffer.flip();
@ -104,13 +105,13 @@ public class LocalTemplateDownloader extends TemplateDownloaderBase implements T
} catch (IOException e) { } catch (IOException e) {
s_logger.warn("Unable to download", e); s_logger.warn("Unable to download", e);
} }
String downloaded = "(incomplete download)"; String downloaded = "(incomplete download)";
if (_totalBytes == _remoteSize) { if (_totalBytes == _remoteSize) {
_status = TemplateDownloader.Status.DOWNLOAD_FINISHED; _status = TemplateDownloader.Status.DOWNLOAD_FINISHED;
downloaded = "(download complete)"; downloaded = "(download complete)";
} }
_errorString = "Downloaded " + _remoteSize + " bytes " + downloaded; _errorString = "Downloaded " + _remoteSize + " bytes " + downloaded;
_downloadTime += System.currentTimeMillis() - _start; _downloadTime += System.currentTimeMillis() - _start;
return _totalBytes; return _totalBytes;
@ -125,14 +126,28 @@ public class LocalTemplateDownloader extends TemplateDownloaderBase implements T
} catch (IOException e) { } catch (IOException e) {
} }
} }
if (foc != null) { if (foc != null) {
try { try {
foc.close(); foc.close();
} catch (IOException e) { } catch (IOException e) {
} }
} }
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
if (_status == Status.UNRECOVERABLE_ERROR && dst.exists()) { if (_status == Status.UNRECOVERABLE_ERROR && dst.exists()) {
dst.delete(); dst.delete();
} }
@ -141,7 +156,7 @@ public class LocalTemplateDownloader extends TemplateDownloaderBase implements T
} }
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
String url ="file:///home/ahuang/Download/E3921_P5N7A-VM_manual.zip"; String url ="file:///home/ahuang/Download/E3921_P5N7A-VM_manual.zip";
TemplateDownloader td = new LocalTemplateDownloader(null, url,"/tmp/mysql", TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES, null); TemplateDownloader td = new LocalTemplateDownloader(null, url,"/tmp/mysql", TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES, null);

View File

@ -25,9 +25,8 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.exception.InternalErrorException; import com.cloud.exception.InternalErrorException;
import com.cloud.storage.StorageLayer;
import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.template.Processor.FormatInfo; import com.cloud.storage.StorageLayer;
import com.cloud.utils.component.AdapterBase; import com.cloud.utils.component.AdapterBase;
@Local(value=Processor.class) @Local(value=Processor.class)

View File

@ -24,7 +24,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Date; import java.util.Date;
import org.apache.cloudstack.storage.command.DownloadCommand.ResourceType;
import org.apache.commons.httpclient.ChunkedInputStream; import org.apache.commons.httpclient.ChunkedInputStream;
import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.Header;
@ -43,8 +42,6 @@ import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.ProgressEvent; import com.amazonaws.services.s3.model.ProgressEvent;
import com.amazonaws.services.s3.model.ProgressListener; import com.amazonaws.services.s3.model.ProgressListener;
@ -52,6 +49,9 @@ import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.StorageClass; import com.amazonaws.services.s3.model.StorageClass;
import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload; import com.amazonaws.services.s3.transfer.Upload;
import org.apache.cloudstack.storage.command.DownloadCommand.ResourceType;
import com.cloud.agent.api.storage.Proxy; import com.cloud.agent.api.storage.Proxy;
import com.cloud.agent.api.to.S3TO; import com.cloud.agent.api.to.S3TO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@ -89,15 +89,15 @@ public class S3TemplateDownloader implements TemplateDownloader {
public S3TemplateDownloader(S3TO storageLayer, String downloadUrl, String installPath, public S3TemplateDownloader(S3TO storageLayer, String downloadUrl, String installPath,
DownloadCompleteCallback callback, long maxTemplateSizeInBytes, String user, String password, Proxy proxy, DownloadCompleteCallback callback, long maxTemplateSizeInBytes, String user, String password, Proxy proxy,
ResourceType resourceType) { ResourceType resourceType) {
this.s3 = storageLayer; s3 = storageLayer;
this.downloadUrl = downloadUrl; this.downloadUrl = downloadUrl;
this.installPath = installPath; this.installPath = installPath;
this.status = TemplateDownloader.Status.NOT_STARTED; status = TemplateDownloader.Status.NOT_STARTED;
this.resourceType = resourceType; this.resourceType = resourceType;
this.maxTemplateSizeInByte = maxTemplateSizeInBytes; maxTemplateSizeInByte = maxTemplateSizeInBytes;
this.totalBytes = 0; totalBytes = 0;
this.client = new HttpClient(s_httpClientManager); client = new HttpClient(s_httpClientManager);
myretryhandler = new HttpMethodRetryHandler() { myretryhandler = new HttpMethodRetryHandler() {
@Override @Override
@ -121,12 +121,12 @@ public class S3TemplateDownloader implements TemplateDownloader {
}; };
try { try {
this.request = new GetMethod(downloadUrl); request = new GetMethod(downloadUrl);
this.request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler); request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
this.completionCallback = callback; completionCallback = callback;
Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl); Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl);
this.fileName = StringUtils.substringAfterLast(downloadUrl, "/"); fileName = StringUtils.substringAfterLast(downloadUrl, "/");
if (proxy != null) { if (proxy != null) {
client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort());
@ -388,7 +388,7 @@ public class S3TemplateDownloader implements TemplateDownloader {
@Override @Override
public String getDownloadLocalPath() { public String getDownloadLocalPath() {
return this.s3Key; return s3Key;
} }
@Override @Override
@ -398,7 +398,7 @@ public class S3TemplateDownloader implements TemplateDownloader {
@Override @Override
public long getMaxTemplateSizeInBytes() { public long getMaxTemplateSizeInBytes() {
return this.maxTemplateSizeInByte; return maxTemplateSizeInByte;
} }
@Override @Override

View File

@ -22,9 +22,10 @@ import java.net.URISyntaxException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.trilead.ssh2.SCPClient;
import com.cloud.storage.StorageLayer; import com.cloud.storage.StorageLayer;
import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.CloudRuntimeException;
import com.trilead.ssh2.SCPClient;
public class ScpTemplateDownloader extends TemplateDownloaderBase implements TemplateDownloader { public class ScpTemplateDownloader extends TemplateDownloaderBase implements TemplateDownloader {
private static final Logger s_logger = Logger.getLogger(ScpTemplateDownloader.class); private static final Logger s_logger = Logger.getLogger(ScpTemplateDownloader.class);
@ -83,7 +84,6 @@ public class ScpTemplateDownloader extends TemplateDownloaderBase implements Tem
if (port == -1) { if (port == -1) {
port = 22; port = 22;
} }
long length = 0;
File file = new File(_toFile); File file = new File(_toFile);
com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(uri.getHost(), port); com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(uri.getHost(), port);

View File

@ -16,9 +16,6 @@
// under the License. // under the License.
package com.cloud.storage.template; package com.cloud.storage.template;
import com.cloud.storage.template.TemplateUploader.UploadCompleteCallback;
import com.cloud.storage.template.TemplateUploader.Status;
public interface TemplateUploader extends Runnable{ public interface TemplateUploader extends Runnable{
/** /**

View File

@ -26,7 +26,6 @@ import com.cloud.agent.api.storage.Proxy;
import com.cloud.agent.api.to.DataStoreTO; import com.cloud.agent.api.to.DataStoreTO;
import com.cloud.agent.api.to.NfsTO; import com.cloud.agent.api.to.NfsTO;
import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Volume;
public class DownloadCommand extends AbstractDownloadCommand implements InternalIdentity { public class DownloadCommand extends AbstractDownloadCommand implements InternalIdentity {
@ -53,29 +52,29 @@ public class DownloadCommand extends AbstractDownloadCommand implements Internal
public DownloadCommand(DownloadCommand that) { public DownloadCommand(DownloadCommand that) {
super(that); super(that);
this.hvm = that.hvm; hvm = that.hvm;
this.checksum = that.checksum; checksum = that.checksum;
this.id = that.id; id = that.id;
this.description = that.description; description = that.description;
this.auth = that.getAuth(); auth = that.getAuth();
this.setSecUrl(that.getSecUrl()); setSecUrl(that.getSecUrl());
this.maxDownloadSizeInBytes = that.getMaxDownloadSizeInBytes(); maxDownloadSizeInBytes = that.getMaxDownloadSizeInBytes();
this.resourceType = that.resourceType; resourceType = that.resourceType;
this.installPath = that.installPath; installPath = that.installPath;
this._store = that._store; _store = that._store;
} }
public DownloadCommand(TemplateObjectTO template, Long maxDownloadSizeInBytes) { public DownloadCommand(TemplateObjectTO template, Long maxDownloadSizeInBytes) {
super(template.getName(), template.getOrigUrl(), template.getFormat(), template.getAccountId()); super(template.getName(), template.getOrigUrl(), template.getFormat(), template.getAccountId());
this._store = template.getDataStore(); _store = template.getDataStore();
this.installPath = template.getPath(); installPath = template.getPath();
this.hvm = template.isRequiresHvm(); hvm = template.isRequiresHvm();
this.checksum = template.getChecksum(); checksum = template.getChecksum();
this.id = template.getId(); id = template.getId();
this.description = template.getDescription(); description = template.getDescription();
if (_store instanceof NfsTO) { if (_store instanceof NfsTO) {
this.setSecUrl(((NfsTO) _store).getUrl()); setSecUrl(((NfsTO) _store).getUrl());
} }
this.maxDownloadSizeInBytes = maxDownloadSizeInBytes; this.maxDownloadSizeInBytes = maxDownloadSizeInBytes;
} }
@ -87,12 +86,12 @@ public class DownloadCommand extends AbstractDownloadCommand implements Internal
public DownloadCommand(VolumeObjectTO volume, Long maxDownloadSizeInBytes, String checkSum, String url, ImageFormat format) { public DownloadCommand(VolumeObjectTO volume, Long maxDownloadSizeInBytes, String checkSum, String url, ImageFormat format) {
super(volume.getName(), url, format, volume.getAccountId()); super(volume.getName(), url, format, volume.getAccountId());
this.checksum = checkSum; checksum = checkSum;
this.id = volume.getVolumeId(); id = volume.getVolumeId();
this.installPath = volume.getPath(); installPath = volume.getPath();
this._store = volume.getDataStore(); _store = volume.getDataStore();
this.maxDownloadSizeInBytes = maxDownloadSizeInBytes; this.maxDownloadSizeInBytes = maxDownloadSizeInBytes;
this.resourceType = ResourceType.VOLUME; resourceType = ResourceType.VOLUME;
} }
@Override @Override
public long getId() { public long getId() {
@ -184,6 +183,6 @@ public class DownloadCommand extends AbstractDownloadCommand implements Internal
} }
public DataStoreTO getCacheStore() { public DataStoreTO getCacheStore() {
return this.cacheStore; return cacheStore;
} }
} }

View File

@ -27,7 +27,6 @@ import java.util.Date;
import org.junit.Test; import org.junit.Test;
import com.cloud.agent.api.BackupSnapshotCommand; import com.cloud.agent.api.BackupSnapshotCommand;
import com.cloud.agent.api.to.StorageFilerTO;
import com.cloud.agent.api.to.SwiftTO; import com.cloud.agent.api.to.SwiftTO;
import com.cloud.storage.Storage.StoragePoolType; import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.StoragePool; import com.cloud.storage.StoragePool;