mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Maxim Thomas
17.31.2024 974cbbeaa39873742bdfdaca8d6f3ddb868cf36c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2007-2010 Sun Microsystems, Inc.
 * Portions Copyright 2012-2016 ForgeRock AS.
 */
package org.opends.server.tools.tasks;
 
import static org.opends.messages.TaskMessages.*;
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.util.StaticUtils.*;
 
import static com.forgerock.opendj.cli.Utils.*;
import static com.forgerock.opendj.cli.CommonArguments.*;
 
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.DecodeException;
import org.opends.server.admin.client.cli.TaskScheduleArgs;
import org.opends.server.backends.task.FailedDependencyAction;
import org.opends.server.backends.task.TaskState;
import org.opends.server.core.DirectoryServer;
import org.opends.server.loggers.JDKLogging;
import org.opends.server.tools.LDAPConnection;
import org.opends.server.tools.LDAPConnectionException;
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDAPException;
import org.opends.server.types.OpenDsException;
import org.opends.server.util.BuildVersion;
import org.opends.server.util.cli.LDAPConnectionArgumentParser;
 
import com.forgerock.opendj.cli.Argument;
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentGroup;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.ClientException;
import com.forgerock.opendj.cli.StringArgument;
 
/**
 * Base class for tools that are capable of operating either by running
 * local within this JVM or by scheduling a task to perform the same
 * action running within the directory server through the tasks interface.
 */
public abstract class TaskTool implements TaskScheduleInformation {
 
  /**
   * Magic value used to indicate that the user would like to schedule
   * this operation to run immediately as a task as opposed to running
   * the operation in the local VM.
   */
  public static final String NOW = TaskScheduleArgs.NOW;
 
  /**
   * The error code used by the mixed-script to know if the java
   * arguments for the off-line mode must be used.
   */
  private static final int RUN_OFFLINE = 51;
  /**
   * The error code used by the mixed-script to know if the java
   * arguments for the on-line mode must be used.
   */
  private static final int RUN_ONLINE = 52;
 
  /**
   * Number of milliseconds this utility will wait before reloading
   * this task's entry in the directory while it is polling for status.
   */
  private static final int SYNCHRONOUS_TASK_POLL_INTERVAL = 1000;
 
  private LDAPConnectionArgumentParser argParser;
 
  private TaskScheduleArgs taskScheduleArgs;
 
  /** Argument used to know if the tool should be run in offline mode. */
  protected BooleanArgument runOfflineArg;
 
  /** This CLI is always using the administration connector with SSL. */
  private static final boolean alwaysSSL = true;
 
  /**
   * Called when this utility should perform its actions locally in this
   * JVM.
   *
   * @param initializeServer indicates whether to initialize the
   *        directory server in the case of a local action
   * @param out stream to write messages; may be null
   * @param err stream to write messages; may be null
   * @return int indicating the result of this action
   */
  protected abstract int processLocal(boolean initializeServer,
                                      PrintStream out,
                                      PrintStream err);
 
  /**
   * Cleanup task environment after offline run.
   * Default implementation does nothing.
   * Tasks which initialize some static part of the DirectoryServer instance (e.g admin data local backends) must
   * override this method to shutdown all needed component to prevent JVM environment alteration.
   */
  protected void cleanup()
  {
    // Do nothing by default.
  }
 
  /**
   * Creates an argument parser prepopulated with arguments for processing
   * input for scheduling tasks with the task backend.
   *
   * @param className of this tool
   * @param toolDescription of this tool
   * @return LDAPConnectionArgumentParser for processing CLI input
   */
  protected LDAPConnectionArgumentParser createArgParser(String className,
    LocalizableMessage toolDescription)
  {
    ArgumentGroup ldapGroup = new ArgumentGroup(
      INFO_DESCRIPTION_TASK_LDAP_ARGS.get(), 1001);
 
    argParser = new LDAPConnectionArgumentParser(className,
      toolDescription, false, ldapGroup, alwaysSSL);
 
    ArgumentGroup taskGroup = new ArgumentGroup(
      INFO_DESCRIPTION_TASK_TASK_ARGS.get(), 1000);
 
    try {
      StringArgument propertiesFileArgument =
          propertiesFileArgument();
      argParser.addArgument(propertiesFileArgument);
      argParser.setFilePropertiesArgument(propertiesFileArgument);
 
      BooleanArgument noPropertiesFileArgument =
          noPropertiesFileArgument();
      argParser.addArgument(noPropertiesFileArgument);
      argParser.setNoPropertiesFileArgument(noPropertiesFileArgument);
 
      taskScheduleArgs = new TaskScheduleArgs();
 
      for (Argument arg : taskScheduleArgs.getArguments())
      {
        argParser.addArgument(arg, taskGroup);
      }
 
      runOfflineArg = BooleanArgument.builder("offline")
              .description(getOfflineDescriptionMessage())
              .buildAndAddToParser(argParser);
 
    } catch (ArgumentException e) {
      // should never happen
    }
 
    return argParser;
  }
 
