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-2016 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 /** The client-side constraint handler. */ 041 private final class ClientHandler extends ClientConstraintHandler { 042 043 /** Private constructor. */ 044 private ClientHandler() { 045 // No implementation required. 046 } 047 048 @Override 049 public boolean isAddAcceptable(ManagementContext context, ManagedObject<?> managedObject, 050 Collection<LocalizableMessage> unacceptableReasons) throws LdapException { 051 if (!condition.evaluate(context, managedObject)) { 052 unacceptableReasons.add(getSynopsis()); 053 return false; 054 } else { 055 return true; 056 } 057 } 058 059 @Override 060 public boolean isModifyAcceptable(ManagementContext context, ManagedObject<?> managedObject, 061 Collection<LocalizableMessage> unacceptableReasons) throws LdapException { 062 if (!condition.evaluate(context, managedObject)) { 063 unacceptableReasons.add(getSynopsis()); 064 return false; 065 } else { 066 return true; 067 } 068 } 069 } 070 071 /** The server-side constraint handler. */ 072 private final class ServerHandler extends ServerConstraintHandler { 073 074 /** Private constructor. */ 075 private ServerHandler() { 076 // No implementation required. 077 } 078 079 @Override 080 public boolean isUsable(ServerManagedObject<?> managedObject, 081 Collection<LocalizableMessage> unacceptableReasons) throws ConfigException { 082 if (!condition.evaluate(managedObject)) { 083 unacceptableReasons.add(getSynopsis()); 084 return false; 085 } else { 086 return true; 087 } 088 } 089 } 090 091 /** The client-side constraint handler. */ 092 private final ClientConstraintHandler clientHandler = new ClientHandler(); 093 094 /** The condition associated with this constraint. */ 095 private final Condition condition; 096 097 /** The managed object definition associated with this constraint. */ 098 private final AbstractManagedObjectDefinition<?, ?> definition; 099 100 /** The constraint ID. */ 101 private final int id; 102 103 /** The server-side constraint handler. */ 104 private final ServerConstraintHandler serverHandler = new ServerHandler(); 105 106 /** 107 * Creates a new generic constraint. 108 * 109 * @param definition 110 * The managed object definition associated with this constraint. 111 * @param id 112 * The constraint ID. 113 * @param condition 114 * The condition associated with this constraint. 115 */ 116 public GenericConstraint(AbstractManagedObjectDefinition<?, ?> definition, int id, Condition condition) { 117 this.definition = definition; 118 this.id = id; 119 this.condition = condition; 120 } 121 122 @Override 123 public Collection<ClientConstraintHandler> getClientConstraintHandlers() { 124 return Collections.singleton(clientHandler); 125 } 126 127 @Override 128 public Collection<ServerConstraintHandler> getServerConstraintHandlers() { 129 return Collections.singleton(serverHandler); 130 } 131 132 /** 133 * Gets the synopsis of this constraint in the default locale. 134 * 135 * @return Returns the synopsis of this constraint in the default locale. 136 */ 137 public final LocalizableMessage getSynopsis() { 138 return getSynopsis(Locale.getDefault()); 139 } 140 141 /** 142 * Gets the synopsis of this constraint in the specified locale. 143 * 144 * @param locale 145 * The locale. 146 * @return Returns the synopsis of this constraint in the specified locale. 147 */ 148 public final LocalizableMessage getSynopsis(Locale locale) { 149 ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance(); 150 String property = "constraint." + id + ".synopsis"; 151 return resource.getMessage(definition, property, locale); 152 } 153 154 @Override 155 protected void initialize() throws Exception { 156 condition.initialize(definition); 157 } 158 159}