Store Java objects in the Apache directory server, part 2nd: (next)

Source: Internet
Author: User

Application 4. Searching for stored data

Start with a simple search on the ApacheDS. Suppose there are many users in the ApacheDS. And you want to find all the details of the user Alice. All known things about Alice are listed below:

Alice is a user, so you should be able to find her data entry in the user's data organization unit. (The concept of an organizational unit or "OU" has been introduced in part 1th.) )

Alice's username is "Alice" (case-insensitive).

Alice is a human being, so the object class used by her data entry must extend the person object class directly or indirectly.

Now, see listing 6, which shows an application named Searchforalice. Searchforalice demonstrates a very simple search scenario, and I'll expand the application to fit the more advanced search scenario later.

Listing 6. Searchforalice

public class Searchforalice {


public Searchforalice () {


Try


     {


       //------------------------------------------


//step1:setting up JNDI properties for ApacheDS


       //------------------------------------------


InputStream InputStream = new FileInputStream ("Apacheds.properties");


Properties Properties = new properties ();


properties.load (InputStream);


properties.setproperty ("Java.naming.security.credentials", "secret");





       //------------------------------------------


//Step2:fetching a DirContext object


       //------------------------------------------


DirContext ctx = new InitialDirContext (properties);





       //---------------------------------------------


//step3:setting Search Context


       //---------------------------------------------


String searchcontext = "Ou=users";





       //--------------------------------------------


//step4:creating Search attributes for Alice


       //--------------------------------------------


Attribute uid = new BasicAttribute ("UID");


Attribute objclass = new BasicAttribute ("objectclass");





//adding Attribute Values


Uid.add ("Alice");


objclass.add ("person");





//instantiate Attributes object and put search Attributes in it.


Attributes attrs = new Basicattributes (true);


Attrs.put (UID);


Attrs.put (objclass);





       //------------------------------------------


//step5:executing Search


       //------------------------------------------


namingenumeration ne = ctx.search (searchcontext, attrs);





if (NE!= null)


       {


//step 6:iterating through SearchResults


while (Ne.hasmore ()) {


//step 7:getting Individual SearchResult object


SearchResult sr = (SearchResult) ne.next ();





//step 8:


String Entryrdn = Sr.getname ();


System.out.println ("RDN of the searched entry:" +entryrdn);





//step 9:


Attributes srattrs = Sr.getattributes ();





if (srattrs!= null) {


//step 10:


for (Enumeration e = Attrs.getall (); e.hasmoreelements ();)


              {


attribute attr = (attribute) e.nextelement ();





//step 11:


String Attrid = Attr.getid ();


System.out.println ("Attribute Name:" +attrid);


System.out.println ("Attribute Value (s):");





Namingenumeration e1 = Attr.getall ();


while (E1.hasmore ())


System.out.println ("\t\t" +e1.nextelement ());


}//for ()


}//if (srattrs)


         }


}//if (NE!= null)





} catch (Exception e) {


System.out.println ("Operation failed:" + e);


     }


   }





public static void Main (string[] args) {


Searchforalice searchalice = new Searchforalice ();


   }


  }

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.