2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.examples.servlet;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28 import javax.servlet.annotation.WebListener;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
31 import org.onap.policy.apex.service.engine.main.ApexMain;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * This class is a listener that is called when the servlet is started and stopped. It brings up the Apex engine on
37 * servlet start and shuts it down on servlet stop.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class ApexServletListener implements ServletContextListener {
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServletListener.class);
45 // The Apex engine reference
46 private ApexMain apexMain;
52 public void contextInitialized(final ServletContextEvent servletContextEvent) {
53 // The web.xml file contains the context parameters for the Apex engine
54 final String configFileName = servletContextEvent.getServletContext().getInitParameter("config-file");
55 final String modelFileName = servletContextEvent.getServletContext().getInitParameter("model-file");
57 LOGGER.info("Apex Servliet has been started, config-file={}, model-file={}", configFileName, modelFileName);
59 // Check that a configuration file have been specified
60 if (servletContextEvent.getServletContext().getInitParameter("config-file") == null) {
61 final var errorMessage =
62 "Apex servlet start failed, servlet parameter \"config-file\" has not been specified";
63 LOGGER.error("Apex servlet start failed, servlet parameter \"config-file\" has not been specified");
64 throw new ApexRuntimeException(errorMessage);
67 // Construct the Apex command line arguments
68 final List<String> argsList = new ArrayList<>();
69 argsList.add("-config-file");
70 argsList.add(configFileName);
72 // Model file name is an optional parameter
73 if (modelFileName != null) {
74 argsList.add("-model-file");
75 argsList.add(modelFileName);
79 apexMain = new ApexMain(argsList.toArray(new String[argsList.size()]));
87 public void contextDestroyed(final ServletContextEvent servletContextEvent) {
92 } catch (final ApexException e) {
93 LOGGER.error("Apex servlet stop did not execute normally", e);
96 LOGGER.info("Apex Servliet has been stopped");