Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backported embedded API from master. #28

Open
wants to merge 1 commit into
base: sustaining/3.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions opendj-embedded-server-examples/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#! /bin/sh

# To run the examples in this module, you can either type the following instructions
# in a terminal or run this file

# Build the whole opendj project and then start from the opendj-embedded-server-examples directory
echo ">>> Building OpenDJ project"
#cd ..
#mvn clean install
#cd opendj-embedded-server-examples

# Define the class path to use
export CLASSPATH=target/opendj-embedded-server-examples-4.0.0-SNAPSHOT.jar:../opendj-server-legacy/target/package/opendj/lib/opendj-slf4j-adapter.jar
echo ">>> classpath set to: $CLASSPATH"

# Choose a location for the root directory (where the directory server will be installed)
export EXAMPLE_ROOT_DIR=`pwd`/target/examples/opendj
echo ">>> root directory set to: $EXAMPLE_ROOT_DIR"

# Setup a server from the OpenDJ archive resulting from the build
echo ">>> Example 1: setup a server"
java -cp $CLASSPATH org.forgerock.opendj.examples.SetupServer \
../opendj-server-legacy/target/package/opendj-4.0.0-SNAPSHOT.zip $EXAMPLE_ROOT_DIR

# Then you can run any of the example using the installed server

# Start and stop the server
echo ">>> Example 2: start and stop the server"
java -cp $CLASSPATH org.forgerock.opendj.examples.StartStopServer $EXAMPLE_ROOT_DIR

# Read and update the configuration of the server
echo ">>> Example 3: read and update configuration of the server"
java -cp $CLASSPATH org.forgerock.opendj.examples.ConfigureServer $EXAMPLE_ROOT_DIR "dc=example,dc=com"
91 changes: 91 additions & 0 deletions opendj-embedded-server-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The contents of this file are subject to the terms of the Common Development and
Distribution License (the License). You may not use this file except in compliance with the
License.

You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
specific language governing permission and limitations under the License.

When distributing Covered Software, include this CDDL Header Notice in each file and include
the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
Header, with the fields enclosed by brackets [] replaced by your own identifying
information: "Portions Copyright [year] [name of copyright owner]".

Copyright 2016 ForgeRock AS.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>opendj-parent</artifactId>
<groupId>org.forgerock.opendj</groupId>
<version>4.0.0-M3</version>
</parent>

<artifactId>opendj-embedded-server-examples</artifactId>
<name>Wren:DS Embedded Server Examples</name>
<description>Examples illustrating usage of the Wren:DS embedded server API</description>

<dependencies>
<dependency>
<groupId>org.forgerock.opendj</groupId>
<artifactId>opendj-server-legacy</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>


<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<links>
<link>http://commons.forgerock.org/i18n-framework/i18n-core/apidocs</link>
</links>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.2</version>
<reportSets>
<reportSet>
<reports>
<report>jxr</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
39 changes: 39 additions & 0 deletions opendj-embedded-server-examples/src/main/assembly/examples.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<!--
The contents of this file are subject to the terms of the Common Development and
Distribution License (the License). You may not use this file except in compliance with the
License.

You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
specific language governing permission and limitations under the License.

When distributing Covered Software, include this CDDL Header Notice in each file and include
the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
Header, with the fields enclosed by brackets [] replaced by your own identifying
information: "Portions Copyright [year] [name of copyright owner]".

Copyright 2016 ForgeRock AS.
-->
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>opendj-embedded-server-examples</id>

<includeBaseDirectory>false</includeBaseDirectory>

<fileSets>
<fileSet>
<outputDirectory>resources</outputDirectory>
<directory>src/main/java</directory>
</fileSet>
<fileSet>
<outputDirectory>resources</outputDirectory>
<directory>src/main/javadoc</directory>
</fileSet>
</fileSets>

<formats>
<format>jar</format>
</formats>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2016 ForgeRock AS.
*/

package org.forgerock.opendj.examples;

import static org.forgerock.opendj.server.embedded.ConfigParameters.configParams;
import static org.forgerock.opendj.server.embedded.ConnectionParameters.connectionParams;
import static org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer.manageEmbeddedDirectoryServer;
import static java.util.Arrays.asList;

import java.io.File;
import java.io.IOException;

import org.forgerock.opendj.config.AdminException;
import org.forgerock.opendj.config.client.ManagementContext;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.server.config.client.BackendCfgClient;
import org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer;
import org.forgerock.opendj.server.embedded.EmbeddedDirectoryServerException;

