Coverage Report - com.btmatthews.maven.plugins.ldap.mojos.LDIFLoaderMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
LDIFLoaderMojo
58%
34/59
53%
8/15
3.286
 
 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.DataInputStream;
 20  
 import java.io.File;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.FileNotFoundException;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 
 28  
 import netscape.ldap.LDAPAttribute;
 29  
 import netscape.ldap.LDAPAttributeSet;
 30  
 import netscape.ldap.LDAPConnection;
 31  
 import netscape.ldap.LDAPEntry;
 32  
 import netscape.ldap.LDAPException;
 33  
 import netscape.ldap.util.LDIF;
 34  
 import netscape.ldap.util.LDIFAddContent;
 35  
 import netscape.ldap.util.LDIFAttributeContent;
 36  
 import netscape.ldap.util.LDIFContent;
 37  
 import netscape.ldap.util.LDIFModDNContent;
 38  
 import netscape.ldap.util.LDIFModifyContent;
 39  
 import netscape.ldap.util.LDIFRecord;
 40  
 
 41  
 /**
 42  
  * Implement the goal that loads a LDIF file.
 43  
  * 
 44  
  * @author <a href="mailto:brian.matthews@btmatthews.com">Brian Matthews</a>
 45  
  * @version 1.0
 46  
  * @goal ldif-load
 47  
  */
 48  
 public final class LDIFLoaderMojo
 49  
     extends AbstractLDAPMojo
 50  
 {
 51  
     /**
 52  
      * The LDIF files to be processed.
 53  
      * 
 54  
      * @parameter
 55  
      * @required
 56  
      */
 57  
     private File[] ldapFiles;
 58  
 
 59  
     /**
 60  
      * Indicates if the plugin should continue if there is an error. The default
 61  
      * is to halt on error.
 62  
      * 
 63  
      * @parameter default-value="false"
 64  
      */
 65  
     private boolean continueOnError;
 66  
 
 67  
     /**
 68  
      * The default constructor.
 69  
      */
 70  
     public LDIFLoaderMojo()
 71  2
     {
 72  2
     }
 73  
 
 74  
     /**
 75  
      * Execute the plugin goal.
 76  
      * 
 77  
      * @throws MojoExecutionException
 78  
      *             If something unexpected happens.
 79  
      */
 80  
     public void execute()
 81  
         throws MojoExecutionException
 82  
     {
 83  
         // Connect to the LDAP server
 84  
 
 85  2
         final LDAPConnection connection = this.connect();
 86  
 
 87  
         // Process the LDIF files
 88  
 
 89  
         try
 90  
         {
 91  5
             for (int i = 0; i < this.ldapFiles.length; ++i)
 92  
             {
 93  
                 try
 94  
                 {
 95  4
                     this.getLog().info("Processing " + this.ldapFiles[i]);
 96  4
                     final InputStream inputStream = new FileInputStream(
 97  
                         this.ldapFiles[i]);
 98  4
                     final DataInputStream dataInputStream = new DataInputStream(
 99  
                         inputStream);
 100  4
                     final LDIF ldif = new LDIF(dataInputStream);
 101  4
                     LDIFRecord record = ldif.nextRecord();
 102  4
                     while (record != null)
 103  
                     {
 104  4
                         this.processRecord(connection, record);
 105  0
                         record = ldif.nextRecord();
 106  
                     }
 107  
                 }
 108  4
                 catch (LDAPException e)
 109  
                 {
 110  4
                     if (!this.continueOnError)
 111  
                     {
 112  1
                         throw new MojoExecutionException("Error processing: "
 113  
                             + this.ldapFiles[i], e);
 114  
                     }
 115  
                     else
 116  
                     {
 117  3
                         this.getLog().warn(
 118  
                             "Ignoring error processing: " + this.ldapFiles[i],
 119  
                             e);
 120  
                     }
 121  
                 }
 122  0
                 catch (FileNotFoundException e)
 123  
                 {
 124  0
                     if (!this.continueOnError)
 125  
                     {
 126  0
                         throw new MojoExecutionException("File not found: "
 127  
                             + this.ldapFiles[i], e);
 128  
                     }
 129  
                     else
 130  
                     {
 131  0
                         this.getLog().warn(
 132  
                             "Skipping missing file: " + this.ldapFiles[i], e);
 133  
                     }
 134  
                 }
 135  0
                 catch (IOException e)
 136  
                 {
 137  0
                     if (!this.continueOnError)
 138  
                     {
 139  0
                         throw new MojoExecutionException("Error reading from: "
 140  
                             + this.ldapFiles[i], e);
 141  
                     }
 142  
                     else
 143  
                     {
 144  0
                         this.getLog()
 145  
                             .warn(
 146  
                                 "Ignoring error reading from: "
 147  
                                     + this.ldapFiles[i], e);
 148  
                     }
 149  3
                 }
 150  
             }
 151  
         }
 152  
         finally
 153  
         {
 154  
 
 155  
             // Disconnect from the LDAP Server
 156  
 
 157  1
             try
 158  
             {
 159  2
                 connection.disconnect();
 160  
             }
 161  0
             catch (LDAPException e)
 162  
             {
 163  0
                 this.getLog().warn(
 164  
                     "Ignoring error disconnecting from the LDAP server", e);
 165  3
             }
 166  0
         }
 167  1
     }
 168  
 
 169  
     /**
 170  
      * Process a parsed LDIF record. Delegate to the appropriate handler for
 171  
      * each operation.
 172  
      * 
 173  
      * @param connection
 174  
      *            The connection to the LDAP server.
 175  
      * @param record
 176  
      *            The parsed record.
 177  
      * @throws LDAPException
 178  
      *             If there was an LDAP error.
 179  
      */
 180  
     private void processRecord(final LDAPConnection connection,
 181  
         final LDIFRecord record)
 182  
         throws LDAPException
 183  
     {
 184  4
         final LDIFContent content = record.getContent();
 185  4
         final int contentType = content.getType();
 186  4
         switch (contentType)
 187  
         {
 188  
             case LDIFContent.MODDN_CONTENT:
 189  1
                 this.renameEntry(connection, record.getDN(),
 190  
                     (LDIFModDNContent)content);
 191  0
                 break;
 192  
             case LDIFContent.MODIFICATION_CONTENT:
 193  0
                 this.modifyEntry(connection, record.getDN(),
 194  
                     (LDIFModifyContent)content);
 195  0
                 break;
 196  
             case LDIFContent.DELETE_CONTENT:
 197  2
                 this.deleteEntry(connection, record.getDN());
 198  0
                 break;
 199  
             case LDIFContent.ADD_CONTENT:
 200  1
                 this.addEntry(connection, record.getDN(),
 201  
                     ((LDIFAddContent)content).getAttributes());
 202  0
                 break;
 203  
             default:
 204  0
                 this.addEntry(connection, record.getDN(),
 205  
                     ((LDIFAttributeContent)content).getAttributes());
 206  
                 break;
 207  
         }
 208  0
     }
 209  
 
 210  
     /**
 211  
      * Handle an LDIF add record.
 212  
      * 
 213  
      * @param connection
 214  
      *            The connection to the LDAP server.
 215  
      * @param entryDn
 216  
      *            The distinguished name of the record being added.
 217  
      * @param attributes
 218  
      *            The attributes.
 219  
      * @throws LDAPException
 220  
      *             If the operation failed.
 221  
      */
 222  
     private void addEntry(final LDAPConnection connection,
 223  
         final String entryDn, final LDAPAttribute[] attributes)
 224  
         throws LDAPException
 225  
     {
 226  1
         this.getLog().info("Add Entry: " + entryDn);
 227  1
         final LDAPAttributeSet attributeSet = new LDAPAttributeSet(attributes);
 228  1
         final LDAPEntry entry = new LDAPEntry(entryDn, attributeSet);
 229  1
         connection.add(entry);
 230  0
     }
 231  
 
 232  
     /**
 233  
      * Handle an LDIF delete record.
 234  
      * 
 235  
      * @param connection
 236  
      *            The connection to the LDAP server.
 237  
      * @param entryDn
 238  
      *            The distinguished name of the record to be deleted.
 239  
      * @throws LDAPException
 240  
      *             If the operation failed.
 241  
      */
 242  
     private void deleteEntry(final LDAPConnection connection,
 243  
         final String entryDn)
 244  
         throws LDAPException
 245  
     {
 246  2
         this.getLog().info("Deleting " + entryDn);
 247  2
         connection.delete(entryDn);
 248  0
     }
 249  
 
 250  
     /**
 251  
      * Handle an LDIF modification record.
 252  
      * 
 253  
      * @param connection
 254  
      *            The connection to the LDAP server.
 255  
      * @param entryDn
 256  
      *            The distinguished name of the record being modified.
 257  
      * @param content
 258  
      *            The details of the modification operation.
 259  
      * @throws LDAPException
 260  
      *             If the operation failed.
 261  
      */
 262  
     private void modifyEntry(final LDAPConnection connection,
 263  
         final String entryDn, final LDIFModifyContent content)
 264  
         throws LDAPException
 265  
     {
 266  0
         this.getLog().info("Modify Entry: " + entryDn);
 267  0
         connection.modify(entryDn, content.getModifications());
 268  0
     }
 269  
 
 270  
     /**
 271  
      * Handle a LDIF distinguished name change operation.
 272  
      * 
 273  
      * @param connection
 274  
      *            The connection to the LDAP server.
 275  
      * @param entryDn
 276  
      *            The distinguished name of the record being renamed.
 277  
      * @param content
 278  
      *            The details of the name change operation.
 279  
      * @throws LDAPException
 280  
      *             If the operation failed.
 281  
      */
 282  
     private void renameEntry(final LDAPConnection connection,
 283  
         final String entryDn, final LDIFModDNContent content)
 284  
         throws LDAPException
 285  
     {
 286  1
         this.getLog().info("Rename Entry: " + entryDn);
 287  1
         connection.rename(entryDn, content.getRDN(), content.getNewParent(),
 288  
             content.getDeleteOldRDN());
 289  0
     }
 290  
 }