オブジェクトメタデータの例

ORBコンパイラおよびランタイムは、Javaオブジェクトを値で渡すための標準的なサポートを備えています。リモートオブジェクトへの引数またはリモートオブジェクトからの戻り値は、SerializableのJavaタイプです。これには、Javaの初期タイプ、およびjava.io.Serializableインタフェースを実装するJavaオブジェクトが含まれます。

この例では、メタデータオブジェクトのリモートオブジェクトを指定します。メタオブジェクトのリモートインタフェースを使用すると、プロパティとして任意のSerializableのjava.lang.Objectを設定/取得できます。

1 Componentインタフェース

package metaData;
                                                                           
public interface Component extends java.rmi.Remote
{
    MetaData getMetaData() throws java.rmi.RemoteException;
}

2 MetaDataインタフェース

package metaData;
                                                                           
import java.util.Enumeration;
import java.rmi.RemoteException;
                                                                           
/**
   The MetaData emulates the java.util.Dictionary interface.
 */
public interface MetaData extends java.rmi.Remote
{
    // returns the number of keys
    int size() throws RemoteException;
                                                                           
    // tests if there is currently no keys
    boolean isEmpty() throws RemoteException;
                                                                           
    // get the value for the given key
    Object get(Object key) throws RemoteException;
                                                                           
    // put the value with the given key
    Object put(Object key, Object value) throws RemoteException;
                                                                           
    // remove the key and its corresponding value
    Object remove(Object key) throws RemoteException;
                                                                           
    // get the enumeration of keys
    Enumeration keys() throws RemoteException;
                                                                           
    // get the enumeration of values
    Enumeration elements() throws RemoteException;
}
いくつかのMetaDataメソッドには、署名にプレーン形式のjava.lang.Objectがあります。プログラマはランタイムにjava.io.Serializableオブジェクトを渡す必要があります。 それ以外の場合、ORBはクラスキャストまたはマーシャル例外を発生させます。 もちろん、署名タイプを特定することもできます。たとえば、上のkeyを常にStringに制限することもできます。

3 Componentの実装

package metaData;
                                                                           
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
                                                                           
public class ComponentImpl extends PortableRemoteObject implements Component
{
    private MetaData _metaData;
                                                                           
    ComponentImpl() throws RemoteException
    {
    |   _metaData = new MetaDataImpl();
    }
                                                                           
    public MetaData getMetaData() throws RemoteException
    {
    |   return _metaData;
    }
}

4 MetaDataの実装

MetaDataの実装には、標準のjava.util.Hashtableを使用します。ハッシュテーブルのエミュレータはSerializableでないため、固有のバージョンを作成する必要があります。
package metaData;
                                                                           
import java.io.Serializable;
                                                                           
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
                                                                           
import java.util.Hashtable;
import java.util.Enumeration;
                                                                           
public class MetaDataImpl extends PortableRemoteObject implements MetaData
{
    private Hashtable _metaData;
                                                                           
    MetaDataImpl() throws RemoteException
    {
    |   _metaData = new Hashtable();
    }
                                                                           
    public int size() throws RemoteException
    {
    |   return _metaData.size();
    }
                                                                           
    public boolean isEmpty() throws RemoteException
    {
    |   return _metaData.isEmpty();
    }
                                                                           
    public Object get(Object key) throws RemoteException
    {
    |   return _metaData.get(key);
    }
                                                                           
    public Object put(Object key, Object value) throws RemoteException
    {
    |   return _metaData.put(key, value);
    }
                                                                           
    public Object remove(Object key) throws RemoteException
    {
    |   return _metaData.remove(key);
    }
                                                                           
    public Enumeration keys() throws RemoteException
    {
    |   synchronized (_metaData) {
    |   |   return new MetaDataEnumerator(_metaData.keys(), size());
    |   }
    }
                                                                           
    public Enumeration keys() throws RemoteException
    {
    |   synchronized (_metaData) {
    |   |   return new MetaDataEnumerator(_metaData.elements(), size());
    |   }
    }
}
                                                                           
class MetaDataEnumerator implements Enumeration, Serializable
{
    private int      _cursor;
    private Object[] _elements;
                                                                           
    MetaDataEnumerator(Enumeration e, int size)
    {
    |   _elements = new Object[size];
    |                                                                      
    |   int i=0;
    |   while (e.hasMoreElements()) {
    |   |   _elements[i++] = e.nextElement();
    |   }
    |                                                                      
    |   _cursor = 0;
    }
                                                                           
    public boolean hasMoreElements()
    {
    |   return _cursor != _elements.length;
    }
                                                                           
    public Object nextElement()
    {
    |   return _elements[_cursor++];
    }
}

5 MetaDataサーバ

サーバはORBを作成し、Java RMIサーバントを作成し、その文字列化されたオブジェクト参照を書き出し、呼び出しを待機します。HelloImplはCORBAオブジェクトではないため(org.omg.CORBA.Objectを実装しません)、object_to_stringメソッドを使用する前にまずスタブ/オブジェクト参照を取得する必要があります。
package metaData;
                                                                           
import util.Util;
                                                                           
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
                                                                           
import javax.rmi.PortableRemoteObject;
                                                                           
public class Server
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Component component = new ComponentImpl();
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String ior = orb.object_to_string((Object) PortableRemoteObject.
    |   |       toStub(component));
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   Util.writeIOR(ior, "ior", true);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

6 MetaDataクライアント

クライアントはORBを作成し、Componentオブジェクトの文字列化されたオブジェクト参照を読み取り、Componentタイプにそれをキャストします。次に、ComponentからMetaDataインタフェースを取得し、それにメタデータを設定し、取得します。

java.lang.Stringおよびjava.util.Vectorは、すでにSerializableであるJDKクラスです。Enumerationは、上のセクションで作成したSerializableオブジェクトです。

package metaData;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
import java.util.Vector;
import java.util.Enumeration;
import javax.rmi.PortableRemoteObject;
                                                                           
public class Client
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // read the stringified object reference
    |   |   String ior = Util.readIOR("ior");
    |   |                                                                  
    |   |   // narrow the stringified object
    |   |   Component component = (Component) orb.string_to_object(ior);
    |   |                                                                  
    |   |   // get the component's meta-data interface
    |   |   MetaData metaData = component.getMetaData();
    |   |                                                                  
    |   |   // add lifecycle and timeout meta data
    |   |   metaData.put("lifecycle", "transient");
    |   |   metaData.put("timeout", new Integer(5000));
    |   |                                                                  
    |   |   // set the access control list (ACL)
    |   |   Vector acl = new Vector();
    |   |   acl.addElement("user1");
    |   |   acl.addElement("user2");
    |   |   metaData.put("acl", acl);
    |   |                                                                  
    |   |   // enumerate the meta data keys
    |   |   System.out.print("Component Keys: ");
    |   |   Enumeration keys = metaData.keys();
    |   |   while (keys.hasMoreElements()) {
    |   |   |   System.out.print(keys.nextElement() + ", ");
    |   |   }
    |   |   System.out.println();
    |   |                                                                  
    |   |   // print out the Component ACL
    |   |   System.out.print("Component ACL : ");
    |   |   acl = (Vector) metaData.get("acl");
    |   |   for (int i=0; i < acl.size(); i++) {
    |   |   |   System.out.print(acl.elementAt(i) + ((i==(acl.size()-1)) ?
    |   |   |       "\n" : ", "));
    |   |   }
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc.All rights reserved.