View Javadoc
1   package com.atlassian.dbexporter;
2   
3   import com.atlassian.activeobjects.spi.ImportExportException;
4   import com.atlassian.dbexporter.exporter.ExportConfiguration;
5   import com.atlassian.dbexporter.exporter.Exporter;
6   import com.atlassian.dbexporter.node.NodeCreator;
7   import com.atlassian.dbexporter.node.NodeStreamWriter;
8   import com.atlassian.dbexporter.progress.ProgressMonitor;
9   
10  import java.util.List;
11  
12  import static com.atlassian.dbexporter.node.NodeBackup.RootNode;
13  import static com.google.common.base.Preconditions.checkArgument;
14  import static com.google.common.base.Preconditions.checkNotNull;
15  import static com.google.common.collect.Lists.newArrayList;
16  
17  /**
18   * Creates an export of a database. What exactly the export 'looks' like depends heavily on the exporters passed-in.
19   * <p>
20   * A {@link ProgressMonitor} can be supplied to be notified of the progress as the export is being made.
21   *
22   * @author Erik van Zijst
23   * @author Samuel Le Berrigaud
24   */
25  public final class DbExporter {
26      private final List<Exporter> exporters;
27  
28      public DbExporter(final Exporter... exporters) {
29          this(newArrayList(checkNotNull(exporters)));
30      }
31  
32      public DbExporter(final List<Exporter> exporters) {
33          checkArgument(!checkNotNull(exporters).isEmpty(), "DbExporter must be created with at least one Exporter!");
34          this.exporters = exporters;
35      }
36  
37      /**
38       * Imports the XML document read from the stream
39       *
40       * @param streamWriter  the stream to write the XML to
41       * @param configuration the export configuration
42       * @throws ImportExportException or one of its sub-types if an unexpected exception happens during the import.
43       */
44      public void exportData(NodeStreamWriter streamWriter, ExportConfiguration configuration) {
45          final ProgressMonitor monitor = configuration.getProgressMonitor();
46          monitor.begin();
47  
48          final NodeCreator node = RootNode.add(streamWriter);
49          final Context context = new Context();
50          for (Exporter exporter : exporters) {
51              exporter.export(node, configuration, context);
52          }
53  
54          monitor.end();
55      }
56  }