Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provides a main framework for custom scripting #1960

Merged
merged 8 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 52 additions & 0 deletions src/main/java/sirius/biz/scripting/CustomEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.biz.scripting;

import sirius.kernel.health.HandledException;

import java.util.Optional;

/**
* Provides a base class for all custom events handled by a {@link CustomEventDispatcher}.
*/
public abstract class CustomEvent {
sabieber marked this conversation as resolved.
Show resolved Hide resolved

protected boolean success;
protected boolean failed;
protected HandledException error;
sabieber marked this conversation as resolved.
Show resolved Hide resolved

/**
* Determines if the event was successful.
*
* @return <tt>true</tt> if the event was successful, <tt>false</tt> otherwise
*/
public boolean isSuccess() {
return success;
}

/**
* Determines if the event failed (an exception occurred within an event handler).
* <p>
* Note, that the exception itself can be obtained via {@link #getError()}.
*
* @return <tt>true</tt> if the event failed, <tt>false</tt> otherwise
*/
public boolean isFailed() {
return failed;
}

/**
* Provides the error which occurred when handling the event.
*
* @return the error that occurred wrapped as optional or an empty optional if the event was successful
*/
public Optional<HandledException> getError() {
return Optional.ofNullable(error);
}
}
33 changes: 33 additions & 0 deletions src/main/java/sirius/biz/scripting/CustomEventDispatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.biz.scripting;

/**
* Describes a dispatcher which can handle custom events.
* <p>
* This is usually fetched via {@link CustomEvents} and will handle all events for a given tenant,
* based on a given script. If no custom handling script is present, a <tt>NOOP</tt> dispatcher is used
* which will be marked as {@link #isActive() inactive} (so that some events might get optimized away).
*/
public interface CustomEventDispatcher {

/**
* Determines if this dispatcher is active and will actually handle events.
*
* @return <tt>true</tt> if a real dispatcher is present, <tt>false</tt> if a <tt>NOOP</tt> dispatcher is used
*/
boolean isActive();

/**
* Handles the given event.
*
* @param event the event to handle
*/
void handleEvent(CustomEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.biz.scripting;

import sirius.kernel.di.std.AutoRegister;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;

/**
* Provides a repository which stores and manages {@link CustomEventDispatcher custom event dispatchers}.
* <p>
* Note that the repository isn't usually accessed directly. Instead, the {@link CustomEvents} class should be used.
*/
@AutoRegister
public interface CustomEventDispatcherRepository {

/**
* Fetches all available dispatchers for the given tenant.
*
* @param tenantId the tenant for which to fetch the dispatchers
* @return a list of all available dispatchers for the given tenant
*/
List<String> fetchAvailableDispatchers(@Nonnull String tenantId);

/**
* Fetches the dispatcher with the given name for the given tenant.
*
* @param tenantId the tenant for which to fetch the dispatcher
* @param name the name of the dispatcher to fetch
* @return the dispatcher with the given name for the given tenant wrapped as optional or an empty optional if
* no such dispatcher exists. <b>NOTE:</b> if an empty name is given, the first dispatcher for the given tenant
* is used. This helps to simplify the usage of custom events.
*/
Optional<CustomEventDispatcher> fetchDispatcher(@Nonnull String tenantId, @Nullable String name);
}
42 changes: 42 additions & 0 deletions src/main/java/sirius/biz/scripting/CustomEventRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.biz.scripting;

import sirius.kernel.commons.Callback;

/**
* Provides the interface as seen by the scripting engine to register custom event handlers.
* <p>
* The tenant specific script is provided with an instance of this registry and can then add handlers
* as needed.
*/
public interface CustomEventRegistry {

/**
* Adds a handler for the given event type.
*
* @param eventType the type of events to handle
* @param handler the handler to handle the event
* @param <E> the generic type of the event
*/
<E extends CustomEvent> void registerHandler(Class<E> eventType, Callback<E> handler);

/**
* Adds a typed handler for the given event type and inner type
*
* @param eventType the type of events to handle
* @param type the inner type within the event to process
* @param handler the handler to handle the event
* @param <T> the generic inner type of the event
* @param <E> the generic type of the event
*/
<T, E extends TypedCustomEvent<T>> void registerTypedHandler(Class<E> eventType,
Class<T> type,
Callback<E> handler);
}
93 changes: 93 additions & 0 deletions src/main/java/sirius/biz/scripting/CustomEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.biz.scripting;

import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.web.security.ScopeInfo;
import sirius.web.security.UserContext;
import sirius.web.security.UserInfo;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

/**
* Provides access to tenant specific custom event dispatchers.
* <p>
* Event dispatchers are stored and managed by a {@link CustomEventDispatcherRepository}. Commonly these are
* defined via scripts which modify a {@link CustomEventRegistry} and are then transformed into a
* {@link CustomEventDispatcher} with the help of a {@link ScriptBasedCustomEventDispatcher}.
*/
@Register(classes = CustomEvents.class)
public class CustomEvents {

private static final CustomEventDispatcher NOOP_DISPATCHER = new CustomEventDispatcher() {

@Override
public boolean isActive() {
return false;
}

@Override
public void handleEvent(CustomEvent event) {
// do nothing
}
};

@Part
@Nullable
private CustomEventDispatcherRepository dispatcherRepository;

/**
* Fetches the dispatcher for the current tenant.
*
* @param name the name of the dispatcher to fetch
* @return the dispatcher for the current tenant with the given name or a NOOP dispatcher if no such dispatcher
* exists. Note, if an empty <tt>name</tt> is given, the first available dispatcher for the current tenant is used.
* This way, if exactly one dispatcher is present, it will be used in all import processes etc.
*/
public CustomEventDispatcher fetchDispatcherForCurrentTenant(@Nullable String name) {
if (dispatcherRepository == null) {
return NOOP_DISPATCHER;
}

if (!ScopeInfo.DEFAULT_SCOPE.getScopeType().equals(UserContext.getCurrentScope().getScopeType())) {
return NOOP_DISPATCHER;
}

UserInfo currentUser = UserContext.getCurrentUser();
if (!currentUser.isLoggedIn()) {
return NOOP_DISPATCHER;
}

return dispatcherRepository.fetchDispatcher(currentUser.getTenantId(), name).orElse(NOOP_DISPATCHER);
}

/**
* Fetches all available dispatchers for the current tenant.
*
* @return a list of all available dispatchers for the current tenant
*/
public List<String> fetchDispatchersForCurrentTenant() {
if (dispatcherRepository == null) {
return Collections.emptyList();
}
if (!ScopeInfo.DEFAULT_SCOPE.getScopeType().equals(UserContext.getCurrentScope().getScopeType())) {
return Collections.emptyList();
}

UserInfo currentUser = UserContext.getCurrentUser();
if (!currentUser.isLoggedIn()) {
return Collections.emptyList();
}

return dispatcherRepository.fetchAvailableDispatchers(currentUser.getTenantId());
}
}
40 changes: 40 additions & 0 deletions src/main/java/sirius/biz/scripting/EntityCustomEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.biz.scripting;

import sirius.db.mixing.Entity;

/**
* Provides a base class for all custom events which are associated with an entity.
*
* @param <E> the generic type of the entity
*/
public abstract class EntityCustomEvent<E extends Entity> extends TypedCustomEvent<E> {

private final E entity;

protected EntityCustomEvent(E entity) {
this.entity = entity;
}

/**
* Returns the entity associated with this event.
*
* @return the entity associated with this event
*/
public E getEntity() {
return entity;
}

@SuppressWarnings("unchecked")
@Override
public Class<E> getType() {
return (Class<E>) entity.getClass();
}
}
Loading