Skip to content

modules

James Moger edited this page Nov 20, 2015 · 1 revision

Modules

There are two kinds of modules in Fathom, fathom.Module and fathom.ServletsModule.

Fathom modules are discovered by a Java ServiceLoader.

fathom.Module

Subclass this module type to provide injectable services & components to your Fathom application.

Configuration

Create a Java ServiceLoader definition file for your module...

META-INF/services/fathom.Module

com.package.MyModule

... and then define it.

public class MyModule extends Module {

  @Override
  protected void setup() {

    // register MyService
    bind(MyService.class);

  }

}

fathom.ServletsModule

Subclass this module type to provide injectable services & components, Filters, and Servlets.

Configuration

Create a Java ServiceLoader definition file for your module...

META-INF/services/fathom.ServletsModule

com.package.MyServletsModule

... and then define it.

public class MyServletsModule extends ServletsModule {

  @Override
  protected void setup() {

    // serve all requests with MyServlet
    serve("/*").with(MyServlet.class);

    // add a debug filter for dev mode
    if (getSettings().isDev()) {
      filter("/*").through(MyFilter.class);
    }

    // register MyService
    bind(MyService.class);

  }

}

Configuration

Requiring Settings

Your module may need one or more settings to function and you may specify them as annotated requirements.

Each required setting must be present in the runtime profile configuration and must have a non-empty value otherwise the module will not be loaded.

@RequireSetting("myService.url")
@RequireSetting("myService.username")
@RequireSetting("myService.password")
public class MyModule extends Module {

  @Override
  protected void init() {

    // register MyService
    bind(MyService.class);

  }

}

Requiring Modes

You might only want to load your module in a particular runtime mode. This is easily accomplished by using one or more of the mode-specific annotations: @DEV, @TEST, and @PROD.

@DEV @TEST
public class DebugModule extends Module {

  @Override
  protected void init() {

    // register DebugService
    bind(DebugService.class);

  }

}

Usage

Add your module to the classpath of your Fathom application.