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

jvergara
07.43.2009 b471bc6b9f2a087e8bb8e3adc6e5d1b32e80db8d
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2008-2009 Sun Microsystems, Inc.
 */
 
package org.opends.guitools.controlpanel.ui;
 
import static org.opends.messages.AdminToolMessages.*;
import static org.opends.messages.CoreMessages.*;
import static org.opends.messages.ToolMessages.*;
 
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;
 
import org.opends.guitools.controlpanel.datamodel.BackupDescriptor;
import org.opends.guitools.controlpanel.datamodel.BackupTableModel;
import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
import org.opends.guitools.controlpanel.event.BrowseActionListener;
import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
import org.opends.guitools.controlpanel.ui.renderer.BackupTableCellRenderer;
import org.opends.guitools.controlpanel.util.BackgroundTask;
import org.opends.guitools.controlpanel.util.Utilities;
import org.opends.messages.Message;
import org.opends.server.types.BackupDirectory;
import org.opends.server.types.BackupInfo;
import org.opends.server.types.OpenDsException;
import org.opends.server.util.StaticUtils;
 
/**
 * Abstract class used to refactor code in panels that contain a backup list on
 * it.
 *
 */
public abstract class BackupListPanel extends StatusGenericPanel
{
  /**
   * The text field containing the parent directory.
   */
  protected JTextField parentDirectory;
 
  /**
   * The refreshing list message, displayed when the list of backups is
   * refreshed.
   */
  protected final Message REFRESHING_LIST =
    INFO_CTRL_PANEL_REFRESHING_LIST_SUMMARY.get();
 
  /**
   * The message informing that no backups where found.
   */
  protected final Message NO_BACKUPS_FOUND =
    INFO_CTRL_PANEL_NO_BACKUPS_FOUND.get();
 
  /**
   * Label for the path field.
   */
  protected JLabel lPath;
  /**
   * Label for the list.
   */
  protected JLabel lAvailableBackups;
  /**
   * Refreshing list label (displayed instead of the list when this one is
   * being refreshed).
   */
  protected JLabel lRefreshingList;
  /**
   * Refresh list button.
   */
  protected JButton refreshList;
  /**
   * Verify backup button.
   */
  protected JButton verifyBackup;
  /**
   * Browse button.
   */
  protected JButton browse;
  /**
   * The scroll that contains the list of backups (actually is a table).
   */
  protected JScrollPane tableScroll;
  /**
   * The list of backups.
   */
  protected JTable backupList;
 
  private JLabel lRemoteFileHelp;
 
  /**
   * Whether the backup parent directory has been initialized with a value or
   * not.
   */
  private boolean backupDirectoryInitialized;
 
  private BackupTableCellRenderer renderer;
 
  private static final Logger LOG =
    Logger.getLogger(BackupListPanel.class.getName());
 
  /**
   * Default constructor.
   *
   */
  protected BackupListPanel()
  {
    super();
  }
 
  /**
   * {@inheritDoc}
   */
  public Component getPreferredFocusComponent()
  {
    return parentDirectory;
  }
 
  /**
   * Returns the selected backup in the list.
   * @return the selected backup in the list.
   */
  protected BackupDescriptor getSelectedBackup()
  {
    BackupDescriptor backup = null;
    int row = backupList.getSelectedRow();
    if (row != -1)
    {
      BackupTableModel model = (BackupTableModel)backupList.getModel();
      backup = model.get(row);
    }
    return backup;
  }
 
  /**
   * Notification that the verify button was clicked.  Whatever is required
   * to be done must be done in this method.
   *
   */
  protected abstract void verifyBackupClicked();
 
