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