/** Apply a single modification to the current change-set. */ @SuppressWarnings("unchecked") private <T> void modifyPropertyValues( ManagedObject<?> mo, PropertyDefinition<T> pd, Map<PropertyDefinition, Set> changes, ModificationType modType, String s) throws ArgumentException { Set<T> values = changes.get(pd); if (values == null) { values = mo.getPropertyValues(pd); } if (s == null || s.length() == 0) { // Reset back to defaults. values.clear(); } else { T value; try { value = pd.decodeValue(s); } catch (IllegalPropertyValueStringException e) { throw ArgumentExceptionFactory.adaptPropertyException(e, mo.getManagedObjectDefinition()); } switch (modType) { case ADD: values.add(value); break; case REMOVE: if (!values.remove(value)) { // value was not part of values throw ArgumentExceptionFactory.unknownValueForMultiValuedProperty(s, pd.getName()); } break; case SET: values = new TreeSet<T>(pd); values.add(value); break; } } changes.put(pd, values); }
/** {@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); } }