Chapter 3. Getting Started with Flamingo

This chapter describes where you can get Exadel Flamingo, how to install it and work with a simple helloworld demo.

3.1. Downloading and Installing Flamingo 2.1

The latest release of Flamingo is available for download at:

http://flamingo.exadel.com

Please unzip "flamingo-2.1.zip" file to the chosen folder of your existing JBoss Seam application, e.g.: "flamingo-2.1". Optionally, you can add <flamingo-2.1/bin> directory to the path system variable. It will enable Flamingo commands to run from anywhere. Then run "flamingoinstall" shell script from the "bin" folder.

 

After that select which one of the following client-server techniques is preferable for you or just generate a ready project with the desired configuration using Flamingo Generator Commands as described in the Create New Flamingo Project

3.2. Seam

Here a necessary sequence of operations to use Exadel Flamingo with JBoss Seam is described. This chapter also describes how to create a simple HelloWorld application using Adobe Flex and JavaFX technologies.

3.2.1. Server Configuration

This section of the guide will discuss necessary server configuration for JBoss Seam.

3.2.1.1. Copying files

Copy files "flamingo-services-common-2.1.jar", "flamingo-service-2.1.jar", "scannotation-1.0.2.jar", "amf-serializer-2.1.jar" (for AMF) and/or "hessian-3.1.3.jar"(for Hessian) into the "WEB-INF/lib" folder of your application. For EAR projects file "flamingo-service-2.1.jar" should be copied to the EAR root and specified in the "application.xml" file as a module.

Tip

File "hessian-3.1.3.jar" is available at

http://hessian.caucho.com/#Java

Choose one of the following steps, according to the fact you are using Seam Remoting or Servlets.

Seam Remoting is optional. Flamingo can use it if it is enabled, otherwise, Flamingo requires servlet configuration.

3.2.1.2. Using Seam Remoting

If Seam Remoting is used in your project, you don't need any additional configuration and now can call the Seam components directly from Flex 2 using AMF and/or Hessian protocols.

Note:

By default, Seam Remoting only allows calling methods marked with the @WebRemote annotation. Flamingo by default is not dependent on this annotation, and can call any method of any Seam component. If necessary, you can limit Flamingo to only use @WebRemote methods: add the following configuration into the "web.xml" file of your application:

<context-param>
    <param-name>flamingoConfig.webRemoteEnabled</param-name>
    <param-value>true</param-value>
</context-param>

3.2.1.3. Using Servlets

