ExportLDIF.java revision 891
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 *
868N/A * Portions Copyright 2006-2007 Sun Microsystems, Inc.
0N/A */
0N/Apackage org.opends.server.tools;
0N/A
0N/A
0N/A
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.List;
0N/A
0N/Aimport org.opends.server.api.Backend;
0N/Aimport org.opends.server.api.plugin.PluginType;
0N/Aimport org.opends.server.config.ConfigEntry;
0N/Aimport org.opends.server.config.ConfigException;
0N/Aimport org.opends.server.config.DNConfigAttribute;
0N/Aimport org.opends.server.config.StringConfigAttribute;
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;
0N/Aimport org.opends.server.loggers.StartupErrorLogger;
0N/Aimport org.opends.server.types.AttributeType;
338N/Aimport org.opends.server.types.DirectoryException;
0N/Aimport org.opends.server.types.DN;
0N/Aimport org.opends.server.types.ErrorLogCategory;
0N/Aimport org.opends.server.types.ErrorLogSeverity;
0N/Aimport org.opends.server.types.ExistingFileBehavior;
338N/Aimport org.opends.server.types.InitializationException;
0N/Aimport org.opends.server.types.LDIFExportConfig;
0N/Aimport org.opends.server.types.SearchFilter;
0N/Aimport org.opends.server.util.args.ArgumentException;
0N/Aimport org.opends.server.util.args.ArgumentParser;
0N/Aimport org.opends.server.util.args.BooleanArgument;
0N/Aimport org.opends.server.util.args.IntegerArgument;
0N/Aimport org.opends.server.util.args.StringArgument;
0N/A
0N/Aimport static org.opends.server.config.ConfigConstants.*;
0N/Aimport static org.opends.server.loggers.Error.*;
0N/Aimport static org.opends.server.messages.ConfigMessages.*;
0N/Aimport static org.opends.server.messages.MessageHandler.*;
0N/Aimport static org.opends.server.messages.ToolMessages.*;
536N/Aimport static org.opends.server.util.ServerConstants.*;
0N/Aimport static org.opends.server.util.StaticUtils.*;
0N/A
0N/A
0N/A
0N/A/**
0N/A * This program provides a utility that may be used to export the contents of a
0N/A * Directory Server backend to an LDIF file. This will be a process that is
0N/A * intended to run separate from Directory Server and not internally within the
0N/A * server process (e.g., via the tasks interface).
0N/A */
0N/Apublic class ExportLDIF
0N/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
0N/A public static void main(String[] args)
0N/A {
0N/A int retCode = mainExportLDIF(args);
0N/A
0N/A if(retCode != 0)
0N/A {
0N/A System.exit(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 {
891N/A return mainExportLDIF(args, true);
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.
891N/A *
891N/A * @return The error code.
891N/A */
891N/A public static int mainExportLDIF(String[] args, boolean initializeServer)
891N/A {
0N/A // Define the command-line arguments that may be used with this program.
0N/A BooleanArgument appendToLDIF = null;
0N/A BooleanArgument compressLDIF = null;
0N/A BooleanArgument displayUsage = null;
0N/A BooleanArgument encryptLDIF = null;
601N/A BooleanArgument excludeOperationalAttrs = null;
0N/A BooleanArgument signHash = null;
0N/A IntegerArgument wrapColumn = null;
0N/A StringArgument backendID = null;
0N/A StringArgument configClass = null;
0N/A StringArgument configFile = null;
0N/A StringArgument excludeAttributeStrings = null;
0N/A StringArgument excludeBranchStrings = null;
0N/A StringArgument excludeFilterStrings = null;
0N/A StringArgument includeAttributeStrings = null;
0N/A StringArgument includeBranchStrings = null;
0N/A StringArgument includeFilterStrings = null;
0N/A StringArgument ldifFile = null;
0N/A
0N/A
0N/A // Create the command-line argument parser for use with this program.
536N/A String toolDescription = getMessage(MSGID_LDIFEXPORT_TOOL_DESCRIPTION);
0N/A ArgumentParser argParser =
536N/A new ArgumentParser("org.opends.server.tools.ExportLDIF",
536N/A toolDescription, false);
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 =
0N/A new StringArgument("configclass", 'C', "configClass", true, false,
0N/A true, "{configClass}",
0N/A ConfigFileHandler.class.getName(), null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_CONFIG_CLASS);
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,
0N/A true, "{configFile}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_CONFIG_FILE);
534N/A configFile.setHidden(true);
0N/A argParser.addArgument(configFile);
0N/A
0N/A
0N/A ldifFile =
0N/A new StringArgument("ldiffile", 'l', "ldifFile", true, false, true,
0N/A "{ldifFile}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_LDIF_FILE);
0N/A argParser.addArgument(ldifFile);
0N/A
0N/A
0N/A appendToLDIF =
0N/A new BooleanArgument("appendldif", 'a', "appendToLDIF",
0N/A MSGID_LDIFEXPORT_DESCRIPTION_APPEND_TO_LDIF);
0N/A argParser.addArgument(appendToLDIF);
0N/A
0N/A
0N/A backendID =
0N/A new StringArgument("backendid", 'n', "backendID", true, false, true,
0N/A "{backendID}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_BACKEND_ID);
0N/A argParser.addArgument(backendID);
0N/A
0N/A
0N/A includeBranchStrings =
0N/A new StringArgument("includebranch", 'b', "includeBranch", false,
0N/A true, true, "{branchDN}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_INCLUDE_BRANCH);
0N/A argParser.addArgument(includeBranchStrings);
0N/A
0N/A
0N/A excludeBranchStrings =
0N/A new StringArgument("excludebranch", 'B', "excludeBranch", false,
0N/A true, true, "{branchDN}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_EXCLUDE_BRANCH);
0N/A argParser.addArgument(excludeBranchStrings);
0N/A
0N/A
0N/A includeAttributeStrings =
0N/A new StringArgument("includeattribute", 'i', "includeAttribute",
0N/A false, true, true, "{attribute}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_INCLUDE_ATTRIBUTE);
0N/A argParser.addArgument(includeAttributeStrings);
0N/A
0N/A
0N/A excludeAttributeStrings =
0N/A new StringArgument("excludeattribute", 'e', "excludeAttribute",
0N/A false, true, true, "{attribute}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_EXCLUDE_ATTRIBUTE);
0N/A argParser.addArgument(excludeAttributeStrings);
0N/A
0N/A
0N/A includeFilterStrings =
0N/A new StringArgument("includefilter", 'I', "includeFilter",
0N/A false, true, true, "{filter}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_INCLUDE_FILTER);
0N/A argParser.addArgument(includeFilterStrings);
0N/A
0N/A
0N/A excludeFilterStrings =
0N/A new StringArgument("excludefilter", 'E', "excludeFilter",
0N/A false, true, true, "{filter}", null, null,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_EXCLUDE_FILTER);
0N/A argParser.addArgument(excludeFilterStrings);
0N/A
0N/A
601N/A excludeOperationalAttrs =
601N/A new BooleanArgument("excludeoperational", 'O', "excludeOperational",
601N/A MSGID_LDIFEXPORT_DESCRIPTION_EXCLUDE_OPERATIONAL);
601N/A argParser.addArgument(excludeOperationalAttrs);
601N/A
601N/A
0N/A wrapColumn =
0N/A new IntegerArgument("wrapcolumn", 'w', "wrapColumn", false, false,
0N/A true, "{wrapColumn}", 0, null, true, 0, false, 0,
0N/A MSGID_LDIFEXPORT_DESCRIPTION_WRAP_COLUMN);
0N/A argParser.addArgument(wrapColumn);
0N/A
0N/A
0N/A compressLDIF =
0N/A new BooleanArgument("compressldif", 'c', "compressLDIF",
0N/A MSGID_LDIFEXPORT_DESCRIPTION_COMPRESS_LDIF);
0N/A argParser.addArgument(compressLDIF);
0N/A
0N/A
0N/A encryptLDIF =
0N/A new BooleanArgument("encryptldif", 'y', "encryptLDIF",
0N/A MSGID_LDIFEXPORT_DESCRIPTION_ENCRYPT_LDIF);
0N/A argParser.addArgument(encryptLDIF);
0N/A
0N/A
0N/A signHash =
0N/A new BooleanArgument("signhash", 's', "signHash",
0N/A MSGID_LDIFEXPORT_DESCRIPTION_SIGN_HASH);
0N/A argParser.addArgument(signHash);
0N/A
0N/A
0N/A displayUsage =
0N/A new BooleanArgument("help", 'H', "help",
0N/A MSGID_LDIFEXPORT_DESCRIPTION_USAGE);
0N/A argParser.addArgument(displayUsage);
0N/A argParser.setUsageArgument(displayUsage);
0N/A }
0N/A catch (ArgumentException ae)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_ARGS;
0N/A String message = getMessage(msgID, ae.getMessage());
0N/A
536N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
0N/A return 1;
0N/A }
0N/A
0N/A
0N/A // Parse the command-line arguments provided to this program.
0N/A try
0N/A {
0N/A argParser.parseArguments(args);
0N/A }
0N/A catch (ArgumentException ae)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_ERROR_PARSING_ARGS;
0N/A String message = getMessage(msgID, ae.getMessage());
0N/A
536N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
0N/A System.err.println(argParser.getUsage());
0N/A return 1;
0N/A }
0N/A
0N/A
0N/A // If we should just display usage information, then print it and exit.
0N/A if (displayUsage.isPresent())
0N/A {
0N/A return 0;
0N/A }
0N/A
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 {
891N/A directoryServer.bootstrapClient();
891N/A directoryServer.initializeJMX();
891N/A }
891N/A catch (Exception e)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_SERVER_BOOTSTRAP_ERROR;
891N/A String message = getMessage(msgID, stackTraceToSingleLineString(e));
891N/A System.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 {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOAD_CONFIG;
891N/A String message = getMessage(msgID, ie.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOAD_CONFIG;
891N/A String message = getMessage(msgID, stackTraceToSingleLineString(e));
891N/A System.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 {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOAD_SCHEMA;
891N/A String message = getMessage(msgID, ce.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOAD_SCHEMA;
891N/A String message = getMessage(msgID, ie.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOAD_SCHEMA;
891N/A String message = getMessage(msgID, stackTraceToSingleLineString(e));
891N/A System.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 {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_CORE_CONFIG;
891N/A String message = getMessage(msgID, ce.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_CORE_CONFIG;
891N/A String message = getMessage(msgID, ie.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_CORE_CONFIG;
891N/A String message = getMessage(msgID, stackTraceToSingleLineString(e));
891N/A System.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 {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_CRYPTO_MANAGER;
891N/A String message = getMessage(msgID, ce.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_CRYPTO_MANAGER;
891N/A String message = getMessage(msgID, ie.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_CRYPTO_MANAGER;
891N/A String message = getMessage(msgID, stackTraceToSingleLineString(e));
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
0N/A
0N/A
891N/A // FIXME -- Install a custom logger to capture information about the state
891N/A // of the export.
891N/A StartupErrorLogger startupLogger = new StartupErrorLogger();
891N/A startupLogger.initializeErrorLogger(null);
891N/A addErrorLogger(startupLogger);
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 {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_PLUGINS;
891N/A String message = getMessage(msgID, ce.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (InitializationException ie)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_PLUGINS;
891N/A String message = getMessage(msgID, ie.getMessage());
891N/A System.err.println(wrapText(message, MAX_LINE_WIDTH));
891N/A return 1;
891N/A }
891N/A catch (Exception e)
891N/A {
891N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INITIALIZE_PLUGINS;
891N/A String message = getMessage(msgID, stackTraceToSingleLineString(e));
891N/A System.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 {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_PARSE_EXCLUDE_FILTER;
0N/A String message = getMessage(msgID, filterString,
0N/A de.getErrorMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_PARSE_EXCLUDE_FILTER;
0N/A String message = getMessage(msgID, filterString,
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
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 {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_PARSE_INCLUDE_FILTER;
0N/A String message = getMessage(msgID, filterString,
0N/A de.getErrorMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_PARSE_INCLUDE_FILTER;
0N/A String message = getMessage(msgID, filterString,
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
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 ConfigEntry configEntry = 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>();
0N/A ArrayList<ConfigEntry> entryList = new ArrayList<ConfigEntry>();
0N/A ArrayList<List<DN>> dnList = new ArrayList<List<DN>>();
0N/A 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 configEntry = entryList.get(i);
0N/A baseDNList = dnList.get(i);
0N/A defaultIncludeBranches = dnList.get(i);
0N/A }
0N/A else
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_MULTIPLE_BACKENDS_FOR_ID;
0N/A String message = getMessage(msgID, backendID.getValue());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 1;
0N/A }
0N/A }
0N/A
0N/A if (backend == null)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_NO_BACKENDS_FOR_ID;
0N/A String message = getMessage(msgID, backendID.getValue());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
0N/A return 1;
0N/A }
0N/A else if (! backend.supportsLDIFExport())
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_EXPORT_BACKEND;
0N/A String message = getMessage(msgID, backendID.getValue());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
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 {
0N/A DN excludeBranch = null;
0N/A try
0N/A {
0N/A excludeBranch = DN.decode(s);
0N/A }
0N/A catch (DirectoryException de)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DECODE_EXCLUDE_BASE;
0N/A String message = getMessage(msgID, s, de.getErrorMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DECODE_EXCLUDE_BASE;
0N/A String message = getMessage(msgID, s,
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
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 {
0N/A DN includeBranch = null;
0N/A try
0N/A {
0N/A includeBranch = DN.decode(s);
0N/A }
0N/A catch (DirectoryException de)
0N/A {
0N/A int msgID = MSGID_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE;
0N/A String message = getMessage(msgID, s, de.getErrorMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 1;
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE;
0N/A String message = getMessage(msgID, s,
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 1;
0N/A }
0N/A
0N/A if (! Backend.handlesEntry(includeBranch, defaultIncludeBranches,
0N/A excludeBranches))
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_INVALID_INCLUDE_BASE;
0N/A String message = getMessage(msgID, s, backendID.getValue());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
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 {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DECODE_WRAP_COLUMN_AS_INTEGER;
0N/A String message = getMessage(msgID, wrapColumn.getValue());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
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 {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOCK_BACKEND;
0N/A String message = getMessage(msgID, backend.getBackendID(),
0N/A String.valueOf(failureReason));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 0;
0N/A }
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOCK_BACKEND;
0N/A String message = getMessage(msgID, backend.getBackendID(),
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A return 0;
0N/A }
0N/A
0N/A
0N/A // Launch the export.
0N/A try
0N/A {
0N/A backend.exportLDIF(configEntry, baseDNs, exportConfig);
0N/A }
0N/A catch (DirectoryException de)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_ERROR_DURING_EXPORT;
0N/A String message = getMessage(msgID, de.getErrorMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_ERROR_DURING_EXPORT;
0N/A String message = getMessage(msgID, stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
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 {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_UNLOCK_BACKEND;
0N/A String message = getMessage(msgID, backend.getBackendID(),
0N/A String.valueOf(failureReason));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_WARNING,
0N/A message, msgID);
0N/A }
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_UNLOCK_BACKEND;
0N/A String message = getMessage(msgID, backend.getBackendID(),
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_WARNING,
0N/A message, msgID);
0N/A }
0N/A
0N/A
0N/A // Clean up after the export by closing the export config.
0N/A exportConfig.close();
0N/A return 0;
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Retrieves information about the backends defined in the Directory Server
0N/A * configuration.
0N/A *
0N/A * @param backendList A list into which instantiated (but not initialized)
0N/A * backend instances will be placed.
0N/A * @param entryList A list into which the config entries associated with
0N/A * the backends will be placed.
0N/A * @param dnList A list into which the set of base DNs for each backend
0N/A * will be placed.
0N/A */
0N/A private static void getBackends(ArrayList<Backend> backendList,
0N/A ArrayList<ConfigEntry> entryList,
0N/A ArrayList<List<DN>> dnList)
0N/A {
0N/A // Get the base entry for all backend configuration.
0N/A DN backendBaseDN = null;
0N/A try
0N/A {
0N/A backendBaseDN = DN.decode(DN_BACKEND_BASE);
0N/A }
0N/A catch (DirectoryException de)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DECODE_BACKEND_BASE_DN;
0N/A String message = getMessage(msgID, DN_BACKEND_BASE, de.getErrorMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
0N/A System.exit(1);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DECODE_BACKEND_BASE_DN;
0N/A String message = getMessage(msgID, DN_BACKEND_BASE,
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
0N/A System.exit(1);
0N/A }
0N/A
0N/A ConfigEntry baseEntry = null;
0N/A try
0N/A {
0N/A baseEntry = DirectoryServer.getConfigEntry(backendBaseDN);
0N/A }
0N/A catch (ConfigException ce)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_RETRIEVE_BACKEND_BASE_ENTRY;
0N/A String message = getMessage(msgID, DN_BACKEND_BASE, ce.getMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
0N/A System.exit(1);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_RETRIEVE_BACKEND_BASE_ENTRY;
0N/A String message = getMessage(msgID, DN_BACKEND_BASE,
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR, message,
0N/A msgID);
0N/A System.exit(1);
0N/A }
0N/A
0N/A
0N/A // Iterate through the immediate children, attempting to parse them as
0N/A // backends.
0N/A for (ConfigEntry configEntry : baseEntry.getChildren().values())
0N/A {
0N/A // Get the backend ID attribute from the entry. If there isn't one, then
0N/A // skip the entry.
0N/A String backendID = null;
0N/A try
0N/A {
0N/A int msgID = MSGID_CONFIG_BACKEND_ATTR_DESCRIPTION_BACKEND_ID;
0N/A StringConfigAttribute idStub =
0N/A new StringConfigAttribute(ATTR_BACKEND_ID, getMessage(msgID),
0N/A true, false, true);
0N/A StringConfigAttribute idAttr =
0N/A (StringConfigAttribute) configEntry.getConfigAttribute(idStub);
0N/A if (idAttr == null)
0N/A {
0N/A continue;
0N/A }
0N/A else
0N/A {
0N/A backendID = idAttr.activeValue();
0N/A }
0N/A }
0N/A catch (ConfigException ce)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DETERMINE_BACKEND_ID;
0N/A String message = getMessage(msgID, String.valueOf(configEntry.getDN()),
0N/A ce.getMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A System.exit(1);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DETERMINE_BACKEND_ID;
0N/A String message = getMessage(msgID, String.valueOf(configEntry.getDN()),
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A System.exit(1);
0N/A }
0N/A
0N/A
0N/A // Get the backend class name attribute from the entry. If there isn't
0N/A // one, then just skip the entry.
0N/A String backendClassName = null;
0N/A try
0N/A {
0N/A int msgID = MSGID_CONFIG_BACKEND_ATTR_DESCRIPTION_CLASS;
0N/A StringConfigAttribute classStub =
0N/A new StringConfigAttribute(ATTR_BACKEND_CLASS, getMessage(msgID),
0N/A true, false, false);
0N/A StringConfigAttribute classAttr =
0N/A (StringConfigAttribute) configEntry.getConfigAttribute(classStub);
0N/A if (classAttr == null)
0N/A {
0N/A continue;
0N/A }
0N/A else
0N/A {
0N/A backendClassName = classAttr.activeValue();
0N/A }
0N/A }
0N/A catch (ConfigException ce)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DETERMINE_BACKEND_CLASS;
0N/A String message = getMessage(msgID, String.valueOf(configEntry.getDN()),
0N/A ce.getMessage());
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A System.exit(1);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DETERMINE_BACKEND_CLASS;
0N/A String message = getMessage(msgID, String.valueOf(configEntry.getDN()),
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A System.exit(1);
0N/A }
0N/A
0N/A Class backendClass = null;
0N/A try
0N/A {
0N/A backendClass = Class.forName(backendClassName);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_LOAD_BACKEND_CLASS;
0N/A String message = getMessage(msgID, backendClassName,
0N/A String.valueOf(configEntry.getDN()),
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A System.exit(1);
0N/A }
0N/A
0N/A Backend backend = null;
0N/A try
0N/A {
0N/A backend = (Backend) backendClass.newInstance();
0N/A backend.setBackendID(backendID);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_INSTANTIATE_BACKEND_CLASS;
0N/A String message = getMessage(msgID, backendClassName,
0N/A String.valueOf(configEntry.getDN()),
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A System.exit(1);
0N/A }
0N/A
0N/A
0N/A // Get the base DN attribute from the entry. If there isn't one, then
0N/A // just skip this entry.
0N/A List<DN> baseDNs = null;
0N/A try
0N/A {
0N/A int msgID = MSGID_CONFIG_BACKEND_ATTR_DESCRIPTION_BASE_DNS;
0N/A DNConfigAttribute baseDNStub =
0N/A new DNConfigAttribute(ATTR_BACKEND_BASE_DN, getMessage(msgID),
0N/A true, true, true);
0N/A DNConfigAttribute baseDNAttr =
0N/A (DNConfigAttribute) configEntry.getConfigAttribute(baseDNStub);
0N/A if (baseDNAttr == null)
0N/A {
0N/A msgID = MSGID_LDIFEXPORT_NO_BASES_FOR_BACKEND;
0N/A String message = getMessage(msgID,
0N/A String.valueOf(configEntry.getDN()));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A }
0N/A else
0N/A {
0N/A baseDNs = baseDNAttr.activeValues();
0N/A }
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A int msgID = MSGID_LDIFEXPORT_CANNOT_DETERMINE_BASES_FOR_BACKEND;
0N/A String message = getMessage(msgID, String.valueOf(configEntry.getDN()),
0N/A stackTraceToSingleLineString(e));
0N/A logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
0N/A message, msgID);
0N/A System.exit(1);
0N/A }
0N/A
0N/A
0N/A backendList.add(backend);
0N/A entryList.add(configEntry);
0N/A dnList.add(baseDNs);
0N/A }
0N/A }
0N/A}
0N/A