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 ...@@ -11,7 +11,7 @@ RUN mkdir -p $WEBPATH
COPY src/ $WEBPATH COPY src/ $WEBPATH
WORKDIR $WEBPATH WORKDIR $WEBPATH
RUN javac *.java RUN javac *.java task/*.java
EXPOSE 8000 EXPOSE 8000
......
...@@ -37,27 +37,12 @@ import task.TaskImpl; ...@@ -37,27 +37,12 @@ import task.TaskImpl;
public class TaskHello extends TaskImpl { public class TaskHello extends TaskImpl {
public TaskHello(String input) {
super(input);
}
@Override @Override
public void execute() { public void execute() {
result = "Hello " + input; 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: * When the project is compiled, up and running, the task can be called like:
`curl -d TaskHello -d World http://localhost:8000/` `curl -d TaskHello -d World http://localhost:8000/`
......
...@@ -12,34 +12,23 @@ public enum TaskExecutor { ...@@ -12,34 +12,23 @@ public enum TaskExecutor {
//Singleton //Singleton
INSTANCE; INSTANCE;
public static final String TASKS_PACKAGE = "task.";
public String run(String parameters) { public String run(String parameters) {
Task task; String taskName, input = "";
List<String> list = Arrays.stream(parameters.split("&")).collect(Collectors.toList()); List<String> list = Arrays.stream(parameters.split("&")).collect(Collectors.toList());
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
if (list.size() != 2) { if (list.size() != 2) {
task = new TaskIncorrectParams(""); taskName = "TaskIncorrectParams";
} else { } else {
String taskName = list.get(0); taskName = list.get(0);
String input = list.get(1); 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();
} }
Task task = createTask(taskName);
task.setInput(input);
task.execute();
TasksList.INSTANCE.add(task); TasksList.INSTANCE.add(task);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
...@@ -48,4 +37,18 @@ public enum TaskExecutor { ...@@ -48,4 +37,18 @@ public enum TaskExecutor {
return task.getResult(); 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 { ...@@ -4,6 +4,8 @@ public interface Task {
String getInput(); String getInput();
void setInput(String input);
void execute(); void execute();
void setExecutedTime(String executedTime); void setExecutedTime(String executedTime);
......
...@@ -10,10 +10,6 @@ import java.util.List; ...@@ -10,10 +10,6 @@ import java.util.List;
public class TaskBitcoin extends TaskImpl { public class TaskBitcoin extends TaskImpl {
public TaskBitcoin(String input) {
super(input);
}
@Override @Override
public void execute() { public void execute() {
try { try {
......
...@@ -5,10 +5,6 @@ import java.util.StringJoiner; ...@@ -5,10 +5,6 @@ import java.util.StringJoiner;
public class TaskBubbleSort extends TaskImpl { public class TaskBubbleSort extends TaskImpl {
public TaskBubbleSort(String input) {
super(input);
}
@Override @Override
public void execute() { public void execute() {
String[] stringArray = input.split(","); String[] stringArray = input.split(",");
......
...@@ -2,10 +2,6 @@ package task; ...@@ -2,10 +2,6 @@ package task;
public class TaskFiboRecursive extends TaskImpl { public class TaskFiboRecursive extends TaskImpl {
public TaskFiboRecursive(String input) {
super(input);
}
@Override @Override
public void execute() { public void execute() {
try { try {
......
...@@ -6,15 +6,16 @@ public abstract class TaskImpl implements Task { ...@@ -6,15 +6,16 @@ public abstract class TaskImpl implements Task {
protected String result; protected String result;
protected String executedTime; protected String executedTime;
public TaskImpl(String input) {
this.input = input;
}
@Override @Override
public String getInput() { public String getInput() {
return input; return input;
} }
@Override
public void setInput(String input) {
this.input = input;
}
@Override @Override
public String getResult() { public String getResult() {
return "Result: " + result + "; Executed in: " + executedTime + "\n"; return "Result: " + result + "; Executed in: " + executedTime + "\n";
......
...@@ -2,10 +2,6 @@ package task; ...@@ -2,10 +2,6 @@ package task;
public final class TaskIncorrectParams extends TaskImpl { public final class TaskIncorrectParams extends TaskImpl {
public TaskIncorrectParams(String input) {
super(input);
}
@Override @Override
public void execute() { } public void execute() { }
......
...@@ -2,10 +2,6 @@ package task; ...@@ -2,10 +2,6 @@ package task;
public final class TaskNotFound extends TaskImpl { public final class TaskNotFound extends TaskImpl {
public TaskNotFound(String input) {
super(input);
}
@Override @Override
public void execute() { } 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