JAMES-2272 Adding a TaskManager API
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fbce4b0b Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fbce4b0b Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fbce4b0b Branch: refs/heads/master Commit: fbce4b0bfbce1c8b1abc73d1c114744d3b0539ff Parents: 97cf430 Author: benwa <[email protected]> Authored: Wed Dec 27 11:43:25 2017 +0700 Committer: benwa <[email protected]> Committed: Thu Jan 4 15:03:35 2018 +0700 ---------------------------------------------------------------------- .../main/java/org/apache/james/task/Task.java | 2 +- .../apache/james/task/TaskExecutionDetails.java | 164 +++++++++++++++++++ .../main/java/org/apache/james/task/TaskId.java | 64 ++++++++ .../java/org/apache/james/task/TaskManager.java | 63 +++++++ .../james/task/TaskNotFoundException.java | 23 +++ .../java/org/apache/james/task/TaskIdTest.java | 34 ++++ 6 files changed, 349 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/fbce4b0b/server/task/src/main/java/org/apache/james/task/Task.java ---------------------------------------------------------------------- diff --git a/server/task/src/main/java/org/apache/james/task/Task.java b/server/task/src/main/java/org/apache/james/task/Task.java index fcfc8af..82b7670 100644 --- a/server/task/src/main/java/org/apache/james/task/Task.java +++ b/server/task/src/main/java/org/apache/james/task/Task.java @@ -81,7 +81,7 @@ public interface Task { return UNKNOWN; } - default Optional<Object> details() { + default Optional<TaskExecutionDetails.AdditionalInformation> details() { return Optional.empty(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/fbce4b0b/server/task/src/main/java/org/apache/james/task/TaskExecutionDetails.java ---------------------------------------------------------------------- diff --git a/server/task/src/main/java/org/apache/james/task/TaskExecutionDetails.java b/server/task/src/main/java/org/apache/james/task/TaskExecutionDetails.java new file mode 100644 index 0000000..0122fa7 --- /dev/null +++ b/server/task/src/main/java/org/apache/james/task/TaskExecutionDetails.java @@ -0,0 +1,164 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.task; + +import java.time.ZonedDateTime; +import java.util.Optional; + +import com.google.common.base.Preconditions; + +public class TaskExecutionDetails { + + public interface AdditionalInformation { + + } + + public static TaskExecutionDetails from(Task task, TaskId id) { + return new TaskExecutionDetails( + id, + task.type(), + TaskManager.Status.WAITING, + task.details(), + Optional.of(ZonedDateTime.now()), + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.empty()); + } + + private final TaskId taskId; + private final String type; + private final TaskManager.Status status; + private final Optional<AdditionalInformation> additionalInformation; + private final Optional<ZonedDateTime> submitDate; + private final Optional<ZonedDateTime> startedDate; + private final Optional<ZonedDateTime> completedDate; + private final Optional<ZonedDateTime> canceledDate; + private final Optional<ZonedDateTime> failedDate; + + public TaskExecutionDetails(TaskId taskId, String type, TaskManager.Status status, + Optional<AdditionalInformation> additionalInformation, + Optional<ZonedDateTime> submitDate, Optional<ZonedDateTime> startedDate, + Optional<ZonedDateTime> completedDate, Optional<ZonedDateTime> canceledDate, + Optional<ZonedDateTime> failedDate) { + this.taskId = taskId; + this.type = type; + this.status = status; + this.additionalInformation = additionalInformation; + this.submitDate = submitDate; + this.startedDate = startedDate; + this.completedDate = completedDate; + this.canceledDate = canceledDate; + this.failedDate = failedDate; + } + + public TaskId getTaskId() { + return taskId; + } + + public String getType() { + return type; + } + + public TaskManager.Status getStatus() { + return status; + } + + public Optional<AdditionalInformation> getAdditionalInformation() { + return additionalInformation; + } + + public Optional<ZonedDateTime> getSubmitDate() { + return submitDate; + } + + public Optional<ZonedDateTime> getStartedDate() { + return startedDate; + } + + public Optional<ZonedDateTime> getCompletedDate() { + return completedDate; + } + + public Optional<ZonedDateTime> getCanceledDate() { + return canceledDate; + } + + public Optional<ZonedDateTime> getFailedDate() { + return failedDate; + } + + public TaskExecutionDetails start() { + Preconditions.checkState(status == TaskManager.Status.WAITING); + return new TaskExecutionDetails( + taskId, + type, + TaskManager.Status.IN_PROGRESS, + additionalInformation, + submitDate, + Optional.of(ZonedDateTime.now()), + Optional.empty(), + Optional.empty(), + Optional.empty()); + } + + public TaskExecutionDetails completed() { + Preconditions.checkState(status == TaskManager.Status.IN_PROGRESS); + return new TaskExecutionDetails( + taskId, + type, + TaskManager.Status.COMPLETED, + additionalInformation, + submitDate, + startedDate, + Optional.of(ZonedDateTime.now()), + Optional.empty(), + Optional.empty()); + } + + public TaskExecutionDetails failed() { + Preconditions.checkState(status == TaskManager.Status.IN_PROGRESS); + return new TaskExecutionDetails( + taskId, + type, + TaskManager.Status.FAILED, + additionalInformation, + submitDate, + startedDate, + Optional.empty(), + Optional.empty(), + Optional.of(ZonedDateTime.now())); + } + + public TaskExecutionDetails cancel() { + Preconditions.checkState(status == TaskManager.Status.IN_PROGRESS + || status == TaskManager.Status.WAITING); + return new TaskExecutionDetails( + taskId, + type, + TaskManager.Status.CANCELLED, + additionalInformation, + submitDate, + startedDate, + Optional.empty(), + Optional.of(ZonedDateTime.now()), + Optional.empty()); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/fbce4b0b/server/task/src/main/java/org/apache/james/task/TaskId.java ---------------------------------------------------------------------- diff --git a/server/task/src/main/java/org/apache/james/task/TaskId.java b/server/task/src/main/java/org/apache/james/task/TaskId.java new file mode 100644 index 0000000..4b66feb --- /dev/null +++ b/server/task/src/main/java/org/apache/james/task/TaskId.java @@ -0,0 +1,64 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.task; + +import java.util.Objects; +import java.util.UUID; + +import com.google.common.base.MoreObjects; + +public class TaskId { + + public static TaskId generateTaskId() { + return new TaskId(UUID.randomUUID()); + } + + private final UUID value; + + public TaskId(UUID value) { + this.value = value; + } + + public UUID getValue() { + return value; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof TaskId) { + TaskId taskId = (TaskId) o; + + return Objects.equals(this.value, taskId.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("value", value) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/fbce4b0b/server/task/src/main/java/org/apache/james/task/TaskManager.java ---------------------------------------------------------------------- diff --git a/server/task/src/main/java/org/apache/james/task/TaskManager.java b/server/task/src/main/java/org/apache/james/task/TaskManager.java new file mode 100644 index 0000000..a3fabc3 --- /dev/null +++ b/server/task/src/main/java/org/apache/james/task/TaskManager.java @@ -0,0 +1,63 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.task; + +import java.util.Arrays; +import java.util.List; + +public interface TaskManager { + enum Status { + WAITING("waiting"), + IN_PROGRESS("inProgress"), + COMPLETED("completed"), + CANCELLED("canceled"), + FAILED("failed"); + + public static Status fromString(String value) { + return Arrays.stream(values()) + .filter(status -> status.value.equalsIgnoreCase(value)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException( + String.format("Unknown status value '%s'", value))); + } + + private final String value; + + Status(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + + TaskId submit(Task task); + + TaskExecutionDetails getExecutionDetails(TaskId id); + + List<TaskExecutionDetails> list(); + + List<TaskExecutionDetails> list(Status status); + + void cancel(TaskId id); + + TaskExecutionDetails await(TaskId id); +} http://git-wip-us.apache.org/repos/asf/james-project/blob/fbce4b0b/server/task/src/main/java/org/apache/james/task/TaskNotFoundException.java ---------------------------------------------------------------------- diff --git a/server/task/src/main/java/org/apache/james/task/TaskNotFoundException.java b/server/task/src/main/java/org/apache/james/task/TaskNotFoundException.java new file mode 100644 index 0000000..cec026a --- /dev/null +++ b/server/task/src/main/java/org/apache/james/task/TaskNotFoundException.java @@ -0,0 +1,23 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.task; + +public class TaskNotFoundException extends RuntimeException { +} http://git-wip-us.apache.org/repos/asf/james-project/blob/fbce4b0b/server/task/src/test/java/org/apache/james/task/TaskIdTest.java ---------------------------------------------------------------------- diff --git a/server/task/src/test/java/org/apache/james/task/TaskIdTest.java b/server/task/src/test/java/org/apache/james/task/TaskIdTest.java new file mode 100644 index 0000000..c1f378a --- /dev/null +++ b/server/task/src/test/java/org/apache/james/task/TaskIdTest.java @@ -0,0 +1,34 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.task; + +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class TaskIdTest { + + @Test + public void taskIdShouldMatchBeanContract() { + EqualsVerifier.forClass(TaskId.class) + .allFieldsShouldBeUsed() + .verify(); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
