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