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;
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28 import javax.servlet.annotation.WebListener;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
32 import org.onap.policy.apex.service.engine.main.ApexMain;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * This class is a listener that is called when the servlet is started and stopped. It brings up the Apex engine on
38 * servlet start and shuts it down on servlet stop.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class ApexServletListener implements ServletContextListener {
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServletListener.class);
46 // The Apex engine reference
47 private ApexMain apexMain;
53 public void contextInitialized(final ServletContextEvent servletContextEvent) {
54 // The web.xml file contains the context parameters for the Apex engine
55 final String configFileName = servletContextEvent.getServletContext().getInitParameter("config-file");
56 final String modelFileName = servletContextEvent.getServletContext().getInitParameter("model-file");
58 LOGGER.info("Apex Servliet has been started, config-file={}, model-file={}", configFileName, modelFileName);
60 // Check that a configuration file have been specified
61 if (servletContextEvent.getServletContext().getInitParameter("config-file") == null) {
62 final String errorMessage =
63 "Apex servlet start failed, servlet parameter \"config-file\" has not been specified";
64 LOGGER.error("Apex servlet start failed, servlet parameter \"config-file\" has not been specified");
65 throw new ApexRuntimeException(errorMessage);
68 // Construct the Apex command line arguments
69 final List<String> argsList = new ArrayList<>();
70 argsList.add("-config-file");
71 argsList.add(configFileName);
73 // Model file name is an optional parameter
74 if (modelFileName != null) {
75 argsList.add("-model-file");
76 argsList.add(modelFileName);
80 apexMain = new ApexMain(argsList.toArray(new String[argsList.size()]));
88 public void contextDestroyed(final ServletContextEvent servletContextEvent) {
93 } catch (final ApexException e) {
94 final String errorMessage = "Apex servlet stop did not execute normally";
95 LOGGER.error(errorMessage, e);
98 LOGGER.info("Apex Servliet has been stopped");