001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.admin;
028    
029    
030    
031    /**
032     * A default behavior provider which retrieves default values from a
033     * parent managed object. It should be used by properties which
034     * inherit their default value(s) from properties held in an other
035     * managed object.
036     *
037     * @param <T>
038     *          The type of values represented by this provider.
039     */
040    public final class RelativeInheritedDefaultBehaviorProvider<T> extends
041        DefaultBehaviorProvider<T> {
042    
043      // The type of managed object expected at the relative offset.
044      private final AbstractManagedObjectDefinition<?, ?> d;
045    
046      // The relative offset (where 1 = parent, 2 = grandparent) of the
047      // managed object containing the property.
048      private final int offset;
049    
050      // The name of the property containing the inherited default values.
051      private final String propertyName;
052    
053    
054    
055      /**
056       * Create a relative inherited default behavior provider associated
057       * with a parent managed object.
058       *
059       * @param d
060       *          The type of parent managed object expected at the
061       *          relative location.
062       * @param propertyName
063       *          The name of the property containing the inherited
064       *          default values.
065       * @param offset
066       *          The relative location of the parent managed object
067       *          (where 0 is the managed object itself, 1 is the parent,
068       *          and 2 is the grand-parent).
069       * @throws IllegalArgumentException
070       *           If the offset is less than 0.
071       */
072      public RelativeInheritedDefaultBehaviorProvider(
073          AbstractManagedObjectDefinition<?, ?> d, String propertyName, int offset)
074          throws IllegalArgumentException {
075        // We do not decode the property name now because the property
076        // might not have been constructed at this point (e.g. when the
077        // offset is 0).
078        if (offset < 0) {
079          throw new IllegalArgumentException("Negative offset");
080        }
081        this.d = d;
082        this.propertyName = propertyName;
083        this.offset = offset;
084      }
085    
086    
087    
088      /**
089       * {@inheritDoc}
090       */
091      public <R, P> R accept(DefaultBehaviorProviderVisitor<T, R, P> v, P p) {
092        return v.visitRelativeInherited(this, p);
093      }
094    
095    
096    
097      /**
098       * Get the definition of the parent managed object containing the
099       * inherited default values.
100       *
101       * @return Returns the definition of the parent managed object
102       *         containing the inherited default values.
103       */
104      public AbstractManagedObjectDefinition<?, ?> getManagedObjectDefinition() {
105        return d;
106      }
107    
108    
109    
110      /**
111       * Get the absolute path of the managed object containing the
112       * property which has the default values.
113       *
114       * @param path
115       *          The path of the current managed object from which the
116       *          relative path should be determined.
117       * @return Returns the absolute path of the managed object
118       *         containing the property which has the default values.
119       */
120      public ManagedObjectPath<?, ?> getManagedObjectPath(
121          ManagedObjectPath<?, ?> path) {
122        return path.parent(offset);
123      }
124    
125    
126    
127      /**
128       * Gets the name of the property containing the inherited default
129       * values.
130       *
131       * @return Returns the name of the property containing the inherited
132       *         default values.
133       */
134      public String getPropertyName() {
135        return propertyName;
136      }
137    
138    
139    
140      /**
141       * Get the relative location of the parent managed object.
142       *
143       * @return Returns the relative location of the parent managed
144       *         object (where 0 is the managed object itself, 1 is the
145       *         parent, and 2 is the grand-parent).
146       */
147      public int getRelativeOffset() {
148        return offset;
149      }
150    }