0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License, Version 1.0 only
0N/A * (the "License"). You may not use this file except in compliance
0N/A * with the License.
0N/A *
0N/A * You can obtain a copy of the license at
0N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
0N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
0N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at
0N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
0N/A * add the following below this CDDL HEADER, with the fields enclosed
873N/A * by brackets "[]" replaced with your own identifying information:
0N/A * Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A *
0N/A *
4289N/A * Copyright 2006-2009 Sun Microsystems, Inc.
6238N/A * Portions copyright 2012-2013 ForgeRock AS.
0N/A */
0N/Apackage org.opends.server.tools;
0N/A
1795N/Aimport java.io.OutputStream;
1795N/Aimport java.io.PrintStream;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.List;
0N/A
0N/Aimport org.opends.server.api.Backend;
1514N/Aimport org.opends.server.api.ErrorLogPublisher;
3363N/Aimport org.opends.server.api.DebugLogPublisher;
0N/Aimport org.opends.server.api.plugin.PluginType;
0N/Aimport org.opends.server.config.ConfigException;
2187N/Aimport static org.opends.server.config.ConfigConstants.*;
0N/Aimport org.opends.server.core.CoreConfigManager;
0N/Aimport org.opends.server.core.DirectoryServer;
0N/Aimport org.opends.server.core.LockFileManager;
421N/Aimport org.opends.server.extensions.ConfigFileHandler;
1280N/Aimport org.opends.server.loggers.TextWriter;
3363N/Aimport org.opends.server.loggers.TextErrorLogPublisher;
3363N/Aimport org.opends.server.loggers.ErrorLogger;
3363N/Aimport org.opends.server.loggers.debug.TextDebugLogPublisher;
3363N/Aimport org.opends.server.loggers.debug.DebugLogger;
4134N/Aimport org.opends.server.types.*;
6238N/Aimport org.opends.server.util.BuildVersion;
0N/Aimport org.opends.server.util.args.ArgumentException;
0N/Aimport org.opends.server.util.args.BooleanArgument;
0N/Aimport org.opends.server.util.args.IntegerArgument;
0N/Aimport org.opends.server.util.args.StringArgument;
2187N/Aimport org.opends.server.util.args.LDAPConnectionArgumentParser;
4289N/Aimport org.opends.server.util.cli.CLIException;
0N/A
2187N/Aimport org.opends.messages.Message;
2086N/Aimport static org.opends.messages.ToolMessages.*;
1280N/Aimport static org.opends.server.loggers.ErrorLogger.*;
536N/Aimport static org.opends.server.util.ServerConstants.*;
0N/Aimport static org.opends.server.util.StaticUtils.*;
1060N/Aimport static org.opends.server.tools.ToolConstants.*;
2187N/Aimport org.opends.server.tools.tasks.TaskTool;
1329N/Aimport org.opends.server.admin.std.server.BackendCfg;
2187N/Aimport org.opends.server.protocols.ldap.LDAPAttribute;
2187N/Aimport org.opends.server.tasks.ExportTask;
0N/A
0N/A
0N/A/**
0N/A * This program provides a utility that may be used to export the contents of a
2187N/A * Directory Server backend to an LDIF file. Depending on the arguments given,
2187N/A * this program will either perform the export directly as a process that
2187N/A * runs separate from Directory Server; or by scheduling a task to perform the
2187N/A * action within the Directory Server via the tasks interface.
0N/A */
2187N/Apublic class ExportLDIF extends TaskTool {
2187N/A
0N/A /**
0N/A * The main method for ExportLDIF tool.
0N/A *
0N/A * @param args The command-line arguments provided to this program.
0N/A */
0N/A public static void main(String[] args)
0N/A {
1795N/A int retCode = mainExportLDIF(args, true, System.out, System.err);
0N/A
0N/A if(retCode != 0)
0N/A {
1727N/A System.exit(filterExitCode(retCode));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Processes the command-line arguments and invokes the export process.
0N/A *
0N/A * @param args The command-line arguments provided to this program.
0N/A *
0N/A * @return The error code.
0N/A */
0N/A public static int mainExportLDIF(String[] args)
0N/A {
1795N/A return mainExportLDIF(args, true, System.out, System.err);
891N/A }
891N/A
891N/A /**
891N/A * Processes the command-line arguments and invokes the export process.
891N/A *
891N/A * @param args The command-line arguments provided to this
891N/A * program.
891N/A * @param initializeServer Indicates whether to initialize the server.
1795N/A * @param outStream The output stream to use for standard output, or
1795N/A * {@code null} if standard output is not needed.
1795N/A * @param errStream The output stream to use for standard error, or
1795N/A * {@code null} if standard error is not needed.
891N/A *
891N/A * @return The error code.
891N/A */
1795N/A public static int mainExportLDIF(String[] args, boolean initializeServer,
1795N/A OutputStream outStream,
1795N/A OutputStream errStream)
891N/A {
2187N/A ExportLDIF tool = new ExportLDIF();
2187N/A return tool.process(args, initializeServer, outStream, errStream);
2187N/A }
2187N/A
2187N/A // Define the command-line arguments that may be used with this program.
2187N/A private BooleanArgument appendToLDIF = null;
2187N/A private BooleanArgument compressLDIF = null;
2187N/A private BooleanArgument displayUsage = null;
2187N/A private BooleanArgument encryptLDIF = null;
2187N/A private BooleanArgument excludeOperationalAttrs = null;
2187N/A private BooleanArgument signHash = null;
2187N/A private IntegerArgument wrapColumn = null;
2187N/A private StringArgument backendID = null;
2187N/A private StringArgument configClass = null;
2187N/A private StringArgument configFile = null;
2187N/A private StringArgument excludeAttributeStrings = null;
2187N/A private StringArgument excludeBranchStrings = null;
2187N/A private StringArgument excludeFilterStrings = null;
2187N/A private StringArgument includeAttributeStrings = null;
2187N/A private StringArgument includeBranchStrings = null;
2187N/A private StringArgument includeFilterStrings = null;
2187N/A private StringArgument ldifFile = null;
2187N/A
2187N/A private int process(String[] args, boolean initializeServer,
2187N/A OutputStream outStream, OutputStream errStream) {
2187N/A
1795N/A PrintStream out;
1795N/A if (outStream == null)
1795N/A {
1795N/A out = NullOutputStream.printStream();
1795N/A }
1795N/A else
1795N/A {
1795N/A out = new PrintStream(outStream);
1795N/A }
1795N/A
1795N/A PrintStream err;
1795N/A if (errStream == null)
1795N/A {
1795N/A err = NullOutputStream.printStream();
1795N/A }
1795N/A else
1795N/A {
1795N/A err = new PrintStream(errStream);
1795N/A }
1795N/A
0N/A // Create the command-line argument parser for use with this program.
2187N/A LDAPConnectionArgumentParser argParser =
2520N/A createArgParser("org.opends.server.tools.ExportLDIF",
2520N/A INFO_LDIFEXPORT_TOOL_DESCRIPTION.get());
0N/A
0N/A
0N/A // Initialize all the command-line argument types and register them with the
0N/A // parser.
0N/A try
0N/A {
0N/A configClass =
1060N/A new StringArgument("configclass", OPTION_SHORT_CONFIG_CLASS,
1060N/A OPTION_LONG_CONFIG_CLASS, true, false,
3220N/A true, INFO_CONFIGCLASS_PLACEHOLDER.get(),
0N/A ConfigFileHandler.class.getName(), null,
2086N/A INFO_DESCRIPTION_CONFIG_CLASS.get());
534N/A configClass.setHidden(true);
0N/A argParser.addArgument(configClass);
0N/A
0N/A
0N/A configFile =
0N/A new StringArgument("configfile", 'f', "configFile", true, false,
3220N/A true, INFO_CONFIGFILE_PLACEHOLDER.get(), null,
3220N/A null,
2086N/A INFO_DESCRIPTION_CONFIG_FILE.get());
534N/A configFile.setHidden(true);
0N/A argParser.addArgument(configFile);
0N/A
0N/A
0N/A ldifFile =
1060N/A new StringArgument("ldiffile", OPTION_SHORT_LDIF_FILE,
1060N/A OPTION_LONG_LDIF_FILE,true, false, true,
3220N/A INFO_LDIFFILE_PLACEHOLDER.get(), null, null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_LDIF_FILE.get());
0N/A argParser.addArgument(ldifFile);
0N/A
0N/A
2086N/A appendToLDIF = new BooleanArgument(
2086N/A "appendldif", 'a', "appendToLDIF",
2086N/A INFO_LDIFEXPORT_DESCRIPTION_APPEND_TO_LDIF.get());
0N/A argParser.addArgument(appendToLDIF);
0N/A
0N/A
0N/A backendID =
0N/A new StringArgument("backendid", 'n', "backendID", true, false, true,
3220N/A INFO_BACKENDNAME_PLACEHOLDER.get(), null, null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_BACKEND_ID.get());
0N/A argParser.addArgument(backendID);
0N/A
0N/A
0N/A includeBranchStrings =
0N/A new StringArgument("includebranch", 'b', "includeBranch", false,
3220N/A true, true, INFO_BRANCH_DN_PLACEHOLDER.get(),
3220N/A null, null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_INCLUDE_BRANCH.get());
0N/A argParser.addArgument(includeBranchStrings);
0N/A
0N/A
0N/A excludeBranchStrings =
0N/A new StringArgument("excludebranch", 'B', "excludeBranch", false,
3220N/A true, true, INFO_BRANCH_DN_PLACEHOLDER.get(),
3220N/A null, null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_BRANCH.get());
0N/A argParser.addArgument(excludeBranchStrings);
0N/A
0N/A
0N/A includeAttributeStrings =
2086N/A new StringArgument(
2086N/A "includeattribute", 'i', "includeAttribute",
3220N/A false, true, true, INFO_ATTRIBUTE_PLACEHOLDER.get(), null,
3220N/A null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_INCLUDE_ATTRIBUTE.get());
0N/A argParser.addArgument(includeAttributeStrings);
0N/A
0N/A
0N/A excludeAttributeStrings =
2086N/A new StringArgument(
2086N/A "excludeattribute", 'e', "excludeAttribute",
3220N/A false, true, true, INFO_ATTRIBUTE_PLACEHOLDER.get(), null,
3220N/A null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_ATTRIBUTE.get());
0N/A argParser.addArgument(excludeAttributeStrings);
0N/A
0N/A
0N/A includeFilterStrings =
0N/A new StringArgument("includefilter", 'I', "includeFilter",
3220N/A false, true, true, INFO_FILTER_PLACEHOLDER.get(),
3220N/A null, null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_INCLUDE_FILTER.get());
0N/A argParser.addArgument(includeFilterStrings);
0N/A
0N/A
0N/A excludeFilterStrings =
0N/A new StringArgument("excludefilter", 'E', "excludeFilter",
3220N/A false, true, true, INFO_FILTER_PLACEHOLDER.get(),
3220N/A null, null,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_FILTER.get());
0N/A argParser.addArgument(excludeFilterStrings);
0N/A
0N/A
601N/A excludeOperationalAttrs =
601N/A new BooleanArgument("excludeoperational", 'O', "excludeOperational",
2086N/A INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_OPERATIONAL.get());
601N/A argParser.addArgument(excludeOperationalAttrs);
601N/A
601N/A
0N/A wrapColumn =
2187N/A new IntegerArgument("wrapcolumn", null, "wrapColumn", false, false,
3220N/A true, INFO_WRAP_COLUMN_PLACEHOLDER.get(), 0,
3220N/A null, true, 0, false, 0,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_WRAP_COLUMN.get());
0N/A argParser.addArgument(wrapColumn);
0N/A
0N/A
0N/A compressLDIF =
1060N/A new BooleanArgument("compressldif", OPTION_SHORT_COMPRESS,
1060N/A OPTION_LONG_COMPRESS,
2086N/A INFO_LDIFEXPORT_DESCRIPTION_COMPRESS_LDIF.get());
0N/A argParser.addArgument(compressLDIF);
0N/A
0N/A
0N/A encryptLDIF =
0N/A new BooleanArgument("encryptldif", 'y', "encryptLDIF",
2086N/A INFO_LDIFEXPORT_DESCRIPTION_ENCRYPT_LDIF.get());
3085N/A encryptLDIF.setHidden(true); // See issue #27
0N/A argParser.addArgument(encryptLDIF);
0N/A
0N/A
0N/A signHash =
0N/A new BooleanArgument("signhash", 's', "signHash",
2086N/A INFO_LDIFEXPORT_DESCRIPTION_SIGN_HASH.get());
3085N/A signHash.setHidden(true); // See issue #28
0N/A argParser.addArgument(signHash);
0N/A
0N/A
0N/A displayUsage =
1060N/A new BooleanArgument("help", OPTION_SHORT_HELP,
1060N/A OPTION_LONG_HELP,
2086N/A INFO_DESCRIPTION_USAGE.get());
0N/A argParser.addArgument(displayUsage);
0N/A argParser.setUsageArgument(displayUsage);
0N/A }
0N/A catch (ArgumentException ae)
0N/A {
2086N/A Message message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
0N/A
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
0N/A return 1;
0N/A }
0N/A
0N/A
4360N/A // Init the default values so that they can appear also on the usage.
4360N/A try
4360N/A {
4360N/A argParser.getArguments().initArgumentsWithConfiguration();
4360N/A }
4360N/A catch (ConfigException ce)
4360N/A {
4360N/A // Ignore.
4360N/A }
4360N/A
0N/A // Parse the command-line arguments provided to this program.
0N/A try
0N/A {
0N/A argParser.parseArguments(args);
2520N/A validateTaskArgs();
0N/A }
0N/A catch (ArgumentException ae)
0N/A {
2086N/A Message message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
0N/A
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
1795N/A err.println(argParser.getUsage());
0N/A return 1;
0N/A }
4289N/A catch (CLIException ce)
4289N/A {
4289N/A // No need to display the usage since the problem comes with a provided
4289N/A // value.
4289N/A err.println(wrapText(ce.getMessageObject(), MAX_LINE_WIDTH));
4289N/A return 1;
4289N/A }
0N/A
0N/A
1340N/A // If we should just display usage or version information,
1340N/A // then print it and exit.
1340N/A if (argParser.usageOrVersionDisplayed())
0N/A {
0N/A return 0;
0N/A }
0N/A
6238N/A // Checks the version - if upgrade required, the tool is unusable
6238N/A try
6238N/A {
6238N/A BuildVersion.checkVersionMismatch();
6238N/A }
6238N/A catch (InitializationException e)
6238N/A {
6238N/A err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
6238N/A return 1;
6238N/A }
6238N/A
2187N/A return process(argParser, initializeServer, out, err);
2187N/A }
2187N/A
2187N/A /**
2187N/A * {@inheritDoc}
2187N/A */
2187N/A public void addTaskAttributes(List<RawAttribute> attributes)
2187N/A {
2187N/A //
2187N/A // Required attributes
2187N/A //
4134N/A ArrayList<ByteString> values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf(ldifFile.getValue()));
2187N/A attributes.add(new LDAPAttribute(ATTR_TASK_EXPORT_LDIF_FILE, values));
2187N/A
4134N/A values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf(backendID.getValue()));
2187N/A attributes.add(new LDAPAttribute(ATTR_TASK_EXPORT_BACKEND_ID, values));
2187N/A
2187N/A //
2187N/A // Optional attributes
2187N/A //
2187N/A if (appendToLDIF.getValue() != null &&
2187N/A !appendToLDIF.getValue().equals(appendToLDIF.getDefaultValue())) {
4134N/A values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf(appendToLDIF.getValue()));
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_APPEND_TO_LDIF, values));
2187N/A }
2187N/A
2187N/A if (compressLDIF.getValue() != null &&
2187N/A !compressLDIF.getValue().equals(compressLDIF.getDefaultValue())) {
4134N/A values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf(compressLDIF.getValue()));
2187N/A attributes.add(new LDAPAttribute(ATTR_TASK_EXPORT_COMPRESS_LDIF, values));
2187N/A }
2187N/A
2187N/A if (encryptLDIF.getValue() != null &&
2187N/A !encryptLDIF.getValue().equals(encryptLDIF.getDefaultValue())) {
4134N/A values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf(encryptLDIF.getValue()));
2187N/A attributes.add(new LDAPAttribute(ATTR_TASK_EXPORT_ENCRYPT_LDIF, values));
2187N/A }
2187N/A
2187N/A if (signHash.getValue() != null &&
2187N/A !signHash.getValue().equals(signHash.getDefaultValue())) {
4134N/A values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf(signHash.getValue()));
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_SIGN_HASH, values));
2187N/A }
2187N/A
2187N/A List<String> includeAttributes = includeAttributeStrings.getValues();
2187N/A if (includeAttributes != null && includeAttributes.size() > 0) {
4134N/A values = new ArrayList<ByteString>(includeAttributes.size());
2187N/A for (String includeAttribute : includeAttributes) {
4134N/A values.add(ByteString.valueOf(includeAttribute));
2187N/A }
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_INCLUDE_ATTRIBUTE, values));
2187N/A }
2187N/A
2187N/A List<String> excludeAttributes = excludeAttributeStrings.getValues();
2187N/A if (excludeAttributes != null && excludeAttributes.size() > 0) {
4134N/A values = new ArrayList<ByteString>(excludeAttributes.size());
2187N/A for (String excludeAttribute : excludeAttributes) {
4134N/A values.add(ByteString.valueOf(excludeAttribute));
2187N/A }
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_EXCLUDE_ATTRIBUTE, values));
2187N/A }
2187N/A
2187N/A List<String> includeFilters = includeFilterStrings.getValues();
2187N/A if (includeFilters != null && includeFilters.size() > 0) {
4134N/A values = new ArrayList<ByteString>(includeFilters.size());
2187N/A for (String includeFilter : includeFilters) {
4134N/A values.add(ByteString.valueOf(includeFilter));
2187N/A }
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_INCLUDE_FILTER, values));
2187N/A }
2187N/A
2187N/A List<String> excludeFilters = excludeFilterStrings.getValues();
2187N/A if (excludeFilters != null && excludeFilters.size() > 0) {
4134N/A values = new ArrayList<ByteString>(excludeFilters.size());
2187N/A for (String excludeFilter : excludeFilters) {
4134N/A values.add(ByteString.valueOf(excludeFilter));
2187N/A }
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_EXCLUDE_FILTER, values));
2187N/A }
2187N/A
2187N/A List<String> includeBranches = includeBranchStrings.getValues();
2187N/A if (includeBranches != null && includeBranches.size() > 0) {
4134N/A values = new ArrayList<ByteString>(includeBranches.size());
2187N/A for (String includeBranche : includeBranches) {
4134N/A values.add(ByteString.valueOf(includeBranche));
2187N/A }
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_INCLUDE_BRANCH, values));
2187N/A }
2187N/A
2187N/A List<String> excludeBranches = excludeBranchStrings.getValues();
2187N/A if (excludeBranches != null && excludeBranches.size() > 0) {
4134N/A values = new ArrayList<ByteString>(excludeBranches.size());
2187N/A for (String excludeBranche : excludeBranches) {
4134N/A values.add(ByteString.valueOf(excludeBranche));
2187N/A }
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_EXCLUDE_BRANCH, values));
2187N/A }
2187N/A
2187N/A if (wrapColumn.getValue() != null &&
2187N/A !wrapColumn.getValue().equals(wrapColumn.getDefaultValue())) {
4134N/A values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf(wrapColumn.getValue()));
2187N/A attributes.add(
2187N/A new LDAPAttribute(ATTR_TASK_EXPORT_WRAP_COLUMN, values));
2187N/A }
3478N/A
3478N/A if (excludeOperationalAttrs.isPresent())
3478N/A {
4134N/A values = new ArrayList<ByteString>(1);
4134N/A values.add(ByteString.valueOf("false"));
3478N/A attributes.add(
3478N/A new LDAPAttribute(ATTR_TASK_EXPORT_INCLUDE_OPERATIONAL_ATTRIBUTES,
3478N/A values));
3478N/A }
2187N/A }
2187N/A
2187N/A /**
2187N/A * {@inheritDoc}
2187N/A */
2187N/A public String getTaskObjectclass() {
2187N/A return "ds-task-export";
2187N/A }
2187N/A
2187N/A /**
2187N/A * {@inheritDoc}
2187N/A */
5786N/A public Class<?> getTaskClass() {
2187N/A return ExportTask.class;
2187N/A }
2187N/A
2187N/A /**
2187N/A * {@inheritDoc}
2187N/A */
2187N/A protected int processLocal(boolean initializeServer,
2187N/A PrintStream out,
2187N/A PrintStream err) {
0N/A
0N/A // Perform the initial bootstrap of the Directory Server and process the
0N/A // configuration.
0N/A DirectoryServer directoryServer = DirectoryServer.getInstance();
891N/A if (initializeServer)
0N/A {
891N/A try
891N/A {
1329N/A DirectoryServer.bootstrapClient();
1329N/A DirectoryServer.initializeJMX();
891N/A }
891N/A catch (Exception e)
891N/A {
2086N/A Message message =
2086N/A ERR_SERVER_BOOTSTRAP_ERROR.get(getExceptionMessage(e));
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A
891N/A try
891N/A {
891N/A directoryServer.initializeConfiguration(configClass.getValue(),
891N/A configFile.getValue());
891N/A }
891N/A catch (InitializationException ie)
891N/A {
2086N/A Message message = ERR_CANNOT_LOAD_CONFIG.get(ie.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
2086N/A Message message = ERR_CANNOT_LOAD_CONFIG.get(getExceptionMessage(e));
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
0N/A
0N/A
0N/A
891N/A // Initialize the Directory Server schema elements.
891N/A try
891N/A {
891N/A directoryServer.initializeSchema();
891N/A }
891N/A catch (ConfigException ce)
891N/A {
2086N/A Message message = ERR_CANNOT_LOAD_SCHEMA.get(ce.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
2086N/A Message message = ERR_CANNOT_LOAD_SCHEMA.get(ie.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
2086N/A Message message = ERR_CANNOT_LOAD_SCHEMA.get(getExceptionMessage(e));
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
0N/A
0N/A
891N/A // Initialize the Directory Server core configuration.
891N/A try
891N/A {
891N/A CoreConfigManager coreConfigManager = new CoreConfigManager();
891N/A coreConfigManager.initializeCoreConfig();
891N/A }
891N/A catch (ConfigException ce)
891N/A {
2086N/A Message message =
2086N/A ERR_CANNOT_INITIALIZE_CORE_CONFIG.get(ce.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
2086N/A Message message =
2086N/A ERR_CANNOT_INITIALIZE_CORE_CONFIG.get(ie.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
2086N/A Message message =
2086N/A ERR_CANNOT_INITIALIZE_CORE_CONFIG.get(getExceptionMessage(e));
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
0N/A
0N/A
891N/A // Initialize the Directory Server crypto manager.
891N/A try
891N/A {
891N/A directoryServer.initializeCryptoManager();
891N/A }
891N/A catch (ConfigException ce)
891N/A {
2086N/A Message message =
2086N/A ERR_CANNOT_INITIALIZE_CRYPTO_MANAGER.get(ce.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
2086N/A Message message =
2086N/A ERR_CANNOT_INITIALIZE_CRYPTO_MANAGER.get(ie.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
2086N/A Message message =
2086N/A ERR_CANNOT_INITIALIZE_CRYPTO_MANAGER.get(
2086N/A getExceptionMessage(e));
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
0N/A
0N/A
1280N/A try
1280N/A {
5786N/A ErrorLogPublisher<?> errorLogPublisher =
5786N/A TextErrorLogPublisher.getToolStartupTextErrorPublisher(
3363N/A new TextWriter.STREAM(out));
5786N/A DebugLogPublisher<?> debugLogPublisher =
3363N/A TextDebugLogPublisher.getStartupTextDebugPublisher(
3363N/A new TextWriter.STREAM(out));
3363N/A ErrorLogger.addErrorLogPublisher(errorLogPublisher);
3363N/A DebugLogger.addDebugLogPublisher(debugLogPublisher);
1280N/A }
1280N/A catch(Exception e)
1280N/A {
1795N/A err.println("Error installing the custom error logger: " +
1795N/A stackTraceToSingleLineString(e));
1280N/A }
1280N/A
0N/A
0N/A
891N/A // Make sure that the Directory Server plugin initialization is performed.
891N/A try
891N/A {
891N/A HashSet<PluginType> pluginTypes = new HashSet<PluginType>(1);
891N/A pluginTypes.add(PluginType.LDIF_EXPORT);
891N/A directoryServer.initializePlugins(pluginTypes);
891N/A }
891N/A catch (ConfigException ce)
891N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_CANNOT_INITIALIZE_PLUGINS.get(ce.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_CANNOT_INITIALIZE_PLUGINS.get(ie.getMessage());
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_CANNOT_INITIALIZE_PLUGINS.get(
2086N/A getExceptionMessage(e));
1795N/A err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
0N/A }
0N/A
0N/A
0N/A // See if there were any user-defined sets of include/exclude attributes or
0N/A // filters. If so, then process them.
0N/A HashSet<AttributeType> excludeAttributes;
0N/A if (excludeAttributeStrings == null)
0N/A {
0N/A excludeAttributes = null;
0N/A }
0N/A else
0N/A {
0N/A excludeAttributes = new HashSet<AttributeType>();
0N/A for (String attrName : excludeAttributeStrings.getValues())
0N/A {
0N/A String lowerName = attrName.toLowerCase();
0N/A AttributeType attrType = DirectoryServer.getAttributeType(lowerName);
0N/A if (attrType == null)
0N/A {
0N/A attrType = DirectoryServer.getDefaultAttributeType(attrName);
0N/A }
0N/A
0N/A excludeAttributes.add(attrType);
0N/A }
0N/A }
0N/A
0N/A HashSet<AttributeType> includeAttributes;
0N/A if (includeAttributeStrings == null)
0N/A {
0N/A includeAttributes = null;
0N/A }
0N/A else
0N/A {
0N/A includeAttributes =new HashSet<AttributeType>();
0N/A for (String attrName : includeAttributeStrings.getValues())
0N/A {
0N/A String lowerName = attrName.toLowerCase();
0N/A AttributeType attrType = DirectoryServer.getAttributeType(lowerName);
0N/A if (attrType == null)
0N/A {
0N/A attrType = DirectoryServer.getDefaultAttributeType(attrName);
0N/A }
0N/A
0N/A includeAttributes.add(attrType);
0N/A }
0N/A }
0N/A
0N/A ArrayList<SearchFilter> excludeFilters;
0N/A if (excludeFilterStrings == null)
0N/A {
0N/A excludeFilters = null;
0N/A }
0N/A else
0N/A {
0N/A excludeFilters = new ArrayList<SearchFilter>();
0N/A for (String filterString : excludeFilterStrings.getValues())
0N/A {
0N/A try
0N/A {
0N/A excludeFilters.add(SearchFilter.createFilterFromString(filterString));
0N/A }
0N/A catch (DirectoryException de)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_PARSE_EXCLUDE_FILTER.get(
2086N/A filterString, de.getMessageObject());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_PARSE_EXCLUDE_FILTER.get(
2086N/A filterString, getExceptionMessage(e));
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A }
0N/A }
0N/A
0N/A ArrayList<SearchFilter> includeFilters;
0N/A if (includeFilterStrings == null)
0N/A {
0N/A includeFilters = null;
0N/A }
0N/A else
0N/A {
0N/A includeFilters = new ArrayList<SearchFilter>();
0N/A for (String filterString : includeFilterStrings.getValues())
0N/A {
0N/A try
0N/A {
0N/A includeFilters.add(SearchFilter.createFilterFromString(filterString));
0N/A }
0N/A catch (DirectoryException de)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_PARSE_INCLUDE_FILTER.get(
2086N/A filterString, de.getMessageObject());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_PARSE_INCLUDE_FILTER.get(
2086N/A filterString, getExceptionMessage(e));
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A // Get information about the backends defined in the server. Iterate
0N/A // through them, finding the one backend that should be used for the export,
0N/A // and also finding backends with subordinate base DNs that should be
0N/A // excluded from the export.
0N/A Backend backend = null;
0N/A List<DN> baseDNList = null;
0N/A List<DN> defaultIncludeBranches = null;
0N/A ArrayList<DN> excludeBranches = null;
0N/A
0N/A ArrayList<Backend> backendList = new ArrayList<Backend>();
1329N/A ArrayList<BackendCfg> entryList = new ArrayList<BackendCfg>();
0N/A ArrayList<List<DN>> dnList = new ArrayList<List<DN>>();
1094N/A BackendToolUtils.getBackends(backendList, entryList, dnList);
0N/A
0N/A int numBackends = backendList.size();
0N/A for (int i=0; i < numBackends; i++)
0N/A {
0N/A Backend b = backendList.get(i);
0N/A if (! backendID.getValue().equals(b.getBackendID()))
0N/A {
0N/A continue;
0N/A }
0N/A
0N/A if (backend == null)
0N/A {
0N/A backend = b;
0N/A baseDNList = dnList.get(i);
0N/A defaultIncludeBranches = dnList.get(i);
0N/A }
0N/A else
0N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_MULTIPLE_BACKENDS_FOR_ID.get(backendID.getValue());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A }
0N/A
0N/A if (backend == null)
0N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_NO_BACKENDS_FOR_ID.get(backendID.getValue());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A else if (! backend.supportsLDIFExport())
0N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_CANNOT_EXPORT_BACKEND.get(backendID.getValue());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A
0N/A if (excludeBranchStrings.isPresent())
0N/A {
0N/A excludeBranches = new ArrayList<DN>();
0N/A for (String s : excludeBranchStrings.getValues())
0N/A {
1329N/A DN excludeBranch;
0N/A try
0N/A {
0N/A excludeBranch = DN.decode(s);
0N/A }
0N/A catch (DirectoryException de)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_DECODE_EXCLUDE_BASE.get(
2086N/A s, de.getMessageObject());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_DECODE_EXCLUDE_BASE.get(
2086N/A s, getExceptionMessage(e));
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A
0N/A if (! excludeBranches.contains(excludeBranch))
0N/A {
0N/A excludeBranches.add(excludeBranch);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A List<DN> includeBranches;
0N/A if (includeBranchStrings.isPresent())
0N/A {
0N/A includeBranches = new ArrayList<DN>();
0N/A for (String s : includeBranchStrings.getValues())
0N/A {
1329N/A DN includeBranch;
0N/A try
0N/A {
0N/A includeBranch = DN.decode(s);
0N/A }
0N/A catch (DirectoryException de)
0N/A {
2086N/A Message message = ERR_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE.get(
2086N/A s, de.getMessageObject());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
2086N/A Message message = ERR_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE.get(
2086N/A s, getExceptionMessage(e));
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A
0N/A if (! Backend.handlesEntry(includeBranch, defaultIncludeBranches,
0N/A excludeBranches))
0N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_INVALID_INCLUDE_BASE.get(s, backendID.getValue());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A
0N/A includeBranches.add(includeBranch);
0N/A }
0N/A }
0N/A else
0N/A {
0N/A includeBranches = defaultIncludeBranches;
0N/A }
0N/A
0N/A
0N/A // Create the LDIF export configuration to use when reading the LDIF.
0N/A ExistingFileBehavior existingBehavior;
0N/A if (appendToLDIF.isPresent())
0N/A {
0N/A existingBehavior = ExistingFileBehavior.APPEND;
0N/A }
0N/A else
0N/A {
0N/A existingBehavior = ExistingFileBehavior.OVERWRITE;
0N/A }
0N/A
0N/A LDIFExportConfig exportConfig = new LDIFExportConfig(ldifFile.getValue(),
0N/A existingBehavior);
0N/A exportConfig.setCompressData(compressLDIF.isPresent());
0N/A exportConfig.setEncryptData(encryptLDIF.isPresent());
0N/A exportConfig.setExcludeAttributes(excludeAttributes);
0N/A exportConfig.setExcludeBranches(excludeBranches);
0N/A exportConfig.setExcludeFilters(excludeFilters);
0N/A exportConfig.setIncludeAttributes(includeAttributes);
0N/A exportConfig.setIncludeBranches(includeBranches);
0N/A exportConfig.setIncludeFilters(includeFilters);
0N/A exportConfig.setSignHash(signHash.isPresent());
601N/A exportConfig.setIncludeOperationalAttributes(
601N/A (! excludeOperationalAttrs.isPresent()));
0N/A
0N/A // FIXME -- Should this be conditional?
0N/A exportConfig.setInvokeExportPlugins(true);
0N/A
0N/A try
0N/A {
0N/A exportConfig.setWrapColumn(wrapColumn.getIntValue());
0N/A }
0N/A catch (ArgumentException ae)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_DECODE_WRAP_COLUMN_AS_INTEGER.get(
2086N/A wrapColumn.getValue());
2086N/A logError(message);
0N/A return 1;
0N/A }
0N/A
0N/A
0N/A // Get the set of base DNs for the backend as an array.
0N/A DN[] baseDNs = new DN[baseDNList.size()];
0N/A baseDNList.toArray(baseDNs);
0N/A
0N/A
0N/A // Acquire a shared lock for the backend.
0N/A try
0N/A {
0N/A String lockFile = LockFileManager.getBackendLockFileName(backend);
0N/A StringBuilder failureReason = new StringBuilder();
0N/A if (! LockFileManager.acquireSharedLock(lockFile, failureReason))
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_LOCK_BACKEND.get(
2086N/A backend.getBackendID(), String.valueOf(failureReason));
2086N/A logError(message);
3263N/A return 1;
0N/A }
0N/A }
0N/A catch (Exception e)
0N/A {
2086N/A Message message = ERR_LDIFEXPORT_CANNOT_LOCK_BACKEND.get(
2086N/A backend.getBackendID(), getExceptionMessage(e));
2086N/A logError(message);
3263N/A return 1;
0N/A }
0N/A
3263N/A boolean errorOccurred = false;
0N/A
0N/A // Launch the export.
0N/A try
0N/A {
1329N/A backend.exportLDIF(exportConfig);
0N/A }
0N/A catch (DirectoryException de)
0N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_ERROR_DURING_EXPORT.get(de.getMessageObject());
2086N/A logError(message);
3263N/A errorOccurred = true;
0N/A }
0N/A catch (Exception e)
0N/A {
2086N/A Message message =
2086N/A ERR_LDIFEXPORT_ERROR_DURING_EXPORT.get(getExceptionMessage(e));
2086N/A logError(message);
3263N/A errorOccurred = true;
0N/A }
0N/A
0N/A
0N/A // Release the shared lock on the backend.
0N/A try
0N/A {
0N/A String lockFile = LockFileManager.getBackendLockFileName(backend);
0N/A StringBuilder failureReason = new StringBuilder();
0N/A if (! LockFileManager.releaseLock(lockFile, failureReason))
0N/A {
2086N/A Message message = WARN_LDIFEXPORT_CANNOT_UNLOCK_BACKEND.get(
2086N/A backend.getBackendID(), String.valueOf(failureReason));
2086N/A logError(message);
0N/A }
0N/A }
0N/A catch (Exception e)
0N/A {
2086N/A Message message = WARN_LDIFEXPORT_CANNOT_UNLOCK_BACKEND.get(
2086N/A backend.getBackendID(), getExceptionMessage(e));
2086N/A logError(message);
0N/A }
0N/A
0N/A
0N/A // Clean up after the export by closing the export config.
0N/A exportConfig.close();
3263N/A if (!errorOccurred)
3263N/A {
3263N/A return 0;
3263N/A }
3263N/A else
3263N/A {
3263N/A return 1;
3263N/A }
0N/A }
4005N/A
4005N/A /**
4005N/A * {@inheritDoc}
4005N/A */
4005N/A public String getTaskId() {
4005N/A // NYI.
4005N/A return null;
4005N/A }
0N/A}
0N/A