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 * Portions Copyright 2013-2014 ForgeRock AS 025 */ 026 027package org.opends.server.tools.upgrade; 028 029 030 031import static org.opends.messages.ToolMessages.*; 032 033import javax.security.auth.callback.Callback; 034import javax.security.auth.callback.CallbackHandler; 035import javax.security.auth.callback.ConfirmationCallback; 036import javax.security.auth.callback.TextOutputCallback; 037 038import org.forgerock.i18n.LocalizableMessage; 039 040import com.forgerock.opendj.cli.ClientException; 041import com.forgerock.opendj.cli.ReturnCode; 042 043import org.opends.server.types.InitializationException; 044import org.opends.server.util.BuildVersion; 045 046 047 048/** 049 * Context information which is passed to upgrade tasks. This might include 050 * server configuration, etc. 051 */ 052public final class UpgradeContext 053{ 054 055 /** 056 * The version we upgrade from. 057 */ 058 private final BuildVersion fromVersion; 059 060 /** 061 * The version we want to upgrade to. 062 */ 063 private final BuildVersion toVersion; 064 065 /** 066 * The call-back handler for interacting with the upgrade application. 067 */ 068 private final CallbackHandler handler; 069 070 /** 071 * If ignore errors is enabled. 072 */ 073 private boolean isIgnoreErrorsMode; 074 075 /** 076 * If accept license is enabled. 077 */ 078 private boolean isAcceptLicenseMode; 079 080 /** 081 * If interactive mode is enabled. 082 */ 083 private boolean isInteractiveMode; 084 085 /** 086 * If force upgrade is enabled. 087 */ 088 private boolean isForceUpgradeMode; 089 090 091 092 /** 093 * Creates a new upgrade context for upgrading from the instance version (as 094 * obtained from config/buildinfo) to the binary version. 095 * 096 * @param handler 097 * The call-back handler for interacting with the upgrade 098 * application. 099 * @throws InitializationException 100 * If an error occurred while reading or parsing the version. 101 */ 102 public UpgradeContext(CallbackHandler handler) throws InitializationException 103 { 104 this(BuildVersion.instanceVersion(), BuildVersion.binaryVersion(), handler); 105 } 106 107 108 109 /** 110 * Constructor for the upgrade context. 111 * 112 * @param fromVersion 113 * The version number from we upgrade from. 114 * @param toVersion 115 * The version number we want to upgrade to. 116 * @param handler 117 * The call-back handler for interacting with the upgrade 118 * application. 119 */ 120 public UpgradeContext(final BuildVersion fromVersion, 121 final BuildVersion toVersion, CallbackHandler handler) 122 { 123 this.fromVersion = fromVersion; 124 this.toVersion = toVersion; 125 this.handler = handler; 126 } 127 128 129 130 /** 131 * Returns the old version. 132 * 133 * @return The old version. 134 */ 135 BuildVersion getFromVersion() 136 { 137 return fromVersion; 138 } 139 140 141 142 /** 143 * Returns the new version. 144 * 145 * @return The new version. 146 */ 147 BuildVersion getToVersion() 148 { 149 return toVersion; 150 } 151 152 153 154 /** 155 * Returns the ignore error mode. 156 * 157 * @return {code true} if ignore error mode is activated. 158 */ 159 boolean isIgnoreErrorsMode() 160 { 161 return isIgnoreErrorsMode; 162 } 163 164 165 166 /** 167 * Sets the ignore errors mode. 168 * 169 * @param isIgnoreErrorsMode 170 * {@code true} if ignore error mode is activated. 171 * @return This upgrade context. 172 */ 173 public UpgradeContext setIgnoreErrorsMode(boolean isIgnoreErrorsMode) 174 { 175 this.isIgnoreErrorsMode = isIgnoreErrorsMode; 176 return this; 177 } 178 179 180 181 /** 182 * Returns the accept license mode. 183 * 184 * @return {@code true} if accept license mode is activated. 185 */ 186 boolean isAcceptLicenseMode() 187 { 188 return isAcceptLicenseMode; 189 } 190 191 192 193 /** 194 * Sets the accept license mode. 195 * 196 * @param isAcceptLicenseMode 197 * {@code true} if the accept license mode is activated. 198 * @return This upgrade context. 199 */ 200 public UpgradeContext setAcceptLicenseMode(boolean isAcceptLicenseMode) 201 { 202 this.isAcceptLicenseMode = isAcceptLicenseMode; 203 return this; 204 } 205 206 207 208 /** 209 * Returns the callback handler. 210 * 211 * @return The actual callback handler. 212 */ 213 CallbackHandler getHandler() 214 { 215 return handler; 216 } 217 218 219 220 /** 221 * Returns the status of the interactive mode. 222 * 223 * @return {@code true} if interactive mode is activated. 224 */ 225 boolean isInteractiveMode() 226 { 227 return isInteractiveMode; 228 } 229 230 231 232 /** 233 * Sets the interactive mode. 234 * 235 * @param isInteractiveMode 236 * {@code true} if the interactive mode is activated. 237 * @return This upgrade context. 238 */ 239 public UpgradeContext setInteractiveMode(boolean isInteractiveMode) 240 { 241 this.isInteractiveMode = isInteractiveMode; 242 return this; 243 } 244 245 246 247 /** 248 * Returns the status of the force upgrade mode. 249 * 250 * @return {@code true} if the force upgrade mode is activated. 251 */ 252 boolean isForceUpgradeMode() 253 { 254 return isForceUpgradeMode; 255 } 256 257 258 259 /** 260 * Sets the force upgrade mode. 261 * 262 * @param isForceUpgradeMode 263 * {@code true} if the force upgrade mode is activated. 264 * @return This upgrade context. 265 */ 266 public UpgradeContext setForceUpgradeMode(boolean isForceUpgradeMode) 267 { 268 this.isForceUpgradeMode = isForceUpgradeMode; 269 return this; 270 } 271 272 273 274 /** 275 * Sends notification message to the application via the call-back handler. 276 * 277 * @param message 278 * The message to be reported. 279 * @throws ClientException 280 * If an error occurred while reporting the message. 281 */ 282 void notify(final LocalizableMessage message) throws ClientException 283 { 284 try 285 { 286 handler.handle(new Callback[] { new TextOutputCallback( 287 TextOutputCallback.INFORMATION, message.toString()) }); 288 } 289 catch (final Exception e) 290 { 291 throw new ClientException(ReturnCode.ERROR_UNEXPECTED, 292 ERR_UPGRADE_DISPLAY_NOTIFICATION_ERROR.get(e.getMessage())); 293 } 294 } 295 296 297 298 /** 299 * Sends notification message to the application via the call-back handler 300 * containing specific sub type message. 301 * 302 * @param message 303 * The message to be reported. 304 * @param msgType 305 * The sub type message. The message to be reported. 306 * @throws ClientException 307 * If an error occurred while reporting the message. 308 */ 309 void notify(final LocalizableMessage message, final int msgType) throws ClientException 310 { 311 try 312 { 313 handler.handle(new Callback[] { new FormattedNotificationCallback( 314 message, msgType) }); 315 } 316 catch (final Exception e) 317 { 318 throw new ClientException(ReturnCode.ERROR_UNEXPECTED, 319 ERR_UPGRADE_DISPLAY_NOTIFICATION_ERROR.get(e.getMessage())); 320 } 321 } 322 323 324 325 /** 326 * Displays a progress callback. 327 * 328 * @param callback 329 * The callback to display. 330 * @throws ClientException 331 * If an error occurred while reporting the message. 332 */ 333 void notifyProgress(final ProgressNotificationCallback callback) 334 throws ClientException 335 { 336 try 337 { 338 handler.handle(new Callback[] { callback }); 339 } 340 catch (final Exception e) 341 { 342 throw new ClientException(ReturnCode.ERROR_UNEXPECTED, 343 ERR_UPGRADE_DISPLAY_NOTIFICATION_ERROR.get(e.getMessage())); 344 } 345 } 346 347 348 349 /** 350 * Asks a confirmation to the user. Answer is yes or no. 351 * 352 * @param message 353 * The message to be reported. 354 * @param defaultOption 355 * The default selected option for this callback. 356 * @throws ClientException 357 * If an error occurred while reporting the message. 358 * @return an integer corresponding to the user's answer. 359 */ 360 int confirmYN(final LocalizableMessage message, final int defaultOption) 361 throws ClientException 362 { 363 final ConfirmationCallback confirmYNCallback = new ConfirmationCallback( 364 message.toString(), ConfirmationCallback.WARNING, 365 ConfirmationCallback.YES_NO_OPTION, defaultOption); 366 try 367 { 368 handler.handle(new Callback[] { confirmYNCallback }); 369 } 370 catch (final Exception e) 371 { 372 throw new ClientException(ReturnCode.ERROR_UNEXPECTED, 373 ERR_UPGRADE_DISPLAY_CONFIRM_ERROR.get(e.getMessage())); 374 } 375 return confirmYNCallback.getSelectedIndex(); 376 } 377}