If Seam Remoting is not used in your project:

  • Add the following content into the "web.xml" file of your application if you use AMF:

    
    
    <servlet>
        <servlet-name>AMF Remote Servlet</servlet-name>
        <servlet-class>com.exadel.flamingo.service.seam.AMFToSeamServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>AMF Remote Servlet</servlet-name>
        <url-pattern>/flamingo/amf/*</url-pattern>
    </servlet-mapping>

This parameter shows that calls coming to URL "[server_name]/[context]/flamingo/amf/*" will be passed to the com.exadel.flamingo.service.seam.AMFToSeamServlet servlet.

Thus, using URL "/flamingo/amf/" you can call the servlet to access the Seam components via the AMF protocol.

  • OR this content if you use the Hessian protocol:

    
    
    <servlet>
        <servlet-name>Hessian Remote Servlet</servlet-name>
        <servlet-class>com.exadel.flamingo.service.seam.HessianToSeamServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Hessian Remote Servlet</servlet-name>
        <url-pattern>/flamingo/hessian/*</url-pattern>
    </servlet-mapping>

This parameter shows that calls coming to URL "[server_name]/[context]/flamingo/hessian/*" will be passed to the com.exadel.flamingo.service.seam.HessianToSeamServlet servlet.

Thus, using URL "/flamingo/hessian/ " you can call the servlet to access the Seam components via the Hessian protocol.

Tip

See also Chapter 6: Supported Communication Protocols to find more information about both Protocols and their configuration.

Note:

If you want to limit calling to the @WebRemote methods only (as designed in Seam Remoting), add the following configuration into the "web.xml" file of your application:


<context-param>
    <param-name>flamingoConfig.webRemoteEnabled</param-name>
    <param-value>true</param-value>
</context-param>

3.2.2. Seam Component Creation

Now we will create a Seam component to implement a simple HelloWorld demo:

  • Add a new file "HelloAction.java" with Seam component helloAction to the existing Seam application:


import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Scope(ScopeType.STATELESS)
@Name("helloAction")
public class HelloAction {
    public String hello(String name) {
        return "Hello, " + name;
    }
}

The method of this class accepts a string sent from the client as an argument, and it is forwarded then back with string Hello, added at the beginning.

Note

Using Flamingo you can call not only stateless but stateful components as well, and the state will be saved from call to call.

3.2.3. Flex Client

Please add the client source code according to the protocol you use: AMF or Hessian. Fore more information about these protocols and their benefits please see Chapter 6: Supported Communication Protocols.

3.2.3.1. AMF

  • Create file "client.mxml" with the following content:

    
    ...                    
     <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:word="*">               
    <mx:RemoteObject id="service" destination="<property>helloAction</property>"/>               
    <mx:Panel title="Flamingo AMF Hello World Sample"
    paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">               
    <mx:HBox>
       <mx:Label text='Whom do you want to say "Hello" to?'/>              
        <mx:TextInput id="who"/>            
        <mx:Button label="Say Hello" click="<property>service.hello(who.text)</property>"/>
    </mx:HBox>              
    <mx:Label text='The server said: "{service.hello.lastResult}"'/>             
    </mx:Panel>
    </mx:Application>
    ...

    This sample shows how the hello method of server Seam component helloAction can be called using "RemoteObject" when clicking on the "Say Hello" button.

  • Create file "services-config.xml" with the following content:

    
    ...
    <services-config>
    <services>
    <service
       id="hello-service"
       class="flex.messaging.services.RemotingService"
       messageTypes="flex.messaging.messages.RemotingMessage">
        <destination id="<property>helloAction</property>">
          <channels>
            <channel ref="seam-amf"/>
          </channels>
        </destination>
    </service>
    </services>
    <channels>
      <channel-definition id="seam-amf" class="mx.messaging.channels.AMFChannel">
       <endpoint
       uri="http://<property>{server.name}:{server.port}</property>/<property>helloworld</property>/<property>seam/resource/amf</property>"
       class="flex.messaging.endpoints.AMFEndpoint"/>
      </channel-definition>
    </channels>               
    </services-config>
    ...

    This sample shows that channel seam-amf with a set url to access the server is applied in order to use destination helloAction;

where

"{server.name}:{server.port}" is server name:server port where your existing JBoss Seam application is running;

"helloworld" is the name of your existing application;

"helloAction" is the name of the component specified in the "HelloAction.java" file (see annotation @Name("helloAction") in 3.3.1);

"seam/resource/amf/" is the standard configuration while using Remoting. If you use another mapping for SeamResourceServlet or one of the Flamingo servlets, use the path specified in the "web.xml" file of your application for destination:

uri="http://{server.name}:{server.port}/helloworld/flamingo/amf"

  • Compile an .swf file using parameter "mxmlc -output client.swf -compiler.services services-config.xml client.mxml" in the command line.

Note

Make sure, that the version of Flex2 compiler you use is not earlier than 2.0.1 Hotfix2: run "mxmlc -version" in the command line and verify that the build number is equal or higher than 166910.

  • Deploy your "client.swf" file into your server application.

  • Type in your browser the url to the .swf file: "http://localhost:8080/helloworld/client.swf"

  • Enjoy the results!

HelloWorld.swf

Figure 3.1. HelloWorld.swf


3.2.3.2. Hessian

  • Create file "client.mxml" with the following content:


...
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:word="*">
            
<hessian:HessianService xmlns:hessian="hessian.mxml.*"
id="service" destination="seam/resource/hessian/<property>helloAction</property>"/>
            
<mx:Panel title="Flamingo Hessian Hello World Sample"
paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
            
<mx:HBox>
   <mx:Label text='Whom do you want to say "Hello" to?'/>
            
   <mx:TextInput id="who"/>
            
   <mx:Button label="Say Hello" click="service.hello.send(who.text)"/>
</mx:HBox>
            
<mx:Label text='The server said: "{service.hello.lastResult}"'/>
            
</mx:Panel>
</mx:Application>
...

This sample shows how the hello method of server Seam component helloAction can be called using HessianService when clicking on the "Say Hello" button,

where

"helloAction" is the name of the component specified in the "HelloAction.java" file (see annotation @Name("helloAction") in 3.3.1);

"seam/resource/hessian/" is the standard configuration while using Remoting. Please note that the URL is relative, so the application will contact http://[origin server]/[application context]/[path to Seam/Flamingo servlet]/helloAction as a service. If you use another mapping for SeamResourceServlet or one of the Flamingo servlets, use the path specified in the "web.xml" file of your application in the URL pattern:

destination="flamingo/hessian/helloAction"

  • Compile an .swf file using parameter "mxmlc -output client.swf -library-path+=hessian-flash-3.1.2.swc client.mxml" in the command line.

Note

  • Make sure, that the version of Flex2 compiler you use is not earlier than 2.0.1 Hotfix2: run "mxmlc -version" in the command line and verify that the build number is equal or higher than 166910.

  • File "hessian-flash-3.1.2.swc" is available at

    http://hessian.caucho.com/#Flash/Flex.

  • Deploy your "client.swf" file into your server application.

  • Type in your browser the url to the .swf file: "http://localhost:8080/helloworld/client.swf".

  • Enjoy the results!

HelloWorld.swf

Figure 3.2. HelloWorld.swf


3.2.4. JavaFX Client

In this release only one method of communication between JavaFX client application and JBoss Seam is supported: via the Hessian protocol. Fore more information about this protocol and its benefits please see Chapter 6: Supported Communication Protocols

3.2.4.1. Hessian

  • Create the following structure of the working directory:

- classes

- lib

- src

  • Put libraries "hessian-3.1.3.jar", "jboss-seam.jar", "jboss-seam-remoting.jar", "javafxrt.jar", "javafxgui.jar" and "javafx-swing.jar" into the lib directory.

Note

"javafxrt.jar", "javafxgui.jar" and "javafx-swing.jar" can be copied from JavaFX SDK lib directory; "jboss-seam.jar" and "jboss-seam-remoting.jar" can be copied from Seam runtime lib directory.

File "hessian-3.1.3.jar" can be downloaded via this link: hessian.caucho.com/download/hessian-3.1.3.jar.

  • Create file "FXClient.fx" with the following content:


...


package com.exadel.flamingo.javafx;
          
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextOrigin;
import javafx.ext.swing.SwingButton;
import javafx.scene.layout.VBox;
import javafx.scene.control.TextBox;
          
import com.exadel.flamingo.javafx.samples.helloworld.javafx.HelloworldClient;
          
class Hello {
        public var name:String;
        public var str:String;
}
var helloModel = new Hello();
          
HelloworldClient.setServerUrl("http://localhost:8080/helloworld/seam/resource/hessian/<property>helloAction</property>");
          
          
var helloText: TextBox = TextBox {
       text: bind helloModel.name with inverse
       columns: 7
       selectOnFocus:true
}
          
var helloLabel = Text{
      y:8
      font: Font { name:"sansserif", size: 12 }
      fill: Color.BLACK
      content: bind "Server says: {helloModel.str}"
      textOrigin: TextOrigin.TOP
}
          
var helloButton = SwingButton  {
      text:"Say Hello!"
      action: function(){
             helloModel.str = HelloworldClient.CLIENT.<property>hello</property>(helloModel.name);
       }
}
          
Stage {
        title: "Helloworld Sample"
        width: 200
        height: 150
        scene: Scene {
                 content: VBox {
                         translateX: 5
                         translateY: 5
                         spacing: 10
                         content: [                
                                 HBox {
                                            content: helloText
                                            spacing: 10
                                 },
                                 HBox {
                                            content: helloButton
                                            spacing: 10
                             },
                                 HBox {
                                            content: helloLabel
                                            spacing: 10
                           }]
                 }
         }
}  
...

This file creates a visual form to send data from the client side to the server,

where

"helloAction" is the name of the component specified in the "HelloAction.java" file created for the Seam application (see annotation @Name("helloAction") in 3.3.1);

"http://localhost:8080/helloworld/seam/resource/hessian/" is the standard configuration while using Remoting. Please note that the URL is relative, so the application will contact http://[origin server]/[application context]/[path to Seam/Flamingo servlet]/helloAction as a service. If you use another mapping for SeamResourceServlet or one of the Flamingo servlets, use the path specified in the "web.xml" file of your application in the URL pattern:

destination="flamingo/hessian/helloAction"

  • Create interface “IHelloAction.java” for the helloAction Seam component:


package com.exadel.flamingo.javafx.samples;
public interface IHelloAction {
    public String hello(String name);
}

Note

The interface should contain all methods that can be called by the client on the server and their cignatures should be 100% identical.

  • Create file "HelloworldClient.java":


...
 package com.exadel.flamingo.javafx.samples.javafx;

import com.caucho.hessian.client.HessianProxyFactory;
import com.exadel.flamingo.javafx.samples.IHelloAction;
import java.net.MalformedURLException;

public class HelloworldClient {
    public static HelloworldClient CLIENT;
    private String _url;
    private HelloworldClient(String string) {
        _url = string;
    }
    private IHelloAction _service;
    public static void setServerUrl(String url) {
        CLIENT = new HelloworldClient(url);
    }
    private IHelloAction getService() {
        if (_service == null) {
            try {
                HessianProxyFactory factory = new HessianProxyFactory();

                _service = (IHelloAction) factory.create(IHelloAction.class, _url);
            } catch (MalformedURLException ex) {
                System.out.println(ex);
            }

        }

        return _service;
    }

    public String <property>hello</property>(String s) {
        return getService().hello(s);
    }
} 
...

This sample shows how the HelloworldClient class creates a connection with the server with the help of the HessianProxyFactory class and calls the corresponding methods of the component on the server.

 

Here is an example of usage of the JavaFX interpretator:

  • Compile java classes using the following parameter in the command line:

    
    
    javac -sourcepath src -classpath lib/hessian-3.1.3.jar;lib/jboss-seam.GA.jar;
    lib/jboss-seam.jar src/com/exadel/flamingo/javafx/samples/javafx/
    HelloworldClient.java -d classes

            
  • Compile javafx classes using the following parameter in the command line:

    
    javafxc -cp classes src/com/exadel/flamingo/javafx/MainFrame.fx -d classes
  • Create a jar file using the following parameter in the command line:

    
    jar cvf lib/HelloworldJFX.jar -C classes com
  • Run the application using the following parameter:

    
    javafx -cp lib/HelloworldJFX.jar:lib/hessian-3.1.3.jar:lib/javafx-swing.jar:
    lib/javafxgui.jar:lib/javafxrt.jar:lib/jboss-seam.jar:lib/jboss-seam-remoting.jar com.exadel.flamingo.javafx.MainFrame

Note

Change ":" to ";" if using Microsoft® Windows.

  • Enjoy the results!

HelloWorld

Figure 3.3. HelloWorld


3.2.5. Java ME

In this section you will know how to set up Flamingo in Java ME application.

  • Put libraries "flamingo-microedition-2.1.jar" into lib directory.
  • J2ME implementation doesn't to support reflection. Based on this code for object serialization/deserialization is necessary for Flamingo. Following sample display how to implement serialization/deserialization mechanism for Car object.

    
    
    public class Car {
        private String name;
        private String description;
        private double price;
        private String color;

        // Getters/Setters
        ... 
    }

    Serialization:

    
    
    public class ProductCatalogFlamingoHessianOutput extends FlamingoHessianOutput {
        
        protected Hashtable storeObjectAsHashtable(Object object) throws IOException {
            Hashtable returnValue = super.storeObjectAsHashtable(object);
            if (object != null) {
                Class objectClass = object.getClass();
                if (object instanceof Car) {
                    Car car = (Car) object;
                    returnValue = new Hashtable();
                    fillInformationAboutCar(car, returnValue);
                }
                ...
            }
            return returnValue;
        }

        private void fillInformationAboutCar(Car car, Hashtable fieldsValues) {
            putStringValue("color", car.getColor(), fieldsValues);
            putStringValue("description", car.getDescription(), fieldsValues);
            putStringValue("name", car.getName(), fieldsValues);
            fieldsValues.put("price", new Double(car.getPrice()));
        }

        private void putStringValue(String key, String value, Hashtable table) {
            if (value != null) {
                table.put(key, value);
            }
        }

    }

    Deserialization:

    
    
    public class ProductCatalogFlamingoHessianInput extends FlamingoHessianInput {

        protected Object createObject(Class objectClass, Hashtable fieldsValues) throws IOException {
            if (Car.class.equals(objectClass)) {
                Car car = new Car();
                fillInformationAboutCar(car, fieldsValues);
                return car;
            }
        }

        private void fillInformationAboutCar(Car car, Hashtable fieldsValues) {
            car.setName((String) fieldsValues.get("name"));
            car.setDescription((String) fieldsValues.get("description"));
            Double price = (Double) fieldsValues.get("price");
            if (price != null) {
                car.setPrice(price.doubleValue());
            }
            car.setColor((String) fieldsValues.get("color"));
        }

    }

    After that this classes must be registered in Flamingo as shown bellow.

    
    
        FlamingoServiceFactory.setHessianInputDecoratorClass(ProductCatalogFlamingoHessianInput.class);
        FlamingoServiceFactory.setHessianOutputDecoratorClass(ProductCatalogFlamingoHessianOutput.class);
  • Initialize Flamingo before usage in application.

    
    
    String url ="http://hostname:8080/mywebapp/flamingo/hessian";
    if (!FlamingoServiceFactory.isInitialized()) {
        FlamingoServiceFactory.initialize(url);
    }
    ...

    where

    "http://hostname:8080/mywebapp/flamingo/hessian"is URL where remote services deployed. Please note that the URL is relative, so the application will contact http://[origin server]/[application context]/Flamingo servlet]/.
  • When Flamigo initialized you can use server side components in own code. Next code sample displayed how to evaluate el-expression on server-side using ExpressionService.

    
    
    ExpressionService service = FlamingoServiceFactory.getInstance().getExpressionService();
    Class[] types = new Class[]{String.class};
    Object[] parameters = new Object[]{"John"};
    String  result = service.invokeMethod("serverAction.sayHello", String.class, types, parameters);
    ...

For more information about usage Flamingo with Android can be obtained from the examples of the use (examples/expression/javame).

3.2.6. Android

  • Put libraries "flamingo-android-common-2.1.jar", "flamingo-android-hessian-client-2.1.jar", "flamingo-services-common-2.1.jar", "flamingo-service-2.1.jar" and "flamingo-java-client-2.1.jar" into lib directory. If you use Maven for depencency managenent add following strings into pom.xml file.

    
    
            <dependency>
                <groupId>com.exadel.flamingo</groupId>
                <artifactId>flamingo-services-common</artifactId>
                <version>2.1</version>
            </dependency>
            <dependency>
                <groupId>com.exadel.flamingo.android</groupId>
                <artifactId>flamingo-android-common</artifactId>
                <version>2.1</version>
            </dependency>
  • When dependencies on Flamingo libraries added, next step add Flamingo service factory for each activities. For this purposes modify AndroidManifest.xml file - add attribute “name” for tag “application” and required permissions as shown below.

    
    

    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon"      android:name="com.exadel.flamingo.android.FlamingoApplication" 
                     android:label="@string/app_name">
        &..
    </ application>
  • Initialize Flamingo before usage in application.

    
    
    String url ="http://hostname:8080/mywebapp/flamingo/hessian";
    FlamingoApplication flamingoApplication = (FlamingoApplication) getApplication();
    flamingoApplication.initializeFlamingoServiceFactory(url);

    where

    "http://hostname:8080/mywebapp/flamingo/hessian" is URL where remote services deployed. Please note that the URL is relative, so the application will contact http://[origin server]/[application context]/Flamingo servlet]/.
  • When Flamigo initialized you can use server side components. Following sample display access to Seam authentication mechanism via ExpressionService.:

    
    
    Boolean isLogged =  Boolean.FALSE;
    ExpressionService expressionService = flamingoApplication.getService(ExpressionService.class, ExpressionService.NAME);

    expressionService.invokeMethod("identity.setUsername", null, new Class[]{String.class},new Object[]{"mylogin"});
    expressionService.invokeMethod("identity.setPassword", null, new Class[]{String.class},new Object[]{"mypass"});
    expressionService.invokeMethod("identity.login", null, null, null);
    isLogged =  expressionService.invokeMethod("identity.isLoggedIn", boolean.class, null, null);

    if (isLogged) {
      // Some action.
      ...
    }


    For more information about usage Flamingo with Android can be obtained from the examples of the use (examples/expression/android).

3.2.7. Java Widget Toolkits

Supported Java Widget Toolkits

3.2.7.1. Swing

  • Put "flamingo-java-client-2.1.jar", "hessian-3.1.3.jar", "flamingo-services-common-2.1.jar", "flamingo-services-common-2.1.jar" libraries into lib directory.

    Tip

    If you use Maven for dependency management add next lines into yours pom.xml file:
    
    
        <dependency>
            <groupId>com.exadel.flamingo.java</groupId>
            <artifactId>flamingo-java-client</artifactId>
            <version>2.1</version>
        </dependency>
  • Initialize Flamingo before usage in application.

    
    String  serverComponentURL = "http://hostname:8080/mywebapp/resource/hessian";
        ServiceFactory.initialize(serverComponentURL);

    where

    "http://hostname:8080/mywebapp/resource/hessian" is URL where remote services deployed. Please note that the URL is relative, so the application will contact http://[origin server]/[application context]/Flamingo servlet]/.
  • When Flamigo properly initialized you can use server side components. Following sample display access to Seam authentication mechanism via ExpressionService:

    
    ExpressionService service = ServiceFactory.getInstance().getService(ExpressionService.class, ExpressionService.NAME);
        Class[] types = new Class[]{String.class};
        Object[] parameters = new Object[]{"John"};
        String result = String.valueOf(service.invokeMethod("serverAction.sayHello", String.class, types, parameters));

For more information about usage Flamingo with Android can be obtained from the examples of the use (examples/expression/swing).

3.3. Spring

Here a necessary sequence of operations to use Exadel Flamingo with Spring is described. This chapter also describes how to create a simple HelloWord application using Adobe Flex and JavaFX technologies.

3.3.1. Server configuration

This section of the guide will discuss necessary server configuration for Spring.

3.3.1.1. Using Servlets

  • Copy files

    "flamingo-service-spring-2.1.jar", "amf-serializer-2.1.jar" (for AMF) and/or "hessian-3.1.3.jar"(for Hessian) into the "WEB-INF/lib" folder of your application.

Tip

File "hessian-3.1.3.jar" is available at

http://hessian.caucho.com/#Java

  • Add the following content into the "web.xml" file of your application if you use AMF:

    
    
    <servlet>
        <servlet-name>AMF Remote Servlet</servlet-name>
        <servlet-class>com.exadel.flamingo.service.spring.AMFToSpringServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>AMF Remote Servlet</servlet-name>
        <url-pattern>/flamingo/amf/*</url-pattern>
    </servlet-mapping>

This parameter shows that calls coming to URL "[server_name]/[context]/flamingo/amf/*" will be passed to the com.exadel.flamingo.service.spring.AMFToSpringServlet servlet.

Thus, using URL "/flamingo/amf/" you can call the servlet to access the Spring beans via the AMF protocol.

  • OR this content if you use the Hessian protocol:

    
    
    <servlet>
        <servlet-name>Hessian Remote Servlet</servlet-name>
        <servlet-class>com.exadel.flamingo.service.spring.HessianToSpringServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Hessian Remote Servlet</servlet-name>
        <url-pattern>/flamingo/hessian/*</url-pattern>
    </servlet-mapping>

This parameter shows that calls coming to URL "[server_name]/[context]/flamingo/hessian/*" will be passed to the com.exadel.flamingo.service.spring.HessianToSpringServlet servlet.

Thus, using URL "/flamingo/hessian/ " you can call the servlet to access the Seam components via the Hessian protocol.

Tip

See also Chapter 6: Supported Communication Protocols to find more information about both Protocols and their configuration.

3.3.2. Spring Service Bean Creation

Now we will create a Spring bean to implement a simple HelloWorld demo:

Tip

If you want to create a new application from scratch see Chapter 4: Create New Flamingo Project for more details.

  • Add a new file "HelloService.java" to the existing Spring application:


package com.flamingo.sample;

public class HelloService {
    public String hello(String name) {
        return "Hello, " + name;
    }
}

The method of this class accepts a string sent from the client as an argument, and it is forwarded then back with string Hello, added at the beginning.

 

  • Add bean definition to application context file:


<beans>
    <bean id="helloService" class="com.flamingo.sample.HelloService"/>
</beans>

Note

Using Flamingo you can call not only singleton beans but stateful beans as with the session scope.

Note

As of December 2009 Spring 2.5 did not work correctly in JBoss5, so if you use them together, you should use Spring 3 instead of Spring 2.5. Otherwise you may get strange error about of the impossibility to obtain the component through the Spring in JBoss VFS

3.3.3. Flex Client

Please add the client source code according to the protocol you use: AMF or Hessian. Fore more information about these protocols and their benefits please see Chapter 6: Supported Communication Protocols.

3.3.3.1. AMF

  • Create file "client.mxml" with the following content:

    
    
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:word="*">            
    <mx:RemoteObject id="service" destination="<property>helloService</property>"/>             
    <mx:Panel title="Flamingo AMF Spring Hello World Sample"
    paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">           
    <mx:HBox>
       <mx:Label text='Whom do you want to say "Hello" to?'/>           
       <mx:TextInput id="who"/>             
       <mx:Button label="Say Hello" click="<property>service.hello(who.text)</property>"/>
    </mx:HBox>            
    <mx:Label text='The server said: "{service.hello.lastResult}"'/>             
    </mx:Panel>
    </mx:Application>

    This sample shows how the hello method of server Spring bean helloService can be called using "RemoteObject" when clicking on the "Say Hello" button.

  • Create file "services-config.xml" with the following content:

    
    
    <services-config>
    <services>
    <service
       id="hello-service"
       class="flex.messaging.services.RemotingService"
       messageTypes="flex.messaging.messages.RemotingMessage">
       <destination id="<property>helloService</property>">
          <channels>
             <channel ref="spring-amf"/>
             </channels>
         </destination>
      </service>
    </services>
    <channels>
       <channel-definition id="spring-amf" class="mx.messaging.channels.AMFChannel">
       <endpoint
       uri="http://<property>{server.name}:{server.port}</property>/<property>helloworld</property>/<property>flamingo/amf</property>"
       class="flex.messaging.endpoints.AMFEndpoint"/>
       </channel-definition>
    </channels>          
    </services-config>

    This sample shows that channel spring-amf with a set url to access the server is applied in order to use destination helloService;

where

"{server.name}:{server.port}" is server name:server port where your existing JBoss Seam application is running;

"helloworld" is the name of your existing application;

"helloService" is the name of the bean specified in the application context file or via annotation like @Component(“helloService”) added to the HelloService class;

"flamingo/amf/" is the standard configuration while using servlets. If you use another mapping for HessianToSpringServlet, use the path specified in the "web.xml" file of your application for destination.

  • Compile an .swf file using parameter "mxmlc -output client.swf -compiler.services services-config.xml client.mxml" in the command line.

Note

Make sure, that the version of Flex2 compiler you use is not earlier than 2.0.1 Hotfix2: run "mxmlc -version" in the command line and verify that the build number is equal or higher than 166910.

  • Deploy your "client.swf" file into your server application.

  • Type in your browser the url to the .swf file: "http://localhost:8080/helloworld/client.swf"

  • Enjoy the results!

HelloWorld.swf

Figure 3.4. HelloWorld.swf


3.3.3.2. Hessian

  • Create file "client.mxml" with the following content:


...
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:word="*">         
<hessian:HessianService xmlns:hessian="hessian.mxml.*"
id="service" destination="flamingo/hessian/<property>helloService</property>"/>        
<mx:Panel title="Flamingo Spring Hessian Hello World Sample"
paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">       
<mx:HBox>
   <mx:Label text='Whom do you want to say "Hello" to?'/>       
   <mx:TextInput id="who"/>        
   <mx:Button label="Say Hello" click="service.hello.send(who.text)"/>
</mx:HBox>         
<mx:Label text='The server said: "{service.hello.lastResult}"'/>        
</mx:Panel>
</mx:Application>
...

This sample shows how the hello method of server Spring service bean helloService can be called using "HessianService" when clicking on the "Say Hello" button,

where

"helloService" is the name of the component specified in the application context file;

"flamingo/hessian/" is the standard servlet configuration. Please note that the URL is relative, so the application will contact http://[origin server]/[application context]/[path to Spring/Flamingo servlet]/helloService as a service. If you use another mapping for HessianToSpringServlet use the path specified in the "web.xml" file of your application in the URL pattern.

  • Compile an .swf file using parameter "mxmlc -output client.swf -library-path+=hessian-flash-3.1.2.swc client.mxml" in the command line.

Note

  • Make sure, that the version of Flex2 compiler you use is not earlier than 2.0.1 Hotfix2: run "mxmlc -version" in the command line and verify that the build number is equal or higher than 166910.

  • File "hessian-flash-3.1.2.swc" is available at

    http://hessian.caucho.com/#Flash/Flex.

  • Deploy your "client.swf" file into your server application.

  • Type in your browser the url to the .swf file: "http://localhost:8080/helloworld/client.swf".

  • Enjoy the results!

HelloWorld.swf

Figure 3.5. HelloWorld.swf


3.3.4. JavaFX Client

In this release only one method of communication between JavaFX client application and Spring is supported: via the Hessian protocol. Fore more information about this protocol and its benefits please see Chapter 6: Supported Communication Protocols.

Example of creation of a simple HelloWorld demo on JavaFX on Spring is similar to the one described for JavaFX/Seam. Just use helloService Spring Service bean instead of helloAction Seam component on the client side and change the corresponding URL. A working example can be found in the Samples folder of the Exadel Flamingo bundle.

3.3.5. Java ME

Example of creation of a simple application on Java ME and Spring is similar to the one described for Java ME/Seam.

3.3.6. Android

Example of creation of a simple application on Android and Spring is similar to the one described for Android/Seam. Just use correct URL on Flamingo Servlet and call via ExpressionService another Spring Service bean instead Seam.

3.3.7. Java Widget Toolkits

Supported Java Widget Toolkits

3.3.7.1. Swing

Example of creation of a simple application on Swing and Spring is similar to the one described for Swing/Seam.