| | |
| | | */ |
| | | package org.opends.server.types; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.LinkedList; |
| | | import static org.forgerock.util.Reject.*; |
| | | import static org.opends.messages.SchemaMessages.*; |
| | | import static org.opends.server.util.CollectionUtils.*; |
| | | import static org.opends.server.util.ServerConstants.*; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.opendj.ldap.ResultCode; |
| | | import org.forgerock.opendj.ldap.schema.SchemaElement; |
| | | import org.opends.server.util.RemoveOnceSDKSchemaIsUsed; |
| | | |
| | | import static org.forgerock.util.Reject.*; |
| | | import static org.opends.messages.SchemaMessages.*; |
| | | import static org.opends.server.util.CollectionUtils.*; |
| | | import static org.opends.server.util.ServerConstants.*; |
| | | import static org.opends.server.util.StaticUtils.*; |
| | | |
| | | /** |
| | | * An abstract base class for LDAP schema definitions which contain an |
| | | * OID, optional names, description, an obsolete flag, and an optional |
| | |
| | | * protocol may be associated with a particular schema file. |
| | | */ |
| | | @RemoveOnceSDKSchemaIsUsed |
| | | @org.opends.server.types.PublicAPI( |
| | | stability=org.opends.server.types.StabilityLevel.VOLATILE, |
| | | mayInstantiate=false, |
| | | mayExtend=false, |
| | | mayInvoke=true) |
| | | public abstract class CommonSchemaElements implements SchemaElement { |
| | | /** Indicates whether this definition is declared "obsolete". */ |
| | | private final boolean isObsolete; |
| | | |
| | | /** The hash code for this definition. */ |
| | | private final int hashCode; |
| | | |
| | | /** The set of additional name-value pairs associated with this definition. */ |
| | | private final Map<String, List<String>> extraProperties; |
| | | |
| | | /** |
| | | * The set of names for this definition, in a mapping between |
| | | * the all-lowercase form and the user-defined form. |
| | | */ |
| | | private final Map<String, String> names; |
| | | |
| | | /** The description for this definition. */ |
| | | private final String description; |
| | | |
| | | /** The OID that may be used to reference this definition. */ |
| | | private final String oid; |
| | | |
| | | /** The primary name to use for this definition. */ |
| | | private final String primaryName; |
| | | |
| | | /** The lower case name for this definition. */ |
| | | private final String lowerName; |
| | | |
| | | /** |
| | | * Creates a new definition with the provided information. |
| | | * <p> |
| | | * If no <code>primaryName</code> is specified, but a set of |
| | | * <code>names</code> is specified, then the first name retrieved |
| | | * from the set of <code>names</code> will be used as the primary |
| | | * name. |
| | | * |
| | | * @param primaryName |
| | | * The primary name for this definition, or |
| | | * <code>null</code> if there is no primary name. |
| | | * @param names |
| | | * The full set of names for this definition, or |
| | | * <code>null</code> if there are no names. |
| | | * @param oid |
| | | * The OID for this definition (must not be |
| | | * <code>null</code>). |
| | | * @param description |
| | | * The description for the definition, or <code>null</code> |
| | | * if there is no description. |
| | | * @param isObsolete |
| | | * Indicates whether this definition is declared |
| | | * "obsolete". |
| | | * @param extraProperties |
| | | * A set of extra properties for this definition, or |
| | | * <code>null</code> if there are no extra properties. |
| | | * @throws NullPointerException |
| | | * If the provided OID was <code>null</code>. |
| | | */ |
| | | protected CommonSchemaElements(String primaryName, |
| | | Collection<String> names, String oid, String description, |
| | | boolean isObsolete, Map<String, List<String>> extraProperties) |
| | | throws NullPointerException { |
| | | // Make sure mandatory parameters are specified. |
| | | if (oid == null) { |
| | | throw new NullPointerException( |
| | | "No oid specified in constructor"); |
| | | } |
| | | |
| | | this.oid = oid; |
| | | this.description = description; |
| | | this.isObsolete = isObsolete; |
| | | |
| | | // Make sure we have a primary name if possible. |
| | | if (primaryName != null) { |
| | | this.primaryName = primaryName; |
| | | } else if (names != null && !names.isEmpty()) { |
| | | this.primaryName = names.iterator().next(); |
| | | } else { |
| | | this.primaryName = null; |
| | | } |
| | | this.lowerName = this.primaryName != null ? toLowerCase(this.primaryName) : oid; |
| | | |
| | | // OPENDJ-1645: oid changes during server bootstrap, so prefer using lowername if available |
| | | hashCode = this.lowerName.hashCode(); |
| | | |
| | | // Construct the normalized attribute name mapping. |
| | | if (names != null) { |
| | | this.names = new LinkedHashMap<>(names.size()); |
| | | |
| | | // Make sure the primary name is first (never null). |
| | | this.names.put(lowerName, this.primaryName); |
| | | |
| | | // Add the remaining names in the order specified. |
| | | for (String name : names) { |
| | | this.names.put(toLowerCase(name), name); |
| | | } |
| | | } else if (this.primaryName != null) { |
| | | this.names = Collections.singletonMap(lowerName, this.primaryName); |
| | | } else { |
| | | this.names = Collections.emptyMap(); |
| | | } |
| | | |
| | | // FIXME: should really be a deep-copy. |
| | | if (extraProperties != null) { |
| | | this.extraProperties = new LinkedHashMap<>(extraProperties); |
| | | } else { |
| | | this.extraProperties = Collections.emptyMap(); |
| | | } |
| | | public final class CommonSchemaElements |
| | | { |
| | | private CommonSchemaElements() |
| | | { |
| | | // private for utility classes |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * Retrieves an iterable over the set of normalized names that may |
| | | * be used to reference this schema definition. The normalized form |
| | | * of an attribute name is defined as the user-defined name |
| | | * converted to lower-case. |
| | | * |
| | | * @return Returns an iterable over the set of normalized names that |
| | | * may be used to reference this schema definition. |
| | | */ |
| | | public final Set<String> getNormalizedNames() { |
| | | return names.keySet(); |
| | | } |
| | | |
| | | /** |
| | | * Retrieves an iterable over the set of user-defined names that may |
| | | * be used to reference this schema definition. |
| | | * |
| | | * @return Returns an iterable over the set of user-defined names |
| | | * that may be used to reference this schema definition. |
| | | */ |
| | | public final Iterable<String> getUserDefinedNames() { |
| | | return names.values(); |
| | | } |
| | | |
| | | /** |
| | | * Indicates whether this schema definition has the specified name. |
| | | * |
| | | * @param lowerName |
| | | * The lowercase name for which to make the determination. |
| | | * @return <code>true</code> if the specified name is assigned to |
| | | * this schema definition, or <code>false</code> if not. |
| | | */ |
| | | public final boolean hasName(String lowerName) { |
| | | return names.containsKey(lowerName); |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the OID for this schema definition. |
| | | * |
| | | * @return The OID for this schema definition. |
| | | */ |
| | | public final String getOID() { |
| | | return oid; |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the name or OID for this schema definition. If it has |
| | | * one or more names, then the primary name will be returned. If it |
| | | * does not have any names, then the OID will be returned. |
| | | * |
| | | * @return The name or OID for this schema definition. |
| | | */ |
| | | public final String getNameOrOID() { |
| | | if (primaryName != null) { |
| | | return primaryName; |
| | | } |
| | | // Guaranteed not to be null. |
| | | return oid; |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the normalized primary name or OID for this schema |
| | | * definition. If it does not have any names, then the OID will be |
| | | * returned. |
| | | * |
| | | * @return The name or OID for this schema definition. |
| | | */ |
| | | public final String getNormalizedPrimaryNameOrOID() { |
| | | return lowerName; |
| | | } |
| | | |
| | | /** |
| | | * Indicates whether this schema definition has the specified name |
| | | * or OID. |
| | | * |
| | | * @param lowerValue |
| | | * The lowercase value for which to make the determination. |
| | | * @return <code>true</code> if the provided value matches the OID |
| | | * or one of the names assigned to this schema definition, |
| | | * or <code>false</code> if not. |
| | | */ |
| | | public final boolean hasNameOrOID(String lowerValue) { |
| | | return names.containsKey(lowerValue) || oid.equals(lowerValue); |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the name of the schema file that contains the |
| | | * definition for this schema definition. |
| | | * |
| | |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the description for this schema definition. |
| | | * |
| | | * @return The description for this schema definition, or |
| | | * <code>null</code> if there is no description. |
| | | */ |
| | | @Override |
| | | public final String getDescription() { |
| | | return description; |
| | | } |
| | | |
| | | /** |
| | | * Indicates whether this schema definition is declared "obsolete". |
| | | * |
| | | * @return <code>true</code> if this schema definition is declared |
| | | * "obsolete", or <code>false</code> if not. |
| | | */ |
| | | public final boolean isObsolete() { |
| | | return isObsolete; |
| | | } |
| | | |
| | | @Override |
| | | public final Map<String, List<String>> getExtraProperties() |
| | | { |
| | | return extraProperties; |
| | | } |
| | | |
| | | /** |
| | | * Sets the value for an "extra" property for this schema element. |
| | | * If a property already exists with the specified name, then it |
| | | * will be overwritten. If the value is {@code null}, then any |
| | |
| | | } |
| | | |
| | | /** |
| | | * Sets the values for an "extra" property for this schema element. |
| | | * If a property already exists with the specified name, then it |
| | | * will be overwritten. If the set of values is {@code null} or |
| | | * empty, then any existing property with the given name will be |
| | | * removed. |
| | | * |
| | | * @param name The name for the "extra" property. It must not |
| | | * be {@code null}. |
| | | * @param values The set of values for the "extra" property. If |
| | | * it is {@code null} or empty, then any existing |
| | | * definition will be removed. |
| | | */ |
| | | public final void setExtraProperty(String name, |
| | | List<String> values) { |
| | | ifNull(name); |
| | | |
| | | if (values == null || values.isEmpty()) |
| | | { |
| | | extraProperties.remove(name); |
| | | } |
| | | else |
| | | { |
| | | LinkedList<String> valuesCopy = new LinkedList<>(values); |
| | | extraProperties.put(name, valuesCopy); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Indicates whether the provided object is equal to this attribute |
| | | * type. The object will be considered equal if it is an attribute |
| | | * type with the same OID as the current type. |
| | | * |
| | | * @param o |
| | | * The object for which to make the determination. |
| | | * @return <code>true</code> if the provided object is equal to |
| | | * this schema definition, or <code>false</code> if not. |
| | | */ |
| | | @Override |
| | | public final boolean equals(Object o) { |
| | | if (this == o) { |
| | | return true; |
| | | } |
| | | |
| | | if (o instanceof CommonSchemaElements) { |
| | | CommonSchemaElements other = (CommonSchemaElements) o; |
| | | return lowerName.equals(other.lowerName); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the hash code for this schema definition. It will be |
| | | * based on the sum of the bytes of the OID. |
| | | * |
| | | * @return The hash code for this schema definition. |
| | | */ |
| | | @Override |
| | | public final int hashCode() { |
| | | return hashCode; |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the definition string used to create this attribute |
| | | * type and including the X-SCHEMA-FILE extension. |
| | | * |