  /**
   * Creates the components and lays them in the panel.
   * @param gbc the grid bag constraints to be used.
   */
  protected void createLayout(GridBagConstraints gbc)
  {
    gbc.gridy ++;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.weightx = 0.0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridwidth = 1;
    gbc.insets.left = 0;
    lPath = Utilities.createPrimaryLabel(
        INFO_CTRL_PANEL_BACKUP_PATH_LABEL.get());
    add(lPath, gbc);
 
    gbc.gridx = 1;
    gbc.insets.left = 10;
    parentDirectory = Utilities.createLongTextField();
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(parentDirectory, gbc);
    browse = Utilities.createButton(INFO_CTRL_PANEL_BROWSE_BUTTON_LABEL.get());
    browse.setOpaque(false);
    browse.addActionListener(
        new BrowseActionListener(parentDirectory,
            BrowseActionListener.BrowseType.LOCATION_DIRECTORY,  this));
    gbc.gridx = 2;
    gbc.weightx = 0.0;
    add(browse, gbc);
 
    lRemoteFileHelp = Utilities.createInlineHelpLabel(
        INFO_CTRL_PANEL_REMOTE_SERVER_PATH.get());
    gbc.gridx = 1;
    gbc.gridwidth = 2;
    gbc.insets.top = 3;
    gbc.insets.left = 10;
    gbc.gridy ++;
    add(lRemoteFileHelp, gbc);
 
    gbc.gridx = 0;
    gbc.gridy ++;
    gbc.insets.top = 10;
    gbc.insets.left = 0;
    lAvailableBackups = Utilities.createPrimaryLabel(
        INFO_CTRL_PANEL_AVAILABLE_BACKUPS_LABEL.get());
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridwidth = 1;
    add(lAvailableBackups, gbc);
 
    gbc.gridx = 1;
    gbc.gridwidth = 2;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets.left = 10;
    lRefreshingList = Utilities.createDefaultLabel(REFRESHING_LIST);
    lRefreshingList.setHorizontalAlignment(SwingConstants.CENTER);
    gbc.anchor = GridBagConstraints.CENTER;
    add(lRefreshingList, gbc);
 
    backupList = new JTable();
    // Done to provide a good size to the table.
    BackupTableModel model = new BackupTableModel();
    for (BackupDescriptor backup : createDummyBackupList())
    {
      model.add(backup);
    }
    backupList.setModel(model);
    backupList.getSelectionModel().setSelectionMode(
        ListSelectionModel.SINGLE_SELECTION);
    backupList.setShowGrid(false);
    backupList.setIntercellSpacing(new Dimension(0, 0));
    renderer = new BackupTableCellRenderer();
    renderer.setParentPath(new File(DUMMY_PARENT_PATH));
    for (int i=0; i<model.getColumnCount(); i++)
    {
      TableColumn col = backupList.getColumn(model.getColumnName(i));
      col.setCellRenderer(renderer);
    }
    backupList.setTableHeader(null);
    Utilities.updateTableSizes(backupList);
    tableScroll = Utilities.createScrollPane(backupList);
    tableScroll.setColumnHeaderView(null);
    tableScroll.setPreferredSize(backupList.getPreferredSize());
    gbc.anchor = GridBagConstraints.NORTHWEST;
    add(tableScroll, gbc);
    lRefreshingList.setPreferredSize(tableScroll.getPreferredSize());
 
    gbc.gridy ++;
    gbc.anchor = GridBagConstraints.EAST;
    gbc.weightx = 0.0;
    gbc.weighty = 0.0;
    gbc.insets.top = 5;
    JPanel buttonPanel = new JPanel(new GridBagLayout());
    buttonPanel.setOpaque(false);
    add(buttonPanel, gbc);
    GridBagConstraints gbc2 = new GridBagConstraints();
    gbc2.gridx = 0;
    gbc2.gridy = 0;
    gbc2.gridwidth = 1;
    gbc2.anchor = GridBagConstraints.EAST;
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    gbc2.weightx = 1.0;
    buttonPanel.add(Box.createHorizontalGlue(), gbc2);
    refreshList = Utilities.createButton(
        INFO_CTRL_PANEL_REFRESH_LIST_BUTTON_LABEL.get());
    refreshList.setOpaque(false);
    refreshList.addActionListener(new ActionListener()
    {
      /**
       * {@inheritDoc}
       */
      public void actionPerformed(ActionEvent ev)
      {
        refreshList();
      }
    });
    gbc2.weightx = 0.0;
    gbc2.gridx ++;
    buttonPanel.add(refreshList, gbc2);
    gbc2.gridx ++;
    gbc2.insets.left = 5;
    verifyBackup = Utilities.createButton(
        INFO_CTRL_PANEL_VERIFY_BACKUP_BUTTON_LABEL.get());
    verifyBackup.setOpaque(false);
    verifyBackup.addActionListener(new ActionListener()
    {
      /**
       * {@inheritDoc}
       */
      public void actionPerformed(ActionEvent ev)
      {
        verifyBackupClicked();
      }
    });
    ListSelectionListener listener = new ListSelectionListener()
    {
      /**
       * {@inheritDoc}
       */
      public void valueChanged(ListSelectionEvent ev)
      {
        BackupDescriptor backup = getSelectedBackup();
        verifyBackup.setEnabled(backup != null);
      }
    };
    backupList.getSelectionModel().addListSelectionListener(listener);
    listener.valueChanged(null);
    buttonPanel.add(verifyBackup, gbc2);
  }
 
