Worklight アダプターは、Worklight モバイル・アプリケーション・プラットフォーム上にデプロイされ、このプラットフォームによってサービスが提供されるアプリケーションのサーバー・サイド・コードです。アダプターは Worklight Server をエンタープライズ・アプリケーション (WebSphere Message Broker で実行されるアプリケーションなど) に接続します。以下の手順は、モバイル・アプリケーションと、Message Broker 内で稼働している Microsoft .NET Web サービスの間の標準的なデータ・フローを概説しています。
WL.Client.invokeProcedure
) を発行して、プロシージャーを呼び出します。プロシージャーのパラメーターが、JSON 形式のデータとして渡されます。このパターンは、.NET クラス上で選択された各メソッドを呼び出すことができる Worklight アダプターを生成します。このアダプターは、モバイル・アプリケーションからの JSON 要求を、Microsoft .NET クラスに対する Web サービス要求に変換します。例えば、RetailBank と呼ばれるクラス内に以下の C# メソッドがあり、MyBank と呼ばれるパターン・インスタンス用にこれらのメソッドを選択しているとします。
public static string GetBalance(string accountNumber, out string lastUpdated); public static string TransferMoney(string sourceAccountNumber, string targetAccountNumber, string value); public static string FindMissingAccount(string name, ref string lastKnownAccountNumber);
以下の JavaScript の断片は、モバイル・アプリケーションが GetBalance
メソッドを呼び出す方法を示しています。
var invocationData = { adapter : "MyBank", procedure : "GetBalance", parameters : [ accountNo ]}; var options = { onSuccess:lang.hitch(this, this.onResult), onFailure:lang.hitch(this, this.onError)}; WL.Client.invokeProcedure(invocationData, options);
テストするために、Web ブラウザーを使用して以下の URL を指定し、デプロイされた Worklight アダプターを呼び出すことができます。
http://localhost:8080/dev/invoke?adapter=MyBank&procedure=GetBalance¶meters=%5B123456789%5D
このセクションでは、Worklight アダプター用に生成されるファイルについて説明します。
このパターンは 2 つの構成ファイルを使用して Worklight アダプターを作成します。1 つ目の構成ファイル (MyBank.xml) はアダプター XML ファイルです。アダプター XML ファイルは、Message Broker への接続を構成するため、またアダプターがモバイル・アプリケーションや他のアダプターに公開するプロシージャーを宣言するために使用されます。以下の例は、生成されるアダプター XML 構成ファイルを示します。
<?xml version="1.0" encoding="UTF-8"?> <wl:adapter name="MyBank" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wl="http://www.worklight.com/integration" xmlns:http="http://www.worklight.com/integration/http"> <displayName>MyBank</displayName> <description>Worklight integration adapter for the Microsoft .NET service</description> <connectivity> <connectionPolicy xsi:type="http:HTTPConnectionPolicyType"> <protocol>http</protocol> <domain>localhost</domain> <port>7800</port> </connectionPolicy> <loadConstraints maxConcurrentConnectionsPerNode="99" /> </connectivity> <procedure name="GetBalance"/> <procedure name="TransferMoney"/> <procedure name="FindMissingAccount"/> </wl:adapter>
このアダプター XML ファイル内の値の多くは、パターン・パラメーター (displayName
、domain
、port
など) から構成されます。また、.NET メソッドごとに procedure
が構成されています。プロシージャーは、Microsoft .NET Web サービスによって公開される操作にアクセスするプロセスを定義します。Web サービス内の操作ごとに procedure
が 1 つずつあります。
このパターンは、プロシージャーごとの実装関数を含む JavaScript ファイル (MyBank-impl.js) も生成します。
var targetNamespace = "urn://bankingapplication/retailbank_V1"; function PrepareWebService(method, parameters, path) { var request = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="' + targetNamespace + '">' + '<soap:Header/>' + '<soap:Body>' + '<tns:' + method + '>'; for (var key in parameters) { request += '<' + key + '>' + parameters[key] + '</' + key + '>'; } request += '</tns:' + method + '>' + '</soap:Body>' + '</soap:Envelope>'; var input = { method : 'post', returnedContentType : 'xml', path : path, body : { contentType: 'application/xml; charset=utf-8', content: request } }; return input; } var targetPath = "/retailbank"; function sendRequest(name, input) { var response = WL.Server.invokeHttp(input); WL.Logger.log(response); if (response.Envelope) { var data = response.Envelope.Body; data = data[name]; response.result = data; delete response.Envelope; WL.Logger.log(response); } return response; } function GetBalance(accountNumber) { var parameters = new Object(); parameters['accountNumber'] = accountNumber; var input = PrepareWebService('GetBalance', parameters, targetPath); WL.Logger.debug('SOAP request: ' + input.body.content); return sendRequest('GetBalanceResponse', input); } function TransferMoney(sourceAccountNumber, targetAccountNumber, value) { var parameters = new Object(); parameters['sourceAccountNumber'] = sourceAccountNumber; parameters['targetAccountNumber'] = targetAccountNumber; parameters['value'] = value; var input = PrepareWebService('TransferMoney', parameters, targetPath); WL.Logger.debug('SOAP request: ' + input.body.content); return sendRequest('TransferMoneyResponse', input); } function FindMissingAccount(name, lastKnownAccountNumber) { var parameters = new Object(); parameters['name'] = name; parameters['lastKnownAccountNumber'] = lastKnownAccountNumber; var input = PrepareWebService('FindMissingAccount', parameters, targetPath); WL.Logger.debug('SOAP request: ' + input.body.content); return sendRequest('FindMissingAccountResponse', input); }
生成された JavaScript コードは、JSON 要求で提供されたパラメーターを XML SOAP メッセージに変換します。JSON から SOAP への変換は、PrepareWebService 関数で実装されます。SOAP 要求は Worklight に渡され、WL.Server.invokeHttp
関数で Microsoft .NET Web サービスが呼び出されます。
Microsoft .NET Web サービスによって実装された GetBalance
操作の呼び出しのための XML SOAP 要求メッセージは、口座番号が 123456789 の場合に以下のようになります。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn://bankingapplication/retailbank_V1"> <soap:Header/> <soap:Body> <tns:GetBalance> <accountNumber>123456789</accountNumber> </tns:GetBalance> </soap:Body> </soap:Envelope>
以下のように、Worklight Server は Microsoft .NET Web サービスからの XML SOAP 応答を、元のモバイル・アプリケーション用に JSON に自動的に変換します。
{ "errors": [], "info": [], "isSuccessful": true, "result": { "NS1": "urn://bankingapplication/retailbank_V1", "lastUpdated": "20/06/2012 11:46:52", "returnValue": "1000.00" }, "statusCode": 200, "statusReason": "OK", "warnings": [] }