12
返回列表 发新帖
楼主: Sky-Tiger

extend Spring-LDAP with an iBATIS-style XML Data Mapper

[复制链接]
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
11#
 楼主| 发表于 2008-11-29 22:35 | 只看该作者
Figure 5. Sequence Diagram of LdapMapClient Initialization

The factory method ‘buildLdapMapClient()’ reads user-defined property file, builds the Spring XML for bean wiring and calls Spring XmlBeanFactory to build ldapMapClient object.
Spring XML bean factory generates an LdapMapClient bean instance.
Initialize LdapMapClient by invoking its ’initializeResource()’ method: the user-defined map xml file (i.e. ldap.map.xml ) is parsed to build all mapped LDAP operations in HashMaps.
Return LdapMapClient bean to user.
LdapMapClient methods are shown as follows:
public void bind(String id, Object parameterObject) throws DataAccessException;
public void bindList(String id, List parameterObjectList) throws DataAccessException;
public void modify(String id, Object parameterObject) throws DataAccessException;
public void modifyList(String id, List parameterObjectList) throws DataAccessException;
public void rebind(String id, Object parameterObject) throws DataAccessException;
public void unbind(String id, Object parameterObject) throws DataAccessException;
public Object lookup(String id, Object parameterObject) throws DataAccessException;
public Object search(String id, Object parameterObject) throws DataAccessException;
public Object execute(String id, Object parameterObject) throws DataAccessException;

Since the LdapMapClient parses the Map XML file, and binds the JavaBean object to the mapped LDAP operation at runtime, the Spring-LDAP’s AttributeMap or ContextMap are not need for each LDAP invocation.

Advanced Feature: Nested LDAP CRUD Operations Using XML Data Mapper
At this section, I will show you another main advantage of this XML Data Mapper: manipulate nested property element of your JavaBean as nested LDAP operations. From the samples of nested CRUD operations, you will see how to create, search, modify and delete a whole LDAP tree by simply manipulating your local JavaBean object.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
12#
 楼主| 发表于 2008-11-29 22:35 | 只看该作者
Create “Krusty Krab” Company as a sub-tree in LDAP

If you recall the data structure of the JavaBean object, the ‘krustyKrab’ company shown in Figure 1, you will find that it is not a simple JavaBean. It contains not only simple properties (such as name, phone, address, etc.) but also nested properties (such as an Employee object as ‘ceo’, and list of ‘Department’ objects as ‘departments’).

With the nested-operation-mapping, you only need just two-line java code to create the whole sub-tree in LDAP.

1. Define the XML mapping file. The following is the sample mapping file for creating the whole LDAP sub tree.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ldapMap PUBLIC "-//Spring LDAP - XML Data Mapper//DTD Ldap Map 1.0//EN" "ldap-map.dtd">
<ldapMap>
        <parameterMap id="parametermap_create_company"
              className="com.springldap.sample.dto.Company">
                <attrmap propertyName="name" attributeName="o"/>
                <attrmap propertyName="address"
               attributeName="registeredAddress"/>
                <attrmap propertyName="phone"
               attributeName="telephoneNumber"/>
                <attrmap propertyName="postalCode"
               attributeName="postalCode"/>
                <attrmap propertyName="description"
               attributeName="description"/>
                <actionmap propertyName="ceo" actionType="bind"
               id="create_employee"/>
                <actionmap propertyName="departments"
               actionType="bindlist" id="create_department"/>
        </parameterMap>

        <bind id="create_company"
             parameterMapId="parametermap_create_company">
                <objectClass>organization</objectClass>
                <dn>${dn}</dn>
        </bind>

...
</ ldapMap>

As shown in the sample Map XML file, ‘actionMap’ elements are defined in ‘parameterMap’ to support nested mapped LDAP operations.

2. Write Java code to populate a ‘krustyKrab’ JavaBean object based on the data model in Figure 1. The sample code will look like this:

public static Company buildCompanyDTO(){
                Company company = new Company("o=krustyKrab", "krustyKrab",
                                "(416) 111-1111",
                                "300 Consilium Pl, Toronto, ON", "M1H 3G2",
                                "Krusty Krab Limited",
                                null, new ArrayList());

                ... ...
                rnd.addEmployee(squidward);
                rnd.addEmployee(bob);

                company.setCeo(ceo);
                company.addDepartment(sales);
                company.addDepartment(rnd);
                return company;
        }

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
13#
 楼主| 发表于 2008-11-29 22:36 | 只看该作者
Write Java code to invoke LDAP operation through LdapMapClient to create the whole sub-tree on LDAP. It is just two-line java code:

LdapMapClient ldapClient = LdapMapClientFactory.buildLdapMapClient(
          "com/ldapmapper/sample/ldap.properties");
ldapClient.bind("create_company", company);

When you run this two-line java code, the whole sub-tree is created in LDAP.

2gv91z8.jpg (21.45 KB, 下载次数: 5)

2gv91z8.jpg

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
14#
 楼主| 发表于 2008-11-29 22:36 | 只看该作者
Query the “Krusty Krab” sub-tree from LDAP into your Company JavaBean

Similarly, using the nested nested-operation-mapping, you can read a whole LDAP sub-tree from LDAP to your complex JavaBean by a single java call.

1. Define the XML mapping file.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ldapMap PUBLIC "-//Spring LDAP - XML Data Mapper//DTD Ldap Map 1.0//EN" "ldap-map.dtd">
  <ldapMap>
