mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
26.08.2015 8b3cd28204e15e0a98ce038b355f100cd7c44e3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2015-2016 ForgeRock AS
 */
package org.opends.server.schema;
 
import static org.opends.server.util.ServerConstants.*;
 
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldap.schema.SchemaBuilder;
import org.opends.server.config.ConfigConstants;
import org.opends.server.core.ServerContext;
import org.opends.server.types.CommonSchemaElements;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.SchemaFileElement;
import org.opends.server.util.RemoveOnceSDKSchemaIsUsed;
import org.opends.server.util.ServerConstants;
 
/**
 * Represents a schema element which is either a SDK attribute type or an objectclass from the server.
 * <p>
 * In absence of a common interface, this class allows to process all elements in the same way,
 * and to provide useful server-oriented methods like {@code getSchemaFile()} or
 * {@code getOrigin()}.
 */
@RemoveOnceSDKSchemaIsUsed("This class is a temporary mechanism"
    + " to manage in the same way SDK and server schema element classes")
public class SomeSchemaElement implements SchemaFileElement
{
  private final ObjectClass objectClass;
  private AttributeType attributeType;
 
  /**
   * Builds SomeSchemaElement.
   *
   * @param objectClass
   *          the common schema element to wrap
   */
  public SomeSchemaElement(ObjectClass objectClass)
  {
    this.objectClass = objectClass;
    this.attributeType = null;
  }
 
  /**
   * Builds SomeSchemaElement.
   *
   * @param attributeType
   *          the attribute type element to wrap
   */
  public SomeSchemaElement(AttributeType attributeType)
  {
    this.objectClass = null;
    this.attributeType = attributeType;
  }
 
  /**
   * Returns the wrapped schema element as an object class.
   *
   * @return the wrapped object class
   */
  public ObjectClass getObjectClass()
  {
    return objectClass;
  }
 
  /**
   * Returns the wrapped schema element as an attribute type.
   *
   * @return the wrapped attribute type
   */
  public AttributeType getAttributeType()
  {
    return attributeType;
  }
 
  /**
   * Returns whether the wrapped element is an attribute type.
   *
   * @return {@code true} when the wrapped element is an attribute type, {@code false} otherwise
   */
  public boolean isAttributeType()
  {
    return attributeType != null;
  }
 
  /**
   * Returns whether the wrapped element is an object class.
   *
   * @return {@code true} when the wrapped element is an object class, {@code false} otherwise
   */
  public boolean isObjectClass()
  {
    return objectClass != null;
  }
 
  /**
   * Returns the OID of the wrapped element.
   *
   * @return the OID of the wrapped element.
   */
  public String getOID()
  {
    return attributeType != null ? attributeType.getOID() : objectClass.getOID();
  }
 
  /**
   * Returns the name or OID of the wrapped element.
   *
   * @return the name or OID of the wrapped element.
   */
  public String getNameOrOID()
  {
    return attributeType != null ? attributeType.getNameOrOID() : objectClass.getNameOrOID();
  }
 
  /**
   * Returns the names of the wrapped element.
   *
   * @return the names of the wrapped element.
   */
  public Iterable<String> getNames()
  {
    return attributeType != null ? attributeType.getNames() : objectClass.getNormalizedNames();
  }
 
  @Override
  public Map<String, List<String>> getExtraProperties()
  {
    return attributeType != null ? attributeType.getExtraProperties() : objectClass.getExtraProperties();
  }
 
  @Override
  public String toString()
  {
    return attributeType != null ? attributeType.toString() : objectClass.toString();
  }
 
  /**
   * Retrieves the definition string used to create this attribute
   * type and including the X-SCHEMA-FILE extension.
   *
   * @return  The definition string used to create this attribute
   *          type including the X-SCHEMA-FILE extension.
   */
  public String getDefinitionWithFileName()
  {
    final String schemaFile = getSchemaFile();
    final String definition = toString();
    if (schemaFile != null)
    {
      int pos = definition.lastIndexOf(')');
      return definition.substring(0, pos).trim() + " " + SCHEMA_PROPERTY_FILENAME + " '" + schemaFile + "' )";
    }
    return definition;
  }
 
  /**
   * Returns the name of the schema file that contains the definition of the wrapped element.
   *
   * @return the name of the schema file that contains the definition of the wrapped element.
   */
  public String getSchemaFile()
  {
    return getExtraPropertySingleValue(ServerConstants.SCHEMA_PROPERTY_FILENAME);
  }
 
  /**
   * Sets the name of the schema file that contains the definition of the wrapped element.
   *
   * @param serverContext
   *          the server context
   * @param schemaFile
   *          the name of the schema file that contains the definition of the wrapped element.
   */
  public void setSchemaFile(ServerContext serverContext, String schemaFile)
  {
    setExtraPropertySingleValue(serverContext, SCHEMA_PROPERTY_FILENAME, schemaFile);
  }
 
  /**
   * Returns the origin of the provided schema element.
   * @return the origin of the provided schema element.
   */
  public String getOrigin()
  {
    return getExtraPropertySingleValue(ServerConstants.SCHEMA_PROPERTY_ORIGIN);
  }
 
  private String getExtraPropertySingleValue(String schemaPropertyOrigin)
  {
    if (objectClass != null)
    {
      return CommonSchemaElements.getSingleValueProperty(objectClass, schemaPropertyOrigin);
    }
    List<String> values = attributeType.getExtraProperties().get(schemaPropertyOrigin);
    return values != null && !values.isEmpty() ? values.get(0) : null;
  }
 
  /**
   * Returns the attribute name of the wrapped element.
   * @return the attribute name of the wrapped element.
   */
  public String getAttributeName()
  {
    return attributeType!= null ? ConfigConstants.ATTR_ATTRIBUTE_TYPES : ConfigConstants.ATTR_OBJECTCLASSES;
  }
 
  /**
   * Sets a single-valued extra property on the wrapped element.
   *
   * @param serverContext
   *          the server context
   * @param property
   *          the property to set
   * @param value
   *          the value to set
   */
  public void setExtraPropertySingleValue(ServerContext serverContext, String property, String value)
  {
    if (attributeType != null)
    {
      List<String> values = value != null ?  Arrays.asList(value) : null;
      setExtraPropertyMultipleValues(serverContext, property, values);
    }
    else
    {
      CommonSchemaElements.setExtraProperty(objectClass, property, value);
    }
  }
 
  /**
   * Sets a multi-valued extra property on the wrapped element.
   *
   * @param serverContext
   *          the server context
   * @param property
   *          the property to set
   * @param values
   *          the values to set
   */
  public void setExtraPropertyMultipleValues(ServerContext serverContext, String property, List<String> values)
  {
    if (attributeType != null)
    {
      SchemaBuilder schemaBuilder = serverContext != null ?
          new SchemaBuilder(serverContext.getSchemaNG()) : new SchemaBuilder(Schema.getDefaultSchema());
      AttributeType.Builder builder =
          schemaBuilder.buildAttributeType(attributeType).removeExtraProperty(property, (String) null);
      if (values != null  && !values.isEmpty())
      {
        builder.extraProperties(property, values);
      }
      attributeType = builder.addToSchemaOverwrite().toSchema().getAttributeType(attributeType.getNameOrOID());
    }
    else
    {
      objectClass.setExtraProperty(property, values);
    }
  }
 
  /**
   * Returns a copy of the provided attribute type, changing the superior attribute type.
   *
   * @param attributeType
   *          the attribute type for which a modified copy must be built
   * @param newSuperiorType
   *          the new superior attribute type to set, {@code null} means remove the superior type
   * @return an attribute type builder to build an updated copy of the provided attribute type
   */
  public static AttributeType changeSuperiorType(AttributeType attributeType, AttributeType newSuperiorType)
  {
    String superiorTypeOID = newSuperiorType != null ? newSuperiorType.getNameOrOID() : null;
    Schema schema = new SchemaBuilder()
      .buildAttributeType(attributeType)
      .superiorType(superiorTypeOID)
      .addToSchemaOverwrite()
      .toSchema();
    return schema.getAttributeType(attributeType.getNameOrOID());
  }
}