import java.io.File; import java.net.Socket; import netscape.ldap.*; import org.opends.server.protocols.internal.InternalLDAPSocket; import org.opends.server.types.DirectoryEnvironmentConfig; import org.opends.server.util.EmbeddedUtils; public class EmbeddedMozillaLDAPSDKTest implements LDAPSocketFactory { /** * Creates a new instance of this embedded test and runs that test. * * @param args The command-line arguments provided to this program. */ public static void main(String[] args) throws Exception { try { new EmbeddedTest().run(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } /** * Performs the actual processing for this test. * * @throws Exception If an unexpected problem occurs. */ public void run() throws Exception { // First, start the Directory Server. System.out.println("Going to start the Directory Server..."); DirectoryEnvironmentConfig envConfig = new DirectoryEnvironmentConfig(); envConfig.setServerRoot(new File(".")); envConfig.setDisableConnectionHandlers(true); EmbeddedUtils.startServer(envConfig); System.out.println(" Successfully started the server."); // Create a new LDAP connection using the OpenDS internal LDAP socket // factory. Connect and bind to the server as a root user. System.out.println("Going to connect and authenticate..."); LDAPConnection conn = new LDAPConnection(this); conn.connect(3, "doesntmatter", 389, "cn=Directory Manager", "password"); System.out.println(" Successfully connected and authenticated"); // Use the connection to add a new entry to the server. System.out.println("Going to add an entry dc=example,dc=com..."); LDAPAttributeSet attrSet = new LDAPAttributeSet(); attrSet.add(new LDAPAttribute("objectClass", new String[] { "top", "domain" })); attrSet.add(new LDAPAttribute("dc", "example")); LDAPEntry entry = new LDAPEntry("dc=example,dc=com", attrSet); conn.add(entry); System.out.println(" Successfully added the entry"); // Perform a search operation to read the entry back. System.out.println("Going to perform a search for the entry..."); LDAPSearchResults results = conn.search("dc=example,dc=com", LDAPConnection.SCOPE_BASE, "(objectClass=*)", null, false); while (results.hasMoreElements()) { LDAPEntry e = results.next(); System.out.println(" Found matching entry " + e.getDN() + "."); } System.out.println(" Search operation complete."); // Perform a compare against the entry. System.out.println("Going to perform a compare against the entry..."); if (conn.compare("dc=example,dc=com", new LDAPAttribute("dc", "example"))) { System.out.println(" The compare matched the entry."); } else { System.out.println(" The compare did not match the entry."); } // Modify the entry. System.out.println("Going to modify the entry..."); conn.modify("dc=example,dc=com", new LDAPModification(LDAPModification.REPLACE, new LDAPAttribute("description", "foo"))); System.out.println(" Successfully updated the entry."); // Delete the entry. System.out.println("Going to delete the entry..."); conn.delete("dc=example,dc=com"); System.out.println(" Successfully deleted the entry."); // Close the connection to the server. System.out.println("Going to disconnect..."); conn.disconnect(); System.out.println(" Successfully disconnected from the server."); // Stop the Directory Server. System.out.println("Going to stop the Directory Server..."); EmbeddedUtils.stopServer(null, null); System.out.println(" Successfully stopped the server."); } /** * Creates a socket for use by the Mozilla LDAP SDK for Java. In this case, * the socket will be an OpenDS internal LDAP socket, which can be used to * perform internal operations using a standard LDAP SDK, as long as that SDK * provides the ability to use a custom socket factory. * * @param host The address to which the connection should be established. * @param port The port to which the connection should be established. * * @return A socket for use by the Mozilla LDAP SDK for Java. */ public Socket makeSocket(String host, int port) { return new InternalLDAPSocket(); } }