All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.docs.plugins.html Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
{#==========================================
Docs : "Plugins"
==========================================#}

Plugins

{#========================================== What is a plugin? ==========================================#}

What is a Spincast plugin?

A plugin can be a simple library, like any other Maven artifacts you add to your project. It can provide components and utilities to be used in your application.

But a plugin can also contribute to the Guice context of your application. They can add some bindings to that context and can even modify/remove some! All plugins are applied, in the order they are registered, during the bootstrapping phase.

Some plugins may also suggest an add-on to install.

{#========================================== Installing a plugin ==========================================#}

Installing a plugin

You first need to add the Maven artifact of the plugin to your pom.xml (or build.gradle). For example :

<dependency>
    <groupId>com.example</groupId>
    <artifactId>some-wonderful-plugin</artifactId>
    <version>1.2.3</version>
</dependency>

Most of the plugins need to bind some components to the Guice context of the application. For them to be able to do so, you need to register them using the plugin(...) method of the Bootstrapper. For example :

public static void main(String[] args) {

    Spincast.configure()
            .module(new AppModule())
            .plugin(new SomeWonderfulPlugin())
            .requestContextImplementationClass(AppRequestContextDefault.class)
            .init(args);
   //...
}

Here, SomeWonderfulPlugin is the main class of the plugin, the one implementing the SpincastPlugin interface. To know what class to use to register a plugin, you have to read its documentation.

{#========================================== Installing a Request Context add-on ==========================================#}

Installing a Request Context add-on

One of the coolest features of Spincast is the ability to extend the Request Context type. The Request Context are objects, associated with a request, that Spincast automatically creates and passes to your Route Handlers. You can extend the type of those object by adding add-ons. Some plugins may suggest that you use one of their components as such add-on.

To do so, you first add the add-on entry point to your Request Context interface. This entry point is simply a method, with a meaningful name, and that returns the add-on's main component :

public interface AppRequestContext extends RequestContext {

    public WonderfulComponent wonderful();
    
    //... other add-ons and methods
}

Here, the add-on is named "wonderful()" and its main component is "WonderfulComponent".

Then, you inject a Provider for the main component in your Request Context implementation, and you use it to return component instances :

public class AppRequestContext extends RequestContextBase<AppRequestContext>
                               implements AppRequestContext {

    private final Provider<WonderfulComponent> wonderfulComponentProvider;

    @AssistedInject
    public AppRequestContext(@Assisted Object exchange,
                             RequestContextBaseDeps<AppRequestContext> requestContextBaseDeps,
                             Provider<WonderfulComponent> wonderfulComponentProvider) {
        super(exchange, requestContextBaseDeps);
        this.wonderfulComponentProvider = wonderfulComponentProvider;
    }
    
    @Override
    public WonderfulComponent wonderful() {
        return this.wonderfulComponentProvider.get();
    }
    
    //...
}

It's a good practice to always use a Provider for the add-on's component, because it is often not a singleton and may even be request scoped.

You can now use the newly installed add-on, directly in your Route Handlers! For example :

public void myRouteHandler(AppRequestContext context) {

    context.wonderful().doSomething();
    
    //...
}

{#========================================== Default plugins ==========================================#}

Default plugins

By using the spincast-default Maven artifact and the Bootstrapper, some plugins are installed by default. Those plugins provide implementations for the main components required in any Spincast application. If you disable one of those plugins, you have to bind by yourself implementations for the components that this plugin was binding.

  • SpincastCorePlugin : this is the only plugin which is not listed in the plugins section because it is the very core of Spincast. But it's interesting to know that even this core is a plugin!
  • Spincast Routing : binds all the components related to routing, allows the creation of the Routes and provides the "routing()" add-on.
  • Spincast Request : provides the "request()" add-on which allows Route Handlers to get information about the current request.
  • Spincast Response : provides the "response()" add-on which allows Route Handlers to build the response to send.
  • Spincast Undertow : provides an implementation for the required HTTP/Websocket Server component, using Undertow.
  • Spincast Locale Resolver : provides an implementation for the required LocaleResolver component.
  • Spincast TimeZone Resolver : provides an implementation for the required TimeZoneResolver component.
  • Spincast Variables add-on : as its name suggests, this plugin simply provides a "variables()" add-on to write and read information in the request scope. This can be used to pass information from a Route Handler to another.
  • Spincast Templating add-on : provides a "templating()" add-on giving access to utilities to render some text based templates.
  • Spincast Pebble : provides an implementation for the required TemplatingEngine component, using Pebble.
  • Spincast Jackson Json : provides an implementation for the required JsonManager component, using Jackson.
  • Spincast Jackson XML : provides an implementation for the required XmlManager component, using Jackson.
  • Spincast Config : provides everything that is required to configure a Spincast application. It allows you to tweak the default configurations used by the Spincast core components, and to create configurations that are specific to your application.
  • Spincast Dictionary : provides an implementation of the Dictionary interface, allowing internationalization ("i18n"). It is used to specify labels in a multilingual application.
  • Spincast HTTP Caching Addon : provides a "cacheHeaders()" add-on to help dealing with HTTP caching.





© 2015 - 2024 Weber Informatics LLC | Privacy Policy