Searches

 

The Directory Operations lesson showed how to use the various DirContext.search() methods. When you perform a search using a SearchControls parameter, you can control what gets returned in each SearchResult. Specifically, if you invoke SearchControls.setReturningObjFlag() with true and pass that to search(), then each entry in the SearchResult will contain the corresponding Java object.

The following example searches the context used to bind the various objects in the Storing Objects in the Directory lesson and prints each result's object (class name and object reference).

SearchControls ctls = new SearchControls();
ctls.setReturningObjFlag(true);

// Specify the search filter to match any object
String filter = "(objectclass=*)";

// Search for objects by using the filter
NamingEnumeration answer = ctx.search("", filter, ctls);
The output of this example is as follows.

# java -Djava.security.manager -Djava.security.policy=.policy Search
ou=Groups: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@723fdc9a
ou=People: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@642fdc9a
cn=Fruits: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@63afdc9a
cn=Button: java.awt.Button: java.awt.Button[button0,0,0,0x0,invalid,label=Push me]
cn=Flower: Flower: pink rose
cn=favorite: Fruit: orange
cn=favDrink: Drink: water
cn=CorbaHello: com.sun.CORBA.idl.CORBAObjectImpl: com.sun.CORBA.idl.CORBAObjectImpl:com.sun.CORBA.idl.GenericCORBAClientSC@8a9e173a
cn=RemoteHello: HelloImpl_Stub: HelloImpl_Stub[RemoteStub [ref: [endpoint:[129.111.111.111:46547](remote),objID:[75de816d:d98468df8c:-8000, 0]]]]
cn=RefHello: HelloImpl_Stub: HelloImpl_Stub[RemoteStub [ref: [endpoint:[129.111.111.111:46550](remote),objID:[272f25a1:d9846946ca:-8000, 0]]]]
cn=Custom: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@db7a1739
cn=John Smith: Person: My name is Smith, John Smith.
cn=RmiiiopHello: com.sun.CORBA.idl.CORBAObjectImpl: com.sun.CORBA.idl.CORBAObjectImpl:com.sun.CORBA.idl.GenericCORBAClientSC@ac6b5dd2
The same notes that apply to the list bindings example also apply to this example.

 

  1. Continue on in this trail to learn how object factories interact with the reading of object representations from the directory.
  2. Go to the Representation in the Directory lesson to read about the physical representation of Java objects in the directory.
  3. Reading Objects from the Directory