View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.buildtools;
20  
21  import com.unboundid.ldap.listener.Base64PasswordEncoderOutputFormatter;
22  import com.unboundid.ldap.listener.InMemoryDirectoryServer;
23  import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
24  import com.unboundid.ldap.listener.InMemoryListenerConfig;
25  import com.unboundid.ldap.listener.UnsaltedMessageDigestInMemoryPasswordEncoder;
26  import com.unboundid.ldap.sdk.schema.Schema;
27  import java.net.InetAddress;
28  import java.security.MessageDigest;
29  import javax.servlet.ServletContextEvent;
30  import javax.servlet.ServletContextListener;
31  import javax.servlet.annotation.WebListener;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.context.ApplicationContext;
35  import org.springframework.web.context.support.WebApplicationContextUtils;
36  
37  /**
38   * Start and stop an in-memory LDAP server instance alongside with Servlet Context.
39   */
40  @WebListener
41  public class LDAPStartStopListener implements ServletContextListener {
42  
43      private static final Logger LOG = LoggerFactory.getLogger(LDAPStartStopListener.class);
44  
45      private InMemoryDirectoryServer ldapServer;
46  
47      @Override
48      public void contextInitialized(final ServletContextEvent sce) {
49          try {
50              ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
51  
52              InMemoryDirectoryServerConfig config =
53                      new InMemoryDirectoryServerConfig(ctx.getEnvironment().getProperty("testds.rootDn"));
54  
55              config.addAdditionalBindCredentials(
56                      ctx.getEnvironment().getProperty("testds.bindDn"),
57                      ctx.getEnvironment().getProperty("testds.password"));
58  
59              InMemoryListenerConfig listenerConfig = InMemoryListenerConfig.createLDAPConfig(
60                      "test-listener",
61                      InetAddress.getLoopbackAddress(),
62                      Integer.parseInt(ctx.getEnvironment().getProperty("testds.port")),
63                      null);
64              config.setListenerConfigs(listenerConfig);
65  
66              config.setSchema(Schema.getDefaultStandardSchema());
67  
68              config.setPasswordEncoders(
69                      new UnsaltedMessageDigestInMemoryPasswordEncoder(
70                              "{SHA}",
71                              Base64PasswordEncoderOutputFormatter.getInstance(),
72                              MessageDigest.getInstance("SHA-1")));
73  
74              ldapServer = new InMemoryDirectoryServer(config);
75              ldapServer.importFromLDIF(false, ctx.getResource("classpath:/content.ldif").getFile());
76              ldapServer.startListening();
77          } catch (Exception e) {
78              LOG.error("Fatal error in context init", e);
79              throw new RuntimeException(e);
80          }
81      }
82  
83      @Override
84      public void contextDestroyed(final ServletContextEvent sce) {
85          if (ldapServer != null) {
86              ldapServer.shutDown(true);
87          }
88      }
89  }