Flex⇔Axis2との通信でObject(AS3)を使う

Axis2Flex間で、Objectを利用するためのサンプル。Objectは、Javaのほうのjava.lang.Objectじゃなくて、Flex側のObjectデータの送受信についてのメモ。Flex側は単に引数に{foo:"foo", bar:"bar"}を渡してあげるだけ。

Flex

var webService:WebService = new WebService();
webService.wsdl = "http://localhost:8080/WebServiceProject/services/Test?wsdl";
webService.loadWSDL();
webService.echoOption({foo:"foo", bar:"bar"});

Axis2

パラメータとしてOMElementを利用するのがポイント。サンプル・コードでは、

  1. OMElementを操作して、HashMapのkey、valueに変換してあげている。
  2. さらに、fooノードを追加した、OMElementを返している。
public OMElement echoOption(OMElement option)
{
    OMFactory factory = OMAbstractFactory.getOMFactory();
    HashMap<String, String> map = new HashMap<String, String>();

    // 1
    for(Iterator<OMElement> i=option.getChildElements();i.hasNext();)
    {
        OMElement element = i.next();
        map.put(element.getLocalName(), element.getText());
    };

    // 2
    OMElement child = factory.createOMElement("foo", null);
    child.setText("aaa");
    option.addChild(child);

    System.out.println(map.toString());
    
    return option;
};

そうすると、echoOptionの返り値をFlex側で良きにはからって次のような値にマッピングしてくれる。