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.condition;
028    
029    
030    
031    import java.util.SortedSet;
032    
033    import org.opends.server.admin.AbstractManagedObjectDefinition;
034    import org.opends.server.admin.IllegalPropertyValueStringException;
035    import org.opends.server.admin.PropertyDefinition;
036    import org.opends.server.admin.client.AuthorizationException;
037    import org.opends.server.admin.client.CommunicationException;
038    import org.opends.server.admin.client.ManagedObject;
039    import org.opends.server.admin.client.ManagementContext;
040    import org.opends.server.admin.server.ServerManagedObject;
041    import org.opends.server.config.ConfigException;
042    import org.opends.server.util.Validator;
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     */
050    public 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 IllegalPropertyValueStringException {
071          this.pd = pd;
072          this.value = value;
073        }
074    
075    
076    
077        /**
078         * {@inheritDoc}
079         */
080        public boolean evaluate(ManagementContext context,
081            ManagedObject<?> managedObject) throws AuthorizationException,
082            CommunicationException {
083          SortedSet<T> values = managedObject.getPropertyValues(pd);
084          return values.contains(value);
085        }
086    
087    
088    
089        /**
090         * {@inheritDoc}
091         */
092        public boolean evaluate(ServerManagedObject<?> managedObject)
093            throws ConfigException {
094          SortedSet<T> values = managedObject.getPropertyValues(pd);
095          return values.contains(value);
096        }
097    
098    
099    
100        /**
101         * {@inheritDoc}
102         */
103        public void initialize(AbstractManagedObjectDefinition<?, ?> d)
104            throws Exception {
105          // Not used.
106        }
107    
108    
109    
110        // Private implementation of fix() method.
111        private void setPropertyValue(ManagedObject<?> managedObject) {
112          managedObject.setPropertyValue(pd, value);
113        }
114    
115      }
116    
117      // The strongly typed private implementation.
118      private Impl<?> impl = null;
119    
120      // The property name.
121      private final String propertyName;
122    
123      // The string representation of the required property value.
124      private final String propertyStringValue;
125    
126    
127    
128      /**
129       * Creates a new contains value condition.
130       *
131       * @param propertyName
132       *          The property name.
133       * @param stringValue
134       *          The string representation of the required property
135       *          value.
136       */
137      public ContainsCondition(String propertyName, String stringValue) {
138        Validator.ensureNotNull(propertyName, stringValue);
139        this.propertyName = propertyName;
140        this.propertyStringValue = stringValue;
141      }
142    
143    
144    
145      /**
146       * {@inheritDoc}
147       */
148      public boolean evaluate(ManagementContext context,
149          ManagedObject<?> managedObject) throws AuthorizationException,
150          CommunicationException {
151        return impl.evaluate(context, managedObject);
152      }
153    
154    
155    
156      /**
157       * {@inheritDoc}
158       */
159      public boolean evaluate(ServerManagedObject<?> managedObject)
160          throws ConfigException {
161        return impl.evaluate(managedObject);
162      }
163    
164    
165    
166      /**
167       * Modifies the provided managed object so that it has the property
168       * value associated with this condition.
169       *
170       * @param managedObject
171       *          The managed object.
172       */
173      public void setPropertyValue(ManagedObject<?> managedObject) {
174        impl.setPropertyValue(managedObject);
175      }
176    
177    
178    
179      /**
180       * {@inheritDoc}
181       */
182      public void initialize(AbstractManagedObjectDefinition<?, ?> d)
183          throws Exception {
184        // Decode the property.
185        buildImpl(d.getPropertyDefinition(propertyName));
186      }
187    
188    
189    
190      // Creates the new private implementation.
191      private <T> void buildImpl(PropertyDefinition<T> pd)
192          throws IllegalPropertyValueStringException {
193        T value = pd.decodeValue(propertyStringValue);
194        this.impl = new Impl<T>(pd, value);
195      }
196    
197      /**
198       * Returns the property definition associated with this condition.
199       * @return the property definition associated with this condition.
200       */
201      public PropertyDefinition<?> getPropertyDefinition()
202      {
203        return impl.pd;
204      }
205    
206      /**
207       * Returns the value that must be set for this condition to be fulfilled.
208       * @return the value that must be set for this condition to be fulfilled.
209       */
210      public Object getValue()
211      {
212        return impl.value;
213      }
214    }