Penrose 0.9.9 Sample Code

Penrose Server distribution includes a sample program in the src/examples directory.
https://svn.safehaus.org/repos/penrose/trunk/src/examples/Demo.java

Creating Penrose Configuration

In this code it creates a new configuration based on the default one, then it adds the example.schema and the sample partition in samples/conf.

PenroseConfig penroseConfig = new DefaultPenroseConfig();

SchemaConfig schemaConfig = new SchemaConfig("samples/schema/example.schema");
penroseConfig.addSchemaConfig(schemaConfig);

PartitionConfig partitionConfig = new PartitionConfig("example", "samples/conf");
penroseConfig.addPartitionConfig(partitionConfig);

Starting Penrose Service

The code starts Penrose Service with the configuration previously created:

Penrose penrose = new Penrose(penroseConfig);
penrose.start();

PenroseSession session = penrose.newSession();

Performing LDAP Operations

To interact with Penrose Service, first you will need to create a session, then use the session to execute LDAP operations. The following code performs a bind operation, followed by a search operation, and finally an unbind operation.

PenroseSession session = penrose.newSession();
session.bind("uid=admin,ou=system", "secret");

PenroseSearchControls sc = new PenroseSearchControls();
sc.setScope(PenroseSearchControls.SCOPE_ONE);

PenroseSearchResults results = session.search(
    "ou=Categories,"+SUFFIX,
    "(objectClass=*)",
    sc);

for (Iterator i = results.iterator(); i.hasNext();) {
    LDAPEntry entry = (LDAPEntry) i.next();
    System.out.println(toString(entry));
}

session.unbind();
session.close();

Stopping Penrose Service

Stop Penrose Service to kill all threads.

penrose.stop();