用于在多个安全域中扩展模式的样本代码

此处提供了用于在多个安全域中扩展属性模式的配置步骤、命令、样本代码和数据图。

配置命令和样本代码片段中涵盖以下步骤:

  1. 创建多个安全域。
  2. 将安全域映射到特定应用程序服务器。
  3. 安装必备的系统应用程序。
  4. 将新属性添加到每个服务器实例。
  5. 通过使用在不同实例上扩展的属性,来执行搜索操作。

必备条件

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

您必须首先完成以下配置步骤,然后才能在您的应用程序中使用样本代码。启动 wsadmin 工具并执行以下命令。将变量替换为您要使用的实际值。

  1. 通过使用“WebSphere Application Server 概要文件管理工具”来创建基本概要文件。确保启用了“管理安全性”。 创建新的应用程序服务器,例如 server1
  2. 添加其他服务器,例如 server2
    $AdminTask createApplicationServer nodeName {-name server2}
  3. 创建安全域。
    $AdminTask copySecurityDomainFromGlobalSecurity {-securityDomainName domain1}
  4. 将此域映射到步骤 2 中创建的 server2
    $AdminTask mapResourceToSecurityDomain { -securityDomainName domain1 -resourceName Cell=:Node=nodeName:Server=server2}
  5. 在多安全域环境中,可设置 useGlobalSchema 属性,以使用全局域,而不是特定于域的模式。全局模式引用管理域的模式。设置为使用全局模式的应用程序域共享管理域的相同模式。 因此,如果在一个域中扩展应用程序的模式,必须考虑此操作可能会以何种方式影响其他域的应用程序,因为这些应用程序也由相同模式绑定。例如,添加一个应用程序的必需属性可能会造成其他应用程序失败。

    如果要使用全局模式,请完成此步骤。如果要使用特定于域的模式,请跳过此步骤。

    $AdminTask setIdMgrUseGlobalSchemaForModel { -useGlobalSchema true -securityDomainName domain1 }
  6. server1 上将 wim.ear 安装为系统应用程序。
    $AdminApp install app_server_root/systemApps/wim.ear { -appname wim -server server1 -systemApp}
  7. server2 上将 wimperdomain.ear 安装为一般应用程序。 在多安全域环境中,必须在每个目标服务器上部署 virtual member manager EJB,其中服务器作用域与此安全域关联,以获取此域中 virtual member manager 实例的引用。通过此过程,可为特定域通过 EJB 调用 virtual member manager API。只有此域中具有 virtual member manager API 或超级用户的所需访问角色的用户才可调用各个 API。
    $AdminApp install app_server_root/installableApps/wimperdomain.ear {
      			-appname wimperdomain1 
      			-BindJndiForEJBNonMessageBinding {{ wim.ejb WIMService wimejb.jar,
              META-INF/ejb-jar.xml ejbd1/com/ibm/websphere/wim/ejb/WIMServiceHome}} 
      			-MapModulesToServers {{ wim.ejb wimejb.jar,META-INF/ejb-jar.xml 
              WebSphere:cell=cellName,node=nodeName,server=server2 }}}
  8. 保存配置。
    $AdminConfig save

样本代码

