From 554d4e007681f0997df54a084765a92b9892ea9b Mon Sep 17 00:00:00 2001 From: Jasper Siepkes Date: Sat, 23 Feb 2019 15:29:21 +0100 Subject: [PATCH] Backported embedded API from master. Adds new classes to hold parameters of operations on the directory server: ConnectionParameters, ImportParameters, RebuildIndexParameters, ReplicationParameters, SetupParameters, UpgradeParameters. --- opendj-embedded-server-examples/README | 33 + opendj-embedded-server-examples/pom.xml | 91 +++ .../src/main/assembly/examples.xml | 39 ++ .../opendj/examples/ConfigureServer.java | 88 +++ .../opendj/examples/SetupServer.java | 100 +++ .../opendj/examples/StartStopServer.java | 75 +++ .../opendj/examples/package-info.java | 21 + .../src/main/javadoc/overview.html | 23 + .../src/site/site.xml | 33 + .../src/site/xdoc/index.xml.vm | 47 ++ opendj-server-legacy/pom.xml | 7 - .../server/embedded/ConfigParameters.java | 117 ++++ .../server/embedded/ConnectionParameters.java | 202 ++++++ .../embedded/EmbeddedDirectoryServer.java | 601 ++++++++++++++++++ .../EmbeddedDirectoryServerException.java | 52 ++ .../server/embedded/ImportParameters.java | 87 +++ .../embedded/RebuildIndexParameters.java | 65 ++ .../embedded/ReplicationParameters.java | 201 ++++++ .../server/embedded/SetupParameters.java | 148 +++++ .../server/embedded/UpgradeParameters.java | 75 +++ .../opendj/server/embedded/package-info.java | 17 + .../org/opends/server/util/EmbeddedUtils.java | 17 +- .../org/opends/server/util/StaticUtils.java | 98 +++ .../org/opends/messages/utility.properties | 39 +- pom.xml | 1 + 25 files changed, 2266 insertions(+), 11 deletions(-) create mode 100755 opendj-embedded-server-examples/README create mode 100644 opendj-embedded-server-examples/pom.xml create mode 100644 opendj-embedded-server-examples/src/main/assembly/examples.xml create mode 100644 opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/ConfigureServer.java create mode 100644 opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/SetupServer.java create mode 100644 opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java create mode 100644 opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/package-info.java create mode 100644 opendj-embedded-server-examples/src/main/javadoc/overview.html create mode 100644 opendj-embedded-server-examples/src/site/site.xml create mode 100644 opendj-embedded-server-examples/src/site/xdoc/index.xml.vm create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConfigParameters.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConnectionParameters.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServerException.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ImportParameters.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/RebuildIndexParameters.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ReplicationParameters.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/SetupParameters.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/UpgradeParameters.java create mode 100644 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/package-info.java diff --git a/opendj-embedded-server-examples/README b/opendj-embedded-server-examples/README new file mode 100755 index 0000000000..0d70067c72 --- /dev/null +++ b/opendj-embedded-server-examples/README @@ -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" \ No newline at end of file diff --git a/opendj-embedded-server-examples/pom.xml b/opendj-embedded-server-examples/pom.xml new file mode 100644 index 0000000000..335a47d7a6 --- /dev/null +++ b/opendj-embedded-server-examples/pom.xml @@ -0,0 +1,91 @@ + + + + 4.0.0 + + + opendj-parent + org.forgerock.opendj + 4.0.0-M3 + + + opendj-embedded-server-examples + Wren:DS Embedded Server Examples + Examples illustrating usage of the Wren:DS embedded server API + + + + org.forgerock.opendj + opendj-server-legacy + ${project.version} + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + false + + + + package + + shade + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + http://commons.forgerock.org/i18n-framework/i18n-core/apidocs + + + + + + org.apache.maven.plugins + maven-jxr-plugin + 2.2 + + + + jxr + + + + + + + diff --git a/opendj-embedded-server-examples/src/main/assembly/examples.xml b/opendj-embedded-server-examples/src/main/assembly/examples.xml new file mode 100644 index 0000000000..d1cb685b28 --- /dev/null +++ b/opendj-embedded-server-examples/src/main/assembly/examples.xml @@ -0,0 +1,39 @@ + + + + opendj-embedded-server-examples + + false + + + + resources + src/main/java + + + resources + src/main/javadoc + + + + + jar + + diff --git a/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/ConfigureServer.java b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/ConfigureServer.java new file mode 100644 index 0000000000..8b398442cb --- /dev/null +++ b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/ConfigureServer.java @@ -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. + *

