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