View Javadoc

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