Coverage Report - com.btmatthews.maven.plugins.ldap.mojos.AbstractLDAPDumperMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractLDAPDumperMojo
71%
29/41
100%
2/2
2.5
 
 1  
 /*
 2  
  * Copyright 2008 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.mojos;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileOutputStream;
 21  
 import java.io.IOException;
 22  
 import java.io.OutputStream;
 23  
 import java.io.PrintWriter;
 24  
 
 25  
 import org.apache.maven.plugin.MojoExecutionException;
 26  
 
 27  
 import netscape.ldap.LDAPConnection;
 28  
 import netscape.ldap.LDAPEntry;
 29  
 import netscape.ldap.LDAPException;
 30  
 import netscape.ldap.LDAPSearchResults;
 31  
 import netscape.ldap.LDAPv2;
 32  
 import netscape.ldap.util.LDAPWriter;
 33  
 
 34  
 /**
 35  
  * This is the abstract base class for all Mojos in the ldap-maven-plugin plugin
 36  
  * that dump content from the directory server. Concrete classes must implement
 37  
  * the getLDAPWriter() method to return the writer that will format the output
 38  
  * appropriately.
 39  
  * 
 40  
  * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
 41  
  * @version 1.0
 42  
  */
 43  
 public abstract class AbstractLDAPDumperMojo
 44  
     extends AbstractLDAPMojo
 45  
 {
 46  
     /**
 47  
      * The search base.
 48  
      * 
 49  
      * @parameter
 50  
      * @required
 51  
      */
 52  
     private String searchBase;
 53  
 
 54  
     /**
 55  
      * The search filter.
 56  
      * 
 57  
      * @parameter default-value="(objectclass=*)"
 58  
      * @required
 59  
      */
 60  4
     private String searchFilter = "(objectclass=*)";
 61  
 
 62  
     /**
 63  
      * The target output directory.
 64  
      * 
 65  
      * @parameter expression="${project.outputDirectory}"
 66  
      * @required
 67  
      */
 68  
     private File outputDirectory;
 69  
 
 70  
     /**
 71  
      * The output file name.
 72  
      * 
 73  
      * @parameter
 74  
      * @required
 75  
      */
 76  
     private String filename;
 77  
 
 78  
     /**
 79  
      * The default constructor.
 80  
      */
 81  
     public AbstractLDAPDumperMojo()
 82  4
     {
 83  4
     }
 84  
 
 85  
     /**
 86  
      * Execute the plugin goal.
 87  
      * 
 88  
      * @throws MojoExecutionException
 89  
      *             If something unexpected happens.
 90  
      */
 91  
     public final void execute()
 92  
         throws MojoExecutionException
 93  
     {
 94  4
         final LDAPConnection connection = this.connect();
 95  
         try
 96  
         {
 97  4
             final PrintWriter writer = this.getPrintWriter();
 98  2
             final LDAPWriter ldapWriter = this.openLDAPWriter(writer);
 99  2
             this.dump(connection, ldapWriter);
 100  2
             this.closeLDAPWriter(writer, ldapWriter);
 101  2
             writer.close();
 102  
         }
 103  
         finally
 104  
         {
 105  2
             try
 106  
             {
 107  4
                 connection.disconnect();
 108  
             }
 109  0
             catch (LDAPException e)
 110  
             {
 111  0
                 final String message = "Error disconnecting from the LDAP directory server";
 112  0
                 this.getLog().warn(message, e);
 113  6
             }
 114  0
         }
 115  2
     }
 116  
 
 117  
     /**
 118  
      * Create the LDAP writer that will dump LDAP entries in appropriate format.
 119  
      * 
 120  
      * @param writer
 121  
      *            The writer for the target output stream.
 122  
      * @return The LDAP writer.
 123  
      */
 124  
     protected abstract LDAPWriter openLDAPWriter(final PrintWriter writer);
 125  
 
 126  
     /**
 127  
      * Close the LDAP wrtier that was returned by openLDAPWriter.
 128  
      * 
 129  
      * @param writer
 130  
      *            The writer for the target output stream.
 131  
      * @param ldapWriter
 132  
      *            The LDAP writer.
 133  
      */
 134  
     protected abstract void closeLDAPWriter(final PrintWriter writer,
 135  
         final LDAPWriter ldapWriter);
 136  
 
 137  
     /**
 138  
      * Search the LDAP directory and dump the contents to a file.
 139  
      * 
 140  
      * @param connection
 141  
      *            The connection to the LDAP directory server.
 142  
      * @param writer
 143  
      *            The writer that is used to dump the content.
 144  
      * @throws MojoExecutionException
 145  
      *             If an error occurred generating the output file content.
 146  
      */
 147  
     private void dump(final LDAPConnection connection, final LDAPWriter writer)
 148  
         throws MojoExecutionException
 149  
     {
 150  
         try
 151  
         {
 152  2
             final LDAPSearchResults results = connection.search(
 153  
                 this.searchBase, LDAPv2.SCOPE_SUB, this.searchFilter, null,
 154  
                 false);
 155  4
             while (results.hasMoreElements())
 156  
             {
 157  2
                 final LDAPEntry entry = results.next();
 158  2
                 this.getLog().info("Dumping: " + entry.getDN());
 159  2
                 writer.printEntry(entry);
 160  2
             }
 161  
         }
 162  0
         catch (LDAPException e)
 163  
         {
 164  0
             final String message = "Error communicating with the LDAP directory";
 165  0
             this.getLog().error(message, e);
 166  0
             throw new MojoExecutionException(message, e);
 167  
         }
 168  0
         catch (IOException e)
 169  
         {
 170  0
             final String message = "Error dumping to the contents of the LDAP directory";
 171  0
             this.getLog().error(message, e);
 172  0
             throw new MojoExecutionException(message, e);
 173  2
         }
 174  2
     }
 175  
 
 176  
     /**
 177  
      * Create the output file and return a print writer encapsulating that file.
 178  
      * 
 179  
      * @return The print writer.
 180  
      * @throws MojoExecutionException
 181  
      *             If the output file could not be created.
 182  
      */
 183  
     private PrintWriter getPrintWriter()
 184  
         throws MojoExecutionException
 185  
     {
 186  
         final File file;
 187  4
         file = new File(this.outputDirectory, this.filename);
 188  
         try
 189  
         {
 190  4
             file.createNewFile();
 191  2
             final OutputStream outputStream = new FileOutputStream(file);
 192  2
             return new PrintWriter(outputStream);
 193  
         }
 194  2
         catch (IOException e)
 195  
         {
 196  2
             final String message = "I/O error creating output file: "
 197  
                 + file.getAbsolutePath();
 198  2
             this.getLog().error(message, e);
 199  2
             throw new MojoExecutionException(message, e);
 200  
         }
 201  
     }
 202  
 }