现在的位置: 首页 > 综合 > 正文

JAVA,OpenLDAP使用心得(4)

2013年10月04日 ⁄ 综合 ⁄ 共 4646字 ⁄ 字号 评论关闭
经过几天的努力,把获取objectClass定义和获取Attribute定义的代码弄出来,这样就方便了以后根据自定义schema动态的获取schema中的objectClass和Attribute。特别是对于做添加修改界面应该有点用处,修改了schema并不需要修改代码做代码调整,只需要根据获取的属性个数挨个排好,让别人填入值,并且可以检测MUST的是不是已经填写了。

    /**
     * 获取指定objectClass的定义
     * @param name
     */
    public void getObjectClassDefinition(String name) {
        try {
            // Get the schema tree root
            DirContext schema = dc.getSchema("");

            // Get the schema object for "person"
            DirContext personSchema = (DirContext) schema.lookup("ClassDefinition/" + name);
            Attributes a = personSchema.getAttributes("");
            NamingEnumeration ane = a.getAll();

            while (ane.hasMore()) {
                Attribute attr = (Attribute) ane.next();
                String attrType = attr.getID();
                NamingEnumeration values = attr.getAll();

                while (values.hasMore()) {
                    Object oneVal = values.nextElement();
                    if (oneVal instanceof String) {
                        System.out.println(attrType + ": " + (String) oneVal);
                    } else {
                        System.out.println(attrType + ": " + new String((byte[]) oneVal));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定DN的objectClass定义
     * @param DN
     */
    public void getDNObjectClassDefinition(String DN) {
        try {
            // Get context containing class definitions for the "cn=Ted Geisel" entry
            DirContext tedClasses = dc.getSchemaClassDefinition(DN);

            // Enumerate the class definitions
            NamingEnumeration enum1 = tedClasses.search("", null);
            while (enum1.hasMore()) {
                Object o = enum1.next();
                System.out.println(((SearchResult) o).getName());

                Attributes a = ((SearchResult) o).getAttributes();
                NamingEnumeration ane = a.getAll();

                while (ane.hasMore()) {
                    Attribute attr = (Attribute) ane.next();
                    String attrType = attr.getID();
                    NamingEnumeration values = attr.getAll();

                    while (values.hasMore()) {
                        Object oneVal = values.nextElement();
                        if (oneVal instanceof String) {
                            System.out.println(attrType + ": " + (String) oneVal);
                        } else {
                            System.out.println(attrType + ": " + new String((byte[]) oneVal));
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定名字的Attribute定义
     * @param name
     */
    public void getAttributeDefinition(String name) {
        try {
            // Get the schema tree root
            DirContext schema = dc.getSchema("");

            // Get the schema object for "person"
            DirContext personSchema = (DirContext) schema.lookup("AttributeDefinition/" + name);
            Attributes a = personSchema.getAttributes("");
            NamingEnumeration ane = a.getAll();

            while (ane.hasMore()) {
                Attribute attr = (Attribute) ane.next();
                String attrType = attr.getID();
                NamingEnumeration values = attr.getAll();

                while (values.hasMore()) {
                    Object oneVal = values.nextElement();
                    if (oneVal instanceof String) {
                        System.out.println(attrType + ": " + (String) oneVal);
                    } else {
                        System.out.println(attrType + ": " + new String((byte[]) oneVal));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定DN中指定名字的Attribute定义
     * @param DN
     * @param name
     */
    public void getDNAttributeDefinition(String DN, String name) {
        try {
            // Get an attribute of that type
            Attributes attrs = dc.getAttributes(DN, new String[]{name});
            Attribute cnAttr = attrs.get(name);

            // Get its attribute type definition
            DirContext cnSchema = cnAttr.getAttributeDefinition();
            // Get cnSchema's attributes
            Attributes cnAttrs = cnSchema.getAttributes("");

            NamingEnumeration ane = cnAttrs.getAll();
            while (ane.hasMore()) {
                Attribute attr = (Attribute) ane.next();
                String attrType = attr.getID();
                NamingEnumeration values = attr.getAll();

                while (values.hasMore()) {
                    Object oneVal = values.nextElement();
                    if (oneVal instanceof String) {
                        System.out.println(attrType + ": " + (String) oneVal);
                    } else {
                        System.out.println(attrType + ": " + new String((byte[]) oneVal));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

抱歉!评论已关闭.