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