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.PropertyDefinition;
025import org.opends.server.admin.client.AuthorizationException;
026import org.opends.server.admin.client.CommunicationException;
027import org.opends.server.admin.client.ManagedObject;
028import org.opends.server.admin.client.ManagementContext;
029import org.opends.server.admin.server.ServerManagedObject;
030import org.forgerock.opendj.config.server.ConfigException;
031import org.forgerock.util.Reject;
032
033
034
035/**
036 * A condition which evaluates to <code>true</code> if and only if a
037 * particular property has any values specified.
038 */
039public final class IsPresentCondition implements Condition {
040
041  /** The property name. */
042  private final String propertyName;
043
044  /** The property definition. */
045  private PropertyDefinition<?> pd;
046
047
048
049  /**
050   * Creates a new is present condition.
051   *
052   * @param propertyName
053   *          The property name.
054   */
055  public IsPresentCondition(String propertyName) {
056    Reject.ifNull(propertyName);
057    this.propertyName = propertyName;
058  }
059
060
061
062  /** {@inheritDoc} */
063  public boolean evaluate(ManagementContext context,
064      ManagedObject<?> managedObject) throws AuthorizationException,
065      CommunicationException {
066    SortedSet<?> values = managedObject.getPropertyValues(pd);
067    return !values.isEmpty();
068  }
069
070
071
072  /** {@inheritDoc} */
073  public boolean evaluate(ServerManagedObject<?> managedObject)
074      throws ConfigException {
075    SortedSet<?> values = managedObject.getPropertyValues(pd);
076    return !values.isEmpty();
077  }
078
079
080
081  /** {@inheritDoc} */
082  public void initialize(AbstractManagedObjectDefinition<?, ?> d)
083      throws Exception {
084    // Decode the property.
085    this.pd = d.getPropertyDefinition(propertyName);
086  }
087
088}