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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.admin.condition;
028
029
030
031import java.util.SortedSet;
032
033import org.opends.server.admin.AbstractManagedObjectDefinition;
034import org.opends.server.admin.PropertyException;
035import org.opends.server.admin.PropertyDefinition;
036import org.opends.server.admin.client.AuthorizationException;
037import org.opends.server.admin.client.CommunicationException;
038import org.opends.server.admin.client.ManagedObject;
039import org.opends.server.admin.client.ManagementContext;
040import org.opends.server.admin.server.ServerManagedObject;
041import org.forgerock.opendj.config.server.ConfigException;
042import org.forgerock.util.Reject;
043
044
045
046/**
047 * A condition which evaluates to <code>true</code> if and only if a
048 * property contains a particular value.
049 */
050public final class ContainsCondition implements Condition {
051
052  /**
053   * The strongly typed underlying implementation.
054   *
055   * @param <T>
056   *          The type of the property value being tested.
057   */
058  private static final class Impl<T> implements Condition {
059
060    /** The property. */
061    final PropertyDefinition<T> pd;
062
063    /** The required property value. */
064    final T value;
065
066
067
068    /** Private constructor. */
069    private Impl(PropertyDefinition<T> pd, T value)
070        throws PropertyException {
071      this.pd = pd;
072      this.value = value;
073    }
074
075
076
077    /** {@inheritDoc} */
078    public boolean evaluate(ManagementContext context,
079        ManagedObject<?> managedObject) throws AuthorizationException,
080        CommunicationException {
081      SortedSet<T> values = managedObject.getPropertyValues(pd);
082      return values.contains(value);
083    }
084
085
086
087    /** {@inheritDoc} */
088    public boolean evaluate(ServerManagedObject<?> managedObject)
089        throws ConfigException {
090      SortedSet<T> values = managedObject.getPropertyValues(pd);
091      return values.contains(value);
092    }
093
094
095
096    /** {@inheritDoc} */
097    public void initialize(AbstractManagedObjectDefinition<?, ?> d)
098        throws Exception {
099      // Not used.
100    }
101
102
103
104    /** Private implementation of fix() method. */
105    private void setPropertyValue(ManagedObject<?> managedObject) {
106      managedObject.setPropertyValue(pd, value);
107    }
108
109  }
110
111  /** The strongly typed private implementation. */
112  private Impl<?> impl;
113
114  /** The property name. */
115  private final String propertyName;
116
117  /** The string representation of the required property value. */
118  private final String propertyStringValue;
119
120
121
122  /**
123   * Creates a new contains value condition.
124   *
125   * @param propertyName
126   *          The property name.
127   * @param stringValue
128   *          The string representation of the required property
129   *          value.
130   */
131  public ContainsCondition(String propertyName, String stringValue) {
132    Reject.ifNull(propertyName, stringValue);
133    this.propertyName = propertyName;
134    this.propertyStringValue = stringValue;
135  }
136
137
138
139  /** {@inheritDoc} */
140  public boolean evaluate(ManagementContext context,
141      ManagedObject<?> managedObject) throws AuthorizationException,
142      CommunicationException {
143    return impl.evaluate(context, managedObject);
144  }
145
146
147
148  /** {@inheritDoc} */
149  public boolean evaluate(ServerManagedObject<?> managedObject)
150      throws ConfigException {
151    return impl.evaluate(managedObject);
152  }
153
154
155
156  /**
157   * Modifies the provided managed object so that it has the property
158   * value associated with this condition.
159   *
160   * @param managedObject
161   *          The managed object.
162   */
163  public void setPropertyValue(ManagedObject<?> managedObject) {
164    impl.setPropertyValue(managedObject);
165  }
166
167
168
169  /** {@inheritDoc} */
170  public void initialize(AbstractManagedObjectDefinition<?, ?> d)
171      throws Exception {
172    // Decode the property.
173    buildImpl(d.getPropertyDefinition(propertyName));
174  }
175
176
177
178  /** Creates the new private implementation. */
179  private <T> void buildImpl(PropertyDefinition<T> pd)
180      throws PropertyException {
181    T value = pd.decodeValue(propertyStringValue);
182    this.impl = new Impl<>(pd, value);
183  }
184
185  /**
186   * Returns the property definition associated with this condition.
187   * @return the property definition associated with this condition.
188   */
189  public PropertyDefinition<?> getPropertyDefinition()
190  {
191    return impl.pd;
192  }
193
194  /**
195   * Returns the value that must be set for this condition to be fulfilled.
196   * @return the value that must be set for this condition to be fulfilled.
197   */
198  public Object getValue()
199  {
200    return impl.value;
201  }
202}