001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2015 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.examples;
019
020import org.forgerock.i18n.LocalizableMessage;
021import org.forgerock.opendj.ldap.Connection;
022import org.forgerock.opendj.ldap.DN;
023import org.forgerock.opendj.ldap.LdapException;
024import org.forgerock.opendj.ldap.LDAPConnectionFactory;
025import org.forgerock.opendj.ldap.schema.AttributeType;
026import org.forgerock.opendj.ldap.schema.MatchingRule;
027import org.forgerock.opendj.ldap.schema.ObjectClass;
028import org.forgerock.opendj.ldap.schema.Schema;
029import org.forgerock.opendj.ldap.schema.Syntax;
030
031/**
032 * An example client application which prints a summary of the schema on the
033 * named server as well as any warnings encountered while parsing the schema.
034 * This example takes the following command line parameters:
035 *
036 * <pre>
037 *  {@code <host> <port> <username> <password>}
038 * </pre>
039 */
040public final class ReadSchema {
041    /**
042     * Main method.
043     *
044     * @param args
045     *            The command line arguments: host, port, username, password.
046     */
047    public static void main(final String[] args) {
048        if (args.length != 4) {
049            System.err.println("Usage: host port username password");
050            System.exit(1);
051        }
052
053        // Parse command line arguments.
054        final String hostName = args[0];
055        final int port = Integer.parseInt(args[1]);
056        final String userName = args[2];
057        final String password = args[3];
058
059        // --- JCite ---
060        // Connect and bind to the server.
061        final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
062        Connection connection = null;
063
064        try {
065            connection = factory.getConnection();
066            connection.bind(userName, password.toCharArray());
067
068            // Read the schema.
069            Schema schema = Schema.readSchemaForEntry(connection, DN.rootDN());
070
071            System.out.println("Attribute types");
072            for (AttributeType at : schema.getAttributeTypes()) {
073                System.out.println("  " + at.getNameOrOID());
074            }
075            System.out.println();
076
077            System.out.println("Object classes");
078            for (ObjectClass oc : schema.getObjectClasses()) {
079                System.out.println("  " + oc.getNameOrOID());
080            }
081            System.out.println();
082
083            System.out.println("Matching rules");
084            for (MatchingRule mr : schema.getMatchingRules()) {
085                System.out.println("  " + mr.getNameOrOID());
086            }
087            System.out.println();
088
089            System.out.println("Syntaxes");
090            for (Syntax s : schema.getSyntaxes()) {
091                System.out.println("  " + s.getDescription());
092            }
093            System.out.println();
094
095            // Etc...
096
097            System.out.println("WARNINGS");
098            for (LocalizableMessage m : schema.getWarnings()) {
099                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}