Java and OpenLDAP experiences (4)

Source: Internet
Author: User
After several days of efforts, we can get the code for getting the objectclass definition and getting the attribute definition, so that we can dynamically obtain the objectclass and attribute in the Schema Based on the custom schema. This is especially useful for adding and modifying interfaces. If you modify the schema, you do not need to modify the code to adjust the code. You only need to arrange the number of retrieved attributes to allow others to enter values, you can also check whether must has been filled in.

/**
* Get the definition of the specified 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 ();
}
}

/**
* Get the objectclass definition of the specified DN
* @ 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 ();
}
}

/**
* Get the attribute definition of the specified name
* @ 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 ();
}
}

/**
* Get the attribute definition of the specified name in the specified DN
* @ 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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.