| | |
| | | import static org.opends.server.messages.ToolMessages.*; |
| | | import static org.opends.server.util.ServerConstants.*; |
| | | import static org.opends.server.util.StaticUtils.*; |
| | | import static org.opends.admin.ads.DsServiceCliReturnCode.*; |
| | | |
| | | |
| | | /** |
| | |
| | | * The fully-qualified name of this class. |
| | | */ |
| | | private static final String CLASS_NAME = |
| | | "org.opends.admin.ads.DsServiceCLI"; |
| | | "org.opends.admin.ads.DsServiceCliMain"; |
| | | |
| | | // The print stream to use for standard error. |
| | | private PrintStream err; |
| | |
| | | err = new PrintStream(errStream); |
| | | } |
| | | |
| | | DsServiceCliMain dsServiceCli = new DsServiceCliMain(out, err); |
| | | return dsServiceCli.execute(args); |
| | | } |
| | | |
| | | /** |
| | | * Parses the provided command-line arguments and uses that information to |
| | | * run the dsservice tool. |
| | | * |
| | | * @param args The command-line arguments provided to this |
| | | * program. |
| | | * |
| | | * @return The error code. |
| | | */ |
| | | public int execute(String[] args) |
| | | { |
| | | // Create the command-line argument parser for use with this |
| | | // program. |
| | | DsServiceCliParser argParser ; |
| | | try |
| | | { |
| | | String toolDescription = getMessage(MSGID_DSSERVICE_TOOL_DESCRIPTION); |
| | | String toolDescription = getMessage(MSGID_ADMIN_TOOL_DESCRIPTION); |
| | | argParser = new DsServiceCliParser(CLASS_NAME, |
| | | toolDescription, false); |
| | | argParser.initializeParser(out); |
| | |
| | | String message = getMessage(msgID, ae.getMessage()); |
| | | |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | return ReturnCode.CANNOT_INITIALIZE_ARGS.getReturnCode(); |
| | | } |
| | | |
| | | // Parse the command-line arguments provided to this program. |
| | |
| | | |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | err.println(argParser.getUsage()); |
| | | return 1; |
| | | return ReturnCode.ERROR_PARSING_ARGS.getReturnCode(); |
| | | } |
| | | |
| | | // If we should just display usage information, then print it and exit. |
| | | if (argParser.usageOrVersionDisplayed()) |
| | | { |
| | | return 0; |
| | | return ReturnCode.SUCCESSFUL.getReturnCode(); |
| | | } |
| | | |
| | | // Get connection parameters |
| | |
| | | String port = argParser.getPort() ; |
| | | String dn = argParser.getBindDN() ; |
| | | String pwd = argParser.getBindPassword(dn,out,err) ; |
| | | if (pwd == null) |
| | | { |
| | | // TODO Should we do something? |
| | | return 1; |
| | | } |
| | | |
| | | // Try to connect |
| | | String ldapUrl = "ldap://"+host+":"+port; |
| | | |
| | | InitialLdapContext ctx = null; |
| | | int returnCode = 0 ; |
| | | ReturnCode returnCode = ReturnCode.SUCCESSFUL ; |
| | | try |
| | | { |
| | | ctx = ConnectionUtils.createLdapContext(ldapUrl, dn, pwd, |
| | |
| | | } |
| | | catch (NamingException e) |
| | | { |
| | | int msgID = MSGID_DSSERVICE_CANNOT_CONNECT_TO_ADS; |
| | | int msgID = MSGID_ADMIN_CANNOT_CONNECT_TO_ADS; |
| | | String message = getMessage(msgID, host); |
| | | |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | return 1; |
| | | return ReturnCode.CANNOT_CONNECT_TO_ADS.getReturnCode(); |
| | | } |
| | | ADSContext adsContext = new ADSContext(ctx); |
| | | |
| | | DirectoryServer.bootstrapClient(); |
| | | // perform the subCommand |
| | | ADSContextException adsException = null ; |
| | | try |
| | | { |
| | | returnCode = argParser.performSubCommand(adsContext, out, err); |
| | | } |
| | | catch (ADSContextException e) |
| | | { |
| | | // TODO Print a nice message |
| | | e.printStackTrace(); |
| | | returnCode = e.error.ordinal(); |
| | | adsException = e; |
| | | returnCode = e.error.getReturnCode(); |
| | | } |
| | | |
| | | // deconnection |
| | |
| | | { |
| | | // TODO Should we do something ? |
| | | } |
| | | return returnCode; |
| | | |
| | | int msgID = returnCode.getMessageId(); |
| | | String message = "" ; |
| | | if ( (returnCode == ReturnCode.SUCCESSFUL) |
| | | || |
| | | (returnCode == ReturnCode.SUCCESSFUL_NOP)) |
| | | { |
| | | if (argParser.isVerbose()) |
| | | { |
| | | out.println(wrapText(getMessage(msgID), MAX_LINE_WIDTH)); |
| | | } |
| | | } |
| | | else |
| | | if (msgID != -1) |
| | | { |
| | | message = getMessage(MSGID_ADMIN_ERROR); |
| | | message = message + getMessage(msgID); |
| | | err.println(wrapText(message, MAX_LINE_WIDTH)); |
| | | if (argParser.isVerbose() && (adsException != null)) |
| | | { |
| | | adsException.printStackTrace(); |
| | | } |
| | | } |
| | | return returnCode.getReturnCode(); |
| | | } |
| | | } |