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.opendj;
18  
19  import com.btmatthews.maven.plugins.ldap.AbstractLDAPServer;
20  import com.btmatthews.utils.monitor.Logger;
21  import org.forgerock.opendj.ldap.*;
22  import org.forgerock.opendj.ldif.LDIFEntryReader;
23  
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  /**
29   * Implements an in-memory LDAP directory server using OpenDJ SDK. See
30   * <a href="http://opendj.forgerock.org/opendj-ldap-sdk-examples/xref/org/forgerock/opendj/examples/Server.html">here</a>
31   * for original code.
32   *
33   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
34   * @since 1.1.1
35   */
36  public final class OpenDJServer extends AbstractLDAPServer {
37  
38      /**
39       * The listener that accepts and dispatches LDAP requests.
40       */
41      private LDAPListener listener;
42  
43      /**
44       * Start the OpenDJ in-memory directory server.
45       *
46       * @param logger Used for logging.
47       */
48      @Override
49      public void start(final Logger logger) {
50          try {
51              logger.logInfo("Starting OpenDJ server");
52              final MemoryBackend backend;
53              if (getLdifFile() == null) {
54                  backend = new MemoryBackend();
55              } else {
56                  final InputStream inputStream = new FileInputStream(getLdifFile());
57                  final LDIFEntryReader reader = new LDIFEntryReader(inputStream);
58                  backend = new MemoryBackend(reader);
59              }
60              final ServerConnectionFactory<LDAPClientContext, Integer> connectionHandler = Connections.newServerConnectionFactory(backend);
61              final LDAPListenerOptions options = new LDAPListenerOptions().setBacklog(4096);
62              listener = new LDAPListener("localhost", getServerPort(), connectionHandler, options);
63              logger.logInfo("Started OpenDJ server");
64          } catch (final IOException e) {
65              logger.logError("Error starting OpenDJ server", e);
66          }
67      }
68  
69      /**
70       * Stop the OpenDJ in-memory directory server.
71       *
72       * @param logger User for logging.
73       */
74      @Override
75      public void stop(final Logger logger) {
76          logger.logInfo("Stopping OpenDJ server");
77          if (listener != null) {
78              listener.close();
79          }
80          logger.logInfo("Stopped OpenDJ server");
81      }
82  }