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