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

matthew_swift
23.26.2007 feb5d90ec016c99712f19c5485cf7633cd38f111
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  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
 *
 *
 *      Portions Copyright 2007 Sun Microsystems, Inc.
 */
package org.opends.server.admin.client;
 
 
 
import static org.opends.server.loggers.Error.removeAllErrorLoggers;
import static org.opends.server.util.ServerConstants.PROPERTY_SERVER_ROOT;
 
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
 
import org.opends.server.admin.AbstractManagedObjectDefinition;
import org.opends.server.admin.ClassLoaderProvider;
import org.opends.server.admin.AggregationRelationDefinition;
import org.opends.server.admin.AttributeTypePropertyDefinition;
import org.opends.server.admin.ClassPropertyDefinition;
import org.opends.server.admin.InstantiableRelationDefinition;
import org.opends.server.admin.ManagedObjectDefinition;
import org.opends.server.admin.OptionalRelationDefinition;
import org.opends.server.admin.RelationDefinition;
import org.opends.server.admin.RelationDefinitionVisitor;
import org.opends.server.admin.SingletonRelationDefinition;
import org.opends.server.admin.std.meta.RootCfgDefn;
import org.opends.server.config.ConfigException;
import org.opends.server.core.DirectoryServer;
import org.opends.server.extensions.ConfigFileHandler;
import org.opends.server.types.InitializationException;
 
 
 
/**
 * An example application which uses introspection to output a list of
 * potential CLI sub-commands.
 */
public final class ExampleIntrospection implements
    RelationDefinitionVisitor<Void, String> {
 
  /**
   * Main application.
   *
   * @param args
   *          The command line arguments.
   * @throws ConfigException
   *           If there was a configuration problem.
   * @throws InitializationException
   *           If there was an initialization problem.
   */
  public static void main(String[] args)
      throws InitializationException, ConfigException {
    ExampleIntrospection app = new ExampleIntrospection();
 
    app.processRootConfiguration();
  }
 
 
 
  // Private constructor.
  private ExampleIntrospection() throws InitializationException,
      ConfigException {
    // Make sure a new instance is created.
    //
    // This is effectively a no-op at the moment, but may do lazy
    // initialization at some point.
    DirectoryServer.getInstance();
 
    // Initialize minimal features such as key syntaxes.
    DirectoryServer.bootstrapClient();
 
    // Many things are dependent on JMX to register an alert
    // generator.
    DirectoryServer.initializeJMX();
 
    removeAllErrorLoggers(true);
 
    // Initialize the configuration.
    File instanceRoot = new File("build/package/OpenDS-0.1");
    File configResourceDirectory = new File(instanceRoot, "config");
    System.setProperty(PROPERTY_SERVER_ROOT, instanceRoot
        .getAbsolutePath());
    File configFile = new File(configResourceDirectory, "config.ldif");
 
    DirectoryServer directoryServer = DirectoryServer.getInstance();
 
    // Bootstrap definition classes.
    ClassLoaderProvider.getInstance().enable();
 
    directoryServer.initializeConfiguration(ConfigFileHandler.class
        .getName(), configFile.getAbsolutePath());
 
    // Initialize and load the schema files.
    DirectoryServer.getInstance().initializeSchema();
 
    // Switch off class name validation in client.
    ClassPropertyDefinition.setAllowClassValidation(false);
 
    // Switch off attribute type name validation in client.
    AttributeTypePropertyDefinition.setCheckSchema(false);
  }
 
 
 
  /**
   * Process the root configuration definition.
   */
  public void processRootConfiguration() {
    RootCfgDefn d = RootCfgDefn.getInstance();
 
    String operands = "";
    for (RelationDefinition<?, ?> r : d.getRelationDefinitions()) {
      r.accept(this, operands);
    }
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public Void visitAggregation(AggregationRelationDefinition<?, ?> d,
      String p) {
    System.err.println("Found aggregation \"" + d.getName()
        + "\": aggregations are not supported yet");
 
    return null;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public Void visitInstantiable(
      InstantiableRelationDefinition<?, ?> d, String p) {
    String operands = p + " NAME";
 
    List<ManagedObjectDefinition<?, ?>> types = getSubTypes(d);
    String typesOption = getSubTypesOption(d, types);
 
    System.out.println("create-" + d.getName() + typesOption
        + operands + " [PROP:VALUE ...]");
    System.out.println("delete-" + d.getName() + operands);
    System.out.println("list-" + d.getPluralName() + p);
    System.out.println("get-" + d.getName() + "-prop" + operands
        + " [PROP ...]");
    System.out.println("set-" + d.getName() + "-prop" + operands
        + " [PROP[+|-]:VALUE ...]");
 
    System.out.println();
 
    // Process relations associated with the referenced definition and
    // its sub-types.
    processRelationDefinition(d, operands);
 
    return null;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public Void visitOptional(OptionalRelationDefinition<?, ?> d,
      String p) {
    String name = d.getName();
 
    List<ManagedObjectDefinition<?, ?>> types = getSubTypes(d);
    String typesOption = getSubTypesOption(d, types);
 
    System.out.println("create-" + name + typesOption + p
        + " [PROP:VALUE ...]");
    System.out.println("delete-" + name + p);
    System.out.println("has-" + name + p);
    System.out.println("get-" + name + "-prop" + p + " [PROP ...]");
    System.out.println("set-" + name + "-prop" + p
        + " [PROP[+|-]:VALUE ...]");
 
    System.out.println();
 
    // Process relations associated with the referenced definition and
    // its sub-types.
    processRelationDefinition(d, p);
 
    return null;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public Void visitSingleton(SingletonRelationDefinition<?, ?> d,
      String p) {
    String name = d.getName();
 
    System.out.println("get-" + name + "-prop" + p + " [PROP ...]");
    System.out.println("set-" + name + "-prop" + p
        + " [PROP[+|-]:VALUE ...]");
 
    System.out.println();
 
    return null;
  }
 
 
 
  // Get all the possible types that can be associated with a
  // relation.
  private List<ManagedObjectDefinition<?, ?>> getSubTypes(
      RelationDefinition<?, ?> d) {
    List<ManagedObjectDefinition<?, ?>> types =
      new LinkedList<ManagedObjectDefinition<?, ?>>();
    AbstractManagedObjectDefinition<?, ?> ad = d.getChildDefinition();
 
    getSubTypesHelp(ad, types);
 
    return types;
  }
 
 
 
  // Get sub-types helper method.
  private void getSubTypesHelp(
      AbstractManagedObjectDefinition<?, ?> ad,
      List<ManagedObjectDefinition<?, ?>> types) {
    // Add this definition if it is not abstract.
    if (ad instanceof ManagedObjectDefinition<?, ?>) {
      ManagedObjectDefinition<?, ?> d = (ManagedObjectDefinition<?, ?>) ad;
      types.add(d);
    }
 
    // Repeat for children.
    for (AbstractManagedObjectDefinition<?, ?> d : ad.getChildren()) {
      getSubTypesHelp(d, types);
    }
  }
 
 
 
  // Convert a list of sub-types to a "-t" option usage.
  private String getSubTypesOption(RelationDefinition<?, ?> r,
      List<ManagedObjectDefinition<?, ?>> types) {
    AbstractManagedObjectDefinition<?, ?> cd = r.getChildDefinition();
    String base = "";
    if (cd instanceof AbstractManagedObjectDefinition<?, ?>) {
      base = cd.getName();
    }
 
    StringBuilder builder = new StringBuilder();
    builder.append(" -t ");
 
    Iterator<ManagedObjectDefinition<?, ?>> i = types.iterator();
    String suffix = "-" + base;
    while (i.hasNext()) {
      ManagedObjectDefinition<?, ?> d = i.next();
 
      String name = d.getName();
      if (name.equals(cd.getName())) {
        // When the base type is itself instantiable use "generic".
        name = "generic";
      } else if (name.endsWith(suffix)) {
        name = name.substring(0, name.length() - suffix.length());
      }
 
      builder.append(name);
      if (i.hasNext()) {
        builder.append('|');
      }
    }
 
    return builder.toString();
  }
 
 
 
  // Process the relations associated with the managed objects
  // referenced from the specified relation.
  private void processRelationDefinition(RelationDefinition<?, ?> d,
      String operands) {
    AbstractManagedObjectDefinition<?, ?> ad = d.getChildDefinition();
    processManagedObjectDefinition(null, ad, operands);
  }
 
 
 
  // Process the relations of a child managed object definition.
  private void processManagedObjectDefinition(
      AbstractManagedObjectDefinition<?, ?> parent,
      AbstractManagedObjectDefinition<?, ?> child, String operands) {
    Set<RelationDefinition<?, ?>> parentRelations;
    Set<RelationDefinition<?, ?>> childRelations;
 
    if (parent != null) {
      parentRelations = new HashSet<RelationDefinition<?, ?>>(parent
          .getRelationDefinitions());
    } else {
      parentRelations = Collections.emptySet();
    }
 
    // Only process relations not defined in parent.
    childRelations = new HashSet<RelationDefinition<?, ?>>(child
        .getRelationDefinitions());
    childRelations.removeAll(parentRelations);
 
    for (RelationDefinition<?, ?> r : childRelations) {
      r.accept(this, operands);
    }
 
    // Process sub-types.
    for (AbstractManagedObjectDefinition<?, ?> d : child
        .getChildren()) {
      processManagedObjectDefinition(child, d, operands);
    }
  }
}