4632N/A/*
4632N/A * CDDL HEADER START
4632N/A *
4632N/A * The contents of this file are subject to the terms of the
4632N/A * Common Development and Distribution License, Version 1.0 only
4632N/A * (the "License"). You may not use this file except in compliance
4632N/A * with the License.
4632N/A *
4632N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
4632N/A * or http://forgerock.org/license/CDDLv1.0.html.
4632N/A * See the License for the specific language governing permissions
4632N/A * and limitations under the License.
4632N/A *
4632N/A * When distributing Covered Code, include this CDDL HEADER in each
4632N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
4632N/A * If applicable, add the following below this CDDL HEADER, with the
4632N/A * fields enclosed by brackets "[]" replaced with your own identifying
4632N/A * information:
4632N/A * Portions Copyright [yyyy] [name of copyright owner]
4632N/A *
4632N/A * CDDL HEADER END
4632N/A *
4632N/A *
4632N/A * Copyright 2008 Sun Microsystems, Inc.
4632N/A * Portions Copyright 2015 ForgeRock AS.
4632N/A */
4632N/Apackage org.opends.server.admin;
4632N/A
4632N/A
4632N/A
4632N/Aimport java.util.Arrays;
4632N/Aimport java.util.HashMap;
4632N/Aimport java.util.List;
4632N/Aimport java.util.Map;
4632N/Aimport java.util.SortedSet;
4632N/Aimport java.util.TreeSet;
4632N/A
4632N/A
4632N/A
4632N/A/**
4632N/A * A default managed object which should be created when a parent
4632N/A * managed object is created. Default managed objects are associated
4632N/A * with a {@link RelationDefinition}.
4632N/A *
4632N/A * @param <C>
4632N/A * The type of client default managed object configuration.
4632N/A * @param <S>
4632N/A * The type of server default managed object configuration.
4632N/A */
4632N/Apublic final class DefaultManagedObject
4632N/A <C extends ConfigurationClient, S extends Configuration>
4632N/A implements PropertyProvider {
4632N/A
4632N/A /**
4632N/A * An interface for incrementally constructing default managed
4632N/A * objects.
4632N/A *
4632N/A * @param <C>
4632N/A * The type of client default managed object configuration.
4632N/A * @param <S>
4632N/A * The type of server default managed object configuration.
4632N/A */
4632N/A public static final class Builder
4632N/A <C extends ConfigurationClient, S extends Configuration> {
4632N/A
4632N/A /** The default managed object's definition. */
4632N/A private final ManagedObjectDefinition<C, S> definition;
4632N/A
4632N/A /** The string encoded default managed object's properties. */
4632N/A private final Map<String, List<String>> propertyStringValues = new HashMap<>();
4632N/A
4632N/A /**
4632N/A * Creates a new default managed object builder.
4632N/A *
4632N/A * @param definition
4632N/A * The default managed object's definition.
4632N/A */
4632N/A public Builder(ManagedObjectDefinition<C, S> definition) {
4632N/A this.definition = definition;
4632N/A }
4632N/A
4632N/A
4632N/A
4632N/A /**
4632N/A * Construct a default managed object based on the properties of
4632N/A * this builder.
4632N/A *
4632N/A * @return Returns the new default managed object.
4632N/A */
4632N/A public DefaultManagedObject<C, S> getInstance() {
4632N/A return new DefaultManagedObject<>(definition, propertyStringValues);
4632N/A }
4632N/A
4632N/A
4632N/A
4632N/A /**
4632N/A * Defines a property's values for the default managed object.
4632N/A *
4632N/A * @param name
4632N/A * The name of the property.
4632N/A * @param values
4632N/A * One or more property values in the string
4632N/A * representation.
4632N/A */
4632N/A public void setPropertyValues(String name, String... values) {
4632N/A if (values == null || values.length == 0) {
4632N/A throw new IllegalArgumentException(
4632N/A "null or empty values specified for property " + name);
4632N/A }
4632N/A
4632N/A propertyStringValues.put(name, Arrays.asList(values));
4632N/A }
4632N/A }
4632N/A
4632N/A /** The default managed object's definition. */
4632N/A private final ManagedObjectDefinition<C, S> definition;
4632N/A
4632N/A /** The string encoded default managed object's properties. */
4632N/A private final Map<String, List<String>> propertyStringValues;
4632N/A
4632N/A
4632N/A
4632N/A /** Private constructor. */
4632N/A private DefaultManagedObject(ManagedObjectDefinition<C, S> definition,
4632N/A Map<String, List<String>> propertyStringValues) {
4632N/A this.definition = definition;
4632N/A this.propertyStringValues = propertyStringValues;
4632N/A }
4632N/A
4632N/A
4632N/A
4632N/A /**
4632N/A * Gets the managed object definition associated with this default
4632N/A * managed object.
4632N/A *
4632N/A * @return Returns the managed object definition associated with
4632N/A * this default managed object.
4632N/A */
4632N/A public ManagedObjectDefinition<C, S> getManagedObjectDefinition() {
4632N/A return definition;
4632N/A }
4632N/A
4632N/A
4632N/A
4632N/A /**
4632N/A * Gets a mutable copy of the set of property values for the
4632N/A * specified property.
4632N/A *
4632N/A * @param <T>
4632N/A * The type of the property to be retrieved.
4632N/A * @param pd
4632N/A * The property to be retrieved.
4632N/A * @return Returns a newly allocated set containing a copy of the
4632N/A * property's values. An empty set indicates that the
4632N/A * property has no values defined and any default behavior
4632N/A * is applicable.
4632N/A * @throws IllegalArgumentException
4632N/A * If the property definition is not associated with this
4632N/A * managed object's definition.
4632N/A */
4632N/A public <T> SortedSet<T> getPropertyValues(PropertyDefinition<T> pd)
4632N/A throws IllegalArgumentException {
4632N/A // Validate the property definition.
4632N/A definition.getPropertyDefinition(pd.getName());
4632N/A
4632N/A // Do a defensive copy.
4632N/A SortedSet<T> values = new TreeSet<>(pd);
4632N/A List<String> stringValues = propertyStringValues.get(pd.getName());
4632N/A if (stringValues != null) {
4632N/A for (String stringValue : stringValues) {
4632N/A values.add(pd.decodeValue(stringValue));
4632N/A }
4632N/A }
4632N/A return values;
4632N/A }
4632N/A
4632N/A
4632N/A
4632N/A /**
4632N/A * Performs run-time initialization of properties.
4632N/A *
4632N/A * @throws Exception
4632N/A * If this default managed object could not be
4632N/A * initialized.
4632N/A */
4632N/A void initialize() throws Exception {
4632N/A // FIXME: it would be nice if we could decode all property values
4632N/A // at this point. However this is not possible on the server side
4632N/A // since some properties will be determined to be invalid since
4632N/A // the schema is not loaded.
4632N/A
4632N/A // Validate provided property names.
4632N/A for (String name : propertyStringValues.keySet()) {
4632N/A definition.getPropertyDefinition(name);
4632N/A }
4632N/A }
4632N/A}
4632N/A