/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at * trunk/opends/resource/legal-notices/OpenDS.LICENSE * or https://OpenDS.dev.java.net/OpenDS.LICENSE. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, * add the following below this CDDL HEADER, with the fields enclosed * by brackets "[]" replaced with your own identifying information: * Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END * * * Portions Copyright 2007-2008 Sun Microsystems, Inc. */ package org.opends.server.admin; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; import java.util.Set; import org.opends.messages.Message; import org.opends.server.admin.DefinitionDecodingException.Reason; /** * Defines the structure of an abstract managed object. Abstract managed objects * cannot be instantiated. *
* Applications can query a managed object definition in order to determine the
* overall configuration model of an application.
*
* @param
* This method must not be called by applications.
*
* @param constraint
* The constraint to be registered.
*/
protected final void registerConstraint(Constraint constraint) {
constraints.add(constraint);
}
/**
* Register a property definition with this managed object definition,
* overriding any existing property definition with the same name.
*
* This method must not be called by applications.
*
* @param d
* The property definition to be registered.
*/
protected final void registerPropertyDefinition(PropertyDefinition> d) {
String name = d.getName();
propertyDefinitions.put(name, d);
allPropertyDefinitions.put(name, d);
}
/**
* Register a relation definition with this managed object definition,
* overriding any existing relation definition with the same name.
*
* This method must not be called by applications.
*
* @param d
* The relation definition to be registered.
*/
protected final void registerRelationDefinition(RelationDefinition, ?> d) {
// Register the relation in this managed object definition.
String name = d.getName();
relationDefinitions.put(name, d);
allRelationDefinitions.put(name, d);
// Now register the relation in the referenced managed object
// definition for reverse lookups.
registerReverseRelationDefinition(d);
}
/**
* Register an option with this managed object definition.
*
* This method must not be called by applications.
*
* @param option
* The option to be registered.
*/
protected final void registerOption(ManagedObjectOption option) {
options.add(option);
}
/**
* Register a tag with this managed object definition.
*
* This method must not be called by applications.
*
* @param tag
* The tag to be registered.
*/
protected final void registerTag(Tag tag) {
allTags.add(tag);
}
/**
* Deregister a constraint from the managed object definition.
*
* This method must not be called by applications and is
* only intended for internal testing.
*
* @param constraint
* The constraint to be deregistered.
*/
final void deregisterConstraint(Constraint constraint) {
if (!constraints.remove(constraint)) {
throw new RuntimeException("Failed to deregister a constraint");
}
}
/**
* Deregister a relation definition from the managed object
* definition.
*
* This method must not be called by applications and is
* only intended for internal testing.
*
* @param d
* The relation definition to be deregistered.
*/
final void deregisterRelationDefinition(
RelationDefinition, ?> d) {
// Deregister the relation from this managed object definition.
String name = d.getName();
relationDefinitions.remove(name);
allRelationDefinitions.remove(name);
// Now deregister the relation from the referenced managed object
// definition for reverse lookups.
d.getChildDefinition().reverseRelationDefinitions.remove(d);
}
/**
* Register this managed object definition in its parent.
*
* This method must not be called by applications and is
* only intended for internal testing.
*/
final void registerInParent() {
if (parent != null) {
parent.children.put(name, this);
}
}
// Register a relation definition in the referenced managed object
// definition's reverse lookup table.
private
* The type of server managed object configuration that this definition
* represents.
*/
public abstract class AbstractManagedObjectDefinition
null if there
* is no parent (only the {@link TopCfgDefn} should have a
* null parent, unless the definition is
* being used for testing).
*/
protected AbstractManagedObjectDefinition(String name,
AbstractManagedObjectDefinition super C, ? super S> parent) {
this.name = name;
this.parent = parent;
this.constraints = new LinkedListnull if there is
* no description.
* @throws UnsupportedOperationException
* If this managed object definition is the
* {@link TopCfgDefn}.
*/
public final Message getDescription() throws UnsupportedOperationException {
return getDescription(Locale.getDefault());
}
/**
* Gets the optional description of this managed object definition
* in the specified locale.
*
* @param locale
* The locale.
* @return Returns the description of this managed object definition
* in the specified locale, or null if there
* is no description.
* @throws UnsupportedOperationException
* If this managed object definition is the
* {@link TopCfgDefn}.
*/
public final Message getDescription(Locale locale)
throws UnsupportedOperationException {
try {
return ManagedObjectDefinitionI18NResource.getInstance()
.getMessage(this, "description", locale);
} catch (MissingResourceException e) {
return null;
}
}
/**
* Get the name of the definition.
*
* @return Returns the name of the definition.
*/
public final String getName() {
return name;
}
/**
* Get the parent managed object definition, if applicable.
*
* @return Returns the parent of this managed object definition, or
* null if this definition is the
* {@link TopCfgDefn}.
*/
public final AbstractManagedObjectDefinition super C,
? super S> getParent() {
return parent;
}
/**
* Get the specified property definition associated with this type
* of managed object. The search will include any inherited property
* definitions.
*
* @param name
* The name of the property definition to be retrieved.
* @return Returns the specified property definition associated with
* this type of managed object.
* @throws IllegalArgumentException
* If the specified property name was null or empty or if
* the requested property definition was not found.
*/
public final PropertyDefinition> getPropertyDefinition(String name)
throws IllegalArgumentException {
if ((name == null) || (name.length() == 0)) {
throw new IllegalArgumentException("null or empty property name");
}
PropertyDefinition> d = allPropertyDefinitions.get(name);
if (d == null) {
throw new IllegalArgumentException("property definition \"" + name
+ "\" not found");
}
return d;
}
/**
* Get the property definitions defined by this managed object
* definition. The returned collection will not contain inherited
* property definitions.
*
* @return Returns an unmodifiable collection containing the
* property definitions defined by this managed object
* definition.
*/
public final Collectiontrue if this type of managed object has any
* child managed object definitions, false otherwise.
*/
public final boolean hasChildren() {
return !children.isEmpty();
}
/**
* Determines whether or not this managed object definition has the
* specified option.
*
* @param option
* The option to test.
* @return Returns true if the option is set, or
* false otherwise.
*/
public final boolean hasOption(ManagedObjectOption option) {
return options.contains(option);
}
/**
* Determines whether or not this managed object definition has the
* specified tag.
*
* @param t
* The tag definition.
* @return Returns true if this managed object
* definition has the specified tag.
*/
public final boolean hasTag(Tag t) {
return allTags.contains(t);
}
/**
* Determines whether or not this managed object definition is a
* sub-type of the provided managed object definition. This managed
* object definition is a sub-type of the provided managed object
* definition if they are both the same or if the provided managed
* object definition can be obtained by recursive invocations of the
* {@link #getParent()} method.
*
* @param d
* The managed object definition to be checked.
* @return Returns true if this managed object
* definition is a sub-type of the provided managed object
* definition.
*/
public final boolean isChildOf(AbstractManagedObjectDefinition, ?> d) {
AbstractManagedObjectDefinition, ?> i;
for (i = this; i != null; i = i.parent) {
if (i == d) {
return true;
}
}
return false;
}
/**
* Determines whether or not this managed object definition is a
* super-type of the provided managed object definition. This
* managed object definition is a super-type of the provided managed
* object definition if they are both the same or if the provided
* managed object definition is a member of the set of children
* returned from {@link #getAllChildren()}.
*
* @param d
* The managed object definition to be checked.
* @return Returns true if this managed object
* definition is a super-type of the provided managed object
* definition.
*/
public final boolean isParentOf(AbstractManagedObjectDefinition, ?> d) {
return d.isChildOf(this);
}
/**
* Determines whether or not this managed object definition is the
* {@link TopCfgDefn}.
*
* @return Returns true if this managed object
* definition is the {@link TopCfgDefn}.
*/
public final boolean isTop() {
// Casting to Object and instanceof check are required
// to workaround a bug in JDK versions prior to 1.5.0_08.
return ((Object) this instanceof TopCfgDefn);
}
/**
* Finds a sub-type of this managed object definition which most closely
* corresponds to the matching criteria of the provided definition resolver.
*
* @param r
* The definition resolver.
* @return Returns the sub-type of this managed object definition which most
* closely corresponds to the matching criteria of the provided
* definition resolver.
* @throws DefinitionDecodingException
* If no matching sub-type could be found or if the resolved
* definition was abstract.
* @see DefinitionResolver
*/
@SuppressWarnings("unchecked")
public final ManagedObjectDefinition extends C, ? extends S>
resolveManagedObjectDefinition(
DefinitionResolver r) throws DefinitionDecodingException {
AbstractManagedObjectDefinition extends C, ? extends S> rd;
rd = resolveManagedObjectDefinitionAux(this, r);
if (rd == null) {
// Unable to resolve the definition.
throw new DefinitionDecodingException(this,
Reason.WRONG_TYPE_INFORMATION);
} else if (rd instanceof ManagedObjectDefinition) {
return (ManagedObjectDefinition extends C, ? extends S>) rd;
} else {
// Resolved definition was abstract.
throw new DefinitionDecodingException(this,
Reason.ABSTRACT_TYPE_INFORMATION);
}
}
/**
* {@inheritDoc}
*/
@Override
public final String toString() {
StringBuilder builder = new StringBuilder();
toString(builder);
return builder.toString();
}
/**
* Append a string representation of the managed object definition to the
* provided string builder.
*
* @param builder
* The string builder where the string representation should be
* appended.
*/
public final void toString(StringBuilder builder) {
builder.append(getName());
}
/**
* Initializes all of the components associated with this managed
* object definition.
*
* @throws Exception
* If this managed object definition could not be
* initialized.
*/
protected final void initialize() throws Exception {
for (PropertyDefinition> pd : getAllPropertyDefinitions()) {
pd.initialize();
pd.getDefaultBehaviorProvider().initialize();
}
for (RelationDefinition, ?> rd : getAllRelationDefinitions()) {
rd.initialize();
}
for (Constraint constraint : getAllConstraints()) {
constraint.initialize();
}
}
/**
* Register a constraint with this managed object definition.
*