用于在缺省名称空间中创建新扩展实体类型的样本代码

使用样本代码片段和数据图,在缺省名称空间中创建一个扩展自内置实体类型的实体类型。

此样本代码片段中涵盖以下步骤:

  1. 在缺省名称空间中,为新实体类型 PersonAccountExt(此新实体类型扩展自内置实体类型 PersonAccount)创建模式。
  2. 创建 PersonAccountExt 实体类型的实体 person1
  3. 将新属性 HomePhone 添加到新创建的实体类型 PersonAccountExt,并将新属性 HomePhone 映射到 LDAP 属性 primaryOwnerContact
  4. HomePhone 属性连同值添加到 person1 实体。
  5. 验证 HomePhone 属性是否已添加到 person1 实体。

必备条件

确保您已阅读了“编程先决条件”主题(包括“扩展属性模式”部分,其中包含有关 propertySchema 和 extensionPropertySchema 数据对象的信息并列出了属性数据类型的有效语法)中的信息,并完成了其中描述的步骤。

也要将以下导入语句添加到您的类:
import com.ibm.websphere.wim.exception.SchemaAlreadyExistException;

确保您的联合存储库配置中配置了 LDAP 存储库。有关更多信息,请参阅 WebSphere Application Server 信息中心中的“在联合存储库配置中配置轻量级目录访问协议”主题。

样本代码

请将以下代码片段添加到您的应用程序代码,并将变量替换为您要使用的实际值。

/**
 * 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();
    }
}
注: 在样本代码中,dynamicUpdateConfig() API 用于添加属性配置,该属性配置可将 virtual member manager 属性映射到 LDAP 属性。如果通过使用 dynamicUpdateConfig() API 在运行时进行配置更新,那么仅会更新内存中的配置,而不会更新配置文件,因此更改不会持续有效。 要让配置更改持续有效,请使用 addIdMgrLDAPAttr wsadmin 命令将 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>


使用条款 | 反馈