/**
* Provides an example of read and update of the configuration of a server that
* is already installed.
* <p>
* The server may be running or not.
*/
public final class ConfigureServer {

/**
* Main method.
* <p>
* Read the current base Dn of user backend and then change it
* to the one provided as argument.
*
* @param args
* The command line arguments: serverRootDir newBaseDn [ldapPort]
*/
public static void main(final String[] args) {
if (args.length != 2 && args.length != 3) {
System.err.println("Usage: serverRootDir newBaseDn [ldapPort]");
System.exit(1);
}
final String serverRootDir = args[0];
final String newBaseDn = args[1];
final int ldapPort = args.length > 2 ? Integer.parseInt(args[2]) : 1500;

EmbeddedDirectoryServer server =
manageEmbeddedDirectoryServer(
configParams()
.serverRootDirectory(serverRootDir)
.configurationFile(serverRootDir + File.separator + "config/config.ldif"),
connectionParams()
.hostName("localhost")
.ldapPort(ldapPort)
.bindDn("cn=Directory Manager")
.bindPassword("password"),
System.out,
System.err);

// read the current base DN(s) of user backend and update it
try (ManagementContext config = server.getConfiguration()) {
BackendCfgClient userRoot = config.getRootConfiguration().getBackend("userRoot");
System.out.println("The current base Dn(s) of the user backend are: " + userRoot.getBaseDN());
userRoot.setBaseDN(asList(DN.valueOf(newBaseDn)));
userRoot.commit();
System.out.println("The base Dn of the user backend has been set to: " + newBaseDn);
} catch (AdminException | IOException | EmbeddedDirectoryServerException e) {
System.err.println("A problem occured when reading/updating configuration: " + e.toString());
}
}

private ConfigureServer() {
// Not used.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2016 ForgeRock AS.
*/

package org.forgerock.opendj.examples;

import static org.forgerock.opendj.server.embedded.ConfigParameters.configParams;
import static org.forgerock.opendj.server.embedded.ConnectionParameters.connectionParams;
import static org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer.manageEmbeddedDirectoryServer;
import static org.forgerock.opendj.server.embedded.SetupParameters.setupParams;

import java.io.File;

import org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer;
import org.forgerock.opendj.server.embedded.EmbeddedDirectoryServerException;

/**
* Setup a server from a OpenDJ archive using the EmbeddedDirectoryServer class.
*/
public final class SetupServer {

/**
* Main method.
* <p>
* The OpenDJ archive is the zip archive that is resulting from a maven build.
*
* The server root directory is the directory where OpenDJ will be installed. Because
* the archive contains the "opendj" directory, it is mandatory to provide a server root
* directory that is named "opendj" (the archive will be automatically extracted in the
* parent directory of the provided server root directory).
*
* Other parameters are usual parameters to setup a server.
*
* @param args
* The command line arguments: openDJArchive, serverRootDir
* and optionally: baseDn, backendType, ldapPort, adminPort, jmxPort
* @throws EmbeddedDirectoryServerException
* If an error occurs
*/
public static void main(final String[] args) throws EmbeddedDirectoryServerException {
if (args.length != 2 && args.length != 4 && args.length != 7) {
System.err.println("Usage: openDJArchive serverRootDir "
+ "[baseDn backendType [ldapPort adminPort jmxPort]]");
System.exit(1);
}

int i = 0;
final String openDJArchive = args[i++];
final String serverRootDir = args[i++];
final String baseDn = (args.length > i) ? args[i++] : "o=example";
final String backendType = (args.length > i) ? args[i++] : "pdb";
final int ldapPort = (args.length > i) ? Integer.parseInt(args[i++]) : 1500;
final int adminPort = (args.length > i) ? Integer.parseInt(args[i++]) : 4500;
final int jmxPort = (args.length > i) ? Integer.parseInt(args[i++]) : 1600;

performSetup(openDJArchive, serverRootDir, baseDn, backendType, ldapPort, adminPort, jmxPort);
}

/** Performs the setup with provided parameters. */
static void performSetup(final String openDJArchive, final String serverRootDir, final String baseDn,
final String backendType, final int ldapPort, final int adminPort, final int jmxPort)
throws EmbeddedDirectoryServerException {
EmbeddedDirectoryServer server =
manageEmbeddedDirectoryServer(
configParams()
.serverRootDirectory(serverRootDir)
.configurationFile(serverRootDir + File.separator + "config/config.ldif"),
connectionParams()
.hostName("localhost")
.ldapPort(ldapPort)
.bindDn("cn=Directory Manager")
.bindPassword("password")
.adminPort(adminPort),
System.out,
System.err);

server.extractArchiveForSetup(new File(openDJArchive));
server.setup(
setupParams()
.baseDn(baseDn)
.backendType(backendType)
.jmxPort(jmxPort));
}

private SetupServer() {
// Not used.
}
}
Loading