  /**
   * Refresh the list of backups by looking in the backups defined under the
   * provided parent backup directory.
   *
   */
  protected void refreshList()
  {
    final boolean refreshEnabled = refreshList.isEnabled();
 
    refreshList.setEnabled(false);
    verifyBackup.setEnabled(false);
    tableScroll.setVisible(false);
    lRefreshingList.setText(REFRESHING_LIST.toString());
    lRefreshingList.setVisible(isLocal());
    final int lastSelectedRow = backupList.getSelectedRow();
 
    final String parentPath = parentDirectory.getText();
    BackgroundTask<Set<BackupInfo>> worker =
      new BackgroundTask<Set<BackupInfo>>()
    {
      /**
       * {@inheritDoc}
       */
      public Set<BackupInfo> processBackgroundTask() throws Throwable
      {
        // Open the backup directory and make sure it is valid.
        LinkedHashSet<BackupInfo> backups = new LinkedHashSet<BackupInfo>();
        Throwable firstThrowable = null;
        try
        {
          BackupDirectory backupDir =
            BackupDirectory.readBackupDirectoryDescriptor(parentPath);
          backups.addAll(backupDir.getBackups().values());
        }
        catch (Throwable t)
        {
          firstThrowable = t;
        }
        // Check the subdirectories
        File f = new File(parentPath);
 
        // Check the first level of directories (we might have done a backup
        // of one backend and then a backup of several backends under the
        // same directory).
        if (f.isDirectory())
        {
          File[] children = f.listFiles();
          for (int i=0; i<children.length; i++)
          {
            if (children[i].isDirectory())
            {
              try
              {
                BackupDirectory backupDir =
                  BackupDirectory.readBackupDirectoryDescriptor(
                      children[i].getAbsolutePath());
 
                backups.addAll(backupDir.getBackups().values());
              }
              catch (Throwable t2)
              {
                if (!children[i].getName().equals("tasks") &&
                    (firstThrowable != null))
                {
                  LOG.log(Level.WARNING, "Error searching backup: "+t2, t2);
                }
              }
            }
          }
        }
        if ((backups.size() == 0) && (firstThrowable != null))
        {
          throw firstThrowable;
        }
        return backups;
      }
      /**
       * {@inheritDoc}
       */
      public void backgroundTaskCompleted(Set<BackupInfo> returnValue,
          Throwable t)
      {
        BackupTableModel model = (BackupTableModel)backupList.getModel();
        model.clear();
        renderer.setParentPath(new File(parentPath));
        if (t == null)
        {
          if (returnValue.size() > 0)
          {
            for (BackupInfo backup : returnValue)
            {
              model.add(new BackupDescriptor(backup));
            }
            model.fireTableDataChanged();
            Utilities.updateTableSizes(backupList);
            tableScroll.setVisible(true);
            lRefreshingList.setVisible(false);
          }
          else
          {
            model.fireTableDataChanged();
            lRefreshingList.setText(NO_BACKUPS_FOUND.toString());
            lRefreshingList.setVisible(isLocal());
          }
          errorPane.setVisible(false);
          // This is done to perform checks against whether we require to
          // display an error message or not.
          configurationChanged(new ConfigurationChangeEvent(null,
              getInfo().getServerDescriptor()));
        }
        else
        {
          model.fireTableDataChanged();
          boolean displayError = true;
          if (t instanceof OpenDsException)
          {
            OpenDsException e = (OpenDsException)t;
            if (e.getMessageObject().getDescriptor().equals(
                ERR_BACKUPDIRECTORY_NO_DESCRIPTOR_FILE))
            {
              displayError = false;
            }
          }
          if (displayError)
          {
            Message details = ERR_RESTOREDB_CANNOT_READ_BACKUP_DIRECTORY.get(
              parentDirectory.getText(), StaticUtils.getExceptionMessage(t));
 
            updateErrorPane(errorPane,
                ERR_ERROR_SEARCHING_BACKUPS_SUMMARY.get(),
                ColorAndFontConstants.errorTitleFont,
                details,
                errorPane.getFont());
            packParentDialog();
          }
          errorPane.setVisible(displayError);
 
          if (!displayError)
          {
            // This is done to perform checks against whether we require to
            // display an error message or not.
            configurationChanged(new ConfigurationChangeEvent(null,
                getInfo().getServerDescriptor()));
          }
 
          lRefreshingList.setText(NO_BACKUPS_FOUND.toString());
        }
        refreshList.setEnabled(refreshEnabled);
        verifyBackup.setEnabled(getSelectedBackup() != null);
        if ((lastSelectedRow != -1) &&
            (lastSelectedRow < backupList.getRowCount()))
        {
          backupList.setRowSelectionInterval(lastSelectedRow, lastSelectedRow);
        }
        else if (backupList.getRowCount() > 0)
        {
          backupList.setRowSelectionInterval(0, 0);
        }
      }
    };
    worker.startBackgroundTask();
  }
 
