c1b7fbc11d51eaa39b8ce0446721fd9de104dc91
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.examples.servlet;
23
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;
34
35 /**
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.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 @WebListener
42 public class ApexServletListener implements ServletContextListener {
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServletListener.class);
44
45     // The Apex engine reference
46     private ApexMain apexMain;
47
48     /**
49      * {@inheritDoc}.
50      */
51     @Override
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");
56
57         LOGGER.info("Apex Servliet has been started, config-file={}, model-file={}", configFileName, modelFileName);
58
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);
65         }
66
67         // Construct the Apex command line arguments
68         final List<String> argsList = new ArrayList<>();
69         argsList.add("-config-file");
70         argsList.add(configFileName);
71
72         // Model file name is an optional parameter
73         if (modelFileName != null) {
74             argsList.add("-model-file");
75             argsList.add(modelFileName);
76         }
77
78         // Initialize apex
79         apexMain = new ApexMain(argsList.toArray(new String[argsList.size()]));
80     }
81
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
87     public void contextDestroyed(final ServletContextEvent servletContextEvent) {
88         // Shut Apex down
89         try {
90             apexMain.shutdown();
91             apexMain = null;
92         } catch (final ApexException e) {
93             LOGGER.error("Apex servlet stop did not execute normally", e);
94         }
95
96         LOGGER.info("Apex Servliet has been stopped");
97     }
98 }