コード例 #1
0
  /** Check that any referenced components are enabled if required. */
  private static MenuResult<Void> checkReferences(
      ConsoleApplication app,
      ManagementContext context,
      ManagedObject<?> mo,
      SubCommandHandler handler)
      throws ClientException, CLIException {
    ManagedObjectDefinition<?, ?> d = mo.getManagedObjectDefinition();
    Message ufn = d.getUserFriendlyName();

    try {
      for (PropertyDefinition<?> pd : d.getAllPropertyDefinitions()) {
        if (pd instanceof AggregationPropertyDefinition<?, ?>) {
          AggregationPropertyDefinition<?, ?> apd = (AggregationPropertyDefinition<?, ?>) pd;

          // Skip this aggregation if the referenced managed objects
          // do not need to be enabled.
          if (!apd.getTargetNeedsEnablingCondition().evaluate(context, mo)) {
            continue;
          }

          // The referenced component(s) must be enabled.
          for (String name : mo.getPropertyValues(apd)) {
            ManagedObjectPath<?, ?> path = apd.getChildPath(name);
            Message rufn = path.getManagedObjectDefinition().getUserFriendlyName();
            ManagedObject<?> ref;
            try {
              ref = context.getManagedObject(path);
            } catch (DefinitionDecodingException e) {
              Message msg = ERR_DSCFG_ERROR_GET_CHILD_DDE.get(rufn, rufn, rufn);
              throw new ClientException(LDAPResultCode.OTHER, msg);
            } catch (ManagedObjectDecodingException e) {
              // FIXME: should not abort here. Instead, display the
              // errors (if verbose) and apply the changes to the
              // partial managed object.
              Message msg = ERR_DSCFG_ERROR_GET_CHILD_MODE.get(rufn);
              throw new ClientException(LDAPResultCode.OTHER, msg, e);
            } catch (ManagedObjectNotFoundException e) {
              Message msg = ERR_DSCFG_ERROR_GET_CHILD_MONFE.get(rufn);
              throw new ClientException(LDAPResultCode.NO_SUCH_OBJECT, msg);
            }

            Condition condition = apd.getTargetIsEnabledCondition();
            while (!condition.evaluate(context, ref)) {
              boolean isBadReference = true;

              if (condition instanceof ContainsCondition) {
                // Attempt to automatically enable the managed object.
                ContainsCondition cvc = (ContainsCondition) condition;
                app.println();
                if (app.confirmAction(
                    INFO_EDITOR_PROMPT_ENABLED_REFERENCED_COMPONENT.get(rufn, name, ufn), true)) {
                  cvc.setPropertyValue(ref);
                  try {
                    ref.commit();

                    // Try to create the command builder
                    if ((app instanceof DSConfig) && app.isInteractive()) {
                      DSConfig dsConfig = (DSConfig) app;
                      String subCommandName =
                          "set-" + path.getRelationDefinition().getName() + "-prop";
                      CommandBuilder builder = dsConfig.getCommandBuilder(subCommandName);

                      if (path.getRelationDefinition()
                          instanceof InstantiableRelationDefinition<?, ?>) {
                        String argName =
                            CLIProfile.getInstance()
                                .getNamingArgument(path.getRelationDefinition());
                        try {
                          StringArgument arg =
                              new StringArgument(
                                  argName,
                                  null,
                                  argName,
                                  false,
                                  true,
                                  INFO_NAME_PLACEHOLDER.get(),
                                  INFO_DSCFG_DESCRIPTION_NAME.get(d.getUserFriendlyName()));
                          arg.addValue(name);
                          builder.addArgument(arg);
                        } catch (Throwable t) {
                          // Bug
                          throw new RuntimeException("Unexpected error: " + t, t);
                        }
                      }

                      try {
                        StringArgument arg =
                            new StringArgument(
                                OPTION_DSCFG_LONG_SET,
                                OPTION_DSCFG_SHORT_SET,
                                OPTION_DSCFG_LONG_SET,
                                false,
                                true,
                                true,
                                INFO_VALUE_SET_PLACEHOLDER.get(),
                                null,
                                null,
                                INFO_DSCFG_DESCRIPTION_PROP_VAL.get());
                        PropertyDefinition<?> propertyDefinition = cvc.getPropertyDefinition();
                        arg.addValue(
                            propertyDefinition.getName()
                                + ':'
                                + castAndGetArgumentValue(propertyDefinition, cvc.getValue()));
                        builder.addArgument(arg);
                      } catch (Throwable t) {
                        // Bug
                        throw new RuntimeException("Unexpected error: " + t, t);
                      }
                      dsConfig.printCommandBuilder(builder);
                    }

                    isBadReference = false;
                  } catch (MissingMandatoryPropertiesException e) {
                    // Give the user the chance to fix the problems.
                    app.println();
                    displayMissingMandatoryPropertyException(app, e);
                    app.println();
                    if (app.confirmAction(INFO_DSCFG_PROMPT_EDIT.get(rufn), true)) {
                      MenuResult<Void> result = modifyManagedObject(app, context, ref, handler);
                      if (result.isQuit()) {
                        return result;
                      } else if (result.isSuccess()) {
                        // The referenced component was modified
                        // successfully, but may still be disabled.
                        isBadReference = false;
                      }
                    }
                  } catch (ConcurrentModificationException e) {
                    Message msg = ERR_DSCFG_ERROR_MODIFY_CME.get(ufn);
                    throw new ClientException(LDAPResultCode.CONSTRAINT_VIOLATION, msg);
                  } catch (OperationRejectedException e) {
                    // Give the user the chance to fix the problems.
                    app.println();
                    displayOperationRejectedException(app, e);
                    app.println();
                    if (app.confirmAction(INFO_DSCFG_PROMPT_EDIT.get(rufn), true)) {
                      MenuResult<Void> result = modifyManagedObject(app, context, ref, handler);
                      if (result.isQuit()) {
                        return result;
                      } else if (result.isSuccess()) {
                        // The referenced component was modified
                        // successfully, but may still be disabled.
                        isBadReference = false;
                      }
                    }
                  } catch (ManagedObjectAlreadyExistsException e) {
                    // Should never happen.
                    throw new IllegalStateException(e);
                  }
                }
              } else {
                app.println();
                if (app.confirmAction(
                    INFO_DSCFG_PROMPT_EDIT_TO_ENABLE.get(rufn, name, ufn), true)) {
                  MenuResult<Void> result =
                      SetPropSubCommandHandler.modifyManagedObject(app, context, ref, handler);
                  if (result.isQuit()) {
                    return result;
                  } else if (result.isSuccess()) {
                    // The referenced component was modified
                    // successfully, but may still be disabled.
                    isBadReference = false;
                  }
                }
              }

              // If the referenced component is still disabled because
              // the user refused to modify it, then give the used the
              // option of editing the referencing component.
              if (isBadReference) {
                app.println();
                app.println(ERR_SET_REFERENCED_COMPONENT_DISABLED.get(ufn, rufn));
                app.println();
                if (app.confirmAction(INFO_DSCFG_PROMPT_EDIT_AGAIN.get(ufn), true)) {
                  return MenuResult.again();
                } else {
                  return MenuResult.cancel();
                }
              }
            }
          }
        }
      }
    } catch (AuthorizationException e) {
      Message msg = ERR_DSCFG_ERROR_MODIFY_AUTHZ.get(ufn);
      throw new ClientException(LDAPResultCode.INSUFFICIENT_ACCESS_RIGHTS, msg);
    } catch (CommunicationException e) {
      Message msg = ERR_DSCFG_ERROR_MODIFY_CE.get(ufn, e.getMessage());
      throw new ClientException(LDAPResultCode.OTHER, msg);
    }

    return MenuResult.success();
  }
コード例 #2
0
  /**
   * Configure the provided managed object and updates the command builder in the pased
   * SetPropSubCommandHandler object.
   *
   * @param app The console application.
   * @param context The management context.
   * @param mo The managed object to be configured.
   * @param handler The SubCommandHandler whose command builder properties must be updated.
   * @return Returns a MenuResult.success() if the managed object was configured successfully, or
   *     MenuResult.quit(), or MenuResult.cancel(), if the managed object was edited interactively
   *     and the user chose to quit or cancel.
   * @throws ClientException If an unrecoverable client exception occurred whilst interacting with
   *     the server.
   * @throws CLIException If an error occurred whilst interacting with the console.
   */
  public static MenuResult<Void> modifyManagedObject(
      ConsoleApplication app,
      ManagementContext context,
      ManagedObject<?> mo,
      SubCommandHandler handler)
      throws ClientException, CLIException {
    ManagedObjectDefinition<?, ?> d = mo.getManagedObjectDefinition();
    Message ufn = d.getUserFriendlyName();

    PropertyValueEditor editor = new PropertyValueEditor(app, context);
    while (true) {
      // Interactively set properties if applicable.
      if (app.isInteractive()) {
        SortedSet<PropertyDefinition<?>> properties = new TreeSet<PropertyDefinition<?>>();
        for (PropertyDefinition<?> pd : d.getAllPropertyDefinitions()) {
          if (pd.hasOption(PropertyOption.HIDDEN)) {
            continue;
          }
          if (!app.isAdvancedMode() && pd.hasOption(PropertyOption.ADVANCED)) {
            continue;
          }
          properties.add(pd);
        }

        MenuResult<Void> result = editor.edit(mo, properties, false);

        // Interactively enable/edit referenced components.
        if (result.isSuccess()) {
          result = checkReferences(app, context, mo, handler);
          if (result.isAgain()) {
            // Edit again.
            continue;
          }
        }

        if (result.isQuit()) {
          if (!app.isMenuDrivenMode()) {
            // User chose to cancel any changes.
            Message msg = INFO_DSCFG_CONFIRM_MODIFY_FAIL.get(ufn);
            app.printVerboseMessage(msg);
          }
          return MenuResult.quit();
        } else if (result.isCancel()) {
          return MenuResult.cancel();
        }
      }

      try {
        // Commit the changes if necessary
        if (mo.isModified()) {
          mo.commit();

          // Output success message.
          if (app.isVerbose() || app.isInteractive()) {
            app.println();
            Message msg = INFO_DSCFG_CONFIRM_MODIFY_SUCCESS.get(ufn);
            app.printVerboseMessage(msg);
          }

          for (PropertyEditorModification<?> mod : editor.getModifications()) {
            try {
              handler.getCommandBuilder().addArgument(createArgument(mod));
            } catch (ArgumentException ae) {
              // This is a bug
              throw new RuntimeException(
                  "Unexpected error generating the command builder: " + ae, ae);
            }
          }

          handler.setCommandBuilderUseful(true);
        }

        return MenuResult.success();
      } catch (MissingMandatoryPropertiesException e) {
        if (app.isInteractive()) {
          // If interactive, give the user the chance to fix the
          // problems.
          app.println();
          displayMissingMandatoryPropertyException(app, e);
          app.println();
          if (!app.confirmAction(INFO_DSCFG_PROMPT_EDIT_AGAIN.get(ufn), true)) {
            return MenuResult.cancel();
          }
        } else {
          throw new ClientException(LDAPResultCode.CONSTRAINT_VIOLATION, e.getMessageObject(), e);
        }
      } catch (AuthorizationException e) {
        Message msg = ERR_DSCFG_ERROR_MODIFY_AUTHZ.get(ufn);
        throw new ClientException(LDAPResultCode.INSUFFICIENT_ACCESS_RIGHTS, msg);
      } catch (ConcurrentModificationException e) {
        Message msg = ERR_DSCFG_ERROR_MODIFY_CME.get(ufn);
        throw new ClientException(LDAPResultCode.CONSTRAINT_VIOLATION, msg);
      } catch (OperationRejectedException e) {
        if (app.isInteractive()) {
          // If interactive, give the user the chance to fix the
          // problems.
          app.println();
          displayOperationRejectedException(app, e);
          app.println();
          if (!app.confirmAction(INFO_DSCFG_PROMPT_EDIT_AGAIN.get(ufn), true)) {
            return MenuResult.cancel();
          }
        } else {
          throw new ClientException(LDAPResultCode.CONSTRAINT_VIOLATION, e.getMessageObject(), e);
        }
      } catch (CommunicationException e) {
        Message msg = ERR_DSCFG_ERROR_MODIFY_CE.get(ufn, e.getMessage());
        throw new ClientException(LDAPResultCode.OTHER, msg);
      } catch (ManagedObjectAlreadyExistsException e) {
        // Should never happen.
        throw new IllegalStateException(e);
      }
    }
  }