  private final String DUMMY_PARENT_PATH = "/local/OpenDS-X.X.X/bak";
  /**
   * Creates a list with backup descriptor.  This is done simply to have a good
   * initial size for the table.
   * @return a list with bogus backup descriptors.
   */
  private ArrayList<BackupDescriptor> createDummyBackupList()
  {
    ArrayList<BackupDescriptor> list = new ArrayList<BackupDescriptor>();
    list.add(new BackupDescriptor(
        new File(DUMMY_PARENT_PATH+"/200704201567Z"),
        new GregorianCalendar(2007, 5, 20, 8, 10).getTime(),
        BackupDescriptor.Type.FULL, "id"));
    list.add(new BackupDescriptor(
        new File(DUMMY_PARENT_PATH+"/200704201567Z"),
        new GregorianCalendar(2007, 5, 22, 8, 10).getTime(),
        BackupDescriptor.Type.INCREMENTAL, "id"));
    list.add(new BackupDescriptor(
        new File(DUMMY_PARENT_PATH+"/200704221567Z"),
        new GregorianCalendar(2007, 5, 25, 8, 10).getTime(),
        BackupDescriptor.Type.INCREMENTAL, "id"));
    return list;
  }
 
  /**
   * {@inheritDoc}
   */
  public void configurationChanged(ConfigurationChangeEvent ev)
  {
    ServerDescriptor desc = ev.getNewDescriptor();
 
    if (!backupDirectoryInitialized &&
        (parentDirectory.getText().length() == 0))
    {
      String path;
 
      if (desc.isLocal() || (desc.isWindows() == Utilities.isWindows()))
      {
        File f = new File(desc.getInstancePath(),
            org.opends.quicksetup.Installation.BACKUPS_PATH_RELATIVE);
        try
        {
          path = f.getCanonicalPath();
        }
        catch (Throwable t)
        {
          path = f.getAbsolutePath();
        }
      }
      else
      {
        String separator = desc.isWindows() ? "\\" : "/";
        path = desc.getInstancePath() + separator +
        org.opends.quicksetup.Installation.BACKUPS_PATH_RELATIVE;
      }
 
      final String fPath = path;
      SwingUtilities.invokeLater(new Runnable()
      {
        public void run()
        {
          parentDirectory.setText(fPath);
          refreshList();
          backupDirectoryInitialized = true;
        }
      });
    }
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        lRemoteFileHelp.setVisible(!isLocal());
        browse.setVisible(isLocal());
        lAvailableBackups.setVisible(isLocal());
        tableScroll.setVisible(isLocal());
        refreshList.setVisible(isLocal());
        verifyBackup.setVisible(isLocal());
      }
    });
  }
 
  /**
   * {@inheritDoc}
   */
  public void toBeDisplayed(boolean visible)
  {
    if (visible && backupDirectoryInitialized)
    {
      refreshList();
    }
  }
}