| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public final class LDIFLoaderMojo |
| 49 | |
extends AbstractLDAPMojo |
| 50 | |
{ |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
private File[] ldapFiles; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
private boolean continueOnError; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public LDIFLoaderMojo() |
| 71 | 2 | { |
| 72 | 2 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public void execute() |
| 81 | |
throws MojoExecutionException |
| 82 | |
{ |
| 83 | |
|
| 84 | |
|
| 85 | 2 | final LDAPConnection connection = this.connect(); |
| 86 | |
|
| 87 | |
|
| 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 | |
|
| 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 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 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 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 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 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 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 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 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 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 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 | |
} |