View Javadoc
1   /*
2    * Copyright 2013-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.dsml;
18  
19  import com.btmatthews.maven.plugins.ldap.AbstractFormatHandler;
20  import com.btmatthews.maven.plugins.ldap.FormatLogger;
21  import com.btmatthews.maven.plugins.ldap.FormatReader;
22  import com.btmatthews.maven.plugins.ldap.FormatWriter;
23  import org.dom4j.DocumentException;
24  import org.jaxen.JaxenException;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  
30  /**
31   * A {@link com.btmatthews.maven.plugins.ldap.FormatHandler} that imports/exports LDAP directory entries from/to
32   * DSML formatted files.
33   *
34   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
35   * @since 1.2.0
36   */
37  public final class DSMLFormatHandler extends AbstractFormatHandler {
38      /**
39       * Create the LDAP writer that will dump LDAP entries to a DSML file.
40       *
41       * @param outputStream The target output stream.
42       * @param logger       Used to log information or error messages.
43       * @return A {@link DSMLFormatWriter} object.
44       */
45      @Override
46      protected FormatWriter createWriter(final OutputStream outputStream,
47                                          final FormatLogger logger) {
48          try {
49              return new DSMLFormatWriter(outputStream);
50          } catch (final IOException e) {
51              logger.logError("Could not create and intialise the DSML writer", e);
52          }
53          return null;
54      }
55  
56      /**
57       * Create the LDAP reader that will load LDAP entries from a DSML file.
58       *
59       * @param inputStream The file input stream.
60       * @param logger      Used to log information or error messages.
61       * @return A {@link DSMLFormatReader} object.
62       */
63      @Override
64      protected FormatReader openReader(final InputStream inputStream,
65                                        final FormatLogger logger) {
66          try {
67              return new DSMLFormatReader(inputStream);
68          } catch (final DocumentException e) {
69              logger.logError("Error parsing DSML file", e);
70          } catch (final JaxenException e) {
71              logger.logError("Error processing DSML file", e);
72          } catch (final IOException e) {
73              logger.logError("Error reading DSML file", e);
74          }
75          return null;
76      }
77  }