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  
20  import com.btmatthews.maven.plugins.ldap.LDAPServer;
21  import com.btmatthews.utils.monitor.mojo.AbstractRunMojo;
22  import org.apache.maven.plugins.annotations.LifecyclePhase;
23  import org.apache.maven.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  
26  import java.io.File;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  /**
31   * This Mojo implements the run goal which launches an embedded LDAP
32   * directory server.
33   *
34   * @author <a href="mailto:brian.matthews@btmatthews.com">Brian Matthews</a>
35   * @since 1.1.0
36   */
37  @Mojo(
38          name = "run",
39          defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST,
40          configurator = "include-server-dependencies")
41  public final class RunLDAPMojo extends AbstractRunMojo {
42  
43      /**
44       * The server can be one of:
45       * <ul>
46       * <li>apacheds</li> - Use ApacheDS 1.5.5
47       * <li>opendj</li> - Use OpenDJ 2.6.0
48       * <li>unboundid</li> - Use UnboundID 3.1.0
49       * <li>dependency-[serverType]</li> - Use custom server included as plugin dependency and identified by <b>serverType</b>
50       * <li>dependency-[serverType]-[groupId]:[artifactId]:[version]</li> - Use custom server specified by the GAV coordinates and identified by <b>serverType</b>
51       * </ul>
52       */
53      @Parameter(property = "ldap.type", defaultValue = "unboundid")
54      private String serverType;
55      /**
56       * The identity of the admin account for tbe directory server.
57       */
58      @Parameter(property = "ldap.authDn", defaultValue = "uid=admin,ou=system")
59      private String authDn;
60      /**
61       * The credentials for the admin account of the directory server.
62       */
63      @Parameter(property = "ldap.passwd", defaultValue = "secret")
64      private String passwd;
65      /**
66       * The root DN for the LDAP server.
67       */
68      @Parameter(property = "ldap.root", required = true)
69      private String rootDn;
70      /**
71       *
72       */
73      @Parameter(property = "ldap.objectClasses", required = false)
74      private String objectClasses;
75      /**
76       * An optional LDIF file that can be used to seed the embedded LDAP server.
77       */
78      @Parameter(property = "ldap.ldif", required = false)
79      private File ldifFile;
80      /**
81       * The port for the LDAP server.
82       */
83      @Parameter(property = "ldap.port", defaultValue = "389")
84      private int ldapPort;
85      /**
86       * The build target directory.
87       */
88      @Parameter(defaultValue = "${project.build.directory}", required = true)
89      private File outputDirectory;
90  
91      /**
92       * Get the server type.
93       *
94       * @return The server type.
95       */
96      @Override
97      public String getServerType() {
98          if (serverType.startsWith("dependency-")) {
99              return serverType.substring(11);
100         }
101         if (serverType.startsWith("gav-")) {
102             int index = serverType.indexOf("-", 4);
103             if (index > 0) {
104                 return serverType.substring(4, index);
105             }
106         }
107         return serverType;
108     }
109 
110     /**
111      * Get the embedded LDAP directory server configuration.
112      *
113      * @return A {@link Map} containing the server configuration.
114      */
115     @Override
116     public Map<String, Object> getServerConfig() {
117         final Map<String, Object> config = new HashMap<String, Object>();
118         config.put(LDAPServer.ROOT, rootDn);
119         if (objectClasses != null) {
120             config.put(LDAPServer.OBJECT_CLASSES, objectClasses.split(","));
121         }
122         config.put(LDAPServer.WORK_DIR, new File(outputDirectory, serverType));
123         if (ldifFile != null) {
124             config.put(LDAPServer.LDIF_FILE, ldifFile);
125         }
126         config.put(LDAPServer.LDAP_PORT, ldapPort);
127         config.put(LDAPServer.AUTH_DN, authDn);
128         config.put(LDAPServer.PASSWD, passwd);
129         return config;
130     }
131 }
132 
133 
134