View Javadoc
1   /*
2    * Copyright 2008-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.mojo;
18  
19  import com.btmatthews.maven.plugins.ldap.FormatLogger;
20  import com.unboundid.ldap.sdk.LDAPConnection;
21  import com.unboundid.ldap.sdk.LDAPException;
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.Parameter;
25  
26  /**
27   * This is the abstract base class for all Mojos in this the ldap-maven-plugin
28   * plugin. It defines the properties for the LDAP directory server connection
29   * and provides a method to connect to the LDAP directory server.
30   *
31   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
32   * @since 1.0.0
33   */
34  public abstract class AbstractLDAPMojo extends AbstractMojo implements FormatLogger {
35      /**
36       * The default host name.
37       */
38      private static final String DEFAULT_HOST = "localhost";
39      /**
40       * The default port number for LDAP servers.
41       */
42      private static final int DEFAULT_PORT = 389;
43      /**
44       * The host name of the LDAP directory server. Defaults to localhost.
45       */
46      @Parameter(defaultValue = "localhost")
47      private String host = AbstractLDAPMojo.DEFAULT_HOST;
48      /**
49       * The port number of the LDAP directory server. Defaults to 389.
50       */
51      @Parameter(defaultValue = "389")
52      private int port = AbstractLDAPMojo.DEFAULT_PORT;
53      /**
54       * The distinguished name used if authentication is required.
55       */
56      @Parameter(required = true)
57      private String authDn;
58      /**
59       * The password used if authentication is required.
60       */
61      @Parameter(required = true)
62      private String passwd;
63      /**
64       * The connection timeout.
65       */
66      @Parameter(defaultValue = "5000")
67      private int connectionTimeout = 5000;
68      /**
69       * The maximum number of connection attempts before failing.
70       */
71      @Parameter(defaultValue = "3")
72      private int connectionRetries = 3;
73      /**
74       * To skip execution of the plugin
75       */
76      @Parameter(defaultValue = "false")
77      private boolean skip;
78  
79      /**
80       * Connect to the LDAP directory server. The connection attempt will be retried {@link #connectionRetries} times
81       * and the connection time is set to {@link #connectionTimeout}.
82       *
83       * @return The connection object.
84       * @throws MojoExecutionException If the connection to the LDAP directory server failed.
85       */
86      protected final LDAPConnection connect() throws MojoExecutionException {
87          final LDAPConnection connection = new LDAPConnection();
88          int i = 0;
89          while (i < connectionRetries) {
90              long start = System.currentTimeMillis();
91              try {
92                  this.getLog().info("Attempting to connect ot LDAP directory server (" + host + ":" + port + ")");
93                  connection.connect(host, port, connectionTimeout);
94                  break;
95              } catch (final LDAPException e) {
96                  final String message = "Could not connect to LDAP directory server (" + host + ":" + port + ")";
97                  this.getLog().error(message, e);
98                  if (i++ < connectionRetries) {
99                      long time = System.currentTimeMillis() - start;
100                     if (time < connectionTimeout) {
101                         try {
102                             Thread.sleep(connectionTimeout - time);
103                         } catch (final InterruptedException e1) {
104                             throw new MojoExecutionException(message, e1);
105                         }
106                     }
107                 } else {
108                     throw new MojoExecutionException(message, e);
109                 }
110             }
111         }
112         try {
113             connection.bind(authDn, passwd);
114         } catch (final LDAPException e) {
115             throw new MojoExecutionException("Could not bind to LDAP directory server as " + authDn, e);
116         }
117         return connection;
118     }
119 
120     public boolean isSkip(){
121         return this.skip;
122     }
123 
124     /**
125      * Write a information message to the Maven log.
126      *
127      * @param message The information message.
128      */
129     public void logInfo(final String message) {
130         getLog().info(message);
131     }
132 
133     /**
134      * Write an error message to the Maven log.
135      *
136      * @param message The error message.
137      */
138     public void logError(final String message) {
139         getLog().error(message);
140     }
141 
142     /**
143      * Write an error message and stack trace to the Maven log.
144      *
145      * @param message   The error message.
146      * @param exception The exception containing the stack trace.
147      */
148     public void logError(final String message, final Throwable exception) {
149         getLog().error(message, exception);
150     }
151 }