| | |
| | | * CDDL HEADER END |
| | | * |
| | | * |
| | | * Copyright 2006-2008 Sun Microsystems, Inc. |
| | | * Copyright 2006-2009 Sun Microsystems, Inc. |
| | | */ |
| | | package org.opends.server.tools.makeldif; |
| | | import org.opends.messages.Message; |
| | |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.PrintStream; |
| | | import java.util.LinkedList; |
| | | import java.util.Random; |
| | | |
| | |
| | | import org.opends.server.types.Entry; |
| | | import org.opends.server.types.ExistingFileBehavior; |
| | | import org.opends.server.types.LDIFExportConfig; |
| | | import org.opends.server.types.NullOutputStream; |
| | | import org.opends.server.util.LDIFWriter; |
| | | import org.opends.server.util.args.ArgumentException; |
| | | import org.opends.server.util.args.ArgumentParser; |
| | |
| | | // The total number of entries that have been written. |
| | | private long entriesWritten; |
| | | |
| | | |
| | | private PrintStream out = System.out; |
| | | private PrintStream err = System.err; |
| | | |
| | | /** |
| | | * Invokes the <CODE>makeLDIFMain</CODE> method with the provided set of |
| | |
| | | entriesWritten = 0L; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Processes the provided set of command-line arguments and begins generating |
| | | * the LDIF content. |
| | | * |
| | | * @param args The command-line arguments provided for this program. |
| | | * |
| | | * @param initializeServer Indicates whether to initialize the server. |
| | | * @param initializeSchema Indicates whether to initialize the schema. |
| | | * @param outStream The output stream to use for standard output, or |
| | | * {@code null} if standard output is not needed. |
| | | * @param errStream The output stream to use for standard error, or |
| | | * {@code null} if standard error is not needed. |
| | | * @return A result code of zero if all processing completed properly, or |
| | | * a nonzero result if a problem occurred. |
| | | * |
| | | */ |
| | | public int makeLDIFMain(String[] args) |
| | | public int makeLDIFMain(String[] args, boolean initializeServer, |
| | | boolean initializeSchema, |
| | | OutputStream outStream, |
| | | OutputStream errStream) |
| | | { |
| | | if (outStream == null) |
| | | { |
| | | out = NullOutputStream.printStream(); |
| | | } |
| | | else |
| | | { |
| | | out = new PrintStream(outStream); |
| | | } |
| | | |
| | | if (errStream == null) |
| | | { |
| | | err = NullOutputStream.printStream(); |
| | | } |
| | | else |
| | | { |
| | | err = new PrintStream(errStream); |
| | | } |
| | | |
| | | // Create and initialize the argument parser for this program. |
| | | Message toolDescription = INFO_MAKELDIF_TOOL_DESCRIPTION.get(); |
| | | ArgumentParser argParser = new ArgumentParser(CLASS_NAME, toolDescription, |
| | |
| | | catch (ArgumentException ae) |
| | | { |
| | | Message message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage()); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | catch (ArgumentException ae) |
| | | { |
| | | Message message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage()); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | System.err.println(argParser.getUsage()); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(argParser.getUsage()); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | if (initializeServer) |
| | | { |
| | | // Initialize the Directory Server configuration handler using the |
| | | // information that was provided. |
| | | DirectoryServer directoryServer = DirectoryServer.getInstance(); |
| | |
| | | { |
| | | Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_JMX.get( |
| | | String.valueOf(configFile.getValue()), e.getMessage()); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | { |
| | | Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_CONFIG.get( |
| | | String.valueOf(configFile.getValue()), e.getMessage()); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | } |
| | | |
| | | if (initializeSchema) |
| | | { |
| | | try |
| | | { |
| | | directoryServer.initializeSchema(); |
| | | DirectoryServer.getInstance().initializeSchema(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | } |
| | | |
| | | |
| | | // Create the random number generator that will be used for the generation |
| | |
| | | { |
| | | Message message = ERR_MAKELDIF_NO_SUCH_RESOURCE_DIRECTORY.get( |
| | | resourcePath.getValue()); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | { |
| | | Message message = ERR_MAKELDIF_IOEXCEPTION_DURING_PARSE.get( |
| | | ioe.getMessage()); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Message message = ERR_MAKELDIF_EXCEPTION_DURING_PARSE.get( |
| | | e.getMessage()); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | { |
| | | for (Message s : warnings) |
| | | { |
| | | System.err.println(wrapText(s, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(s, MAX_LINE_WIDTH)); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | Message message = ERR_MAKELDIF_UNABLE_TO_CREATE_LDIF.get( |
| | | ldifFile.getValue(), String.valueOf(ioe)); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | { |
| | | Message message = ERR_MAKELDIF_ERROR_WRITING_LDIF.get( |
| | | ldifFile.getValue(), stackTraceToSingleLineString(e)); |
| | | System.err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | } |
| | | finally |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Processes the provided set of command-line arguments and begins generating |
| | | * the LDIF content. |
| | | * |
| | | * @param args The command-line arguments provided for this program. |
| | | * |
| | | * @return A result code of zero if all processing completed properly, or |
| | | * a nonzero result if a problem occurred. |
| | | */ |
| | | public int makeLDIFMain(String[] args) |
| | | { |
| | | return makeLDIFMain(args, true, true, System.out, System.err); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Writes the provided entry to the appropriate target. |
| | |
| | | if ((++entriesWritten % 1000) == 0) |
| | | { |
| | | Message message = INFO_MAKELDIF_PROCESSED_N_ENTRIES.get(entriesWritten); |
| | | System.out.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | out.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | } |
| | | |
| | | return true; |
| | |
| | | public void closeEntryWriter() |
| | | { |
| | | Message message = INFO_MAKELDIF_PROCESSING_COMPLETE.get(entriesWritten); |
| | | System.out.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | out.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | } |
| | | } |
| | | |