ナビゲーションをスキップ

WebLogic Web サービス プログラマーズ ガイド

  前 次 前/次ボタンと目次ボタンとの区切り線 目次  

WebLogic Web サービス API の使い方

この章では、WebLogic Web サービス API の使い方に関する情報を提供します。

 


WebLogic Web サービス API の概要

WebLogic Web サービスには、JAX-RPC API と SAAJ API のほかにも、さまざまなタスクに使用できる API が用意されています。通常、これらの API はクライアント アプリケーションで使用しますが、EJB などのサーバサイド コンポーネントで使用できる場合もあります。

各節では、WebLogic Web サービス API の主な使い方について説明します。API の詳細なリファレンス情報については、Javadoc を参照してください。

次の表に、WebLogic Web サービス API と、その主な使い方を示します。

表 8-1 WebLogic Web サービス API の概要

パッケージ名

一般的な使い方

weblogic.webservice.async

クライアント アプリケーションで使用し、オペレーションを非同期に呼び出す。非同期化は、呼び出しを 2 つのメソッドに分割することで行われる。最初のメソッドは、必要なパラメータを指定してオペレーションを呼び出すために使用する。このメソッドでは結果を待たず、後に 2 番目のメソッドで実際の結果を返す。

非同期クライアント アプリケーションの記述」を参照。

weblogic.webservice.binding

クライアント アプリケーションで使用し、プログラムによって verbose モードと文字セットを設定する。

プログラムによる verbose モードの設定」および「Web サービス呼び出し時の文字セットの設定」を参照。

weblogic.webservice.client

クライアント アプリケーションで使用し、SSL をコンフィグレーションする。 「クライアント アプリケーションに対して SSL をコンフィグレーションする」を参照。

weblogic.webservice.context

WebLogic Web サービス オペレーションの実装内から、HttpSession 情報にアクセスするために使用する。 「Web サービス コンポーネントからの HttpSession 情報へのアクセス」を参照。

weblogic.webservice.encoding

動的クライアント アプリケーションで使用し、Web サービス オペレーションの呼び出しでパラメータまたは戻り値に非組み込みデータ型が使用されている場合に、そのデータ型のマッピング情報を登録する。

動的クライアントでのデータ型マッピング情報の登録」を参照。

weblogic.webservice.extensions

クライアント アプリケーションで使用し、呼び出された Web サービスの WSDL を参照する。

クライアント アプリケーションでの WSDL の参照」を参照。

 


動的クライアントでのデータ型マッピング情報の登録

clientgen Ant タスクで生成した JAR ファイルのスタブには、Web サービス オペレーションのパラメータまたは戻り値として使用される非組み込みデータ型のマッピングと登録をクライアント アプリケーションが処理するために必要な Java コードが含まれています。しかし、Web サービスを呼び出す動的クライアントを記述する場合、動的クライアントでは clientgen によって生成されたスタブが使用されないため、開発者自身がデータ型のマッピングと登録を行う必要があります。

データ型のマッピングを登録する標準的な方法は、JAX-RPC TypeMappingRegistry クラスを使用することです。シリアライゼーション クラスは常に手動で登録する必要がありますが、シリアライゼーション クラスの一部は WebLogic の内部クラスであるため、このクラスは使いづらいという難点があります。次の例では、TypeMappingRegistry クラスの使い方を示します。該当するコード箇所は太字になっています。

package examples.jaxrpc.call3;
import javax.xml.soap.SOAPConstants;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Service;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import org.soapinterop.xsd.SOAPStructCodec;
import org.soapinterop.xsd.SOAPStruct;
public class MSInterop{
public static void main( String[] args ) throws Exception{
    // weblogic ServiceFactory を設定
System.setProperty( "javax.xml.rpc.ServiceFactory",
"weblogic.webservice.core.rpc.ServiceFactoryImpl" );
    // サービス ファクトリを作成
ServiceFactory factory = ServiceFactory.newInstance();
    // QName を定義
String targetNamespace = "http://soapinterop.org/";
    QName serviceName = new QName( targetNamespace, "SimpleTest" );
QName portName = new QName( targetNamespace, "SimpleTestSoap" );
    QName operationName = new QName( "http://soapinterop.org/",
"echoStruct" );
    // サービスを作成
Service service = factory.createService( serviceName );
    TypeMappingRegistry registry = service.getTypeMappingRegistry();
    TypeMapping mapping = registry.getTypeMapping(
SOAPConstants.URI_NS_SOAP_ENCODING );
    mapping.register( SOAPStruct.class,
new QName( "http://soapinterop.org/xsd", "SOAPStruct" ),
new SOAPStructCodec(),
new SOAPStructCodec() );
    // 呼び出しを作成
Call call = service.createCall();
    // ポートとオペレーション名を設定
call.setPortTypeName( portName );
call.setOperationName( operationName );
    call.addParameter( "inputStruct",
new QName( "http://soapinterop.org/xsd", "SOAPStruct" ),
ParameterMode.IN);
    call.setReturnType(
new QName( "http://soapinterop.org/xsd", "SOAPStruct" ) );
    // エンド ポイント アドレスを設定
call.setTargetEndpointAddress(
"http://www.mssoapinterop.org/asmx/simple.asmx" );
    SOAPStruct s = new SOAPStruct();
s.setVarInt(2);
s.setVarString("foo");
s.setVarFloat(123123);
System.out.println(s.toString());
    SOAPStruct res = (SOAPStruct) call.invoke(new Object[]{s} );
    System.out.println( res );
}
}

WebLogic Web サービスには、動的クライアントで非組み込みデータ型のマッピング登録を簡単に実行できる「weblogic.webservice.encoding」パッケージが用意されています。この API には、以下の 2 つのメイン クラスが定義されています。

