デフォルトの名前空間で新規拡張エンティティー・タイプを作成するためのサンプル・コード

このサンプル・コード・スニペットおよびデータ・グラフは、デフォルトの名前空間で、組み込みエンティティー・タイプから拡張されたエンティティー・タイプを作成するために使用します。

このサンプル・コード・スニペットでは、以下の手順を実行します。

  1. 組み込みエンティティー・タイプ PersonAccount から拡張された新規エンティティー・タイプ PersonAccountExt のスキーマを、デフォルトの名前空間で作成します。
  2. PersonAccountExt エンティティー・タイプのエンティティー person1 を作成します。
  3. 新規プロパティー HomePhone を、新しく作成されたエンティティー・タイプ PersonAccountExt に追加し、その新規プロパティー HomePhone を LDAP 属性 primaryOwnerContact にマップします。
  4. HomePhone プロパティーを値と共に person1 エンティティーに追加します。
  5. HomePhone プロパティーが person1 エンティティーに追加されたことを確認します。

前提条件

プログラミングの前提条件」トピック (propertySchema および extensionPropertySchema データ・オブジェクトについての情報が記載され、プロパティーのデータ型についての有効な構文がリストされている「プロパティー・スキーマの拡張」セクションを含む) で説明されている情報を参照済みであること、またトピックの手順を実行済みであることを確認してください。

以下の import ステートメントもクラスに追加してください。
import com.ibm.websphere.wim.exception.SchemaAlreadyExistException;

統合リポジトリー構成で、LDAP リポジトリーが構成されていることを確認してください。詳しくは、WebSphere Application Server インフォメーション・センターの「フェデレーテッド・リポジトリー構成における Lightweight Directory Access Protocol の構成」トピックを参照してください。

サンプル・コード

次のコード・スニペットをアプリケーション・コードに追加し、変数 を実際に使用する値に置き換えます。

/**
 * This method uses the schema extension API to add a new entity
 * extending from an existing virtual member manager entity.
 */
public static void testRunTimeAddNewEntity() 
{
    // Create schema for new entity type
    addEntityPersonAccountExt();
    // Create entity of new entity type
    createPersonAccountExt();
    // Add a property 'HomePhone' to 'PersonAccountExt' and 
    // map it to 'primaryOwnerContact' attribute in LDAP
    addPropertyToPersonAccountExt();
    // Add 'HomePhone' to 'person1'
    addHomePhoneForPersonAccountExt();
    // Search for the person
    searchPersonAcctExt();
}

/**
 * Method to create the schema for 'PersonAccountExt' in the default namespace
 */