+ * The server may be running or not. + */ +public final class ConfigureServer { + + /** + * Main method. + *

+ * 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. + } +} diff --git a/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/SetupServer.java b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/SetupServer.java new file mode 100644 index 0000000000..2e8cddecc6 --- /dev/null +++ b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/SetupServer.java @@ -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. + *

+ * 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. + } +} diff --git a/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java new file mode 100644 index 0000000000..d835d2537c --- /dev/null +++ b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java @@ -0,0 +1,75 @@ +/* + * 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.EmbeddedDirectoryServer.*; + +import java.nio.file.Paths; + +import org.forgerock.i18n.LocalizableMessage; +import org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer; +import org.forgerock.opendj.server.embedded.EmbeddedDirectoryServerException; + +/** + * Start and stop a server that is already installed. + */ +public final class StartStopServer { + + /** + * Main method. + *

+ * The server is started, and this program waits for a Control-C on the terminal to stop the server. + * + * @param args + * The command line arguments: serverRootDir + * @throws EmbeddedDirectoryServerException + * If an error occurs + */ + public static void main(final String[] args) throws EmbeddedDirectoryServerException { + if (args.length != 1) { + System.err.println("Usage: serverRootDir"); + System.exit(1); + } + final String serverRootDir = args[0]; + + final EmbeddedDirectoryServer server = + manageEmbeddedDirectoryServerForRestrictedOps( + configParams() + .serverRootDirectory(serverRootDir) + .configurationFile(Paths.get(serverRootDir, "config", "config.ldif").toString()), + System.out, + System.err); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + System.out.println("Shutting down ..."); + server.stop(StartStopServer.class.getName(), + LocalizableMessage.raw("Stopped after receiving Control-C")); + } + }); + + System.out.println("Starting the server..."); + server.start(); + System.out.println("Type Ctrl-C to stop the server"); + } + + private StartStopServer() { + // Not used. + } +} diff --git a/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/package-info.java b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/package-info.java new file mode 100644 index 0000000000..b28a9a7901 --- /dev/null +++ b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/package-info.java @@ -0,0 +1,21 @@ +/* + * 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. + */ + +/** + * Classes providing examples of usage of Embedded Server API. + */ +package org.forgerock.opendj.examples; + diff --git a/opendj-embedded-server-examples/src/main/javadoc/overview.html b/opendj-embedded-server-examples/src/main/javadoc/overview.html new file mode 100644 index 0000000000..68bc6eb448 --- /dev/null +++ b/opendj-embedded-server-examples/src/main/javadoc/overview.html @@ -0,0 +1,23 @@ + + + + + + The OpenDJ Embedded Server Examples contains various examples illustrating + usage of the Embedded Server API. + + diff --git a/opendj-embedded-server-examples/src/site/site.xml b/opendj-embedded-server-examples/src/site/site.xml new file mode 100644 index 0000000000..474c753eb0 --- /dev/null +++ b/opendj-embedded-server-examples/src/site/site.xml @@ -0,0 +1,33 @@ + + + + + + + lt.velykis.maven.skins + blank-maven-skin + 1.0.0 + + + diff --git a/opendj-embedded-server-examples/src/site/xdoc/index.xml.vm b/opendj-embedded-server-examples/src/site/xdoc/index.xml.vm new file mode 100644 index 0000000000..8d56de9e34 --- /dev/null +++ b/opendj-embedded-server-examples/src/site/xdoc/index.xml.vm @@ -0,0 +1,47 @@ + + + + + About ${project.name} + ${project.organization.name} + + +

+ +

+ The following examples use the OpenDJ Embedded Server API: +

+ + + +
+ + diff --git a/opendj-server-legacy/pom.xml b/opendj-server-legacy/pom.xml index 4990fa4eea..884068862c 100644 --- a/opendj-server-legacy/pom.xml +++ b/opendj-server-legacy/pom.xml @@ -194,13 +194,6 @@ test - - org.forgerock.opendj - opendj-core - test-jar - test - - org.freemarker freemarker diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConfigParameters.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConfigParameters.java new file mode 100644 index 0000000000..f7259d0782 --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConfigParameters.java @@ -0,0 +1,117 @@ +/* + * 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.server.embedded; + +/** Parameters to configure a directory server. */ +public final class ConfigParameters +{ + private String serverRootDirectory; + private String serverInstanceDirectory; + private String configurationFile; + private boolean disableConnectionHandlers; + + private ConfigParameters() + { + // prefer usage of static method for creation + } + + /** + * Creates configuration parameters. + * + * @return the parameters + */ + public static ConfigParameters configParams() + { + return new ConfigParameters(); + } + + String getServerRootDirectory() + { + return serverRootDirectory; + } + + /** This value may be {@code null}, it must always be checked. */ + String getServerInstanceDirectory() + { + return serverInstanceDirectory; + } + + String getConfigurationFile() + { + return configurationFile; + } + + boolean isDisableConnectionHandlers() + { + return disableConnectionHandlers; + } + + /** + * Sets the server root directory of the directory server. + *

+ * The server root is the location where the binaries and default configuration is stored. + * + * @param dir + * the server root directory + * @return this builder + */ + public ConfigParameters serverRootDirectory(String dir) + { + serverRootDirectory = dir; + return this; + } + + /** + * Sets the install root directory of the directory server. + *

+ * The install root is the location where the data and live configuration is stored. + * + * @param dir + * the install root directory + * @return this builder + */ + public ConfigParameters serverInstanceDirectory(String dir) + { + serverInstanceDirectory = dir; + return this; + } + + /** + * Sets the path of the configuration file of the directory server. + * + * @param file + * the configuration file + * @return this builder + */ + public ConfigParameters configurationFile(String file) + { + configurationFile = file; + return this; + } + + /** + * Sets the indicator allowing to disable the connection handlers. + * + * @param disable + * {@code true} to disable the connection handlers + * @return this builder + */ + public ConfigParameters disableConnectionHandlers(boolean disable) + { + disableConnectionHandlers = disable; + return this; + } +} \ No newline at end of file diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConnectionParameters.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConnectionParameters.java new file mode 100644 index 0000000000..9b284dabe7 --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ConnectionParameters.java @@ -0,0 +1,202 @@ +/* + * 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.server.embedded; + +/** Parameters to establish connections to a directory server. */ +public final class ConnectionParameters +{ + private String adminPassword; + private Integer adminPort; + private String adminUid; + private String bindDn; + private String bindPassword; + private String hostName; + private Integer ldapPort; + private Integer ldapsPort; + + private ConnectionParameters() + { + // prefer usage of static method for creation + } + + /** + * Creates connection parameters. + * + * @return the parameters + */ + public static ConnectionParameters connectionParams() + { + return new ConnectionParameters(); + } + + @Override + public String toString() + { + return "ConnectionParameters [" + + "host name=" + hostName + + ", ldap port=" + ldapPort + + ", ldaps port=" + ldapsPort + + ", admin port=" + adminPort + + ", bind DN=" + bindDn + + ", admin uid=" + adminUid + + "]"; + + } + + String getAdminPassword() + { + return adminPassword; + } + + Integer getAdminPort() + { + return adminPort; + } + + String getAdminUid() + { + return adminUid; + } + + String getBindDn() + { + return bindDn; + } + + String getBindPassword() + { + return bindPassword; + } + + String getHostName() + { + return hostName; + } + + Integer getLdapPort() + { + return ldapPort; + } + + Integer getLdapSecurePort() + { + return ldapsPort; + } + + /** + * Sets the password of the Global Administrator to use to bind to the server. + * + * @param password + * the password + * @return this builder + */ + public ConnectionParameters adminPassword(String password) + { + adminPassword = password; + return this; + } + + /** + * Sets the port for directory server administration. + * + * @param port + * the admin port + * @return this builder + */ + public ConnectionParameters adminPort(int port) + { + adminPort = port; + return this; + } + + /** + * Sets the user id of the Global Administrator to use to bind to the server. + * + * @param uid + * the user id + * @return this builder + */ + public ConnectionParameters adminUid(String uid) + { + adminUid = uid; + return this; + } + + /** + * Sets the Dn to use to bind to the directory server. + * + * @param dn + * the bind Dn + * @return this builder + */ + public ConnectionParameters bindDn(String dn) + { + bindDn = dn; + return this; + } + + /** + * Sets the password to use to bind to the directory server. + * + * @param password + * the bind password + * @return this builder + */ + public ConnectionParameters bindPassword(String password) + { + bindPassword = password; + return this; + } + + /** + * Sets the the fully-qualified directory server host name. + * + * @param hostName + * the hostName of the server + * @return this builder + */ + public ConnectionParameters hostName(String hostName) + { + this.hostName = hostName; + return this; + } + + /** + * Sets the port on which the directory server listens for LDAP communication. + * + * @param port + * the LDAP port + * @return this builder + */ + public ConnectionParameters ldapPort(int port) + { + ldapPort = port; + return this; + } + + /** + * Sets the port on which the directory server listens for LDAPS (secure) communication. + * + * @param port + * the LDAPS port + * @return this builder + */ + public ConnectionParameters ldapSecurePort(int port) + { + ldapsPort = port; + return this; + } +} diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java new file mode 100644 index 0000000000..b0c929cefd --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java @@ -0,0 +1,601 @@ +/* + * 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.server.embedded; + +import static org.opends.server.util.ServerConstants.*; +import static org.forgerock.opendj.config.client.ldap.LDAPManagementContext.newManagementContext; +import static org.forgerock.opendj.config.client.ldap.LDAPManagementContext.newLDIFManagementContext; +import static org.opends.messages.UtilityMessages.*; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +import org.forgerock.i18n.LocalizableMessage; +import org.forgerock.opendj.adapter.server3x.Adapters; +import org.forgerock.opendj.config.LDAPProfile; +import org.forgerock.opendj.config.client.ManagementContext; +import org.forgerock.opendj.config.server.ConfigException; +import org.forgerock.opendj.ldap.Connection; +import org.forgerock.opendj.ldap.DN; +import org.forgerock.util.Reject; +import org.opends.quicksetup.TempLogFile; +import org.opends.server.config.ConfigConstants; +import org.opends.server.core.DirectoryServer; +import org.opends.server.protocols.internal.InternalClientConnection; +import org.opends.server.tools.ImportLDIF; +import org.opends.server.tools.InstallDS; +import org.opends.server.tools.RebuildIndex; +import org.opends.server.tools.dsreplication.ReplicationCliMain; +import org.opends.server.tools.upgrade.UpgradeCli; +import org.opends.server.types.DirectoryEnvironmentConfig; +import org.opends.server.types.DirectoryException; +import org.opends.server.types.InitializationException; +import org.opends.server.util.BuildVersion; +import org.opends.server.util.DynamicConstants; +import org.opends.server.util.StaticUtils; + +/** + * Represents an embedded directory server on which high-level operations + * are available (setup, upgrade, start, stop, ...). + */ +public class EmbeddedDirectoryServer +{ + private static final String EMBEDDED_OPEN_DJ_PREFIX = "embeddedOpenDJ"; + private static final String ARCHIVE_ROOT_DIRECTORY = DynamicConstants.SHORT_NAME.toLowerCase(); + private static final String QUICKSETUP_ROOT_PROPERTY = "org.opends.quicksetup.Root"; + private static final String QUICKSETUP_INSTANCE_PROPERTY = "org.opends.quicksetup.instance"; + + /** List of directories resulting from the OpenDJ archive where all files should be set as executable. */ + private static final List EXECUTABLE_DJ_DIRECTORIES = Arrays.asList("opendj/bin"); + /** List of individual files resulting from the OpenDJ archive which should be set as executable. */ + private static final List EXECUTABLE_DJ_FILES = + Arrays.asList("opendj/setup", "opendj/upgrade", "opendj/uninstall", "opendj/setup.bat", "opendj/upgrade.bat", + "opendj/uninstall.bat"); + + /** The parameters for install and instance directories, and configuration file of the server. */ + private final ConfigParameters configParams; + + /** The connection parameters for the server. */ + private final ConnectionParameters connectionParams; + + /** The output stream used for feedback during operations on server. */ + private final OutputStream outStream; + + /** The error stream used for feedback during operations on server. */ + private final OutputStream errStream; + + private EmbeddedDirectoryServer(ConfigParameters configParams, ConnectionParameters connParams, + OutputStream out, OutputStream err) + { + Reject.ifNull(configParams.getServerRootDirectory()); + if (connParams != null) + { + Reject.ifNull( + connParams.getHostName(), + connParams.getLdapPort(), + connParams.getBindDn(), + connParams.getBindPassword()); + } + if (configParams.getConfigurationFile() == null) + { + // use the default path if configuration file is not provided + configParams.configurationFile(getDefaultConfigurationFilePath(configParams.getServerRootDirectory())); + } + this.configParams = configParams; + this.connectionParams = connParams; + this.outStream = out; + this.errStream = err; + System.setProperty(PROPERTY_SERVER_ROOT, configParams.getServerRootDirectory()); + System.setProperty(QUICKSETUP_ROOT_PROPERTY, configParams.getServerRootDirectory()); + String instanceDir = configParams.getServerInstanceDirectory() != null ? + configParams.getServerInstanceDirectory() : + configParams.getServerRootDirectory(); + System.setProperty(QUICKSETUP_INSTANCE_PROPERTY, instanceDir); + System.setProperty(PROPERTY_INSTANCE_ROOT, instanceDir); + + } + + private EmbeddedDirectoryServer(ConfigParameters configParams, ConnectionParameters connParams) + { + this(configParams, connParams, System.out, System.err); + } + + private static String getDefaultConfigurationFilePath(String serverRootDirectory) + { + return Paths.get(serverRootDirectory) + .resolve(ConfigConstants.CONFIG_DIR_NAME) + .resolve(ConfigConstants.CONFIG_FILE_NAME) + .toString(); + } + + /** + * Creates an instance of an embedded directory server for any operation. + * + * @param configParams + * The basic configuration parameters for the server. + * @param connParams + * The connection parameters for the server. + * @param out + * Output stream used for feedback during operations on server + * @param err + * Error stream used for feedback during operations on server + * @return the embedded directory server + */ + public static EmbeddedDirectoryServer manageEmbeddedDirectoryServer(ConfigParameters configParams, + ConnectionParameters connParams, OutputStream out, OutputStream err) + { + return new EmbeddedDirectoryServer(configParams, connParams, out, err); + } + + /** + * Creates an instance of an embedded directory server to perform a restricted set of operations that + * do not need connection parameters. + *

+ * The following operations won't be allowed because they require the connection parameters: setup, + * configureReplication, initializeReplication, isReplicationRunning, importLDIF + * and getInternalConnection without argument + * ({@code getInternalConnection(DN)} method is allowed). + *

+ * To be able to perform any operation on the server, use the alternative {@code manageEmbeddedDirectoryServer()} + * method. + * + * @param configParams + * The basic configuration parameters for the server. + * @param out + * Output stream used for feedback during operations on server + * @param err + * Error stream used for feedback during operations on server + * @return the directory server + */ + public static EmbeddedDirectoryServer manageEmbeddedDirectoryServerForRestrictedOps(ConfigParameters configParams, + OutputStream out, OutputStream err) + { + return new EmbeddedDirectoryServer(configParams, null, out, err); + } + + /** + * Creates an instance of an embedded directory server to perform a restricted set of operations that + * do not need connection parameters. + *

+ * The following operations won't be allowed because they require the connection parameters: setup, + * configureReplication, initializeReplication, isReplicationRunning, importLDIF + * and getInternalConnection without argument + * ({@code getInternalConnection(DN)} method is allowed). + *

+ * To be able to perform any operation on the server, use the alternative {@code manageEmbeddedDirectoryServer()} + * method. + *

+ * Standard out and error streams will be used for any output during operations on the embedded server. + * + * @param configParams + * The basic configuration parameters for the server. + * @return the directory server + */ + public static EmbeddedDirectoryServer manageEmbeddedDirectoryServerForRestrictedOps( + ConfigParameters configParams) + { + return new EmbeddedDirectoryServer(configParams, null, System.out, System.err); + } + + /** + * Configures replication between this directory server (first server) and another server + * (second server). + *

+ * This method updates the configuration of the servers to replicate the data under the + * base DN specified in the parameters. + * + * @param parameters + * The parameters for the replication. + * @throws EmbeddedDirectoryServerException + * If a problem occurs. + */ + public void configureReplication(ReplicationParameters parameters) throws EmbeddedDirectoryServerException + { + Reject.checkNotNull(connectionParams); + int returnCode = ReplicationCliMain.mainCLI( + parameters.toCommandLineArgumentsConfiguration(configParams.getConfigurationFile(), connectionParams), + !isRunning(), outStream, errStream); + + if (returnCode != 0) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_CONFIGURE_REPLICATION.get( + configParams.getServerRootDirectory(), parameters.getReplicationPortSource(), + parameters.getHostNameDestination(), parameters.getReplicationPortDestination(), returnCode)); + } + } + + /** + * Returns the build version of the directory server as a String. + *

+ * This version corresponds to the instance version, as written in the "config/buildinfo" file. + * + * @return the version + * @throws EmbeddedDirectoryServerException + * If an error occurs while retrieving the version + */ + public String getBuildVersion() throws EmbeddedDirectoryServerException + { + try + { + return BuildVersion.instanceVersion().toString(); + } + catch (InitializationException e) { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_BUILD_VERSION.get(e.getLocalizedMessage()), e); + } + } + + /** + * Returns the configuration of this server, which can be read or updated. + *

+ * The returned object is an instance of {@code ManagementContext} class, which allow access to the + * root configuration object using its {@code getRootConfiguration()} method. + * Starting from the root configuration, it is possible to access any configuration object, in order + * to perform read or update operations. + *

+ * Note that {@code ManagementContext} instance must be closed after usage. It is recommended to use + * it inside a try-with-resource statement. + *

+ * Example reading configuration: + *

+   *   try(ManagementContext config = server.getConfiguration()) {
+   *      List syncProviders = config.getRootConfiguration().listSynchronizationProviders();
+   *      System.out.println("sync providers=" + syncProviders);
+   *   }
+   * 
+ *

+ * Example updating configuration: + *

+   *   try(ManagementContext config = server.getConfiguration()) {
+   *      JEBackendCfgClient userRoot = (JEBackendCfgClient) config.getRootConfiguration().getBackend("userRoot");
+   *      userRoot.setBaseDN(Arrays.asList(DN.valueOf("dc=example,dc=com")));
+   *      userRoot.setDBCachePercent(70);
+   *      // changes must be committed to be effective
+   *      userRoot.commit();
+   *   }
+   * 
+ * @return the management context object which gives direct access to the root configuration of the server + * @throws EmbeddedDirectoryServerException + * If the retrieval of the configuration fails + */ + public ManagementContext getConfiguration() throws EmbeddedDirectoryServerException + { + try + { + if (isRunning()) + { + Connection ldapConnection = getInternalConnection(); + return newManagementContext(ldapConnection, LDAPProfile.getInstance()); + } + return newLDIFManagementContext(new File(configParams.getConfigurationFile())); + } + catch (IOException e) + { + throw new EmbeddedDirectoryServerException( + ERR_EMBEDDED_SERVER_LDIF_MANAGEMENT_CONTEXT.get(configParams.getConfigurationFile())); + } + } + + /** + * Returns an internal connection to the server that will be authenticated automatically with + * the bind DN defined for this server. + *

+ * This method is available only if connection parameters are provided for the server. + * If the connection must be authenticated with another DN or if no connection parameters have been + * provided, use the alternate method {@code getInternalConnection(DN userDn)}. + * + * @return the connection + * @throws EmbeddedDirectoryServerException + * If the connection can't be returned + */ + public Connection getInternalConnection() throws EmbeddedDirectoryServerException + { + Reject.checkNotNull(connectionParams); + return getInternalConnection(DN.valueOf(connectionParams.getBindDn())); + } + + /** + * Returns an internal connection to the server that will be authenticated as the specified user. + * + * @param userDn + * The user to be used for authentication to the server + * @return the connection + * @throws EmbeddedDirectoryServerException + * If the connection can't be returned + */ + public Connection getInternalConnection(DN userDn) throws EmbeddedDirectoryServerException + { + try + { + return Adapters.newConnection(new InternalClientConnection(userDn)); + } + catch (DirectoryException e) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_INTERNAL_CONNECTION.get(userDn)); + } + } + + /** + * Imports LDIF data to the directory server, overwriting existing data. + *

+ * The import is implemented only for online use. + * + * @param parameters + * The import parameters. + * @throws EmbeddedDirectoryServerException + * If the import fails + */ + public void importLDIF(ImportParameters parameters) throws EmbeddedDirectoryServerException + { + checkServerIsRunning(); + Reject.checkNotNull(connectionParams); + int returnCode = ImportLDIF.mainImportLDIF( + parameters.toCommandLineArguments(configParams.getConfigurationFile(), connectionParams), + !isRunning(), outStream, errStream); + + if (returnCode != 0) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_IMPORT_DATA.get( + parameters.getLdifFile(), configParams.getServerRootDirectory(), returnCode)); + } + } + + /** + * Initializes replication between this server and another server. + * + * @param parameters + * The parameters for the replication. + * @throws EmbeddedDirectoryServerException + * If a problem occurs. + */ + public void initializeReplication(ReplicationParameters parameters) throws EmbeddedDirectoryServerException + { + Reject.checkNotNull(connectionParams); + int returnCode = ReplicationCliMain.mainCLI( + parameters.toCommandLineArgumentsInitialize(configParams.getConfigurationFile(), connectionParams), + !isRunning(), outStream, errStream); + + if (returnCode != 0) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_INITIALIZE_REPLICATION.get( + configParams.getServerRootDirectory(), connectionParams.getAdminPort(), parameters.getHostNameDestination(), + parameters.getAdminPortDestination(), returnCode)); + } + } + + /** + * Indicates whether replication is currently running for the embedded server. + * + * @return {@code true} if replication is running, {@code false} otherwise + */ + public boolean isReplicationRunning() + { + Reject.checkNotNull(connectionParams); + int returnCode = ReplicationCliMain.mainCLI( + ReplicationParameters.replicationParams() + .toCommandLineArgumentsStatus(configParams.getConfigurationFile(), connectionParams), + !isRunning(), outStream, errStream); + return returnCode == 0; + } + + /** + * Indicates whether this server is currently running. + * + * @return {@code true} if the server is currently running, or {@code false} if not. + */ + public boolean isRunning() + { + return DirectoryServer.isRunning(); + } + + /** + * Set this server up from the root directory. + *

+ * As a pre-requisite, the OpenDJ archive must have been previously extracted to some + * directory. To extract an archive for setup, see {@code extractArchiveForSetup()}. + * + * @param parameters + * The setup parameters. + * @throws EmbeddedDirectoryServerException + * If the setup fails for any reason. + */ + public void setup(SetupParameters parameters) throws EmbeddedDirectoryServerException + { + Reject.checkNotNull(connectionParams); + int returnCode = InstallDS.mainCLI(parameters.toCommandLineArguments(connectionParams), outStream, errStream, + TempLogFile.newTempLogFile(EMBEDDED_OPEN_DJ_PREFIX)); + + if (returnCode != 0) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_SETUP.get( + configParams.getServerRootDirectory(), parameters.getBaseDn(), parameters.getBackendType(), returnCode)); + } + } + + /** + * Extracts the provided archive to the appropriate root directory of the server. + *

+ * As the DJ archive includes the "opendj" directory, it is mandatory to have + * the root directory named after it when using this method. + * + * @param openDJZipFile + * The OpenDJ server archive. + * @throws EmbeddedDirectoryServerException + * If the extraction of the archive fails. + */ + public void extractArchiveForSetup(File openDJZipFile) throws EmbeddedDirectoryServerException + { + try + { + File serverRoot = new File(configParams.getServerRootDirectory()); + if (!ARCHIVE_ROOT_DIRECTORY.equals(serverRoot.getName())) + { + throw new EmbeddedDirectoryServerException( + ERR_EMBEDDED_SERVER_ARCHIVE_SETUP_WRONG_ROOT_DIRECTORY.get(ARCHIVE_ROOT_DIRECTORY, serverRoot)); + } + // the directory where the zip file is extracted should be one level up from the server root. + File deployDirectory = serverRoot.getParentFile(); + StaticUtils.extractZipArchive(openDJZipFile, deployDirectory, EXECUTABLE_DJ_DIRECTORIES, EXECUTABLE_DJ_FILES); + } + catch (IOException e) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_SETUP_EXTRACT_ARCHIVE.get( + openDJZipFile, configParams.getServerRootDirectory(), StaticUtils.stackTraceToSingleLineString(e))); + } + } + + /** + * Rebuilds all the indexes of this server. + *

+ * This operation is done offline, hence the server must be stopped when calling this method. + * + * @param parameters + * The parameters for rebuilding the indexes. + * @throws EmbeddedDirectoryServerException + * If an error occurs. + */ + public void rebuildIndex(RebuildIndexParameters parameters) throws EmbeddedDirectoryServerException + { + checkServerIsNotRunning(); + int returnCode = RebuildIndex.mainRebuildIndex( + parameters.toCommandLineArguments(configParams.getConfigurationFile()), !isRunning(), outStream, errStream); + + if (returnCode != 0) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_REBUILD_INDEX.get( + configParams.getServerRootDirectory(), returnCode)); + } + } + + /** + * Restarts the directory server. + *

+ * This will perform an in-core restart in which the existing server instance will be shut down, a + * new instance will be created, and it will be reinitialized and restarted. + * + * @param className + * The name of the class that initiated the restart. + * @param reason + * A message explaining the reason for the restart. + */ + public void restart(String className, LocalizableMessage reason) + { + DirectoryServer.restart(className, reason, DirectoryServer.getEnvironmentConfig()); + } + + /** + * Starts this server. + * + * @throws EmbeddedDirectoryServerException + * If the server is already running, or if an error occurs during server initialization + * or startup. + */ + public void start() throws EmbeddedDirectoryServerException + { + if (DirectoryServer.isRunning()) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDUTILS_SERVER_ALREADY_RUNNING.get( + configParams.getServerRootDirectory())); + } + + try + { + DirectoryServer directoryServer = DirectoryServer.reinitialize(createEnvironmentConfig()); + directoryServer.startServer(); + } + catch (InitializationException | ConfigException e) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_START.get( + configParams.getServerRootDirectory(), StaticUtils.stackTraceToSingleLineString(e))); + } + } + + private DirectoryEnvironmentConfig createEnvironmentConfig() throws InitializationException + { + // If server root directory or instance directory are not defined, + // the DirectoryEnvironmentConfig class has several ways to find the values, + // including using system properties. + DirectoryEnvironmentConfig env = new DirectoryEnvironmentConfig(); + if (configParams.getServerRootDirectory() != null) + { + env.setServerRoot(new File(configParams.getServerRootDirectory())); + } + if (configParams.getServerInstanceDirectory() != null) + { + env.setInstanceRoot(new File(configParams.getServerInstanceDirectory())); + } + env.setForceDaemonThreads(true); + env.setDisableConnectionHandlers(configParams.isDisableConnectionHandlers()); + env.setConfigFile(new File(configParams.getConfigurationFile())); + return env; + } + + /** + * Stops this server. + * + * @param className + * The name of the class that initiated the shutdown. + * @param reason + * A message explaining the reason for the shutdown. + */ + public void stop(String className, LocalizableMessage reason) + { + DirectoryServer.shutDown(className, reason); + } + + /** + * Upgrades this server. + *

+ * Upgrades the server configuration and application data so that it is compatible + * with the installed binaries. + * + * @param parameters + * The upgrade parameters. + * @throws EmbeddedDirectoryServerException + * If the upgrade fails + */ + public void upgrade(UpgradeParameters parameters) throws EmbeddedDirectoryServerException + { + int returnCode = UpgradeCli.main(parameters.toCommandLineArguments( + configParams.getConfigurationFile()), !isRunning(), outStream, errStream); + + if (returnCode != 0) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_UPGRADE.get( + configParams.getServerRootDirectory(), returnCode)); + } + } + + private void checkServerIsRunning() throws EmbeddedDirectoryServerException + { + if (!isRunning()) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_IMPORT_DATA_SERVER_IS_NOT_RUNNING.get()); + } + } + + private void checkServerIsNotRunning() throws EmbeddedDirectoryServerException + { + if (isRunning()) + { + throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_REBUILD_INDEX_SERVER_IS_RUNNING.get()); + + } + } +} diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServerException.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServerException.java new file mode 100644 index 0000000000..09fecf8cb7 --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServerException.java @@ -0,0 +1,52 @@ +/* + * 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.server.embedded; + +import org.forgerock.i18n.LocalizableMessage; +import org.opends.server.types.OpenDsException; + +/** + * Exception that may be thrown by an embedded directory server if a problem occurs while + * performing an operation on the server. + */ +@SuppressWarnings("serial") +public final class EmbeddedDirectoryServerException extends OpenDsException +{ + + /** + * Creates a new exception with the provided message. + * + * @param message + * The message that explains the problem that occurred. + */ + public EmbeddedDirectoryServerException(LocalizableMessage message) + { + super(message); + } + + /** + * Creates a new exception with the provided message and root cause. + * + * @param message + * The message that explains the problem that occurred. + * @param cause + * The exception that was caught to trigger this exception. + */ + public EmbeddedDirectoryServerException(LocalizableMessage message, Throwable cause) + { + super(message, cause); + } +} diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ImportParameters.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ImportParameters.java new file mode 100644 index 0000000000..41b13634d2 --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ImportParameters.java @@ -0,0 +1,87 @@ +/* + * 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.server.embedded; + +/** Parameters to import LDIF data to a directory server. */ +public final class ImportParameters +{ + private String backendID; + private String ldifFile; + + private ImportParameters() + { + // prefer usage of static method for creation + } + + /** + * Creates the import parameters. + * + * @return parameters + */ + public static ImportParameters importParams() + { + return new ImportParameters(); + } + + /** + * Generates command-line arguments from the parameters. + * + * @return command-line arguments + */ + String[] toCommandLineArguments(String configurationFile, ConnectionParameters connParams) + { + return new String[] { + "--configFile", configurationFile, + "--backendID", backendID, + "--bindDN", connParams.getBindDn(), + "--bindPassword", connParams.getBindPassword(), + "--ldifFile", ldifFile, + "--noPropertiesFile", + "--trustAll" + }; + } + + String getLdifFile() + { + return ldifFile; + } + + /** + * Sets the backend id of the backend to import. + * + * @param id + * the backend id + * @return this builder + */ + public ImportParameters backendId(String id) + { + backendID = id; + return this; + } + + /** + * Sets the path to the LDIF file to be imported. + * + * @param ldifFile + * The path to the LDIF file + * @return this builder + */ + public ImportParameters ldifFile(String ldifFile) + { + this.ldifFile = ldifFile; + return this; + } +} diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/RebuildIndexParameters.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/RebuildIndexParameters.java new file mode 100644 index 0000000000..10f07904c5 --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/RebuildIndexParameters.java @@ -0,0 +1,65 @@ +/* + * 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.server.embedded; + +/** Parameters to rebuild the indexes of a directory server. */ +public final class RebuildIndexParameters +{ + private String baseDN; + + private RebuildIndexParameters() + { + // prefer usage of static method for creation + } + + /** + * Creates a builder for the rebuild index parameters. + * + * @return a builder + */ + public static RebuildIndexParameters rebuildIndexParams() + { + return new RebuildIndexParameters(); + } + + /** + * Generates command-line arguments from the parameters. + * + * @return command-line arguments + */ + String[] toCommandLineArguments(String configurationFile) + { + return new String[] { + "--configFile", configurationFile, + "--baseDN", baseDN, + "--rebuildAll", + "--noPropertiesFile" + }; + } + + /** + * Sets the base Dn for user information in the directory server. + * + * @param baseDN + * the baseDN + * @return this builder + */ + public RebuildIndexParameters baseDN(String baseDN) + { + this.baseDN = baseDN; + return this; + } +} diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ReplicationParameters.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ReplicationParameters.java new file mode 100644 index 0000000000..2e3a4fb83d --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/ReplicationParameters.java @@ -0,0 +1,201 @@ +/* + * 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.server.embedded; + +/** Parameters for replication operations on a directory server. */ +public final class ReplicationParameters +{ + private String baseDn; + private Integer replicationPortSource; + private Integer replicationPortDestination; + private ConnectionParameters connParamsForDestination; + + private ReplicationParameters() + { + // prefer usage of static method for creation + } + + /** + * Creates a builder for the replication parameters. + * + * @return a builder + */ + public static ReplicationParameters replicationParams() + { + return new ReplicationParameters(); + } + + /** + * Generates the command-line arguments for configuring replication, from the parameters. + * + * @return command-line arguments + */ + String[] toCommandLineArgumentsConfiguration(String configurationFile, ConnectionParameters connParams) + { + return new String[] { + "enable", + "--no-prompt", + "--configFile", configurationFile, + "--host1", connParams.getHostName(), + "--port1", s(connParams.getAdminPort()), + "--bindDN1", connParams.getBindDn(), + "--bindPassword1", connParams.getBindPassword(), + "--replicationPort1", s(replicationPortSource), + "--host2", connParamsForDestination.getHostName(), + "--port2", s(connParamsForDestination.getAdminPort()), + "--bindDN2", connParamsForDestination.getBindDn(), + "--bindPassword2", connParamsForDestination.getBindPassword(), + "--replicationPort2", s(replicationPortDestination), + "--adminUID", connParams.getAdminUid(), + "--adminPassword", connParams.getAdminPassword(), + "--baseDN", baseDn, + "--trustAll", + "--noPropertiesFile" }; + } + + /** + * Generates the command-line arguments for initializing replication, from the parameters. + * + * @return command-line arguments + */ + String[] toCommandLineArgumentsInitialize(String configurationFile, ConnectionParameters connParams) + { + return new String[] { + "initialize", + "--no-prompt", + "--configFile", configurationFile, + "--hostSource", connParams.getHostName(), + "--portSource", s(connParams.getAdminPort()), + "--hostDestination", connParamsForDestination.getHostName(), + "--portDestination", s(connParamsForDestination.getAdminPort()), + "--adminUID", connParams.getAdminUid(), + "--adminPassword", connParams.getAdminPassword(), + "--baseDN", baseDn, + "--trustAll", + "--noPropertiesFile" }; + } + + /** + * Generates the command-line arguments for output of the replication status, from the parameters. + * + * @return command-line arguments + */ + String[] toCommandLineArgumentsStatus(String configurationFile, ConnectionParameters connParams) + { + return new String[] { + "status", + "--no-prompt", + "--configFile", configurationFile, + "--hostname", connParams.getHostName(), + "--port", s(connParams.getAdminPort()), + "--adminUID", connParams.getAdminUid(), + "--adminPassword", connParams.getAdminPassword(), + "--script-friendly", + "--noPropertiesFile" }; + } + + @Override + public String toString() + { + return "ReplicationParameters [baseDn=" + baseDn + + ", source replication port=" + replicationPortSource + + ", destination host name=" + getHostNameDestination() + + ", destination replication port=" + replicationPortDestination + + ", destination admin port=" + getAdminPortDestination() + + "]"; + } + + int getReplicationPortSource() + { + return replicationPortSource; + } + + int getReplicationPortDestination() + { + return replicationPortDestination; + } + + String getHostNameDestination() + { + return connParamsForDestination.getHostName(); + } + + int getAdminPortDestination() + { + return connParamsForDestination.getAdminPort(); + } + + /** Convert an integer to a String. */ + private String s(Integer val) + { + return String.valueOf(val); + } + + /** + * Sets the base Dn of the data to be replicated. + * + * @param baseDn + * the base Dn + * @return this builder + */ + public ReplicationParameters baseDn(String baseDn) + { + this.baseDn = baseDn; + return this; + } + + /** + * Sets the replication port of the first server (source) whose contents will be replicated. + *

+ * The source server should correspond to the embedded server on which the replication operation is + * applied. + * + * @param port + * the replication port + * @return this builder + */ + public ReplicationParameters replicationPortSource(int port) + { + this.replicationPortSource = port; + return this; + } + + /** + * Sets the replication port of the second server (destination) whose contents will be replicated. + * + * @param port + * the replication port + * @return this builder + */ + public ReplicationParameters replicationPortDestination(int port) + { + this.replicationPortDestination = port; + return this; + } + + /** + * Sets the connection parameters of the second server (destination) whose contents will be replicated. + * + * @param destinationParams + * The connection parameters for destination server + * @return this builder + */ + public ReplicationParameters connectionParamsForDestination(ConnectionParameters destinationParams) + { + this.connParamsForDestination = destinationParams; + return this; + } +} diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/SetupParameters.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/SetupParameters.java new file mode 100644 index 0000000000..bc61dae574 --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/SetupParameters.java @@ -0,0 +1,148 @@ +/* + * 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.server.embedded; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** Parameters to setup a directory server. */ +public final class SetupParameters +{ + private String baseDn; + private int jmxPort; + private String backendType; + private List ldifFiles = new ArrayList<>(); + + private SetupParameters() + { + // prefer usage of static method for creation + } + + /** + * Creates a builder for the setup parameters. + * + * @return a builder + */ + public static SetupParameters setupParams() + { + return new SetupParameters(); + } + + /** + * Generates command-line arguments from the parameters. + * + * @return command-line arguments + */ + String[] toCommandLineArguments(ConnectionParameters connParams) + { + String[] baseArgs = new String[] { + "--cli", + "--noPropertiesFile", + "--no-prompt", + "--doNotStart", + "--skipPortCheck", + "--baseDN", baseDn, + "--hostname", connParams.getHostName(), + "--rootUserDN", connParams.getBindDn(), + "--rootUserPassword", connParams.getBindPassword(), + "--ldapPort", s(connParams.getLdapPort()), + "--adminConnectorPort", s(connParams.getAdminPort()), + "--jmxPort", s(jmxPort), + "--backendType", backendType + }; + List args = new ArrayList<>(Arrays.asList(baseArgs)); + if (connParams.getLdapSecurePort() != null) + { + args.add("--ldapsPort"); + args.add(s(connParams.getLdapSecurePort())); + args.add("--generateSelfSignedCertificate"); + } + for (final String ldif : ldifFiles) + { + args.add("--ldifFile"); + args.add(ldif); + } + return args.toArray(new String[args.size()]); + } + + String getBaseDn() + { + return baseDn; + } + + String getBackendType() + { + return backendType; + } + + /** Convert an integer to a String. */ + private String s(Integer val) + { + return String.valueOf(val); + } + + /** + * Sets the base Dn for user information in the directory server. + * + * @param baseDn + * the base Dn + * @return this builder + */ + public SetupParameters baseDn(String baseDn) + { + this.baseDn = baseDn; + return this; + } + + /** + * Sets the port on which the directory server should listen for JMX communication. + * + * @param jmxPort + * the JMX port + * @return this builder + */ + public SetupParameters jmxPort(int jmxPort) + { + this.jmxPort = jmxPort; + return this; + } + + /** + * Sets the type of the backend containing user information. + * + * @param backendType + * the backend type (e.g. je, pdb) + * @return this builder + */ + public SetupParameters backendType(String backendType) + { + this.backendType = backendType; + return this; + } + + /** + * Add an ldif file to import after setup. + * + * @param ldif + * the LDIF to import + * @return this builder + */ + public SetupParameters ldifFile(String ldif) { + this.ldifFiles.add(ldif); + return this; + } +} \ No newline at end of file diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/UpgradeParameters.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/UpgradeParameters.java new file mode 100644 index 0000000000..bf7abde7ce --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/UpgradeParameters.java @@ -0,0 +1,75 @@ +/* + * 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.server.embedded; + +import java.util.ArrayList; +import java.util.List; + +/** Parameters to upgrade a Directory Server. */ +public final class UpgradeParameters +{ + private boolean ignoreErrors; + + private UpgradeParameters() + { + // prefer usage of static method for creation + } + + /** + * Starts construction of the upgrade parameters. + * + * @return this builder + */ + public static UpgradeParameters upgradeParams() + { + return new UpgradeParameters(); + } + + /** + * Generates command-line arguments from the parameters. + * + * @return command-line arguments + */ + String[] toCommandLineArguments(String configurationFile) + { + List args = new ArrayList<>(6); + args.add("--acceptLicense"); + args.add("--no-prompt"); + args.add("--force"); + args.add("--configFile"); + args.add(configurationFile); + if (ignoreErrors) { + args.add("--ignoreErrors"); + } + return args.toArray(new String[args.size()]); + } + + /** + * Indicates whether errors should be ignored during the upgrade. + *

+ * This option should be used with caution and may be useful in automated deployments where + * potential errors are known in advance and resolved after the upgrade has completed + * + * @param ignore + * indicates whether errors should be ignored + * @return this builder + */ + public UpgradeParameters isIgnoreErrors(boolean ignore) + { + ignoreErrors = ignore; + return this; + } +} diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/package-info.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/package-info.java new file mode 100644 index 0000000000..191c7c2ea8 --- /dev/null +++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/package-info.java @@ -0,0 +1,17 @@ +/* + * 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. + */ +/** Provides support for an embedded directory server. */ +package org.forgerock.opendj.server.embedded; \ No newline at end of file diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/EmbeddedUtils.java b/opendj-server-legacy/src/main/java/org/opends/server/util/EmbeddedUtils.java index aeb61c7090..724473266b 100644 --- a/opendj-server-legacy/src/main/java/org/opends/server/util/EmbeddedUtils.java +++ b/opendj-server-legacy/src/main/java/org/opends/server/util/EmbeddedUtils.java @@ -12,7 +12,7 @@ * information: "Portions Copyright [year] [name of copyright owner]". * * Copyright 2008 Sun Microsystems, Inc. - * Portions Copyright 2014 ForgeRock AS. + * Portions Copyright 2014-2016 ForgeRock AS. */ package org.opends.server.util; @@ -33,7 +33,10 @@ * This class provides a number of utility methods for using OpenDS in an * embedded manner (i.e., running within the same JVM as another application and * controlled by that application). + * + * @deprecated Use {@code EmbeddedDirectoryServer} class instead */ +@Deprecated @org.opends.server.types.PublicAPI( stability=org.opends.server.types.StabilityLevel.UNCOMMITTED, mayInstantiate=false, @@ -46,7 +49,9 @@ public final class EmbeddedUtils * * @return {@code true} if the server is currently running, or {@code false} * if not. + * @deprecated Use {@code EmbeddedDirectoryServer} class instead */ + @Deprecated public static boolean isRunning() { return DirectoryServer.isRunning(); @@ -62,14 +67,16 @@ public static boolean isRunning() * @throws InitializationException If the Directory Server is already * running, or if an error occurs during * server initialization or startup. + * @deprecated Use {@code EmbeddedDirectoryServer} class instead */ + @Deprecated public static void startServer(DirectoryEnvironmentConfig config) throws InitializationException { if (DirectoryServer.isRunning()) { throw new InitializationException( - ERR_EMBEDUTILS_SERVER_ALREADY_RUNNING.get()); + ERR_EMBEDUTILS_SERVER_ALREADY_RUNNING.get(config.getServerRootAsString())); } DirectoryServer directoryServer = DirectoryServer.reinitialize(config); @@ -90,7 +97,9 @@ public static void startServer(DirectoryEnvironmentConfig config) * * @param className The name of the class that initiated the shutdown. * @param reason A message explaining the reason for the shutdown. + * @deprecated Use {@code EmbeddedDirectoryServer} class instead */ + @Deprecated public static void stopServer(String className, LocalizableMessage reason) { DirectoryServer.shutDown(className, reason); @@ -107,7 +116,9 @@ public static void stopServer(String className, LocalizableMessage reason) * @param reason A message explaining the reason for the retart. * @param config The environment configuration to use for the new server * instance. + * @deprecated Use {@code EmbeddedDirectoryServer} class instead */ + @Deprecated public static void restartServer(String className, LocalizableMessage reason, DirectoryEnvironmentConfig config) { @@ -122,7 +133,9 @@ public static void restartServer(String className, LocalizableMessage reason, * going to be used without the server running (e.g., to facilitate use in an * LDAP client API, for DN processing, etc.). This will have no effect if the * server has already been initialized for client use. + * @deprecated Use {@code EmbeddedDirectoryServer} class instead */ + @Deprecated public static void initializeForClientUse() { DirectoryServer.getInstance(); diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java b/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java index 03c835ca9b..50c02fff71 100644 --- a/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java +++ b/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java @@ -19,6 +19,7 @@ import static org.opends.messages.UtilityMessages.*; import static org.opends.server.util.ServerConstants.*; +import java.io.BufferedOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; @@ -44,6 +45,8 @@ import java.util.Map; import java.util.TimeZone; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -92,6 +95,10 @@ public final class StaticUtils /** The number of bytes of a Java long. A Java int is 64 bits, i.e. 8 bytes. */ public static final int LONG_SIZE = 8; + /** Size of buffer used to copy/move/extract files. */ + public static final int BUFFER_SIZE = 8192; + + private static final String[] EXECUTABLE_FILES_SUFFIXES = { ".sh", ".bat", ".exe" }; /** * Number of bytes in a Kibibyte. *

@@ -2465,5 +2472,96 @@ public static boolean isClassAvailable(final String className) return false; } } + /** + * Extracts the provided zip archive to the provided target directory. + *

+ * A file is set to executable if one or more of the following three conditions apply: + *

+ * + * @param zipFile + * The zip file to extract. + * @param targetDirectory + * The target directory for the content of the archive. + * @param executableDirectories + * List of extracted directories which should have all their files set as executable. + * Each directory must be provided as a path relative to the target directory (e.g. "opendj/bin") + * @param executableFiles + * List of individual files which should be set as executable. + * Each file must be provided as a path relative to the target directory (e.g. "opendj/setup") + * @throws IOException + * If zip archive can't be read or target files can be written. + */ + public static void extractZipArchive(File zipFile, File targetDirectory, List executableDirectories, + List executableFiles) throws IOException + { + try (ZipInputStream zipStream = new ZipInputStream(new FileInputStream(zipFile))) + { + ZipEntry fileEntry; + while ((fileEntry = zipStream.getNextEntry()) != null) + { + File targetFile = new File(targetDirectory.getPath(), fileEntry.getName()); + + if (fileEntry.isDirectory()) + { + targetFile.mkdirs(); + continue; + } + extractFileFromZip(zipStream, targetFile); + + for (String suffix : EXECUTABLE_FILES_SUFFIXES) + { + if (fileEntry.getName().toLowerCase().endsWith(suffix)) + { + targetFile.setExecutable(true); + } + } + } + } + for (String dir: executableDirectories) + { + File directory = new File(targetDirectory.getPath(), dir); + for (File file: directory.listFiles()) + { + if (file.isFile()) { + file.setExecutable(true); + } + } + } + for (String name : executableFiles) + { + File file = new File(targetDirectory.getPath(), name); + if (file.exists()) + { + file.setExecutable(true); + } + } + } + + /** + * Extracts a file from a zip input stream. + * + * @param zipInput + * The input stream of a zip file. + * @param targetFile + * The destination of the extracted file. + * @throws IOException + * If the zip file can't be read or the target file can't be written + */ + private static void extractFileFromZip(ZipInputStream zipInput, File targetFile) throws IOException + { + try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile))) + { + int bytesRead = 0; + byte[] bytes = new byte[BUFFER_SIZE]; + while ((bytesRead = zipInput.read(bytes)) != -1) + { + outputStream.write(bytes, 0, bytesRead); + } + } + } } diff --git a/opendj-server-legacy/src/messages/org/opends/messages/utility.properties b/opendj-server-legacy/src/messages/org/opends/messages/utility.properties index f528d2f7db..3eb4d0eb1a 100644 --- a/opendj-server-legacy/src/messages/org/opends/messages/utility.properties +++ b/opendj-server-legacy/src/messages/org/opends/messages/utility.properties @@ -210,8 +210,8 @@ ERR_SKIP_FILE_EXISTS_164=The specified skip file %s already exists and \ to or replace the file ERR_LDIF_SKIP_165=Skipping entry %s because the DN is not one that \ should be included based on the include and exclude branches -ERR_EMBEDUTILS_SERVER_ALREADY_RUNNING_167=The Directory Server cannot \ - be started because it is already running +ERR_EMBEDUTILS_SERVER_ALREADY_RUNNING_167=The embedded server with server \ + root '%s' cannot be started because it is already running. INFO_EMAIL_TOOL_DESCRIPTION_171=Send an e-mail message via SMTP INFO_EMAIL_HOST_DESCRIPTION_172=The address of the SMTP server to use to send \ the message @@ -400,3 +400,38 @@ ERR_BACKUP_CANNOT_SAVE_FILES_BEFORE_RESTORE_325=An error occurred while \ ERR_BACKUP_CANNOT_CREATE_SAVE_DIRECTORY_326=An error occurred while \ attempting to create a save directory with base path %s before restore of \ backup of %s: %s +ERR_EMBEDDED_SERVER_CONFIGURE_REPLICATION_328=An error occurred while attempting \ + to configure replication between embedded server with server root '%s' and source replication \ + port '%s' and destination server with hostname '%s' and destination replication port '%s'. Error code \ + is: %d +ERR_EMBEDDED_SERVER_IMPORT_DATA_329=An error occurred while attempting \ + to import LDIF file '%s' into embedded server with server root '%s'. Error code \ + is: %d +ERR_EMBEDDED_SERVER_INITIALIZE_REPLICATION_330=An error occurred while attempting \ + to initialize replication between embedded server with server root '%s' and admin \ + source port '%s' and destination server with hostname '%s' and destination admin port '%s'. Error code \ + is: %d +ERR_EMBEDDED_SERVER_SETUP_332=An error occurred while attempting \ + to setup the embedded server with server root '%s', base DN '%s' and backend type '%s'. \ + Error code is: %d +ERR_EMBEDDED_SERVER_SETUP_EXTRACT_ARCHIVE_333=An error occurred while attempting \ + to extract server archive '%s' before setup of embedded server with server root '%s': %s +ERR_EMBEDDED_SERVER_REBUILD_INDEX_334=An error occurred while attempting \ + to rebuild index of embedded server with server root '%s'. Error code is: %d +ERR_EMBEDDED_SERVER_START_336=An error occurred while attempting \ + to start the embedded server with server root '%s' : %s +ERR_EMBEDDED_SERVER_UPGRADE_337=An error occurred while attempting \ + to upgrade the embedded server with server root '%s' : %s +ERR_EMBEDDED_SERVER_INTERNAL_CONNECTION_338=An error occurred while attempting to retrieve \ + an internal connection to the server with the user DN '%s' +ERR_EMBEDDED_SERVER_ARCHIVE_SETUP_WRONG_ROOT_DIRECTORY_339=The setup from an archive \ + can only be done with a server root directory named after the root directory contained \ + in the archive: '%s'. The provided server root was: '%s' +ERR_EMBEDDED_SERVER_IMPORT_DATA_SERVER_IS_NOT_RUNNING_340=The import data operation could \ + not be performed on the embedded server because it is not running +ERR_EMBEDDED_SERVER_REBUILD_INDEX_SERVER_IS_RUNNING_341=The rebuild index operation could \ + not be performed on the embedded server because it is running +ERR_EMBEDDED_SERVER_LDIF_MANAGEMENT_CONTEXT_342=An error occurred while attempting to \ + read the configuration file '%s' +ERR_EMBEDDED_SERVER_BUILD_VERSION_343=An error occurred while attempting to \ + retrieve the build version of the directory server: '%s' \ No newline at end of file diff --git a/pom.xml b/pom.xml index 83a548aa50..d50a5d6b21 100644 --- a/pom.xml +++ b/pom.xml @@ -206,6 +206,7 @@ opendj-legacy opendj-server-legacy opendj-dsml-servlet + opendj-embedded-server-examples opendj-openidm-account-change-notification-handler opendj-doc-generated-ref