Commit b37132b6 authored by Wagner Franchin's avatar Wagner Franchin

Changed the way how tasks are created

parent 107fa80d
......@@ -11,7 +11,7 @@ RUN mkdir -p $WEBPATH
COPY src/ $WEBPATH
WORKDIR $WEBPATH
RUN javac *.java
RUN javac *.java task/*.java
EXPOSE 8000
......
......@@ -37,27 +37,12 @@ import task.TaskImpl;
public class TaskHello extends TaskImpl {
public TaskHello(String input) {
super(input);
}
@Override
public void execute() {
result = "Hello " + input;
}
}
```
* Change `singleton/TaskExecutor.java` by adding a new "case" in the switch-case context for your task.
Example:
```
...
case "TaskHello":
task = new TaskHello(input);
break;
default:
...
```
* When the project is compiled, up and running, the task can be called like:
`curl -d TaskHello -d World http://localhost:8000/`
......
......@@ -12,34 +12,23 @@ public enum TaskExecutor {
//Singleton
INSTANCE;
public static final String TASKS_PACKAGE = "task.";
public String run(String parameters) {
Task task;
String taskName, input = "";
List<String> list = Arrays.stream(parameters.split("&")).collect(Collectors.toList());
long start = System.currentTimeMillis();
if (list.size() != 2) {
task = new TaskIncorrectParams("");
taskName = "TaskIncorrectParams";
} else {
String taskName = list.get(0);
String input = list.get(1);
switch (taskName) {
case "TaskBubbleSort":
task = new TaskBubbleSort(input);
break;
case "TaskFiboRecursive":
task = new TaskFiboRecursive(input);
break;
case "TaskBitcoin":
task = new TaskBitcoin(input);
break;
default:
task = new TaskNotFound("");
break;
}
task.execute();
taskName = list.get(0);
input = list.get(1);
}
Task task = createTask(taskName);
task.setInput(input);
task.execute();
TasksList.INSTANCE.add(task);
long end = System.currentTimeMillis();
......@@ -48,4 +37,18 @@ public enum TaskExecutor {
return task.getResult();
}
private Task createTask(String className) {
Task task = null;
try {
task = (Task)Class.forName(TASKS_PACKAGE + className).newInstance();
} catch (Exception e) {
try {
task = (Task)Class.forName(TASKS_PACKAGE + "TaskNotFound").newInstance();
} catch (Exception e1) {
e1.printStackTrace();
}
}
return task;
}
}
......@@ -4,6 +4,8 @@ public interface Task {
String getInput();
void setInput(String input);
void execute();
void setExecutedTime(String executedTime);
......
......@@ -10,10 +10,6 @@ import java.util.List;
public class TaskBitcoin extends TaskImpl {
public TaskBitcoin(String input) {
super(input);
}
@Override
public void execute() {
try {
......
......@@ -5,10 +5,6 @@ import java.util.StringJoiner;
public class TaskBubbleSort extends TaskImpl {
public TaskBubbleSort(String input) {
super(input);
}
@Override
public void execute() {
String[] stringArray = input.split(",");
......
......@@ -2,10 +2,6 @@ package task;
public class TaskFiboRecursive extends TaskImpl {
public TaskFiboRecursive(String input) {
super(input);
}
@Override
public void execute() {
try {
......
......@@ -6,15 +6,16 @@ public abstract class TaskImpl implements Task {
protected String result;
protected String executedTime;
public TaskImpl(String input) {
this.input = input;
}
@Override
public String getInput() {
return input;
}
@Override
public void setInput(String input) {
this.input = input;
}
@Override
public String getResult() {
return "Result: " + result + "; Executed in: " + executedTime + "\n";
......
......@@ -2,10 +2,6 @@ package task;
public final class TaskIncorrectParams extends TaskImpl {
public TaskIncorrectParams(String input) {
super(input);
}
@Override
public void execute() { }
......
......@@ -2,10 +2,6 @@ package task;
public final class TaskNotFound extends TaskImpl {
public TaskNotFound(String input) {
super(input);
}
@Override
public void execute() { }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment