View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions Copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2009-2010 Sun Microsystems, Inc.
15   * Portions Copyright 2011-2015 ForgeRock AS.
16   */
17  
18  package org.forgerock.opendj.examples;
19  
20  import org.forgerock.i18n.LocalizableMessage;
21  import org.forgerock.opendj.ldap.Connection;
22  import org.forgerock.opendj.ldap.DN;
23  import org.forgerock.opendj.ldap.LdapException;
24  import org.forgerock.opendj.ldap.LDAPConnectionFactory;
25  import org.forgerock.opendj.ldap.schema.AttributeType;
26  import org.forgerock.opendj.ldap.schema.MatchingRule;
27  import org.forgerock.opendj.ldap.schema.ObjectClass;
28  import org.forgerock.opendj.ldap.schema.Schema;
29  import org.forgerock.opendj.ldap.schema.Syntax;
30  
31  /**
32   * An example client application which prints a summary of the schema on the
33   * named server as well as any warnings encountered while parsing the schema.
34   * This example takes the following command line parameters:
35   *
36   * <pre>
37   *  {@code <host> <port> <username> <password>}
38   * </pre>
39   */
40  public final class ReadSchema {
41      /**
42       * Main method.
43       *
44       * @param args
45       *            The command line arguments: host, port, username, password.
46       */
47      public static void main(final String[] args) {
48          if (args.length != 4) {
49              System.err.println("Usage: host port username password");
50              System.exit(1);
51          }
52  
53          // Parse command line arguments.
54          final String hostName = args[0];
55          final int port = Integer.parseInt(args[1]);
56          final String userName = args[2];
57          final String password = args[3];
58  
59          // --- JCite ---
60          // Connect and bind to the server.
61          final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
62          Connection connection = null;
63  
64          try {
65              connection = factory.getConnection();
66              connection.bind(userName, password.toCharArray());
67  
68              // Read the schema.
69              Schema schema = Schema.readSchemaForEntry(connection, DN.rootDN());
70  
71              System.out.println("Attribute types");
72              for (AttributeType at : schema.getAttributeTypes()) {
73                  System.out.println("  " + at.getNameOrOID());
74              }
75              System.out.println();
76  
77              System.out.println("Object classes");
78              for (ObjectClass oc : schema.getObjectClasses()) {
79                  System.out.println("  " + oc.getNameOrOID());
80              }
81              System.out.println();
82  
83              System.out.println("Matching rules");
84              for (MatchingRule mr : schema.getMatchingRules()) {
85                  System.out.println("  " + mr.getNameOrOID());
86              }
87              System.out.println();
88  
89              System.out.println("Syntaxes");
90              for (Syntax s : schema.getSyntaxes()) {
91                  System.out.println("  " + s.getDescription());
92              }
93              System.out.println();
94  
95              // Etc...
96  
97              System.out.println("WARNINGS");
98              for (LocalizableMessage m : schema.getWarnings()) {
99                  System.out.println("  " + m);
100             }
101             System.out.println();
102         } catch (final LdapException e) {
103             System.err.println(e.getMessage());
104             System.exit(e.getResult().getResultCode().intValue());
105             return;
106         } finally {
107             if (connection != null) {
108                 connection.close();
109             }
110         }
111         // --- JCite ---
112     }
113 
114     private ReadSchema() {
115         // Not used.
116     }
117 }