1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }