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;
028
029
030
031import java.util.Collection;
032import java.util.Collections;
033import java.util.Locale;
034
035import org.forgerock.i18n.LocalizableMessage;
036import org.opends.server.admin.client.AuthorizationException;
037import org.opends.server.admin.client.ClientConstraintHandler;
038import org.opends.server.admin.client.CommunicationException;
039import org.opends.server.admin.client.ManagedObject;
040import org.opends.server.admin.client.ManagementContext;
041import org.opends.server.admin.condition.Condition;
042import org.opends.server.admin.server.ServerConstraintHandler;
043import org.opends.server.admin.server.ServerManagedObject;
044import org.forgerock.opendj.config.server.ConfigException;
045
046
047
048/**
049 * A generic constraint which comprises of an underlying condition and
050 * a description. The condition must evaluate to <code>true</code>
051 * in order for a new managed object to be created or modified.
052 */
053public class GenericConstraint extends Constraint {
054
055  /**
056   * The client-side constraint handler.
057   */
058  private class ClientHandler extends ClientConstraintHandler {
059
060    /** Private constructor. */
061    private ClientHandler() {
062      // No implementation required.
063    }
064
065
066
067    /** {@inheritDoc} */
068    @Override
069    public boolean isAddAcceptable(ManagementContext context,
070        ManagedObject<?> managedObject, Collection<LocalizableMessage> unacceptableReasons)
071        throws AuthorizationException, CommunicationException {
072      if (!condition.evaluate(context, managedObject)) {
073        unacceptableReasons.add(getSynopsis());
074        return false;
075      } else {
076        return true;
077      }
078    }
079
080
081
082    /** {@inheritDoc} */
083    @Override
084    public boolean isModifyAcceptable(ManagementContext context,
085        ManagedObject<?> managedObject, Collection<LocalizableMessage> unacceptableReasons)
086        throws AuthorizationException, CommunicationException {
087      if (!condition.evaluate(context, managedObject)) {
088        unacceptableReasons.add(getSynopsis());
089        return false;
090      } else {
091        return true;
092      }
093    }
094  }
095
096  /** The server-side constraint handler. */
097  private class ServerHandler extends ServerConstraintHandler {
098
099    /** Private constructor. */
100    private ServerHandler() {
101      // No implementation required.
102    }
103
104    @Override
105    public boolean isUsable(ServerManagedObject<?> managedObject,
106        Collection<LocalizableMessage> unacceptableReasons) throws ConfigException {
107      if (!condition.evaluate(managedObject)) {
108        unacceptableReasons.add(getSynopsis());
109        return false;
110      } else {
111        return true;
112      }
113    }
114  }
115
116  /** The client-side constraint handler. */
117  private final ClientConstraintHandler clientHandler = new ClientHandler();
118
119  /** The condition associated with this constraint. */
120  private final Condition condition;
121
122  /** The managed object definition associated with this constraint. */
123  private final AbstractManagedObjectDefinition<?, ?> definition;
124
125  /** The constraint ID. */
126  private final int id;
127
128  /** The server-side constraint handler. */
129  private final ServerConstraintHandler serverHandler = new ServerHandler();
130
131
132
133  /**
134   * Creates a new generic constraint.
135   *
136   * @param definition
137   *          The managed object definition associated with this
138   *          constraint.
139   * @param id
140   *          The constraint ID.
141   * @param condition
142   *          The condition associated with this constraint.
143   */
144  public GenericConstraint(AbstractManagedObjectDefinition<?, ?> definition,
145      int id, Condition condition) {
146    this.definition = definition;
147    this.id = id;
148    this.condition = condition;
149  }
150
151
152
153  /** {@inheritDoc} */
154  public Collection<ClientConstraintHandler> getClientConstraintHandlers() {
155    return Collections.singleton(clientHandler);
156  }
157
158
159
160  /** {@inheritDoc} */
161  public Collection<ServerConstraintHandler> getServerConstraintHandlers() {
162    return Collections.singleton(serverHandler);
163  }
164
165
166
167  /**
168   * Gets the synopsis of this constraint in the default locale.
169   *
170   * @return Returns the synopsis of this constraint in the default
171   *         locale.
172   */
173  public final LocalizableMessage getSynopsis() {
174    return getSynopsis(Locale.getDefault());
175  }
176
177
178
179  /**
180   * Gets the synopsis of this constraint in the specified locale.
181   *
182   * @param locale
183   *          The locale.
184   * @return Returns the synopsis of this constraint in the specified
185   *         locale.
186   */
187  public final LocalizableMessage getSynopsis(Locale locale) {
188    ManagedObjectDefinitionI18NResource resource =
189      ManagedObjectDefinitionI18NResource.getInstance();
190    String property = "constraint." + id + ".synopsis";
191    return resource.getMessage(definition, property, locale);
192  }
193
194
195
196  /** {@inheritDoc} */
197  @Override
198  protected void initialize() throws Exception {
199    condition.initialize(definition);
200  }
201
202}