J 2 MEでWebService続一を訪ねる


前回の例に引き続きJ 2 MEエンドコードの作成を完了する. 
 
私たちのMIDletはRunnableインタフェースを継承しています  だからプログラムの中で別のスレッドを使ってWebServicesにアクセスすることができます.(この点は重要です.一部の携帯電話はいくつかの操作を実現する時にマルチスレッドで実行しなければなりません.少なくとも私のソエはそうです.例えば、WMAを呼び出してメールを送受信したり、携帯電話のカメラを開けたりします...)
 
WebServiceにアクセスするためのインタフェースと実装クラスを作成します.
 
package com.joey.j2me.service;

public interface UserService extends java.rmi.Remote {
    public java.lang.String request(java.lang.String username,java.lang.String password)
        throws java.rmi.RemoteException;
}

 
非常に簡単なインタフェースで、実装クラスを作成します.
 
package com.joey.j2me.service.impl;

import javax.microedition.xml.rpc.ComplexType;
import javax.microedition.xml.rpc.Element;
import javax.microedition.xml.rpc.Operation;
import javax.microedition.xml.rpc.Type;

import javax.xml.namespace.QName;
import javax.xml.rpc.JAXRPCException;

import com.joey.j2me.service.UserService;


public class UserServiceBean implements UserService, javax.xml.rpc.Stub {

    protected static final QName _qname_UserName = new QName("", "username");
    protected static final QName _qname_PassWord = new QName("", "password");
    protected static final QName _qname_result = new QName("", "returnText");
    protected static final QName _qname_request = new QName("http://cxf.joey.com/", "login");
    protected static final QName _qname_requestResponse = new QName("http://cxf.joey.com/", "loginResponse");
    protected static final QName _qname_wsdl_request = new QName("http://cxf.joey.com/", "login");
    protected static final Element  _login;
    protected static final Element  _loginResponse;

    static {
        Element username =  new Element(_qname_UserName, Type.STRING, 1, 1, true);
        Element password =  new Element(_qname_PassWord, Type.STRING, 1, 1, true);

        ComplexType _complexType_request;
        _complexType_request = new ComplexType();
        _complexType_request.elements = new Element[2];
        _complexType_request.elements[0] = username;
        _complexType_request.elements[1] = password;
        _login = new Element(_qname_request, _complexType_request);
        

        Element _ReturnText = new Element(_qname_result, Type.STRING, 1, 1, true);
        ComplexType _complexType_requestResponse;
        _complexType_requestResponse = new ComplexType();
        _complexType_requestResponse.elements = new Element[1];
        _complexType_requestResponse.elements[0] = _ReturnText;
        _loginResponse = new Element(_qname_requestResponse, _complexType_requestResponse);
    }

    private String[] _propertyNames;
    private Object[] _propertyValues;

    public UserServiceBean () {
        _propertyNames = new String[] { ENDPOINT_ADDRESS_PROPERTY };
        _propertyValues = new Object[] { "http://localhost:8080/UserManager" };
    }

    public void _setProperty(String name, Object value) {
        System.out.println("_setProperty");
        int size = _propertyNames.length;
        for (int i = 0; i < size; ++i) {
            if (_propertyNames[i].equals(name)) {
                _propertyValues[i] = value;
                return;
            }
        }


        String[] newPropNames = new String[size + 1];
        System.arraycopy(_propertyNames, 0, newPropNames, 0, size);
        _propertyNames = newPropNames;

        Object[] newPropValues = new Object[size + 1];
        System.arraycopy(_propertyValues, 0, newPropValues, 0, size);
        _propertyValues = newPropValues;
        _propertyNames[size] = name;
        _propertyValues[size] = value;
    }

    public Object _getProperty(String name) {
        System.out.println("_getProperty");
        for (int i = 0; i < _propertyNames.length; ++i) {
            if (_propertyNames[i].equals(name)) {
                return _propertyValues[i];
            }
        }

        if (ENDPOINT_ADDRESS_PROPERTY.equals(name) || USERNAME_PROPERTY.equals(name) ||
                PASSWORD_PROPERTY.equals(name)) {
            return null;
        }

        if (SESSION_MAINTAIN_PROPERTY.equals(name)) {
            return new java.lang.Boolean(false);
        }

        throw new JAXRPCException("Stub does not recognize property: " + name);
    }

    protected void _prepOperation(Operation op) {
        for (int i = 0; i < _propertyNames.length; ++i) {
            op.setProperty(_propertyNames[i], _propertyValues[i].toString());
        }
    }

    public java.lang.String login(java.lang.String username,java.lang.String password)
        throws java.rmi.RemoteException {
        Object[] inputObject = new Object[2];
        inputObject[0] = username;
        inputObject[1] = password;
        Operation op = Operation.newInstance(_qname_wsdl_request, _login, _loginResponse);
        _prepOperation(op);
        op.setProperty(Operation.SOAPACTION_URI_PROPERTY, "");
        Object resultObj;
        try {
            resultObj = op.invoke(inputObject);
        } catch (JAXRPCException e) {
            Throwable cause = e.getLinkedCause();
            if (cause instanceof java.rmi.RemoteException) {
                throw (java.rmi.RemoteException)cause;
            }
            throw e;
        }
        java.lang.String result;
        Object resultObj2 = ((Object[])resultObj)[0];
        result = (java.lang.String)resultObj2;
        return result;
    }
}

 
最後にMIdletのrunメソッドでこのloginメソッドを呼び出せばよい.
 
未完...