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