  protected LocalizableMessage getOfflineDescriptionMessage() {
    return INFO_DESCRIPTION_RUN_OFFLINE.get();
  }
 
  /**
   * Validates arguments related to task scheduling.  This should be
   * called after the <code>ArgumentParser.parseArguments</code> has
   * been called.
   *
   * @throws ArgumentException if there is a problem with the arguments.
   * @throws ClientException if there is a problem with one of the values provided
   * by the user.
   */
  protected void validateTaskArgs() throws ArgumentException, ClientException
  {
    if (!runOffline())
    {
      taskScheduleArgs.validateArgs();
    }
    else
    {
      // server is offline => output logs to the console
      JDKLogging.enableConsoleLoggingForOpenDJTool();
      taskScheduleArgs.validateArgsIfOffline();
    }
  }
 
  /** {@inheritDoc} */
  @Override
  public Date getStartDateTime() {
    return taskScheduleArgs.getStartDateTime();
  }
 
  /** {@inheritDoc} */
  @Override
  public String getRecurringDateTime() {
    return taskScheduleArgs.getRecurringDateTime();
  }
 
  /** {@inheritDoc} */
  @Override
  public List<String> getDependencyIds() {
    return taskScheduleArgs.getDependencyIds();
  }
 
  /** {@inheritDoc} */
  @Override
  public FailedDependencyAction getFailedDependencyAction() {
    return taskScheduleArgs.getFailedDependencyAction();
  }
 
  /** {@inheritDoc} */
  @Override
  public List<String> getNotifyUponCompletionEmailAddresses() {
    return taskScheduleArgs.getNotifyUponCompletionEmailAddresses();
  }
 
  /** {@inheritDoc} */
  @Override
  public List<String> getNotifyUponErrorEmailAddresses() {
    return taskScheduleArgs.getNotifyUponErrorEmailAddresses();
  }
 
