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 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
40 changes: 40 additions & 0 deletions src/main/java/sirius/biz/scripting/EntityScriptableEvent.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 EntityScriptableEvent<E extends Entity> extends TypedScriptableEvent<E> {

private final E entity;

protected EntityScriptableEvent(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();
}
}
63 changes: 63 additions & 0 deletions src/main/java/sirius/biz/scripting/ScriptableEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 ScriptableEventDispatcher}.
*/
public abstract class ScriptableEvent {

/**
* Stores if all event handlers completed successfully.
*/
protected boolean success;

/**
* Stores if an error / exception occurred while invoking an event handler.
*/
protected boolean failed;

/**
* Stores the exception which occurred while invoking an event handler.
*/
protected HandledException error;

/**
* 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/ScriptableEventDispatcher.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 ScriptableEvents} 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 ScriptableEventDispatcher {

/**
* 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(ScriptableEvent 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 ScriptableEventDispatcher custom event dispatchers}.
* <p>
* Note that the repository isn't usually accessed directly. Instead, the {@link ScriptableEvents} class should be used.
*/
@AutoRegister
public interface ScriptableEventDispatcherRepository {

/**
* 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<ScriptableEventDispatcher> fetchDispatcher(@Nonnull String tenantId, @Nullable String name);
}
42 changes: 42 additions & 0 deletions src/main/java/sirius/biz/scripting/ScriptableEventRegistry.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 ScriptableEventRegistry {

/**
* 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 ScriptableEvent> 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 TypedScriptableEvent<T>> void registerTypedHandler(Class<E> eventType,
Class<T> type,
Callback<E> handler);
}
93 changes: 93 additions & 0 deletions src/main/java/sirius/biz/scripting/ScriptableEvents.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 ScriptableEventDispatcherRepository}. Commonly these are
* defined via scripts which modify a {@link ScriptableEventRegistry} and are then transformed into a
* {@link ScriptableEventDispatcher} with the help of a {@link SimpleScriptableEventDispatcher}.
*/
@Register(classes = ScriptableEvents.class)
public class ScriptableEvents {

private static final ScriptableEventDispatcher NOOP_DISPATCHER = new ScriptableEventDispatcher() {

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

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

@Part
@Nullable
private ScriptableEventDispatcherRepository 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 ScriptableEventDispatcher 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());
}
}
Loading