2.8. Guice Services

Introduction

Granite Data Services brings support for Google Guice and Wideplay Warp services.

For a complete sample with Granite DS/Guice/Warp/Ejb3 entity beans working together, download graniteds-guice-1.0.0.zip and import it as a new Eclipse project.

Guice Services Configuration

In order to access your Guice services from your Flex application, you must configure your Flex services as follow:

services-config.xml
<services-config>
  <services>
    <service
      id="granite-service"
      class="flex.messaging.services.RemotingService"
      messageTypes="flex.messaging.messages.RemotingMessage">
      <destination id="cars">
        <channels>
          <channel ref="my-graniteamf"/>
        </channels>
        <properties>
          <factory>guiceFactory</factory>
          <source>test.granite.guice.services.Cars</source>
        </properties>
      </destination>
    </service>
  </services>

  <factories>
    <factory id="guiceFactory" class="org.granite.guice.GuiceServiceFactory" />
  </factories>

  <channels>
    <channel-definition id="my-graniteamf" class="mx.messaging.channels.AMFChannel">
      <endpoint
        uri="http://{server.name}:{server.port}/{context.root}/graniteamf/amf"
        class="flex.messaging.endpoints.AMFEndpoint"/>
    </channel-definition>
  </channels>
</services-config>

The org.granite.guice.GuiceServiceFactory is declared in the <fatory> node and used in the "cars" destination (see <service> node). The <source> node then declares the test.granite.guice.services.Cars class as a Guice service (see this class in graniteds-guice-1.0.0.zip for @Inject and @Transactional annotations usage).

Additional Web Configuration (web.xml)

In order to initialize Guice and Warp persistence, you must also customize your web.xml as follow:

web.xml
<web-app version="2.4" ...>
    ...
    <!-- Guice specific context listener (initialize Guice
         injector and WARP persistence) -->
    <listener>
        <listener-class>test.granite.guice.listeners.InitNDestroy</listener-class>
    </listener>

    <!-- WARP persistence filter -->
    <filter>
        <filter-name>sessionPerRequestFilter</filter-name>
        <filter-class>com.wideplay.warp.jpa.SessionPerRequestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sessionPerRequestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    ...
</web-app>

The InitNDestroy listener purpose is to initialize a Guice injector and Warp/Hibernate persistence (see details in graniteds-guice-1.0.0.zip).


Browse Space

- Pages
- Blog
- Labels
- Attachments
- Bookmarks
- Mail
- Advanced

Explore Confluence

- Popular Labels
- Notation Guide

Your Account

Log In

Other Features

Add Content


Copyright © 2011 Granite Data Services S.A.S. All Rights Reserved.