按照以下步骤所述,将以下样本代码添加到您的应用程序代码。将变量替换为您要使用的实际值。

  1. 要在后续步骤中给定的样本代码片段中使用这些方法,需要以下变量声明。您可以将这些声明添加到您的应用程序代码,并将变量替换为您要使用的实际变量。
    //The service object that holds the virtual member manager ejb service reference for server1.
    	private static Service service1 = null;
    	
    //The service object that holds the virtual member manager ejb service reference for server2.
    	private static Service service2 = null;
    
    //A string constant for the new property name to be added for server1.
    	private static final String server1PropName = "postOfficeBox";
    
    //A string constant for the new property name to be added for server2.
    	private static final String server2PropName = "age";
  2. 使用 startServer 命令启动 server1
  3. 使用以下 locateServices() 方法来查找在本地主机上的 WebSphere Application Server 上运行的 virtual member manager。确保在 locateServices() 方法中正确指定了引导程序端口。
    /**
     *  This method locates the virtual member manager services running on 
     *  WebSphere Application Sever on the localhost
     */
    private static void locateServices()
    {
        // Remote access WIM Service EJB
        try
        {
            Hashtable<String, String> environment = new Hashtable<String, String>();
            environment.put(LocalServiceProvider.PROVIDER_URL, "corbaloc:iiop:localhost:2814");
            
            service1 = new LocalServiceProvider(environment);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    
        try
        {
            Hashtable<String, String> environment = new Hashtable<String, String>();
            environment.put(LocalServiceProvider.EJB_JNDI_NAME, 
                    "ejbd1/com/ibm/websphere/wim/ejb/WIMServiceHome");
            environment.put(LocalServiceProvider.PROVIDER_URL, "corbaloc:iiop:localhost:2815");
            
            service2 = new LocalServiceProvider(environment);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  4. 使用以下 createPropertyOnServer1() 方法将新属性添加到 server1 实例。
    /**
     *  This method adds the "postOfficeBox" property to "PersonAccount" entity type.
     */
    private static void createPropertyOnServer1() 
    {
        try
        {
            System.out.println("\nCreating new property type postOfficeBox 
                    and add it to existing entity type PersonAccount");
            
            DataObject root = service1.createRootDataObject();
            DataObject dynaSchemaDO = root.createDataObject(SchemaConstants.DO_SCHEMA);
            
            // Create new property schema data object
            DataObject propSchemaDO = dynaSchemaDO.createDataObject(SchemaConstants.DO_PROPERTY_SCHEMA);
            // Set the values for the property, such as, namespace URI, namespace prefix, property name
            propSchemaDO.set(SchemaConstants.PROP_NS_URI, SchemaConstants.WIM_NS_URI);
            propSchemaDO.set(SchemaConstants.PROP_PROPERTY_NAME, server1PropName);
            propSchemaDO.setBoolean(SchemaConstants.PROP_MULTI_VALUED, false);
            propSchemaDO.set(SchemaConstants.PROP_DATA_TYPE, SchemaConstants.DATA_TYPE_STRING);
            List applicableEntityTypes = propSchemaDO.getList(SchemaConstants.PROP_APPLICABLE_ENTITY_TYPE_NAMES);
            applicableEntityTypes.add(Service.DO_PERSON_ACCOUNT);
            
            System.out.println("Input datagraph -> " + printDO(root));
            // Invoke the create schema API
            root = service1.createSchema(root);
            System.out.println("Output datagraph -> " + printDO(root));
            System.out.println("\nCLIENT: new property type is created.");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  5. 使用 stopServer 命令停止 server1
  6. 使用 startServer 命令启动 server2
  7. 使用以下 createPropertyOnServer2 方法将新属性添加到 server2 实例。
    /**
     *  This method adds the "age" property to "PersonAccount" entity type.
     */
    private static void createPropertyOnServer2() 
    {
        try
        {
            System.out.println("\nCreating new property type age and 
                    add it to existing entity type Person");
            
            DataObject root = service2.createRootDataObject();
            DataObject dynaSchemaDO = root.createDataObject(SchemaConstants.DO_SCHEMA);
            
            // Create new property schema data object
            DataObject propSchemaDO = dynaSchemaDO.createDataObject(SchemaConstants.DO_PROPERTY_SCHEMA);
            // Set the values for the property, such as, namespace URI, namespace prefix, property name
            propSchemaDO.set(SchemaConstants.PROP_NS_URI, SchemaConstants.WIM_NS_URI);
            propSchemaDO.set(SchemaConstants.PROP_PROPERTY_NAME, server2PropName);
            propSchemaDO.setBoolean(SchemaConstants.PROP_MULTI_VALUED, false);
            propSchemaDO.set(SchemaConstants.PROP_DATA_TYPE, SchemaConstants.DATA_TYPE_INT);
            List applicableEntityTypes = propSchemaDO.getList(SchemaConstants.PROP_APPLICABLE_ENTITY_TYPE_NAMES);
            applicableEntityTypes.add(Service.DO_PERSON_ACCOUNT);
            
            SDOUtils.printDataGraph("Input datagraph", root);
            System.out.println("Input datagraph -> " + printDO(root));
            // Invoke the create Schema API
            root = service2.createSchema(root);
            System.out.println("Output datagraph -> " + printDO(root));
            System.out.println("New property type is created.");
        }
        catch (Exception e) {
            e.printStackTrace();
        }		
    }
  8. 使用 stopServer 命令停止 server2
  9. 启动 server2server1
  10. 使用在步骤 4 和 7 中添加的新属性来创建实体。 有关用于添加新属性的样本代码,请参阅“用于在文件存储库中扩展模式的样本代码”主题。
  11. 使用以下 testSearchProperties() 方法,通过使用在不同实例上扩展的属性来执行搜索操作。
    /**
    	 *  Method that runs different searches on different servers.
    	 */
    private static void testSearchProperties() 
    {
        // Search using the server1 property but using service1
        System.out.println("Searching property '" + server1PropName + "' on server1");
        searchserver1PropName(service1);
        
        // Utility stop to synchronize output
        pause();
        
        // Search using the server1 property but using service2
        System.out.println("Searching property '" + server1PropName + "' on server2");
        searchserver1PropName(service2);
        
        // Utility stop to synchronize output
        pause();
        
        // Search using the server2 property but using service2
        System.out.println("Searching property '" + server2PropName + "' on server2");
        searchserver2PropName(service2);
        
        // Utility stop to synchronize output
        pause();
        
        // Search using the server2 property but using service1
        System.out.println("Searching property '" + server2PropName + "' on server1");
        searchserver2PropName(service1);
    }
    
    /**
     *  A utility method added to ensure that the exception is flushed to the console 
     *  before starting with the next operation.
     */
    private static void pause() 
    {
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) 
        {
        }
    }
    
    /**
     * Method to search on 'age'
     * @param service
     */
    @SuppressWarnings("unchecked")
    private static void searchserver2PropName(Service service) 
    {
        try
        {
            //Search
            DataObject root = SDOHelper.createRootDataObject();        DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, 
                    SchemaConstants.DO_SEARCH_CONTROL);
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn"); 
            searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, 
                    "@xsi:type='PersonAccount' and " + server2PropName + " > 4");
            root = service.search(root);        System.out.println("Output datagraph -> " + printDO(root));
            List searchResults = root.getList(SchemaConstants.DO_ENTITIES);
            for (int i = 0; i < searchResults.size(); i++) {
                DataObject ent = (DataObject) searchResults.get(i);
                DataObject id = ent.getDataObject(SchemaConstants.DO_IDENTIFIER);
                if (id != null) {
                    String uniqueName = id.getString(SchemaConstants.PROP_UNIQUE_NAME);
                    if (uniqueName != null) {
                        System.out.println("Found -> " + ent.getString("cn") + "(" + uniqueName + ")");
                    }
                }
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    	}
    
    /**
     *  Method to search on 'postOfficeBox'
     *  @param service
     */
    @SuppressWarnings("unchecked")
    private static void searchserver1PropName(Service service) 
    {
        try
        {
            //Search
            DataObject root = SDOHelper.createRootDataObject();        DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, 
                    SchemaConstants.DO_SEARCH_CONTROL);
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn"); 
            searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, 
                    "@xsi:type='PersonAccount' and " + server1PropName + "='4-23'");
            root = service.search(root);        System.out.println("Output datagraph -> " + printDO(root));
            List searchResults = root.getList(SchemaConstants.DO_ENTITIES);
            for (int i = 0; i < searchResults.size(); i++) {
                DataObject ent = (DataObject) searchResults.get(i);
                DataObject id = ent.getDataObject(SchemaConstants.DO_IDENTIFIER);
                if (id != null) {
                    String uniqueName = id.getString(SchemaConstants.PROP_UNIQUE_NAME);
                    if (uniqueName != null) {
                        System.out.println("Found -> " + ent.getString("cn") + "(" + uniqueName + ")");
                    }
                }
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }

结果

如果您将 useGlobalSchema 属性设置为使用全局模式(如“先决条件”部分的步骤 5 所述),那么在跨域执行搜索操作时,可以使用所有实例上扩展的所有属性。如果不设置 useGlobalSchema,那么仅会扩展特定于域的模式,并且搜索操作仅会基于当前实例中扩展的属性。



使用条款 | 反馈