| 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.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 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public abstract class AbstractLDAPDumperMojo |
| 44 | |
extends AbstractLDAPMojo |
| 45 | |
{ |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private String searchBase; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 4 | private String searchFilter = "(objectclass=*)"; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private File outputDirectory; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
private String filename; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public AbstractLDAPDumperMojo() |
| 82 | 4 | { |
| 83 | 4 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 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 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
protected abstract LDAPWriter openLDAPWriter(final PrintWriter writer); |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
protected abstract void closeLDAPWriter(final PrintWriter writer, |
| 135 | |
final LDAPWriter ldapWriter); |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 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 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 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 | |
} |