<parameterMap id="parametermap_search_company_by_name"
         className="com.ldapmapper.sample.dto.CompanySearch"/>
    <resultMap id="resultmap_company"
             className="com.ldapmapper.sample.dto.Company">
        <attrmap propertyName="name" attributeName="o"/>
        <attrmap propertyName="address"
             attributeName="registeredAddress"/>
        <attrmap propertyName="phone" attributeName="telephoneNumber"/>
        <attrmap propertyName="postalCode" attributeName="postalCode"/>
        <attrmap propertyName="description" attributeName="description"/>
        <actionmap propertyName="ceo" parameterId="ceoSearchDTO"
             actionType="search" id="find_ceo"/>
        <actionmap propertyName="departments"
             parameterId="departmentsSearchDTO" actionType="search"
             id="list_departments"/>
</resultMap>

    <search id="search_company_by_name"
                parameterMapId="parametermap_search_company_by_name"
                resultMapId="resultmap_company">
      <base>${base}</base>
        <scope>${scope}</scope>
      <filter>(&(objectclass=organization)(o=${name}))</filter>
</search>
...
</ ldapMap>

From the sample Map XML file, you will see that the ‘actionMap’ elements are used in ‘resultMap’ to support nested mapped LDAP operations.

2. Write Java code to populate a ‘CompanySearch’ JavaBean object as the input parameter object for the nested search operation.

public static CompanySearch buildCompanySearchDTO(){
                CompanySearch req = new CompanySearch("o=krustyKrab", "",
                    SearchControls.SUBTREE_SCOPE, null, "krustyKrab",
                          new CommonSearchDTO(null, "o=krustyKrab",
                    SearchControls.ONELEVEL_SCOPE, null),
                          new DepartmentSearch(null, "o=krustyKrab",
                    SearchControls.ONELEVEL_SCOPE, null, null,
                          new CommonSearchDTO(null, null,
                    SearchControls.ONELEVEL_SCOPE, null))
                        );
                return req;
        }

3. Write Java code to read the whole sub-tree from LDAP to the local JavaBean object

LdapMapClient ldapClient = LdapMapClientFactory.buildLdapMapClient(
            "com/ldapmapper/sample/ldap.properties", false);
List companies = (List)ldapClient.search(
            "search_company_by_name", buildCompanySearchDTO());
for (Iterator it = companies.iterator(); it.hasNext() {
        Company company = (Company)it.next();
        // manipulating company object here ...
}

You can also find the extensive sample code of other CRUD operations on your complex JavaBean.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
15#
 楼主| 发表于 2008-11-29 22:36 | 只看该作者
Benefits
You can see the following benefits when using this XML Data Mapper.

A Simple JavaBean/LDAP Entry Mapping Framework : It transplants O/RM concept to JNDI/LDAP environment. It can map not only simple JavaBean to single LDAP entry but also complex object to a whole LDAP sub-tree. The mapping is done at runtime by user-defined map xml file.

Easy Coding : It provides a very simple Java API. Developer can easily manipulate their LDAP data through very simple and straight-forward java coding without knowing Spring-LDAP API.

Enhanced Data Mapping : It supports nested LDAP operation, which can significantly reduce the code for manipulating the complex data in LDAP.

Powerful : It leverages powerful features of Spring-LDAP, such as bean wiring, transaction support, context management and exception handling.

Improved development flow: It relieves the tedious and error-prone java code from the developers. It makes the developers focus on the data model and business logic of the applications. By introducing the XML Map file, it decouples LDAP schema from Java application.

Conclusions
This article introduces an iBATIS-style XML Data Mapper framework to extend Spring-LDAP. It simply uses O/RM concept to manipulate LDAP entries through JavaBean operations. From the example in the article, you can find that java code is significantly simplified by moving the mapping logic from java to XML. Moreover, the nested-operation-mapping allows user to manipulate complex data through an intuitive way.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
16#
发表于 2008-11-30 22:31 | 只看该作者
没用过

使用道具 举报

回复
论坛徽章:
25
2008新春纪念徽章
日期:2008-02-13 12:43:032010广州亚运会纪念徽章:龙舟
日期:2010-09-26 10:39:08ITPUB9周年纪念徽章
日期:2010-10-08 09:28:51参与SAP云计算之旅活动纪念
日期:2011-05-23 11:02:23开发板块每日发贴之星
日期:2011-08-01 01:01:02ITPUB十周年纪念徽章
日期:2011-11-01 16:24:512012新春纪念徽章
日期:2012-01-04 11:54:262013年新春福章
日期:2013-02-25 14:51:242014年新春福章
日期:2014-02-18 16:43:092010新春纪念徽章
日期:2010-03-01 11:04:58
17#
发表于 2008-12-2 10:59 | 只看该作者
老大,我该如何收藏你这篇大作呢?

使用道具 举报

回复
求职 : Java研发
论坛徽章:
36
2009新春纪念徽章
日期:2009-01-04 14:52:28蛋疼蛋
日期:2012-03-07 10:09:01复活蛋
日期:2012-03-07 10:09:01生肖徽章2007版:马
日期:2012-03-07 10:13:26茶鸡蛋
日期:2012-03-22 18:44:00蛋疼蛋
日期:2012-04-06 10:44:15蛋疼蛋
日期:2012-05-11 13:33:36鲜花蛋
日期:2012-06-04 13:48:57茶鸡蛋
日期:2012-06-05 08:04:53复活蛋
日期:2012-06-06 11:57:42
18#
发表于 2008-12-13 14:04 | 只看该作者
早看过了,有tss上面。
这是你写的吗,非常不错。

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表