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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.admin;
028
029
030
031 import java.util.Collection;
032 import java.util.Collections;
033 import java.util.Locale;
034
035 import org.opends.messages.Message;
036 import org.opends.server.admin.client.AuthorizationException;
037 import org.opends.server.admin.client.ClientConstraintHandler;
038 import org.opends.server.admin.client.CommunicationException;
039 import org.opends.server.admin.client.ManagedObject;
040 import org.opends.server.admin.client.ManagementContext;
041 import org.opends.server.admin.condition.Condition;
042 import org.opends.server.admin.server.ServerConstraintHandler;
043 import org.opends.server.admin.server.ServerManagedObject;
044 import org.opends.server.config.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 */
053 public 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 /**
068 * {@inheritDoc}
069 */
070 @Override
071 public boolean isAddAcceptable(ManagementContext context,
072 ManagedObject<?> managedObject, Collection<Message> unacceptableReasons)
073 throws AuthorizationException, CommunicationException {
074 if (!condition.evaluate(context, managedObject)) {
075 unacceptableReasons.add(getSynopsis());
076 return false;
077 } else {
078 return true;
079 }
080 }
081
082
083
084 /**
085 * {@inheritDoc}
086 */
087 @Override
088 public boolean isModifyAcceptable(ManagementContext context,
089 ManagedObject<?> managedObject, Collection<Message> unacceptableReasons)
090 throws AuthorizationException, CommunicationException {
091 if (!condition.evaluate(context, managedObject)) {
092 unacceptableReasons.add(getSynopsis());
093 return false;
094 } else {
095 return true;
096 }
097 }
098
099 };
100
101
102
103 /**
104 * The server-side constraint handler.
105 */
106 private class ServerHandler extends ServerConstraintHandler {
107
108 // Private constructor.
109 private ServerHandler() {
110 // No implementation required.
111 }
112
113
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public boolean isUsable(ServerManagedObject<?> managedObject,
120 Collection<Message> unacceptableReasons) throws ConfigException {
121 if (!condition.evaluate(managedObject)) {
122 unacceptableReasons.add(getSynopsis());
123 return false;
124 } else {
125 return true;
126 }
127 }
128
129 };
130
131 // The client-side constraint handler.
132 private final ClientConstraintHandler clientHandler = new ClientHandler();
133
134 // The condition associated with this constraint.
135 private final Condition condition;
136
137 // The managed object definition associated with this constraint.
138 private final AbstractManagedObjectDefinition<?, ?> definition;
139
140 // The constraint ID.
141 private final int id;
142
143 // The server-side constraint handler.
144 private final ServerConstraintHandler serverHandler = new ServerHandler();
145
146
147
148 /**
149 * Creates a new generic constraint.
150 *
151 * @param definition
152 * The managed object definition associated with this
153 * constraint.
154 * @param id
155 * The constraint ID.
156 * @param condition
157 * The condition associated with this constraint.
158 */
159 public GenericConstraint(AbstractManagedObjectDefinition<?, ?> definition,
160 int id, Condition condition) {
161 this.definition = definition;
162 this.id = id;
163 this.condition = condition;
164 }
165
166
167
168 /**
169 * {@inheritDoc}
170 */
171 public Collection<ClientConstraintHandler> getClientConstraintHandlers() {
172 return Collections.singleton(clientHandler);
173 }
174
175
176
177 /**
178 * {@inheritDoc}
179 */
180 public Collection<ServerConstraintHandler> getServerConstraintHandlers() {
181 return Collections.singleton(serverHandler);
182 }
183
184
185
186 /**
187 * Gets the synopsis of this constraint in the default locale.
188 *
189 * @return Returns the synopsis of this constraint in the default
190 * locale.
191 */
192 public final Message getSynopsis() {
193 return getSynopsis(Locale.getDefault());
194 }
195
196
197
198 /**
199 * Gets the synopsis of this constraint in the specified locale.
200 *
201 * @param locale
202 * The locale.
203 * @return Returns the synopsis of this constraint in the specified
204 * locale.
205 */
206 public final Message getSynopsis(Locale locale) {
207 ManagedObjectDefinitionI18NResource resource =
208 ManagedObjectDefinitionI18NResource.getInstance();
209 String property = "constraint." + id + ".synopsis";
210 return resource.getMessage(definition, property, locale);
211 }
212
213
214
215 /**
216 * {@inheritDoc}
217 */
218 @Override
219 protected void initialize() throws Exception {
220 condition.initialize(definition);
221 }
222
223 }