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