Dev/guice

From CubeiaWiki

Jump to: navigation, search

Contents

Firebase Guice Support

Firebase works well with Guice and an add-on is released with Guice support started. It contains base-classes for all Firebase artifacts which sets up the injection contexts properly. This page outlines how the guice support add-on works.

Requirements

A basic understanding of Cubeia Firebase is needed to understand this page.

Building

With Maven

To build with Maven you will need the following dependency and repository:

<dependency>
  <groupId>com.cubeia.firebase</groupId>
  <artifactId>guice-support</artifactId>
  <version>1.2.0</version>
</dependency>
<repository>
  <id>cubeia-nexus</id>
  <url>http://m2.cubeia.com/nexus/content/groups/public/</url>
  <releases>
    <enabled>true</enabled>
  </releases>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
</repository>

Without Maven

You can easily use the Guice add-on without Maven, just download the following file and add to you build class path:

http://m2.cubeia.com/nexus/content/groups/public/com/cubeia/firebase/guice-support/1.2.0/guice-support-1.2.0.jar

Overview

Each artifact in Firebase, service, game and tournament, has a base class in the Guice support JAR. These base classes uses programmatic configuration through abstract methods to set-up an initial injection context. However, they differ in one important way:

  • The game will setup one injection context for each executed event. This context will include the event itself, the table it is executed on and the current state associated with the table. In other words, the injection will be done in a custom event scope.

Guice Game

Quick How-To

Extend GuiceGame:

public class MyGame extends GuiceGame [...]

Override 'getConfigurationHelp' to return a configuraton, with your processor class, and optionally your interceptor and listener classes:

public Configuration getConfigurationHelp() {
    return new Configuration() {

	public Class<? extends GameProcessor> getGameProcessorClass() {
            // Return the class of your processor, mandatory
        }
	
	public Class<? extends TableListener> getTableListenerClass() {
            // Return the class of your listener, or null
        }
	
	public Class<? extends TableInterceptor> getTableInterceptorClass() {
            // Return the class of your interceptor, or null
        }
	
	public Class<?> getGameStateClass() {
            // Return the class of your state object, or null
        }
    };
}

Also, if you want to register your own modules, you can override 'preInjectorCreation':

public void preInjectorCreation(List<Module> list) {
    list.add(new MyGameModule());
}

Note: If you override init() in your game implementation, then you need to call super.init() or Guice will not be initialized.

Scope

All injections in the game will be scoped to the currently executing event. Any additional bindings for a game should be done in the Firebase 'event' scope:

bind(MyContract.class).to(MyImpl.class).in(EventScoped.class);

Special Injections

The following objects are available for ordinary in a game, all in the event scope:

GameProcessor
GameContext
TableGameState
TableMetaData
GameNotifier
TablePlayerSet
TableScheduler
TournamentNotifier
TableWatcherSet
Table

In addition, if you have configured a listener or interceptor or state class via the configuration, these will be injectable as wrll.

Any Firebase service can be injected (must be a service contract), using the special Service injection annotation:

@Service
private DatasourceServiceContract dsContract;  

You can also inject a Log4j logger anywhere using the special Log4j injection annotation:

@Log4j
private Logger log;

Guice Service

Quick How-To

Extend GuiceService:

public class MyService extends GuiceService implements MyServiceContract [...]

Override 'getConfigurationHelp' to return a configuraton, with your actual service implementation class:

public Configuration getConfigurationHelp() {
    return new Configuration() {

	public ContractsConfig getServiceContract() {
            // Return the class of your interface and implementation
        }
    };
}

Also, if you want to register your own modules, you can override 'preInjectorCreation':

public void preInjectorCreation(List<Module> list) {
    list.add(new MyServiceModule());
}

On each service contract method, proxy call via guice instance:

public void myServiceMethod(Object...args) {
    guice(MyServiceContract.class).myServiceMethod(args);
}

Special Injections

Any Firebase service can be injected (must be a service contract), via the special Service' injection annotation:

@Service
private DatasourceServiceContract dsContract;  

If a service is injected into another service during the initiation phase of the service life-cycle, the service must be proxied, otherwise the injection may result in a null value as the injected service may not be known:

@Service(proxy=true)
private DatasourceServiceContract dsContract;  

You can also inject a Log4j logger anywhere using the special Log4j injection annotation:

@Log4j
private Logger log;

Guice Tournament

Quick How-To

Extend GuiceTournament:

public class MyMTT extends GuiceTournament [...]

Override 'getConfigurationHelp' to return a configuraton, with your actual tournament handler class:

public Configuration getConfigurationHelp() {
    return new Configuration() {

	public Class<? extends TournamentHandler> getTournamentHandlerClass() {
            // Return the class of your implementation
        }
    };
}

Also, if you want to register your own modules, you can override 'preInjectorCreation':

public void preInjectorCreation(List<Module> list) {
    list.add(new MyMttModule());
}

Special Injections

To use the support methods from MTTSupport in your handler, there's an assistant interface you can inject:

@Inject
private TournamentAssist mttAssist;  

You can also inject a Log4j logger anywhere using the special Log4j injection annotation:

@Log4j
private Logger log;
Personal tools