Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ public interface WorkflowResource {
@Consumes(MediaType.APPLICATION_JSON)
void bind(@PathParam("type") String type, @PathParam("resourceId") String resourceId, Long milliseconds);

@Path("steps")
WorkflowStepsResource steps();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,6 @@ public List<ScheduledStep> getScheduledStepsByResource(String resourceId) {
.toList();
}

public List<ScheduledStep> getScheduledStepsByStep(String stepId) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<WorkflowStateEntity> query = cb.createQuery(WorkflowStateEntity.class);
Root<WorkflowStateEntity> stateRoot = query.from(WorkflowStateEntity.class);

Predicate byStep = cb.equal(stateRoot.get("scheduledStepId"), stepId);
query.where(byStep);

return em.createQuery(query).getResultStream()
.map(this::toScheduledStep)
.toList();
}

@Override
public void removeByResource(String resourceId) {
CriteriaBuilder cb = em.getCriteriaBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ org.keycloak.storage.clientscope.ClientScopeStorageProviderSpi
org.keycloak.models.session.UserSessionPersisterSpi
org.keycloak.models.session.RevokedTokenPersisterSpi
org.keycloak.cluster.ClusterSpi
org.keycloak.models.workflow.WorkflowStateSpi
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.keycloak.models.workflow;

import java.util.UUID;

import org.keycloak.models.workflow.WorkflowStateProvider.ScheduledStep;

final class DefaultWorkflowExecutionContext implements WorkflowExecutionContext {

private final String resourceId;
private final String executionId;
private final Workflow workflow;
private final WorkflowStep currentStep;

public DefaultWorkflowExecutionContext(Workflow workflow, WorkflowEvent event) {
this(workflow, null, UUID.randomUUID().toString(), event.getResourceId());
}

public DefaultWorkflowExecutionContext(Workflow workflow, WorkflowEvent event, ScheduledStep step) {
this(workflow, null, step.executionId(), event.getResourceId());
}

public DefaultWorkflowExecutionContext(WorkflowsManager manager, Workflow workflow, ScheduledStep step) {
this(workflow, manager.getStepById(workflow, step.stepId()), step.executionId(), step.resourceId());
}

public DefaultWorkflowExecutionContext(Workflow workflow, WorkflowStep currentStep, String executionId, String resourceId) {
this.workflow = workflow;
this.currentStep = currentStep;
this.executionId = executionId;
this.resourceId = resourceId;
}

@Override
public String getResourceId() {
return resourceId;
}

String getExecutionId() {
return this.executionId;
}

Workflow getWorkflow() {
return workflow;
}

WorkflowStep getCurrentStep() {
return currentStep;
}
}
Original file line number Diff line number Diff line change
@@ -1,91 +1,14 @@
package org.keycloak.models.workflow;

import org.jboss.logging.Logger;

import java.util.List;
import java.util.UUID;

import static java.util.Optional.ofNullable;

public class WorkflowExecutionContext {

private static final Logger logger = Logger.getLogger(WorkflowExecutionContext.class);

private String executionId;
private String resourceId;
private Workflow workflow;
private List<WorkflowStep> steps;

// variable that keep track of execution steps
private int lastExecutedStepIndex = -1;

public WorkflowExecutionContext(Workflow workflow, List<WorkflowStep> steps, String resourceId) {
this.workflow = workflow;
this.steps = ofNullable(steps).orElse(List.of());
this.resourceId = resourceId;
}

public WorkflowExecutionContext(Workflow workflow, List<WorkflowStep> steps, String resourceId, String stepId, String executionId) {
this(workflow, steps, resourceId);
this.executionId = executionId;
if (stepId != null) {
for (int i = 0; i < steps.size(); i++) {
if (steps.get(i).getId().equals(stepId)) {
this.lastExecutedStepIndex = i - 1;
break;
}
}
}
}

public void init() {
if (this.executionId == null) {
this.executionId = UUID.randomUUID().toString();
logger.debugf("Started workflow '%s' for resource %s (execution id: %s)", this.workflow.getName(), this.resourceId, this.executionId);
}
}

public void success(WorkflowStep step) {
logger.debugf("Step %s completed successfully (execution id: %s)", step.getProviderId(), executionId);
}

public void fail(WorkflowStep step, String errorMessage) {
StringBuilder sb = new StringBuilder();
sb.append("Step %s failed (execution id: %s)");
if (errorMessage != null) {
sb.append(" - error message: %s");
logger.debugf(sb.toString(), step.getProviderId(), executionId, errorMessage);
}
else {
logger.debugf(sb.toString(), step.getProviderId(), executionId);
}
}

public void complete() {
logger.debugf("Workflow '%s' completed for resource %s (execution id: %s)", workflow.getName(), resourceId, executionId);
}

public void cancel() {
logger.debugf("Workflow '%s' cancelled for resource %s (execution id: %s)", workflow.getName(), resourceId, executionId);
}

public boolean hasNextStep() {
return lastExecutedStepIndex + 1 < steps.size();
}

public WorkflowStep getNextStep() {
if (lastExecutedStepIndex + 1 < steps.size()) {
return steps.get(++lastExecutedStepIndex);
}
return null;
}

public void restart() {
logger.debugf("Restarted workflow '%s' for resource %s (execution id: %s)",workflow.getName(), resourceId, executionId);
this.lastExecutedStepIndex = -1;
}

public String getExecutionId() {
return this.executionId;
}
/**
* A contextual object providing information about the workflow execution.
*/
public interface WorkflowExecutionContext {

/**
* Returns the id of the resource bound to the current workflow execution.
*
* @return the id of the resource
*/
String getResourceId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.keycloak.models.workflow;

import org.keycloak.provider.Provider;

import java.util.List;

import org.keycloak.provider.Provider;

/**
* Interface serves as state check for workflow actions.
*/
Expand Down Expand Up @@ -57,8 +57,6 @@ public interface WorkflowStateProvider extends Provider {

List<ScheduledStep> getScheduledStepsByWorkflow(String workflowId);

List<ScheduledStep> getScheduledStepsByStep(String stepId);

default List<ScheduledStep> getScheduledStepsByWorkflow(Workflow workflow) {
if (workflow == null) {
return List.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.keycloak.representations.workflows.WorkflowConstants.CONFIG_AFTER;
import static org.keycloak.representations.workflows.WorkflowConstants.CONFIG_PRIORITY;

import java.util.Objects;

import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel;

Expand Down Expand Up @@ -91,4 +93,15 @@ public Long getAfter() {
public int compareTo(WorkflowStep other) {
return Integer.compare(this.getPriority(), other.getPriority());
}

@Override
public boolean equals(Object o) {
if (!(o instanceof WorkflowStep that)) return false;
return Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return Objects.hashCode(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

package org.keycloak.models.workflow;

import java.util.List;
import org.keycloak.provider.Provider;

public interface WorkflowStepProvider extends Provider {

void run(List<String> resourceIds);
void run(WorkflowExecutionContext context);

}
Loading