Headless access

This section tackles the topic of enabling access to an Apache Causeway application directly, or at least, not through either the Wicket or Restful viewers.

There are a number of use-cases:

  • implementing a REST end point that needs to reach into the Apache Causeway runtime

    • this could be a custom UI

    • this could be to integrate from other systems, eg a subscriber on a pub/sub event bus, pushing changes through an Apache Causeway domain model.

  • calling from Quartz for a background batch process

Note that the calling thread runs in the same process space as the Apache Causeway domain object model (must be physically linked to the JAR files containing the domain classes).

The SimpleApp starter app demonstrates the pattern with a custom UI controller:

@RestController
@RequiredArgsConstructor(onConstructor_ = {@Inject})
class CustomController {

    private final InteractionService interactionService;
    private final TransactionalProcessor transactionalProcessor;
    private final SimpleObjects simpleObjects;

    @GetMapping("/custom/simpleObjects")
    List<SimpleObject> all() {
        return call("sven", simpleObjects::listAll)
                .orElse(Collections.<SimpleObject>emptyList());                                     (3)
    }

    private <T> Optional<T> call(
            final String username,
            final Callable<T> callable) {

        return interactionService.call(
                InteractionContext.ofUserWithSystemDefaults(UserMemento.ofName(username)),          (1)
                () -> transactionalProcessor.callWithinCurrentTransactionElseCreateNew(callable))
                .ifFailureFail()                                                                    (2)
                .getValue();
    }
}
1 the context of this interaction: who, when and where
2 re-throws exception that has occurred, if any
3 handles null case, if required

The API described here is reasonably low-level, allowing code to interact very directly with the Apache Causeway metamodel and runtime. Such callers should be considered trusted: they do not (by default) honour any business rules eg implicit in the Causeway annotations or hide/disable/validate methods.

Another example: Seed services

Another example is domain services that need to seed reference data during bootstrapping. An example of this can be found here.