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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2009-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS.
026 */
027
028package org.forgerock.opendj.examples;
029
030import org.forgerock.i18n.LocalizableMessage;
031import org.forgerock.opendj.ldap.Connection;
032import org.forgerock.opendj.ldap.DN;
033import org.forgerock.opendj.ldap.LdapException;
034import org.forgerock.opendj.ldap.LDAPConnectionFactory;
035import org.forgerock.opendj.ldap.schema.AttributeType;
036import org.forgerock.opendj.ldap.schema.MatchingRule;
037import org.forgerock.opendj.ldap.schema.ObjectClass;
038import org.forgerock.opendj.ldap.schema.Schema;
039import org.forgerock.opendj.ldap.schema.Syntax;
040
041/**
042 * An example client application which prints a summary of the schema on the
043 * named server as well as any warnings encountered while parsing the schema.
044 * This example takes the following command line parameters:
045 *
046 * <pre>
047 *  {@code <host> <port> <username> <password>}
048 * </pre>
049 */
050public final class ReadSchema {
051    /**
052     * Main method.
053     *
054     * @param args
055     *            The command line arguments: host, port, username, password.
056     */
057    public static void main(final String[] args) {
058        if (args.length != 4) {
059            System.err.println("Usage: host port username password");
060            System.exit(1);
061        }
062
063        // Parse command line arguments.
064        final String hostName = args[0];
065        final int port = Integer.parseInt(args[1]);
066        final String userName = args[2];
067        final String password = args[3];
068
069        // --- JCite ---
070        // Connect and bind to the server.
071        final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
072        Connection connection = null;
073
074        try {
075            connection = factory.getConnection();
076            connection.bind(userName, password.toCharArray());
077
078            // Read the schema.
079            Schema schema = Schema.readSchemaForEntry(connection, DN.rootDN());
080
081            System.out.println("Attribute types");
082            for (AttributeType at : schema.getAttributeTypes()) {
083                System.out.println("  " + at.getNameOrOID());
084            }
085            System.out.println();
086
087            System.out.println("Object classes");
088            for (ObjectClass oc : schema.getObjectClasses()) {
089                System.out.println("  " + oc.getNameOrOID());
090            }
091            System.out.println();
092
093            System.out.println("Matching rules");
094            for (MatchingRule mr : schema.getMatchingRules()) {
095                System.out.println("  " + mr.getNameOrOID());
096            }
097            System.out.println();
098
099            System.out.println("Syntaxes");
100            for (Syntax s : schema.getSyntaxes()) {
101                System.out.println("  " + s.getDescription());
102            }
103            System.out.println();
104
105            // Etc...
106
107            System.out.println("WARNINGS");
108            for (LocalizableMessage m : schema.getWarnings()) {
109                System.out.println("  " + m);
110            }
111            System.out.println();
112        } catch (final LdapException e) {
113            System.err.println(e.getMessage());
114            System.exit(e.getResult().getResultCode().intValue());
115            return;
116        } finally {
117            if (connection != null) {
118                connection.close();
119            }
120        }
121        // --- JCite ---
122    }
123
124    private ReadSchema() {
125        // Not used.
126    }
127}