1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
36
37
38
39
40
41
42
43 public final class Modify {
44
45
46
47
48
49
50
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
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
65
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
79 final LDIFChangeRecordReader reader = new LDIFChangeRecordReader(ldif);
80
81
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
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
114 }
115 }
116
117 }
118
119 private Modify() {
120
121 }
122 }