次の手順では、動的クライアント アプリケーションでの DefaultTypeMapping クラスの使い方について説明します。

  1. autotype Ant タスクを実行して、シリアライゼーション クラス、Java クラス、XML スキーマ、および Web サービスで使用する非組み込みデータ型用の types.xml ファイルを作成します。この例では、types.xml ファイルを mydir ディレクトリに作成します。
  2. 詳細については、「autotype Ant タスクを実行する」を参照してください。

  3. クライアント アプリケーションで、必要な WebLogic Web サービス API パッケージをインポートします。
  4. import weblogic.webservice.encoding.GenericTypeMapping;
    import weblogic.webservice.encoding.DefaultTypeMapping;
  5. JAX-RPC TypeMappingRegistry オブジェクトのインスタンスを作成します。
  6. TypeMappingRegistry registry = service.getTypeMappingRegistry();
  7. 次の抜粋に示すように、DefaultTypeMapping クラスを使用して、autotype タスクによって生成された types.xml ファイルを登録します。
  8.    registry.registerDefault(
    new DefaultTypeMapping( "mydir/types.xml" ) );

 


Web サービス コンポーネントからの HttpSession 情報へのアクセス

weblogic.webservice.context.WebServiceSession」クラスを使用して、HTTP セッションを管理する WebLogic Server 内部のインフラストラクチャにアクセスします。WebLogic Web サービスはサーブレットではなく Java クラスおよび EJB により実装されるため、その HTTP セッション情報を直接的に利用することはできません。そのため、weblogic.webservice.context パッケージを使用してアクセスする必要があります。WebServiceSession の使い方については、「javax.servlet.http.HttpSession」の Javadoc を参照してください。

Web サービスの実装で、WebServiceContext オブジェクトから WebServiceSession オブジェクトを取得し、次に、標準の getAttribute() メソッドおよび setAttribute() メソッドを使用して、セッションの属性を取得および設定します。

注意 :このセッションが正しく動作するには、クライアント アプリケーション側で HTTP クッキーがサポートされている必要があります。

次の例では、weblogic.webservice.context API を使用している Web サービスを実装する Java クラスのメソッドを示します。このメソッドは、Web サービス オペレーションの複数の呼び出し間でセッション ステートを維持します。

import weblogic.webservice.context.WebServiceContext;
import weblogic.webservice.context.ContextNotFoundException;
import weblogic.webservice.context.WebServiceSession;
....
 /*
* 複数の呼び出し間でセッション ステートを維持する
* HTTP セッションの使い方を示す
*/
public int maintainSessionState(){
try{
WebServiceContext wsContext = WebServiceContext.currentContext();
WebServiceSession session = (WebServiceSession)wsContext.getSession();
      Integer count = (Integer)session.getAttribute( "count" );
      count = (count==null) ?
new Integer( 0 ) : new Integer( count.intValue() + 1 );
      session.setAttribute( "count", count );
return count.intValue();
}catch( ContextNotFoundException e ){
e.printStackTrace();
return -1;
}
}

 


クライアント アプリケーションでの WSDL の参照

呼び出し対象となる Web サービスの WSDL を参照するには、動的クライアント アプリケーションで「weblogic.webservice.extensions」パッケージを使用します。特に、WLCall インタフェースは javax.xml.rpc.Call インタフェースを拡張することで、以下の機能が追加されています。

次の例では、weblogic.webservice.extensions.WLCall インタフェースを使用して、呼び出し対象であるオペレーションのパラメータの詳細を、クライアント アプリケーションで取得する方法を示します。

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.Call;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import weblogic.webservice.encoding.GenericTypeMapping;
import weblogic.webservice.extensions.WLCall;
public class BrowserClient{
  public void invoke() throws RemoteException, InvokeFailedException,
ServiceException, IOException{
    String url = "http://localhost:7001/mega/TemperatureService?WSDL";
    System.setProperty( "javax.xml.rpc.ServiceFactory",
"weblogic.webservice.core.rpc.ServiceFactoryImpl" );
    System.setProperty( "weblogic.webservice.servicenamechecking",
"false" );
    ServiceFactory factory = ServiceFactory.newInstance();
    QName serviceName = new QName( "http://www.bea.com/mega-service/",
"MegaWebService" );
    Service service = factory.createService( new URL( url ), serviceName );
    TypeMappingRegistry registry = service.getTypeMappingRegistry();
registry.registerDefault( new GenericTypeMapping() );
    System.out.println( "+ Service: " + service.getServiceName() );
    for( Iterator it = service.getPorts(); it.hasNext(); ){
QName portName = (QName)it.next();
System.out.println( " + Port: " + portName );
Call[] calls = service.getCalls( portName );
printCalls( calls );
}
}
  private void printCalls( Call[] calls ){
for( int i=0; i<calls.length; i++ ){
Call call = calls[i];
System.out.println( " + Operation :" + call.getOperationName() );
printParameters( (WLCall)call );
      if( call.getReturnType() != null ){
System.out.println( " + Return Type:" + call.getReturnType() );
}
      System.out.println( "" );
}
}
  private void printParameters( WLCall call ){
for( Iterator it = call.getParameterNames(); it.hasNext(); ){
String name = (String)it.next();
System.out.println( " + Part :" + name );
      System.out.println( "        - Java Type :" +
call.getParameterJavaType( name ) );
      System.out.println( "        - Mode :" +
call.getParameterMode( name ) );
      System.out.println( "        - XML Type :" +
call.getParameterTypeByName( name ) );
}
}
}

 

フッタのナビゲーションのスキップ  ページの先頭 前 次