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