View Javadoc
1   /*
2    * Copyright 2013-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.unboundid;
18  
19  import com.btmatthews.maven.plugins.ldap.AbstractLDAPServer;
20  import com.btmatthews.utils.monitor.Logger;
21  import com.unboundid.ldap.listener.InMemoryDirectoryServer;
22  import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
23  import com.unboundid.ldap.listener.InMemoryListenerConfig;
24  import com.unboundid.ldap.sdk.Attribute;
25  import com.unboundid.ldap.sdk.DN;
26  import com.unboundid.ldap.sdk.Entry;
27  import com.unboundid.ldap.sdk.LDAPException;
28  import com.unboundid.ldif.LDIFReader;
29  
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  /**
35   * Implements an in-memory LDAP directory server using the
36   * <a href="https://www.unboundid.com/products/ldap-sdk/docs/in-memory-directory-server.php">UnboundID SDK</a>.
37   *
38   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
39   * @since 1.1.1
40   */
41  public final class UnboundIDServer extends AbstractLDAPServer {
42  
43      /**
44       * The default object classes used for the root DN entry.
45       */
46      private static final String[] DEFAULT_ROOT_OBJECT_CLASSES = { "domain", "top" };
47  
48      /**
49       * The in-memory instance of the UnboundID directory server.
50       */
51      private InMemoryDirectoryServer server;
52  
53      /**
54       * Configure and start the embedded UnboundID server creating the root DN and loading the LDIF seed data.
55       *
56       * @param logger Used to log informational and error messages.
57       */
58      @Override
59      public void start(final Logger logger) {
60          try {
61              logger.logInfo("Starting UnboundID server");
62              final InMemoryListenerConfig listenerConfig = InMemoryListenerConfig.createLDAPConfig("default", getServerPort());
63              final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(new DN(getRoot()));
64              config.setListenerConfigs(listenerConfig);
65              if (getAuthDn() != null) {
66                  config.addAdditionalBindCredentials(getAuthDn(), getPasswd());
67              }
68              server = new InMemoryDirectoryServer(config);
69              String[] objectClasses = getObjectClasses();
70              if (objectClasses == null) {
71                  objectClasses = DEFAULT_ROOT_OBJECT_CLASSES;
72              }
73              server.add(new Entry(getRoot(), new Attribute("objectclass", objectClasses)));
74              if (getLdifFile() != null) {
75                  final InputStream in = new FileInputStream(getLdifFile());
76                  try {
77                      final LDIFReader reader = new LDIFReader(in);
78                      server.importFromLDIF(false, reader);
79                  } finally {
80                      in.close();
81                  }
82              }
83              server.startListening();
84              logger.logInfo("Started UnboundID server");
85          } catch (final LDAPException e) {
86              e.printStackTrace();
87              logger.logError("Could not launch embedded UnboundID directory server", e);
88          } catch (final IOException e) {
89              e.printStackTrace();
90              logger.logError("Could not launch embedded UnboundID directory server", e);
91          }
92      }
93  
94      /**
95       * Shutdown the the embedded UnboundID server.
96       *
97       * @param logger Used to log informational and error messages.
98       */
99      @Override
100     public void stop(final Logger logger) {
101         logger.logInfo("Stopping UnboundID server");
102         server.shutDown(true);
103         logger.logInfo("Stopped UnboundID server");
104     }
105 }