@SuppressWarnings("unchecked")
private static void addEntityPersonAccountExt() 
{
    try 
    {
        System.out.println("¥nCLIENT: Creating schema for 'PersonAccountExt' . . . ");
        DataObject root = SDOHelper.createRootDataObject();
        // Create a schema object under root
        DataObject dynaSchemaDO = root.createDataObject(SchemaConstants.DO_SCHEMA);
        // Create new entity type schema object. This object will hold the schema definition for the
        // 'PersonAccountExt' entity type that we want to create.
        DataObject entitySchemaDO = dynaSchemaDO.createDataObject(SchemaConstants.DO_ENTITY_SCHEMA);
        // Set the namespace for new entity type
        entitySchemaDO.setString(SchemaConstants.PROP_NS_URI, SchemaConstants.WIM_NS_URI);
        // Set the entity type name
        entitySchemaDO.set(SchemaConstants.PROP_ENTITY_NAME, "PersonAccountExt");
        // Set the parent entity type name.
        entitySchemaDO.set(SchemaConstants.PROP_PARENT_ENTITY_NAME, Service.DO_PERSON_ACCOUNT);
        // Create an entity configuration data object
        // This data object defines repository details
        DataObject entConfigDO = entitySchemaDO.createDataObject(SchemaConstants.DO_ENTITY_CONFIGURATION);
        // Set the default parent to the base entry of the configured LDAP.
        entConfigDO.set(SchemaConstants.PROP_DEFAULT_PARENT, "dc=yourco,dc=com");
        // Set the RDN (login) property to uid.
        entConfigDO.set(SchemaConstants.PROP_RDN_PROPERTY, "uid");
        // Set the repository id.
        DataObject reposConfigInfo1DO = entConfigDO.createDataObject(SchemaConstants.DO_META_DATA);
        reposConfigInfo1DO.set(SchemaConstants.PROP_NAME, ConfigService.CONFIG_DO_OBJECTCLASSES);
        reposConfigInfo1DO.set(SchemaConstants.PROP_REPOSITORY_ID, "LDAP1");
        // Set the object classes for the added object. These classes are from the LDAP schema.
        List objReadValues = reposConfigInfo1DO.getList(SchemaConstants.PROP_VALUES);
        objReadValues.add("inetorgperson");
        objReadValues.add("organizationalperson");
        objReadValues.add("person");
        // Set the RDN property for the repository
        DataObject reposConfigInfo3DO = entConfigDO.createDataObject(SchemaConstants.DO_META_DATA);
        reposConfigInfo3DO.set(SchemaConstants.PROP_NAME, ConfigService.CONFIG_DO_RDN_ATTRIBUTES);
        reposConfigInfo3DO.set(SchemaConstants.PROP_REPOSITORY_ID, "LDAP1");
        List values = reposConfigInfo3DO.getList(SchemaConstants.PROP_VALUES);
        values.add("uid");
        System.out.println("Create Schema input datagraph -> " + printDO(root));
        // Invoke the create schema API
        DataObject outRoot = service.createSchema(root);
        System.out.println("Create Schema output datagraph -> " + printDO(outRoot));
        System.out.println("¥nCLIENT: new entity type is created.");
    }
    catch (SchemaAlreadyExistException e) 
    {
        System.out.println("CLIENT: entity type already exists.¥n¥n");
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

/**
 *  Method to create an entity of the 'PersonAccountExt' entity type.
 */
private static void createPersonAccountExt() 
{
    //Create Person 1
    try 
    {
        System.out.println("¥nCLIENT: Creating Person 1 . . .");
        // Gets an empty data graph of Person from WIM Service.
        DataObject root = SDOHelper.createRootDataObject();
        // Create a "PersonAccount" entity type data object under root
        DataObject person = root.createDataObject(SchemaConstants.DO_ENTITIES, 
                Service.WIM_NS_URI, "PersonAccountExt");
        // Set the values for required attributes, uid, cn and sn
        person.set("uid", "person1");
        person.set("cn", "Person 1");
        person.set("sn", "Person1LastName");
        System.out.println("Create PersonAccountExt input datagraph -> " + printDO(root));
        // Invoke the create API to create the person account entity defined previously
        root = service.create(root);
        System.out.println("Create PersonAccountExt output datagraph -> " + printDO(root));
        System.out.println("CLIENT: Person 1 has been created. ¥n¥n");
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

/**
 *  Method to add a property 'HomePhone' to 'PersonAccountExt' entity type
 *  and map it to 'primaryOwnerContact' attribute in LDAP
 */
@SuppressWarnings("unchecked")
private static void addPropertyToPersonAccountExt() 
    {
    try 
    {
        DataObject root = SDOHelper.createRootDataObject();
        // Create a new "schema" object under root
        // This object will contain the details of the schema modifications that have to be made
        DataObject schema = root.createDataObject(SchemaConstants.DO_SCHEMA);
        // Create a property schema data object under the schema object created earlier
        DataObject propertySchema = schema.createDataObject(SchemaConstants.DO_PROPERTY_SCHEMA);
        // Set the property name
        propertySchema.setString(SchemaConstants.PROP_PROPERTY_NAME, "HomePhone");
        // Set the property namespace as the default namespace
        propertySchema.setString(SchemaConstants.PROP_NS_URI, SchemaConstants.WIM_NS_URI);
        // Specify that the property is not multi-valued
        propertySchema.setBoolean(SchemaConstants.PROP_MULTI_VALUED, false);
        // Specify the data type of the property as String
        propertySchema.setString(SchemaConstants.PROP_DATA_TYPE, SchemaConstants.DATA_TYPE_STRING);
        // Specify the applicable entity types for this property
        // The property will be added for all entity type names specified in the list
        propertySchema.getList(SchemaConstants.PROP_APPLICABLE_ENTITY_TYPE_NAMES)
                .add("PersonAccountExt");
        System.out.println("Add property input datagraph " + printDO(root));
        // Invoke the create schema API
        root = service.createSchema(root);
        System.out.println("Add property output datagraph " + printDO(root));
        // Map the 'HomePhone' property created previously to the 'primaryOwnerContact' LDAP attribute
        Hashtable configData = new Hashtable();
        // Specify the repository for which this property needs to be mapped
        configData.put(DynamicConfigConstants.DYNA_CONFIG_KEY_REPOS_ID, "LDAP1");
        DataObject configProvider = SDOHelper.createConfigProviderDataObject();
        // Specify the repository type as "Ldap"
        DataObject ldapRepos = SDOHelper.createConfigRepositoryDataObject(configProvider, 
                ConfigConstants.CONFIG_DO_LDAP_REPOSITORY_TYPE);
        // Give the property name and attribute name that need to be mapped to each other
        DataObject attrConfig = ldapRepos.createDataObject(ConfigConstants.CONFIG_DO_ATTRIBUTE_CONFIGUARTION);
        DataObject attr = attrConfig.createDataObject(ConfigConstants.CONFIG_DO_ATTRIBUTES);
        attr.setString(ConfigConstants.CONFIG_PROP_PROPERTY_NAME, "HomePhone");
        attr.setString(ConfigConstants.CONFIG_PROP_NAME, "primaryOwnerContact");
        configData.put(DynamicConfigConstants.DYNA_CONFIG_KEY_PROP_CONFIG, attr);
        // Call the dynamic config update
        // This API updates the mapping for this session only, this mapping is not persistent
        service.dynamicUpdateConfig(DynamicConfigConstants.DYNA_CONFIG_EVENT_ADD_PROPERTY_CONFIG, 
                configData);
    }
    catch(Exception e) 
    {
		    e.printStackTrace();
    }
}  

/**
  * Add 'HomePhone' to 'person1' entity created in LDAP earlier
  */
private static void addHomePhoneForPersonAccountExt() 
{
    try 
    {
    DataObject root = SDOHelper.createRootDataObject();
    DataObject entity = SDOHelper.createEntityDataObject(root, null, "PersonAccountExt");
    // Set the unique name for the object to which the 'HomePhone' value needs to be set
    entity.createDataObject(SchemaConstants.DO_IDENTIFIER).set(SchemaConstants.PROP_UNIQUE_NAME, 
            "uid=person1,dc=yourco,dc=com");
    // Specify the value of 'HomePhone'
    entity.set("HomePhone", "020-2341123");
    System.out.println("Set property input datagraph -> " + printDO(root));
    // Invoke the VMM update API
    root = service.update(root);
    System.out.println("Set property output datagraph -> " + printDO(root));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

/**
  * Search for 'PersonAccountExt' 
  */
@SuppressWarnings("unchecked")
private static void searchPersonAcctExt() 
{
    try 
    {
        DataObject root = SDOHelper.createRootDataObject();
        // Create a search control data object
        DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, 
                SchemaConstants.DO_SEARCH_CONTROL);
        // Specify the properties that need to be fetched for all matching entries
        searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
        searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
        searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
        searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("HomePhone");
        // Specify the matching criteria as the search expression
        searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccountExt'");
        // Invoke the search API
        root = service.search(root);
        System.out.println("Output datagraph -> " + printDO(root));
        // Iterate over the search results
        printIdentifiers(root);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
注: サンプル・コードでは、Virtual member manager プロパティーを LDAP 属性にマップするためのプロパティー構成を追加するために、dynamicUpdateConfig() API が使用されています。dynamicUpdateConfig() API を使用して実行時に構成の更新が行われた場合、メモリー内の構成のみが更新され、構成ファイルは更新されないため、変更内容は永続化されません。構成変更を永続化するには、wsadmin コマンド addIdMgrLDAPAttr を使用して、LDAP 属性を Virtual member manager プロパティーにマップします。詳しくは、WebSphere Application Server インフォメーション・センターの「AdminTask オブジェクトの IdMgrRepositoryConfig コマンド・グループ」トピックを参照してください。 あるいは、以下のステップで説明するように、管理コンソールを使用して LDAP 属性を Virtual member manager プロパティーにマップすることもできます。
  1. WebSphere Application Server 管理コンソールで「セキュリティー」>「グローバル・セキュリティー」をクリックします。
  2. ユーザー・アカウント・リポジトリーの使用可能なレルム定義フィールドから「統合リポジトリー」を選択し、「構成」をクリックします。
    注: 複数のセキュリティー・ドメイン環境内の特定のドメインを構成するには、「セキュリティー・ドメイン」>「domain_name」をクリックします。 「セキュリティー属性」の下で「ユーザー・レルム」を展開し、「このドメイン用にカスタマイズする」をクリックします。 「レルム・タイプ」に「統合リポジトリー」を選択し、「構成」をクリックします。
  3. 「関連項目」の下で「リポジトリーの管理」>「repository_name」とクリックします。
  4. 表示されたパネルの「追加のプロパティー」で、「LDAP 属性」をクリックします。
  5. 追加」をクリックし、ドロップダウン・メニューから「サポート」を選択します。
  6. 名前」フィールドに LDAP 属性名、「プロパティー名」フィールドに Virtual member manager プロパティー名、「エンティティー・タイプ」フィールドに属性マッピングを適用するエンティティー・タイプを入力します。前の例では、以下の値を入力する必要があります。
    • 名前: HomePhone
    • プロパティー: primaryOwnerContact
    • エンティティー・タイプ: PersonAccountExt

入出力データ・グラフ

新規の拡張されたエンティティー・タイプを作成するための入力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:schema>
      <wim:entitySchema nsURI="http://www.ibm.com/websphere/wim"
          entityName="PersonAccountExt" parentEntityName="PersonAccount">
        <wim:entityConfiguration defaultParent="dc=yourco,dc=com" rdnProperty="uid">
          <wim:metaData name="objectClasses" repositoryId="LDAP1">
            <wim:values>inetorgperson</wim:values>
            <wim:values>organizationalperson</wim:values>
            <wim:values>person</wim:values>
          </wim:metaData>
          <wim:metaData name="rdnAttributes" repositoryId="LDAP1">
            <wim:values>uid</wim:values>
          </wim:metaData>
        </wim:entityConfiguration>
      </wim:entitySchema>
    </wim:schema>
  </wim:Root>
</sdo:datagraph>

新規の拡張されたエンティティー・タイプを作成した後の出力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:schema>
      <wim:entitySchema nsURI="http://www.ibm.com/websphere/wim"
          entityName="PersonAccountExt" parentEntityName="PersonAccount">
        <wim:repositoryIds>LDAP1</wim:repositoryIds>
        <wim:entityConfiguration defaultParent="dc=yourco,dc=com" rdnProperty="uid">
          <wim:metaData name="objectClasses" repositoryId="LDAP1">
            <wim:values>inetorgperson</wim:values>
            <wim:values>organizationalperson</wim:values>
            <wim:values>person</wim:values>
          </wim:metaData>
          </wim:metaData>
          <wim:metaData name="rdnAttributes" repositoryId="LDAP1">
            <wim:values>uid</wim:values>
          </wim:metaData>
        </wim:entityConfiguration>
      </wim:entitySchema>
    </wim:schema>
  </wim:Root>
</sdo:datagraph>

新規エンティティー・タイプのエンティティーを作成するための入力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:entities xsi:type="wim:PersonAccountExt">
      <wim:uid>person1</wim:uid>
      <wim:cn>Person 1</wim:cn>
      <wim:sn>Person1LastName</wim:sn>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>

新規エンティティー・タイプのエンティティーを作成した後の出力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:entities xsi:type="wim:PersonAccountExt">
      <wim:identifier externalName="uid=person1,dc=yourco,dc=com" repositoryId="LDAP1"
          uniqueId="cb1ea321-8673-4012-9750-c917d4e7f2f6" uniqueName="uid=person1,dc=yourco,dc=com"/>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>

新規エンティティー・タイプのプロパティーを新規作成するための入力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:schema>
      <wim:propertySchema nsURI="http://www.ibm.com/websphere/wim" dataType="String"
          multiValued="false" propertyName="HomePhone">
        <wim:applicableEntityTypeNames>wim:PersonAccountExt</wim:applicableEntityTypeNames>
      </wim:propertySchema>
    </wim:schema>
  </wim:Root>
</sdo:datagraph>

新規エンティティー・タイプのプロパティーを作成した後の出力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:schema>
      <wim:propertySchema nsURI="http://www.ibm.com/websphere/wim" dataType="String"
          multiValued="false" propertyName="HomePhone">
        <wim:repositoryIds>LDAP1</wim:repositoryIds>
        <wim:applicableEntityTypeNames>wim:PersonAccountExt</wim:applicableEntityTypeNames>
      </wim:propertySchema>
    </wim:schema>
  </wim:Root>
</sdo:datagraph>

プロパティーを値と共にエンティティーに追加するための入力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:entities xsi:type="wim:PersonAccountExt">
      <wim:identifier uniqueName="uid=person1,dc=yourco,dc=com"/>
      <wim:HomePhone>020-2341123</wim:HomePhone>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>

プロパティーを値と共にエンティティーに追加した後の出力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:entities xsi:type="wim:PersonAccountExt">
      <wim:identifier externalName="uid=person1,dc=yourco,dc=com" repositoryId="LDAP1"
          uniqueId="cb1ea321-8673-4012-9750-c917d4e7f2f6" uniqueName="uid=person1,dc=yourco,dc=com"/>
      <wim:HomePhone>020-2341123</wim:HomePhone>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>

拡張プロパティーを検索するための入力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:controls xsi:type="wim:SearchControl" expression="@xsi:type='wim:PersonAccountExt'">
      <wim:properties>uid</wim:properties>
      <wim:properties>sn</wim:properties>
      <wim:properties>cn</wim:properties>
      <wim:properties>wim:HomePhone</wim:properties>
    </wim:controls>
  </wim:Root>
</sdo:datagraph>

拡張プロパティーを検索した後の出力データ・グラフ:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:entities xsi:type="wim:PersonAccountExt">
      <wim:identifier externalName="uid=person1,dc=yourco,dc=com" repositoryId="LDAP1"
          uniqueId="cb1ea321-8673-4012-9750-c917d4e7f2f6" uniqueName="uid=person1,dc=yourco,dc=com"/>
      <wim:uid>person1</wim:uid>
      <wim:cn>Person 1</wim:cn>
      <wim:sn>Person1LastName</wim:sn>
      <wim:HomePhone>020-2341123</wim:HomePhone>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>


利用条件 | フィードバック