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;
028
029import java.util.Collection;
030import java.util.Collections;
031import java.util.Locale;
032
033import org.forgerock.i18n.LocalizableMessage;
034import org.forgerock.opendj.config.client.ClientConstraintHandler;
035import org.forgerock.opendj.config.client.ManagedObject;
036import org.forgerock.opendj.config.client.ManagementContext;
037import org.forgerock.opendj.config.conditions.Condition;
038import org.forgerock.opendj.config.server.ConfigException;
039import org.forgerock.opendj.config.server.ServerConstraintHandler;
040import org.forgerock.opendj.config.server.ServerManagedObject;
041import org.forgerock.opendj.ldap.LdapException;
042
043/**
044 * A generic constraint which comprises of an underlying condition and a
045 * description. The condition must evaluate to <code>true</code> in order for a
046 * new managed object to be created or modified.
047 */
048public class GenericConstraint extends Constraint {
049
050    /**
051     * The client-side constraint handler.
052     */
053    private final class ClientHandler extends ClientConstraintHandler {
054
055        /** Private constructor. */
056        private ClientHandler() {
057            // No implementation required.
058        }
059
060        /** {@inheritDoc} */
061        @Override
062        public boolean isAddAcceptable(ManagementContext context, ManagedObject<?> managedObject,
063            Collection<LocalizableMessage> unacceptableReasons) throws LdapException {
064            if (!condition.evaluate(context, managedObject)) {
065                unacceptableReasons.add(getSynopsis());
066                return false;
067            } else {
068                return true;
069            }
070        }
071
072        /** {@inheritDoc} */
073        @Override
074        public boolean isModifyAcceptable(ManagementContext context, ManagedObject<?> managedObject,
075            Collection<LocalizableMessage> unacceptableReasons) throws LdapException {
076            if (!condition.evaluate(context, managedObject)) {
077                unacceptableReasons.add(getSynopsis());
078                return false;
079            } else {
080                return true;
081            }
082        }
083    }
084
085    /** The server-side constraint handler. */
086    private final class ServerHandler extends ServerConstraintHandler {
087
088        /** Private constructor. */
089        private ServerHandler() {
090            // No implementation required.
091        }
092
093        /** {@inheritDoc} */
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     * Creates a new generic constraint.
123     *
124     * @param definition
125     *            The managed object definition associated with this constraint.
126     * @param id
127     *            The constraint ID.
128     * @param condition
129     *            The condition associated with this constraint.
130     */
131    public GenericConstraint(AbstractManagedObjectDefinition<?, ?> definition, int id, Condition condition) {
132        this.definition = definition;
133        this.id = id;
134        this.condition = condition;
135    }
136
137    /** {@inheritDoc} */
138    public Collection<ClientConstraintHandler> getClientConstraintHandlers() {
139        return Collections.singleton(clientHandler);
140    }
141
142    /** {@inheritDoc} */
143    public Collection<ServerConstraintHandler> getServerConstraintHandlers() {
144        return Collections.singleton(serverHandler);
145    }
146
147    /**
148     * Gets the synopsis of this constraint in the default locale.
149     *
150     * @return Returns the synopsis of this constraint in the default locale.
151     */
152    public final LocalizableMessage getSynopsis() {
153        return getSynopsis(Locale.getDefault());
154    }
155
156    /**
157     * Gets the synopsis of this constraint in the specified locale.
158     *
159     * @param locale
160     *            The locale.
161     * @return Returns the synopsis of this constraint in the specified locale.
162     */
163    public final LocalizableMessage getSynopsis(Locale locale) {
164        ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
165        String property = "constraint." + id + ".synopsis";
166        return resource.getMessage(definition, property, locale);
167    }
168
169    /** {@inheritDoc} */
170    @Override
171    protected void initialize() throws Exception {
172        condition.initialize(definition);
173    }
174
175}