added common lib and added service uri's to properties file

This commit is contained in:
2021-10-27 13:15:29 +02:00
parent d08a6d0b67
commit 6752454838
61 changed files with 1231 additions and 931 deletions

View File

@@ -21,7 +21,7 @@ public class ApplyForTaskController {
public Task applyForTask(@RequestBody ExecutorInfo executorInfo) {
ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(),
executorInfo.getIp(), executorInfo.getPort());
executorInfo.getExecutorURI());
return applyForTaskUseCase.applyForTask(command);

View File

@@ -5,27 +5,17 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import ch.unisg.assignment.common.exception.ErrorResponse;
import ch.unisg.assignment.common.exception.InvalidIP4Exception;
import ch.unisg.assignment.common.exception.PortOutOfRangeException;
import ch.unisg.common.exception.ErrorResponse;
import ch.unisg.common.exception.InvalidExecutorURIException;
@ControllerAdvice
public class WebControllerExceptionHandler {
@ExceptionHandler(PortOutOfRangeException.class)
public ResponseEntity<ErrorResponse> handleException(PortOutOfRangeException e){
@ExceptionHandler(InvalidExecutorURIException.class)
public ResponseEntity<ErrorResponse> handleException(InvalidExecutorURIException e){
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
return new ResponseEntity<>(error, error.getHttpStatus());
}
@ExceptionHandler(InvalidIP4Exception.class)
public ResponseEntity<ErrorResponse> handleException(InvalidIP4Exception e){
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
return new ResponseEntity<>(error, error.getHttpStatus());
}
}

View File

@@ -9,7 +9,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@@ -21,9 +21,11 @@ import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
@Primary
public class GetAllExecutorInExecutorPoolByTypeAdapter implements GetAllExecutorInExecutorPoolByTypePort {
@Value("${executor-pool.url}")
private String server;
@Override
public boolean doesExecutorTypeExist(ExecutorType type) {
String server = "http://127.0.0.1:8083";
Logger logger = Logger.getLogger(PublishNewTaskEventAdapter.class.getName());
@@ -37,17 +39,18 @@ public class GetAllExecutorInExecutorPoolByTypeAdapter implements GetAllExecutor
try {
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == HttpStatus.OK.value()) {
JSONArray jsonArray = new JSONArray(response.body().toString());
JSONArray jsonArray = new JSONArray(response.body());
if (jsonArray.length() > 0) {
return true;
}
}
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return false;
}

View File

@@ -8,6 +8,7 @@ import java.net.http.HttpResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@@ -18,8 +19,11 @@ import ch.unisg.assignment.assignment.domain.event.NewTaskEvent;
@Primary
public class PublishNewTaskEventAdapter implements NewTaskEventPort {
String server = "http://127.0.0.1:8084";
String server2 = "http://127.0.0.1:8085";
@Value("${executor1.url}")
private String server;
@Value("${executor2.url}")
private String server2;
Logger logger = Logger.getLogger(PublishNewTaskEventAdapter.class.getName());
@@ -35,10 +39,11 @@ public class PublishNewTaskEventAdapter implements NewTaskEventPort {
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
HttpClient client2 = HttpClient.newHttpClient();
@@ -49,11 +54,12 @@ public class PublishNewTaskEventAdapter implements NewTaskEventPort {
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
client2.send(request2, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}

View File

@@ -9,6 +9,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@@ -19,7 +20,8 @@ import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent;
@Primary
public class PublishTaskAssignedEventAdapter implements TaskAssignedEventPort {
String server = "http://127.0.0.1:8081";
@Value("${task-list.url}")
private String server;
Logger logger = Logger.getLogger(PublishTaskAssignedEventAdapter.class.getName());
@@ -40,10 +42,11 @@ public class PublishTaskAssignedEventAdapter implements TaskAssignedEventPort {
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}

View File

@@ -9,6 +9,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@@ -19,7 +20,8 @@ import ch.unisg.assignment.assignment.domain.event.TaskCompletedEvent;
@Primary
public class PublishTaskCompletedEventAdapter implements TaskCompletedEventPort {
String server = "http://127.0.0.1:8081";
@Value("${task-list.url}")
private String server;
Logger logger = Logger.getLogger(PublishTaskCompletedEventAdapter.class.getName());
@@ -42,10 +44,11 @@ public class PublishTaskCompletedEventAdapter implements TaskCompletedEventPort
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}

View File

@@ -3,9 +3,8 @@ package ch.unisg.assignment.assignment.application.port.in;
import javax.validation.constraints.NotNull;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
import ch.unisg.assignment.common.SelfValidating;
import ch.unisg.common.validation.SelfValidating;
import ch.unisg.common.valueobject.ExecutorURI;
import lombok.EqualsAndHashCode;
import lombok.Value;
@@ -17,16 +16,11 @@ public class ApplyForTaskCommand extends SelfValidating<ApplyForTaskCommand>{
private final ExecutorType taskType;
@NotNull
private final IP4Adress executorIP;
private final ExecutorURI executorURI;
@NotNull
private final Port executorPort;
public ApplyForTaskCommand(ExecutorType taskType, IP4Adress executorIP, Port executorPort) {
public ApplyForTaskCommand(ExecutorType taskType, ExecutorURI executorURI) {
this.taskType = taskType;
this.executorIP = executorIP;
this.executorPort = executorPort;
this.executorURI = executorURI;
this.validateSelf();
}
}

View File

@@ -3,7 +3,7 @@ package ch.unisg.assignment.assignment.application.port.in;
import javax.validation.constraints.NotNull;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.common.SelfValidating;
import ch.unisg.common.validation.SelfValidating;
import lombok.EqualsAndHashCode;
import lombok.Value;

View File

@@ -2,7 +2,7 @@ package ch.unisg.assignment.assignment.application.port.in;
import javax.validation.constraints.NotNull;
import ch.unisg.assignment.common.SelfValidating;
import ch.unisg.common.validation.SelfValidating;
import lombok.EqualsAndHashCode;
import lombok.Value;

View File

@@ -22,7 +22,7 @@ public class ApplyForTaskService implements ApplyForTaskUseCase {
@Override
public Task applyForTask(ApplyForTaskCommand command) {
Task task = Roster.getInstance().assignTaskToExecutor(command.getTaskType(),
command.getExecutorIP(), command.getExecutorPort());
command.getExecutorURI());
if (task != null) {
taskAssignedEventPort.publishTaskAssignedEvent(new TaskAssignedEvent(task.getTaskID()));

View File

@@ -1,8 +1,5 @@
package ch.unisg.assignment.assignment.application.service;
import java.util.Arrays;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.stereotype.Component;
@@ -27,7 +24,6 @@ public class NewTaskService implements NewTaskUseCase {
@Override
public boolean addNewTaskToQueue(NewTaskCommand command) {
// TODO Get availableTaskTypes from executor pool
if (!getAllExecutorInExecutorPoolByTypePort.doesExecutorTypeExist(command.getTaskType())) {
return false;
}

View File

@@ -1,19 +1,14 @@
package ch.unisg.assignment.assignment.domain;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
import ch.unisg.common.valueobject.ExecutorURI;
import lombok.Getter;
import lombok.Setter;
public class ExecutorInfo {
@Getter
@Setter
private IP4Adress ip;
@Getter
@Setter
private Port port;
private ExecutorURI executorURI;
@Getter
@Setter

View File

@@ -5,8 +5,7 @@ import java.util.Arrays;
import java.util.HashMap;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
import ch.unisg.common.valueobject.ExecutorURI;
public class Roster {
@@ -30,7 +29,7 @@ public class Roster {
}
}
public Task assignTaskToExecutor(ExecutorType taskType, IP4Adress executorIP, Port executorPort) {
public Task assignTaskToExecutor(ExecutorType taskType, ExecutorURI executorURI) {
if (!queues.containsKey(taskType.getValue())) {
return null;
}
@@ -41,7 +40,7 @@ public class Roster {
Task task = queues.get(taskType.getValue()).remove(0);
rosterMap.put(task.getTaskID(), new RosterItem(task.getTaskID(),
task.getTaskType().getValue(), executorIP, executorPort));
task.getTaskType().getValue(), executorURI));
return task;
}

View File

@@ -1,7 +1,6 @@
package ch.unisg.assignment.assignment.domain;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
import ch.unisg.common.valueobject.ExecutorURI;
import lombok.Getter;
public class RosterItem {
@@ -13,17 +12,12 @@ public class RosterItem {
private String taskType;
@Getter
private IP4Adress executorIP;
private ExecutorURI executorURI;
@Getter
private Port executorPort;
public RosterItem(String taskID, String taskType, IP4Adress executorIP, Port executorPort) {
public RosterItem(String taskID, String taskType, ExecutorURI executorURI) {
this.taskID = taskID;
this.taskType = taskType;
this.executorIP = executorIP;
this.executorPort = executorPort;
this.executorURI = executorURI;
}
}

View File

@@ -1,23 +0,0 @@
package ch.unisg.assignment.assignment.domain.valueobject;
import ch.unisg.assignment.common.exception.InvalidIP4Exception;
import lombok.Value;
@Value
public class IP4Adress {
private String value;
public IP4Adress(String ip4) throws InvalidIP4Exception {
if (ip4.equalsIgnoreCase("localhost") ||
ip4.matches("^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$")) {
this.value = ip4;
} else {
throw new InvalidIP4Exception();
}
}
}

View File

@@ -1,17 +0,0 @@
package ch.unisg.assignment.assignment.domain.valueobject;
import ch.unisg.assignment.common.exception.PortOutOfRangeException;
import lombok.Value;
@Value
public class Port {
private int value;
public Port(int port) throws PortOutOfRangeException {
if (1024 <= port && port <= 65535) {
this.value = port;
} else {
throw new PortOutOfRangeException();
}
}
}

View File

@@ -1,31 +0,0 @@
package ch.unisg.assignment.common;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;
public abstract class SelfValidating<T> {
private Validator validator;
protected SelfValidating() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
/**
* Evaluates all Bean Validations on the attributes of this
* instance.
*/
protected void validateSelf() {
@SuppressWarnings("unchecked")
Set<ConstraintViolation<T>> violations = validator.validate((T) this);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
}
}

View File

@@ -1,13 +0,0 @@
package ch.unisg.assignment.common.exception;
import org.springframework.http.HttpStatus;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data
@RequiredArgsConstructor
public class ErrorResponse {
private final HttpStatus httpStatus;
private final String message;
}

View File

@@ -1,7 +0,0 @@
package ch.unisg.assignment.common.exception;
public class InvalidIP4Exception extends Exception {
public InvalidIP4Exception() {
super("IP4 is invalid");
}
}

View File

@@ -1,7 +0,0 @@
package ch.unisg.assignment.common.exception;
public class PortOutOfRangeException extends Exception {
public PortOutOfRangeException() {
super("Port is out of available range (1024-65535)");
}
}

View File

@@ -1 +1,5 @@
server.port=8082
executor-pool.url=http://127.0.0.1:8083
executor1.url=http://127.0.0.1:8084
executor2.url=http://127.0.0.1:8085
task-list.url=http://127.0.0.1:8081