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 java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import org.forgerock.opendj.ldap.Connection;
26  import org.forgerock.opendj.ldap.LdapException;
27  import org.forgerock.opendj.ldap.LDAPConnectionFactory;
28  import org.forgerock.opendj.ldap.ResultCode;
29  import org.forgerock.opendj.ldif.ChangeRecord;
30  import org.forgerock.opendj.ldif.ConnectionChangeRecordWriter;
31  import org.forgerock.opendj.ldif.LDIFChangeRecordReader;
32  
33  /**
34   * An example client application which applies update operations to a Directory
35   * Server. The update operations will be read from an LDIF file, or stdin if no
36   * filename is provided. This example takes the following command line
37   * parameters (it will read from stdin if no LDIF file is provided):
38   *
39   * <pre>
40   *  {@code <host> <port> <username> <password> [<ldifFile>]}
41   * </pre>
42   */
43  public final class Modify {
44      /**
45       * Main method.
46       *
47       * @param args
48       *            The command line arguments: host, port, username, password,
49       *            LDIF file name containing the update operations (will use
50       *            stdin if not provided).
51       */
52      public static void main(final String[] args) {
53          if (args.length < 4 || args.length > 5) {
54              System.err.println("Usage: host port username password [ldifFileName]");
55              System.exit(1);
56          }
57  
58          // Parse command line arguments.
59          final String hostName = args[0];
60          final int port = Integer.parseInt(args[1]);
61          final String userName = args[2];
62          final String password = args[3];
63  
64          // Create the LDIF reader which will either used the named file, if
65          // provided, or stdin.
66          InputStream ldif;
67          if (args.length > 4) {
68              try {
69                  ldif = new FileInputStream(args[4]);
70              } catch (final FileNotFoundException e) {
71                  System.err.println(e.getMessage());
72                  System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
73                  return;
74              }
75          } else {
76              ldif = System.in;
77          }
78          // --- JCite ---
79          final LDIFChangeRecordReader reader = new LDIFChangeRecordReader(ldif);
80  
81          // Connect and bind to the server.
82          final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
83          Connection connection = null;
84  
85          try {
86              connection = factory.getConnection();
87              connection.bind(userName, password.toCharArray());
88  
89              // Write the changes.
90              final ConnectionChangeRecordWriter writer =
91                      new ConnectionChangeRecordWriter(connection);
92              while (reader.hasNext()) {
93                  ChangeRecord changeRecord = reader.readChangeRecord();
94                  writer.writeChangeRecord(changeRecord);
95                  System.err.println("Successfully modified entry " + changeRecord.getName());
96              }
97          } catch (final LdapException e) {
98              System.err.println(e.getMessage());
99              System.exit(e.getResult().getResultCode().intValue());
100             return;
101         } catch (final IOException e) {
102             System.err.println(e.getMessage());
103             System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
104             return;
105         } finally {
106             if (connection != null) {
107                 connection.close();
108             }
109 
110             try {
111                 reader.close();
112             } catch (final IOException ignored) {
113                 // Ignore.
114             }
115         }
116         // --- JCite ---
117     }
118 
119     private Modify() {
120         // Not used.
121     }
122 }