Added local file upload for FTP behaviour. [artf472311]
Fixed config dialog parameter order and new line for longer labels
This commit is contained in:
parent
d9c924009d
commit
e18c09b39d
4 changed files with 77 additions and 37 deletions
|
|
@ -16,13 +16,13 @@ public class UeBehaviourFtpDownload extends UeBehaviourFtp {
|
|||
private static final Logger log = Logger.getLogger(UeBehaviourFtpDownload.class);
|
||||
private static final int BUFFER_SIZE = 512;
|
||||
|
||||
@Configurable("Host")
|
||||
@Configurable(order=1, value="Host")
|
||||
private String host = "ftp.otenet.gr";
|
||||
@Configurable("File Path")
|
||||
@Configurable(order=2, value="File Path")
|
||||
private String filePath = "/test1Mb.db";
|
||||
@Configurable("Username")
|
||||
@Configurable(order=3, value="Username")
|
||||
private String username = "speedtest";
|
||||
@Configurable("Password")
|
||||
@Configurable(order=4, value="Password")
|
||||
private String password = "speedtest";
|
||||
|
||||
private transient long estimatedDataLength = -1;
|
||||
|
|
|
|||
|
|
@ -1,20 +1,13 @@
|
|||
package com.ericsson.uecontrol.core.behaviour;
|
||||
|
||||
import com.ericsson.uecontrol.core.UeBehaviour;
|
||||
import com.ericsson.uecontrol.core.util.ThroughputCalculator;
|
||||
import com.ericsson.uecontrol.core.util.UrlUtil;
|
||||
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
|
||||
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPClientConfig;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.io.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* This behaviour simulates downloading a file from FTP
|
||||
|
|
@ -22,37 +15,35 @@ import java.net.URLConnection;
|
|||
* Created by ezivkoc on 2014-07-15.
|
||||
*/
|
||||
public class UeBehaviourFtpUpload extends UeBehaviourFtp {
|
||||
private static final int BUFFER_SIZE = 512;
|
||||
private static final Logger log = Logger.getLogger(UeBehaviourFtpUpload.class);
|
||||
private static final int BUFFER_SIZE = 512;
|
||||
|
||||
@Configurable("Host")
|
||||
|
||||
@Configurable(order=1, value="Host")
|
||||
private String host;
|
||||
@Configurable("File Path")
|
||||
private String filePath;
|
||||
@Configurable("Username")
|
||||
@Configurable(order=2, value="Remote Path")
|
||||
private String remotePath;
|
||||
@Configurable(order=3, value="Username")
|
||||
private String username;
|
||||
@Configurable("Password")
|
||||
@Configurable(order=4, value="Password")
|
||||
private String password;
|
||||
@Configurable("File Size(Bytes)")
|
||||
private int size;
|
||||
@Configurable(order=5, value="Random data Size(Bytes) or Local File Path")
|
||||
private String sizeOrFile;
|
||||
|
||||
|
||||
@Override
|
||||
protected void executeFtp(FTPClient ftp) throws IOException {
|
||||
log.debug("Uploading to FTP file: " + getFtpUrl(filePath));
|
||||
OutputStream outStream = ftp.storeFileStream(filePath);
|
||||
log.debug("Uploading to FTP file: " + getFtpUrl(remotePath));
|
||||
OutputStream outStream = ftp.storeFileStream(remotePath);
|
||||
if (outStream != null) {
|
||||
OutputStream out = new BufferedOutputStream(outStream);
|
||||
|
||||
// Upload data
|
||||
int total = 0;
|
||||
byte[] data = new byte[BUFFER_SIZE];
|
||||
while (total < size && !stopExecution()) {
|
||||
int writeLength = (total + data.length < size ? data.length : size - total);
|
||||
out.write(data, 0, writeLength);
|
||||
|
||||
total += writeLength;
|
||||
super.setProgress((float) total / size);
|
||||
if(sizeOrFile != null && Pattern.matches("\\d*", sizeOrFile)){
|
||||
int size = Integer.parseInt(sizeOrFile);
|
||||
uploadRandomData(out, size);
|
||||
}
|
||||
else{
|
||||
uploadLocalFile(out, new File(sizeOrFile));
|
||||
}
|
||||
|
||||
out.close();
|
||||
|
|
@ -64,7 +55,38 @@ public class UeBehaviourFtpUpload extends UeBehaviourFtp {
|
|||
}
|
||||
}
|
||||
|
||||
private void uploadRandomData(OutputStream out, int size) throws IOException {
|
||||
// Upload data
|
||||
log.debug("Uploading random data with size "+size);
|
||||
int total = 0;
|
||||
byte[] data = new byte[BUFFER_SIZE];
|
||||
while (total < size && !stopExecution()) {
|
||||
int writeLength = (total + data.length < size ? data.length : size - total);
|
||||
out.write(data, 0, writeLength);
|
||||
|
||||
total += writeLength;
|
||||
super.setProgress((float) total / size);
|
||||
}
|
||||
}
|
||||
|
||||
private void uploadLocalFile(OutputStream out, File file) throws IOException {
|
||||
log.debug("Uploading local file "+file.getAbsolutePath());
|
||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
|
||||
try {
|
||||
// Upload file
|
||||
int writeLength = 0;
|
||||
int total = 0;
|
||||
byte[] data = new byte[BUFFER_SIZE];
|
||||
while ((writeLength = in.read(data)) > 0 && !stopExecution()) {
|
||||
out.write(data, 0, writeLength);
|
||||
|
||||
total += writeLength;
|
||||
super.setProgress((float) total / file.length());
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
|
|
@ -87,7 +109,11 @@ public class UeBehaviourFtpUpload extends UeBehaviourFtp {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Will upload a "+ ThroughputCalculator.getByteString(size) +" file.";
|
||||
if(sizeOrFile != null && Pattern.matches("\\d*", sizeOrFile)){
|
||||
int size = Integer.parseInt(sizeOrFile);
|
||||
return "Will upload "+ ThroughputCalculator.getByteString(size) +" of random data.";
|
||||
}
|
||||
return "Will upload local file "+ sizeOrFile;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,8 @@ public class ConfigureDialog extends DialogFragment {
|
|||
confer = new Configurator(behaviour);
|
||||
for(ConfigurationParam confParam : confer.getConfiguration()){
|
||||
LinearLayout layout = new LinearLayout(context);
|
||||
layout.setOrientation(LinearLayout.HORIZONTAL);
|
||||
layout.setOrientation(
|
||||
(confParam.getNiceName().length() < 25 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL));
|
||||
layout.setLayoutParams(new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.MATCH_PARENT));
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import java.lang.annotation.Target;
|
|||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by ezivkoc on 2014-07-24.
|
||||
|
|
@ -22,6 +23,7 @@ public class Configurator {
|
|||
@Target({ElementType.FIELD}) // This annotation can only be applied to class fields.
|
||||
public static @interface Configurable{
|
||||
String value();
|
||||
int order() default Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
public static enum ConfigType{
|
||||
|
|
@ -47,7 +49,8 @@ public class Configurator {
|
|||
|
||||
Field[] all = c.getDeclaredFields();
|
||||
for(Field f : all){
|
||||
if(f.isAnnotationPresent(Configurable.class) && !Modifier.isStatic(f.getModifiers())) {
|
||||
if(f.isAnnotationPresent(Configurable.class) &&
|
||||
!Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) {
|
||||
try {
|
||||
conf.add(new ConfigurationParam(f));
|
||||
} catch (IllegalAccessException e) {
|
||||
|
|
@ -56,7 +59,9 @@ public class Configurator {
|
|||
}
|
||||
}
|
||||
|
||||
return conf.toArray(new ConfigurationParam[conf.size()]);
|
||||
ConfigurationParam[] list = conf.toArray(new ConfigurationParam[conf.size()]);
|
||||
Arrays.sort(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setConfiguration(){
|
||||
|
|
@ -70,12 +75,13 @@ public class Configurator {
|
|||
}
|
||||
|
||||
|
||||
public class ConfigurationParam{
|
||||
public class ConfigurationParam implements Comparable<ConfigurationParam>{
|
||||
protected Field field;
|
||||
protected String name;
|
||||
protected String niceName;
|
||||
protected ConfigType type;
|
||||
protected Object value;
|
||||
protected int order;
|
||||
|
||||
|
||||
protected ConfigurationParam(Field f) throws IllegalAccessException {
|
||||
|
|
@ -84,6 +90,7 @@ public class Configurator {
|
|||
name = field.getName();
|
||||
niceName = field.getAnnotation(Configurable.class).value();
|
||||
value = field.get(obj);
|
||||
order = field.getAnnotation(Configurable.class).order();
|
||||
|
||||
if (f.getType() == String.class) type = ConfigType.STRING;
|
||||
else if(f.getType() == int.class) type = ConfigType.INT;
|
||||
|
|
@ -111,5 +118,11 @@ public class Configurator {
|
|||
protected void set() throws IllegalAccessException {
|
||||
field.set(obj, value);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(ConfigurationParam configurationParam) {
|
||||
return this.order - configurationParam.order;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue