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 java.io.IOException;
22 import java.net.URL;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28 import javax.servlet.annotation.WebListener;
29 import org.identityconnectors.common.security.SecurityUtil;
30 import org.identityconnectors.framework.impl.api.local.ThreadClassLoaderManager;
31 import org.identityconnectors.framework.server.ConnectorServer;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.core.io.Resource;
36 import org.springframework.web.context.WebApplicationContext;
37 import org.springframework.web.context.support.WebApplicationContextUtils;
38
39 @WebListener
40 public class ConnectorServerStartStopListener implements ServletContextListener {
41
42 private static final Logger LOG = LoggerFactory.getLogger(ConnectorServerStartStopListener.class);
43
44 private static final String SERVER = "ConnIdConnectorServer";
45
46
47
48
49
50
51 private static List<URL> getBundleURLs(final ApplicationContext ctx) {
52 final List<URL> bundleURLs = new ArrayList<>();
53
54 try {
55 for (Resource bundle : ctx.getResources("classpath*:/bundles/*.jar")) {
56 bundleURLs.add(bundle.getURL());
57 }
58 } catch (IOException e) {
59 LOG.error("While getting bundled ConnId bundles", e);
60 }
61
62 LOG.info("ConnId bundles loaded: " + bundleURLs);
63
64 return bundleURLs;
65 }
66
67 @Override
68 public void contextInitialized(final ServletContextEvent sce) {
69 ConnectorServer server = ConnectorServer.newInstance();
70 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
71 try {
72 server.setPort(Objects.requireNonNull(ctx).getEnvironment()
73 .getProperty("testconnectorserver.port", Integer.class));
74
75 server.setBundleURLs(getBundleURLs(ctx));
76
77 server.setKeyHash(SecurityUtil.computeBase64SHA1Hash(
78 ctx.getEnvironment().getProperty("testconnectorserver.key", String.class).toCharArray()));
79
80 server.start();
81 LOG.info("ConnId connector server listening on port {}", server.getPort());
82 } catch (Exception e) {
83 LOG.error("Could not start ConnId connector server", e);
84 }
85
86 sce.getServletContext().setAttribute(SERVER, server);
87 }
88
89 @Override
90 public void contextDestroyed(final ServletContextEvent sce) {
91 final ConnectorServer server = (ConnectorServer) sce.getServletContext().getAttribute(SERVER);
92 if (server != null && server.isStarted()) {
93 server.stop();
94 }
95 ThreadClassLoaderManager.clearInstance();
96 }
97 }