001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2008-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2014 ForgeRock AS 026 */ 027 028package org.opends.quicksetup; 029 030import static com.forgerock.opendj.cli.Utils.wrapText; 031 032import org.forgerock.i18n.LocalizableMessage; 033import org.forgerock.i18n.slf4j.LocalizedLogger; 034 035import org.opends.quicksetup.util.ProgressMessageFormatter; 036import org.opends.quicksetup.util.PlainTextProgressMessageFormatter; 037import org.opends.quicksetup.util.Utils; 038import org.opends.quicksetup.event.ProgressUpdateListener; 039import org.opends.quicksetup.event.ProgressUpdateEvent; 040import com.forgerock.opendj.cli.ClientException; 041 042/** 043 * Class used by Launcher to start a CLI application. 044 * 045 */ 046public class QuickSetupCli { 047 048 /** Arguments passed in the command line. */ 049 protected Launcher launcher; 050 051 private CliApplication cliApp; 052 053 private UserData userData; 054 055 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 056 057 /** 058 * Creates a QuickSetupCli instance. 059 * @param cliApp the application to be run 060 * @param launcher that launched the app 061 */ 062 public QuickSetupCli(CliApplication cliApp, Launcher launcher) { 063 this.cliApp = cliApp; 064 this.launcher = launcher; 065 } 066 067 /** 068 * Gets the user data this application will use when running. 069 * @return UserData to use when running 070 */ 071 public UserData getUserData() { 072 return this.userData; 073 } 074 075 /** 076 * Parses the user data and prompts the user for data if required. If the 077 * user provides all the required data it launches the application. 078 * 079 * @return the return code (SUCCESSFUL, CANCELLED, USER_DATA_ERROR, 080 * ERROR_ACCESSING_FILE_SYSTEM, ERROR_STOPPING_SERVER or BUG. 081 */ 082 public ReturnCode run() 083 { 084 ReturnCode returnValue; 085 // Parse the arguments 086 try 087 { 088 ProgressMessageFormatter formatter = 089 new PlainTextProgressMessageFormatter(); 090 cliApp.setProgressMessageFormatter(formatter); 091 userData = cliApp.createUserData(launcher); 092 if (userData != null) 093 { 094 cliApp.setUserData(userData); 095 if (!userData.isQuiet()) { 096 cliApp.addProgressUpdateListener( 097 new ProgressUpdateListener() { 098 public void progressUpdate(ProgressUpdateEvent ev) { 099 LocalizableMessage newLogs = ev.getNewLogs(); 100 if (newLogs != null) { 101 System.out.print( 102 wrapText(newLogs, Utils.getCommandLineMaxLineWidth())); 103 } 104 } 105 }); 106 } 107 Thread appThread = new Thread(cliApp, "CLI Application"); 108 logger.info(LocalizableMessage.raw("Launching application")); 109 appThread.start(); 110 while (!Thread.State.TERMINATED.equals(appThread.getState())) { 111 try { 112 Thread.sleep(100); 113 } catch (Exception ex) { 114 // do nothing; 115 } 116 } 117 returnValue = cliApp.getReturnCode(); 118 logger.info(LocalizableMessage.raw("Application returnValue: "+returnValue)); 119 if (returnValue == null) { 120 ApplicationException ue = cliApp.getRunError(); 121 if (ue != null) 122 { 123 logger.info(LocalizableMessage.raw("Application run error: "+ue, ue)); 124 returnValue = ue.getType(); 125 } 126 else 127 { 128 returnValue = ReturnCode.SUCCESSFUL; 129 } 130 } 131 } 132 else 133 { 134 // User cancelled operation. 135 returnValue = ReturnCode.CANCELED; 136 } 137 } 138 catch (UserDataException uude) 139 { 140 logger.error(LocalizableMessage.raw("UserDataException: "+uude, uude)); 141 System.err.println(); 142 System.err.println(wrapText(uude.getLocalizedMessage(), 143 Utils.getCommandLineMaxLineWidth())); 144 System.err.println(); 145 if (uude.getCause() instanceof ClientException) 146 { 147 returnValue = ReturnCode.USER_INPUT_ERROR; 148 } 149 else 150 { 151 returnValue = ReturnCode.USER_DATA_ERROR; 152 } 153 } 154 catch (ApplicationException ae) 155 { 156 logger.error(LocalizableMessage.raw("ApplicationException: "+ae, ae)); 157 System.err.println(); 158 System.err.println(ae.getLocalizedMessage()); 159 System.err.println(); 160 returnValue = ae.getType(); 161 } 162 catch (Throwable t) 163 { 164 logger.error(LocalizableMessage.raw("Unexpected error: "+t, t)); 165 returnValue = ReturnCode.UNKNOWN; 166 } 167 logger.info(LocalizableMessage.raw("returnValue: "+returnValue.getReturnCode())); 168 return returnValue; 169 } 170 171}