View Javadoc
1   /*
2    * Copyright 2008-2016 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.ldap.mojo;
18  
19  import com.btmatthews.maven.plugins.ldap.FormatHandler;
20  import com.btmatthews.maven.plugins.ldap.dsml.DSMLFormatHandler;
21  import com.btmatthews.maven.plugins.ldap.ldif.LDIFFormatHandler;
22  import com.unboundid.ldap.sdk.LDAPConnection;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  
27  import java.io.*;
28  
29  /**
30   * This is the abstract base class for all Mojos in the ldap-maven-plugin plugin
31   * that dump content from the directory server. Concrete classes must implement
32   * the getLDAPWriter() method to return the writer that will format the output
33   * appropriately.
34   *
35   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
36   * @since 1.2.0
37   */
38  @Mojo(name = "dump")
39  public final class DumpMojo extends AbstractLDAPMojo {
40      /**
41       * Handler used to dump LDAP directory entries to DSML files.
42       */
43      private final FormatHandler dsmlFormatHandler = new DSMLFormatHandler();
44      /**
45       * Handler used to dump LDAP directory entries to LDIF files.
46       */
47      private final FormatHandler ldifFormatHandler = new LDIFFormatHandler();
48      /**
49       * The search base.
50       */
51      @Parameter(required = true)
52      private String searchBase;
53      /**
54       * The search filter.
55       */
56      @Parameter(defaultValue = "(objectclass=*)", required = true)
57      private String searchFilter;
58      /**
59       * The target output directory.
60       */
61      @Parameter(defaultValue = "${project.build.directory}", required = true)
62      private File outputDirectory;
63      /**
64       * The output file name.
65       */
66      @Parameter(required = true)
67      private String filename;
68      /**
69       * The output file format.
70       * <ul>
71       * <li>ldif</li>
72       * <li>dsml</li>
73       * </ul>
74       */
75      @Parameter(defaultValue = "ldif")
76      private String format;
77  
78      /**
79       * Execute the plugin goal by dumping the matching directory entries to a file in the specified format.
80       *
81       * @throws MojoExecutionException If something unexpected happens.
82       */
83      public final void execute() throws MojoExecutionException {
84          final File outputFile = new File(outputDirectory, filename);
85          if (outputDirectory.exists() || outputDirectory.mkdirs()) {
86              try {
87                  final OutputStream outputStream = new FileOutputStream(outputFile);
88                  try {
89                      final LDAPConnection connection = connect();
90                      try {
91                          final FormatHandler handler = getFormatHandler();
92                          if (handler != null) {
93                              handler.dump(connection, searchBase, searchFilter, outputStream, this);
94                          }
95                      } finally {
96                          connection.close();
97                      }
98                  } finally {
99                      try {
100                         outputStream.close();
101                     } catch (final IOException e) {
102                     }
103                 }
104             } catch (final FileNotFoundException e) {
105             }
106         }
107     }
108 
109     /**
110      * Get the appropriate format handler based on the output file format.
111      *
112      * @return The appropriate file handler or {@code null} if the output file format is not supported.
113      */
114     private FormatHandler getFormatHandler() {
115         if (format.equals("dsml")) {
116             return dsmlFormatHandler;
117         } else if (format.equals("ldif")) {
118             return ldifFormatHandler;
119         } else {
120             return null;
121         }
122     }
123 }