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.IOException;
28  import java.io.InputStream;
29  
30  /**
31   * Implement the goal that loads a LDIF or DSML file into the LDAP directory server.
32   *
33   * @author <a href="mailto:brian.matthews@btmatthews.com">Brian Matthews</a>
34   * @since 1.2.0
35   */
36  @Mojo(name = "load")
37  public final class LoadMojo extends AbstractLDAPMojo {
38  
39      /**
40       * Handler used to load LDAP directory entries from DSML files.
41       */
42      private final FormatHandler dsmlFormatHandler = new DSMLFormatHandler();
43      /**
44       * Handler used to load LDAP directory entries from LDIF files.
45       */
46      private final FormatHandler ldifFormatHandler = new LDIFFormatHandler();
47      /**
48       * The LDIF and DSML files to be processed.
49       */
50      @SuppressWarnings("MismatchedReadAndWriteOfArray")
51      @Parameter(required = true)
52      private Source[] sources;
53      /**
54       * Indicates if the plugin should continue if there is an error. The default
55       * is to halt on error.
56       */
57      @Parameter(defaultValue = "false")
58      private boolean continueOnError;
59  
60      /**
61       * Execute the plugin goal iterating over the list of source files and loading the LDAP directory entries from
62       * each file using the appropriate handler.
63       *
64       * @throws MojoExecutionException If there was an error executing the plugin goal.
65       */
66      public void execute() throws MojoExecutionException {
67          if (!isSkip()){
68              final LDAPConnection connection = connect();
69              try {
70                  for (final Source source : sources) {
71                      try {
72                          getLog().info("Processing input source: " + source);
73                          final FormatHandler handler = getFormatHandler(source);
74                          if (handler == null) {
75                              getLog().warn("No handler for input source: " + source);
76                          } else {
77                              final InputStream inputStream = source.open();
78                              if (inputStream == null) {
79                                  if (!this.continueOnError) {
80                                      throw new MojoExecutionException("Cannot open source for reading: " + source);
81                                  } else {
82                                      getLog().warn("Skipping source that could not be opened for reading: " + source);
83                                  }
84                              } else {
85                                  try {
86                                      handler.load(connection, source.open(), continueOnError, this);
87                                  } finally {
88                                      inputStream.close();
89                                  }
90                              }
91                          }
92                      } catch (final IOException e) {
93                          if (!this.continueOnError) {
94                              throw new MojoExecutionException("Error closing input source: " + source, e);
95                          } else {
96                              this.getLog().warn("Ignoring error closing input source: " + source, e);
97                          }
98                      }
99                  }
100             } finally {
101                 connection.close();
102             }
103         }
104     }
105 
106     /**
107      * Determine which format handler to use for a source file. If the source file is DSML then {@link #dsmlFormatHandler}
108      * will be used and if it is LDIF then {@link #ldifFormatHandler}.
109      *
110      * @param source Describes the source file.
111      * @return The appropriate format handler or {@code null} if the source is not supported.
112      */
113     private FormatHandler getFormatHandler(final Source source) {
114         if (source instanceof Dsml) {
115             return dsmlFormatHandler;
116         } else if (source instanceof Ldif) {
117             return ldifFormatHandler;
118         } else {
119             return null;
120         }
121     }
122 }