  /**
   * Either invokes initiates this tool's local action or schedule this
   * tool using the tasks interface based on user input.
   *
   * @param argParser used to parse user arguments
   * @param initializeServer indicates whether to initialize the
   *        directory server in the case of a local action
   * @param out stream to write messages; may be null
   * @param err stream to write messages; may be null
   * @return int indicating the result of this action
   */
  protected int process(LDAPConnectionArgumentParser argParser,
                        boolean initializeServer,
                        PrintStream out, PrintStream err) {
    if (runOffline())
    {
      try
      {
        return processLocal(initializeServer, out, err);
      }
      finally
      {
        if (initializeServer)
        {
          cleanup();
        }
      }
    }
 
    if (initializeServer)
    {
      try
      {
        DirectoryServer.bootstrapClient();
        DirectoryServer.initializeJMX();
      }
      catch (Exception e)
      {
        printWrappedText(err, ERR_SERVER_BOOTSTRAP_ERROR.get(getExceptionMessage(e)));
        return 1;
      }
    }
 
    LDAPConnection conn = null;
    try {
      conn = argParser.connect(out, err);
      TaskClient tc = new TaskClient(conn);
      TaskEntry taskEntry = tc.schedule(this);
      LocalizableMessage startTime = taskEntry.getScheduledStartTime();
      if (taskEntry.getTaskState() == TaskState.RECURRING) {
        printWrappedText(out, INFO_TASK_TOOL_RECURRING_TASK_SCHEDULED.get(taskEntry.getType(), taskEntry.getId()));
      } else if (startTime == null || startTime.length() == 0) {
        printWrappedText(out, INFO_TASK_TOOL_TASK_SCHEDULED_NOW.get(taskEntry.getType(), taskEntry.getId()));
      } else {
        printWrappedText(out, INFO_TASK_TOOL_TASK_SCHEDULED_FUTURE.get(
            taskEntry.getType(), taskEntry.getId(), taskEntry.getScheduledStartTime()));
      }
      if (!taskScheduleArgs.startArg.isPresent()) {
 
        // Poll the task printing log messages until finished
        String taskId = taskEntry.getId();
        Set<LocalizableMessage> printedLogMessages = new HashSet<>();
        do {
          taskEntry = tc.getTaskEntry(taskId);
          List<LocalizableMessage> logs = taskEntry.getLogMessages();
          for (LocalizableMessage log : logs) {
            if (printedLogMessages.add(log)) {
              out.println(log);
            }
          }
 
          try {
            Thread.sleep(SYNCHRONOUS_TASK_POLL_INTERVAL);
          } catch (InterruptedException e) {
            // ignore
          }
 
        } while (!taskEntry.isDone());
        if (TaskState.isSuccessful(taskEntry.getTaskState())) {
          if (taskEntry.getTaskState() != TaskState.RECURRING) {
            printWrappedText(out, INFO_TASK_TOOL_TASK_SUCESSFULL.get(taskEntry.getType(), taskEntry.getId()));
          }
          return 0;
        } else {
          printWrappedText(out, INFO_TASK_TOOL_TASK_NOT_SUCESSFULL.get(taskEntry.getType(), taskEntry.getId()));
          return 1;
        }
      }
      return 0;
    } catch (LDAPConnectionException e) {
      if (isWrongPortException(e,
          Integer.valueOf(argParser.getArguments().getPort())))
      {
        printWrappedText(err, ERR_TASK_LDAP_FAILED_TO_CONNECT_WRONG_PORT.get(
            argParser.getArguments().getHostName(), argParser.getArguments().getPort()));
      } else {
        printWrappedText(err, ERR_TASK_TOOL_START_TIME_NO_LDAP.get(e.getMessage()));
      }
      return 1;
    } catch (DecodeException ae) {
      printWrappedText(err, ERR_TASK_TOOL_DECODE_ERROR.get(ae.getMessage()));
      return 1;
    } catch (IOException ioe) {
      printWrappedText(err, ERR_TASK_TOOL_IO_ERROR.get(ioe));
      return 1;
    } catch (LDAPException le) {
      printWrappedText(err, ERR_TASK_TOOL_LDAP_ERROR.get(le.getMessage()));
      return 1;
    } catch (OpenDsException e) {
      printWrappedText(err, e.getMessageObject());
      return 1;
    } catch (ArgumentException e) {
      argParser.displayMessageAndUsageReference(err, e.getMessageObject());
      return 1;
    }
    finally
    {
      if (conn != null)
      {
        try
        {
          conn.close(null);
        }
        catch (Throwable t)
        {
          // Ignore.
        }
      }
    }
  }
 
  /**
   * Returns {@code true} if the provided exception was caused by trying to
   * connect to the wrong port and {@code false} otherwise.
   * @param t the exception to be analyzed.
   * @param port the port to which we tried to connect.
   * @return {@code true} if the provided exception was caused by trying to
   * connect to the wrong port and {@code false} otherwise.
   */
  private boolean isWrongPortException(Throwable t, int port)
  {
    boolean isWrongPortException = false;
    boolean isDefaultClearPort = (port - 389) % 1000 == 0;
    while (t != null && isDefaultClearPort)
    {
      isWrongPortException = t instanceof java.net.SocketTimeoutException;
      if (!isWrongPortException)
      {
        t = t.getCause();
      }
      else
      {
        break;
      }
    }
    return isWrongPortException;
  }
 
 
  /**
   * Indicates whether we must return if the command must be run in off-line
   * mode.
   * @return <CODE>true</CODE> if we must return if the command must be run in
   * off-line mode and <CODE>false</CODE> otherwise.
   */
  public boolean runOffline()
  {
    return runOfflineArg.isPresent();
  }
 
  /**
   * Checks that binary version and instance version are the same.
   *
   * @throws InitializationException
   *           If versions mismatch
   */
  protected void checkVersion() throws InitializationException
  {
    // FIXME Do not perform this check if the tool is use in remote mode (see OPENDJ-1166)
    BuildVersion.checkVersionMismatch();
  }
}