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;
52 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
55 public void contextInitialized(final ServletContextEvent servletContextEvent) {
56 // The web.xml file contains the context parameters for the Apex engine
57 final String configFileName = servletContextEvent.getServletContext().getInitParameter("config-file");
58 final String modelFileName = servletContextEvent.getServletContext().getInitParameter("model-file");
60 LOGGER.info("Apex Servliet has been started, config-file={}, model-file={}", configFileName, modelFileName);
62 // Check that a configuration file have been specified
63 if (servletContextEvent.getServletContext().getInitParameter("config-file") == null) {
64 final String errorMessage =
65 "Apex servlet start failed, servlet parameter \"config-file\" has not been specified";
66 LOGGER.error("Apex servlet start failed, servlet parameter \"config-file\" has not been specified");
67 throw new ApexRuntimeException(errorMessage);
70 // Construct the Apex command line arguments
71 final List<String> argsList = new ArrayList<>();
72 argsList.add("-config-file");
73 argsList.add(configFileName);
75 // Model file name is an optional parameter
76 if (modelFileName != null) {
77 argsList.add("-model-file");
78 argsList.add(modelFileName);
82 apexMain = new ApexMain(argsList.toArray(new String[argsList.size()]));
89 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
92 public void contextDestroyed(final ServletContextEvent servletContextEvent) {
97 } catch (final ApexException e) {
98 final String errorMessage = "Apex servlet stop did not execute normally";
99 LOGGER.error(errorMessage, e);
102 LOGGER.info("Apex Servliet has been stopped");