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 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       * Build list of URLs from bundles available in the classpath.
48       *
49       * @param ctx ApplicationContext needed for getting ConnId jar bundles URLs
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  }