/**
  * Creates a new set-xxx-prop sub-command for a singleton relation.
  *
  * @param parser The sub-command argument parser.
  * @param path The parent managed object path.
  * @param r The singleton relation.
  * @return Returns the new set-xxx-prop sub-command.
  * @throws ArgumentException If the sub-command could not be created successfully.
  */
 public static SetPropSubCommandHandler create(
     SubCommandArgumentParser parser,
     ManagedObjectPath<?, ?> path,
     SingletonRelationDefinition<?, ?> r)
     throws ArgumentException {
   return new SetPropSubCommandHandler(parser, path.child(r), r);
 }
Example #2
0
  /**
   * Creates a new DN representing the specified managed object path and relation.
   *
   * @param path The managed object path.
   * @param relation The child relation.
   * @return Returns a new DN representing the specified managed object path and relation.
   */
  public static DN create(ManagedObjectPath<?, ?> path, RelationDefinition<?, ?> relation) {
    DN dn = path.toDN();

    try {
      LDAPProfile profile = LDAPProfile.getInstance();
      DN localName = DN.decode(profile.getRelationRDNSequence(relation));
      return dn.concat(localName);
    } catch (DirectoryException e) {
      throw new RuntimeException(e);
    }
  }
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  @Override
  public MenuResult<Integer> run(ConsoleApplication app, ManagementContextFactory factory)
      throws ArgumentException, ClientException, CLIException {
    // Get the naming argument values.
    List<String> names = getNamingArgValues(app, namingArgs);

    // Reset the command builder
    getCommandBuilder().clearArguments();

    setCommandBuilderUseful(false);

    // Update the command builder.
    updateCommandBuilderWithSubCommand();

    // Get the targeted managed object.
    Message ufn = path.getRelationDefinition().getUserFriendlyName();
    ManagementContext context = factory.getManagementContext(app);
    MenuResult<ManagedObject<?>> result;
    try {
      result = getManagedObject(app, context, path, names);
    } catch (AuthorizationException e) {
      Message msg = ERR_DSCFG_ERROR_MODIFY_AUTHZ.get(ufn);
      throw new ClientException(LDAPResultCode.INSUFFICIENT_ACCESS_RIGHTS, msg);
    } catch (DefinitionDecodingException e) {
      Message msg = ERR_DSCFG_ERROR_GET_CHILD_DDE.get(ufn, ufn, ufn);
      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(ufn);
      throw new ClientException(LDAPResultCode.OTHER, msg, e);
    } catch (CommunicationException e) {
      Message msg = ERR_DSCFG_ERROR_MODIFY_CE.get(ufn, e.getMessage());
      throw new ClientException(LDAPResultCode.OTHER, msg);
    } catch (ConcurrentModificationException e) {
      Message msg = ERR_DSCFG_ERROR_MODIFY_CME.get(ufn);
      throw new ClientException(LDAPResultCode.CONSTRAINT_VIOLATION, msg);
    } catch (ManagedObjectNotFoundException e) {
      String objName = names.get(names.size() - 1);
      ArgumentException except = null;
      Message msg;
      // if object name is 'null', get a user-friendly string to represent this
      if (objName == null) {
        msg = ERR_DSCFG_ERROR_FINDER_NO_CHILDREN_NULL.get();
        except = new ArgumentException(msg);
      } else {
        except = ArgumentExceptionFactory.unknownValueForChildComponent("\"" + objName + "\"");
      }
      if (app.isInteractive()) {
        app.println();
        app.printVerboseMessage(except.getMessageObject());
        return MenuResult.cancel();
      } else {
        throw except;
      }
    }

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

    ManagedObject<?> child = result.getValue();
    ManagedObjectDefinition<?, ?> d = child.getManagedObjectDefinition();
    Map<String, ModificationType> lastModTypes = new HashMap<String, ModificationType>();
    Map<PropertyDefinition, Set> changes = new HashMap<PropertyDefinition, Set>();

    // Reset properties.
    for (String m : propertyResetArgument.getValues()) {

      // Check one does not try to reset with a value
      if (m.contains(":")) {
        throw ArgumentExceptionFactory.unableToResetPropertyWithValue(m, OPTION_DSCFG_LONG_RESET);
      }

      PropertyDefinition<?> pd = getPropertyDefinition(d, m);

      // Mandatory properties which have no defined defaults cannot be reset.
      if (pd.hasOption(PropertyOption.MANDATORY)
          && pd.getDefaultBehaviorProvider() instanceof UndefinedDefaultBehaviorProvider) {
        throw ArgumentExceptionFactory.unableToResetMandatoryProperty(d, m, OPTION_DSCFG_LONG_SET);
      }

      // Save the modification type.
      lastModTypes.put(m, ModificationType.SET);

      // Apply the modification.
      modifyPropertyValues(child, pd, changes, ModificationType.SET, null);
    }

    // Set properties.
    for (String m : propertySetArgument.getValues()) {
      Pair<String, String> pair = parseValue(m);
      String propertyName = pair.getFirst();
      String value = pair.getSecond();

      PropertyDefinition<?> pd = getPropertyDefinition(d, propertyName);

      // Apply the modification.
      if (lastModTypes.containsKey(propertyName)) {
        modifyPropertyValues(child, pd, changes, ModificationType.ADD, value);
      } else {
        lastModTypes.put(propertyName, ModificationType.SET);
        modifyPropertyValues(child, pd, changes, ModificationType.SET, value);
      }
    }

    // Remove properties.
    for (String m : propertyRemoveArgument.getValues()) {
      Pair<String, String> pair = parseValue(m);
      String propertyName = pair.getFirst();
      String value = pair.getSecond();

      PropertyDefinition<?> pd = getPropertyDefinition(d, propertyName);

      // Apply the modification.
      if (lastModTypes.containsKey(propertyName)
          && lastModTypes.get(propertyName) == ModificationType.SET) {
        throw ArgumentExceptionFactory.incompatiblePropertyModification(m);
      }

      lastModTypes.put(propertyName, ModificationType.REMOVE);
      modifyPropertyValues(child, pd, changes, ModificationType.REMOVE, value);
    }

    // Add properties.
    for (String m : propertyAddArgument.getValues()) {
      Pair<String, String> pair = parseValue(m);
      String propertyName = pair.getFirst();
      String value = pair.getSecond();

      PropertyDefinition<?> pd = getPropertyDefinition(d, propertyName);

      // Apply the modification.
      if (lastModTypes.containsKey(propertyName)
          && lastModTypes.get(propertyName) == ModificationType.SET) {
        throw ArgumentExceptionFactory.incompatiblePropertyModification(m);
      }

      lastModTypes.put(propertyName, ModificationType.ADD);
      modifyPropertyValues(child, pd, changes, ModificationType.ADD, value);
    }

    // Apply the command line changes.
    for (PropertyDefinition<?> pd : changes.keySet()) {
      try {
        child.setPropertyValues(pd, changes.get(pd));
      } catch (PropertyException e) {
        throw ArgumentExceptionFactory.adaptPropertyException(e, d);
      }
      setCommandBuilderUseful(true);
    }

    // Now the command line changes have been made, apply the changes
    // interacting with the user to fix any problems if required.
    MenuResult<Void> result2 = modifyManagedObject(app, context, child, this);
    if (result2.isCancel()) {
      return MenuResult.cancel();
    } else if (result2.isQuit()) {
      return MenuResult.quit();
    } else {
      if (propertyResetArgument.hasValue()) {
        getCommandBuilder().addArgument(propertyResetArgument);
      }
      if (propertySetArgument.hasValue()) {
        getCommandBuilder().addArgument(propertySetArgument);
      }
      if (propertyAddArgument.hasValue()) {
        getCommandBuilder().addArgument(propertyAddArgument);
      }
      if (propertyRemoveArgument.hasValue()) {
        getCommandBuilder().addArgument(propertyRemoveArgument);
      }
      return MenuResult.success(0);
    }
  }
 /**
  * Gets the relation definition associated with the type of component that this sub-command
  * handles.
  *
  * @return Returns the relation definition associated with the type of component that this
  *     sub-command handles.
  */
 public RelationDefinition<?, ?> getRelationDefinition() {
   return path.getRelationDefinition();
 }
  /** Private constructor. */
  private SetPropSubCommandHandler(
      SubCommandArgumentParser parser, ManagedObjectPath<?, ?> path, RelationDefinition<?, ?> r)
      throws ArgumentException {
    this.path = path;

    // Create the sub-command.
    String name = "set-" + r.getName() + "-prop";
    Message description =
        INFO_DSCFG_DESCRIPTION_SUBCMD_SETPROP.get(r.getChildDefinition().getUserFriendlyName());
    this.subCommand = new SubCommand(parser, name, false, 0, 0, null, description);

    // Create the naming arguments.
    this.namingArgs = createNamingArgs(subCommand, path, false);

    // Create the --set argument.
    this.propertySetArgument =
        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());
    this.subCommand.addArgument(this.propertySetArgument);

    // Create the --reset argument.
    this.propertyResetArgument =
        new StringArgument(
            OPTION_DSCFG_LONG_RESET,
            OPTION_DSCFG_SHORT_RESET,
            OPTION_DSCFG_LONG_RESET,
            false,
            true,
            true,
            INFO_PROPERTY_PLACEHOLDER.get(),
            null,
            null,
            INFO_DSCFG_DESCRIPTION_RESET_PROP.get());
    this.subCommand.addArgument(this.propertyResetArgument);

    // Create the --add argument.
    this.propertyAddArgument =
        new StringArgument(
            OPTION_DSCFG_LONG_ADD,
            OPTION_DSCFG_SHORT_ADD,
            OPTION_DSCFG_LONG_ADD,
            false,
            true,
            true,
            INFO_VALUE_SET_PLACEHOLDER.get(),
            null,
            null,
            INFO_DSCFG_DESCRIPTION_ADD_PROP_VAL.get());
    this.subCommand.addArgument(this.propertyAddArgument);

    // Create the --remove argument.
    this.propertyRemoveArgument =
        new StringArgument(
            OPTION_DSCFG_LONG_REMOVE,
            OPTION_DSCFG_SHORT_REMOVE,
            OPTION_DSCFG_LONG_REMOVE,
            false,
            true,
            true,
            INFO_VALUE_SET_PLACEHOLDER.get(),
            null,
            null,
            INFO_DSCFG_DESCRIPTION_REMOVE_PROP_VAL.get());
    this.subCommand.addArgument(this.propertyRemoveArgument);

    // Register the tags associated with the child managed objects.
    addTags(path.getManagedObjectDefinition().getAllTags());
  }
  /** 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();
  }
Example #7
0
 /**
  * Creates a new DN representing the specified managed object path.
  *
  * @param path The managed object path.
  * @return Returns a new DN representing the specified managed object path.
  */
 public static DN create(ManagedObjectPath<?, ?> path) {
   return path.toDN();
 }