Changes for checkstyle 8.32
[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 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;
33
34 /**
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.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 @WebListener
41 public class ApexServletListener implements ServletContextListener {
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServletListener.class);
43
44     // The Apex engine reference
45     private ApexMain apexMain;
46
47     /**
48      * {@inheritDoc}.
49      */
50     @Override
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");
55
56         LOGGER.info("Apex Servliet has been started, config-file={}, model-file={}", configFileName, modelFileName);
57
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);
64         }
65
66         // Construct the Apex command line arguments
67         final List<String> argsList = new ArrayList<>();
68         argsList.add("-config-file");
69         argsList.add(configFileName);
70
71         // Model file name is an optional parameter
72         if (modelFileName != null) {
73             argsList.add("-model-file");
74             argsList.add(modelFileName);
75         }
76
77         // Initialize apex
78         apexMain = new ApexMain(argsList.toArray(new String[argsList.size()]));
79     }
80
81
82     /**
83      * {@inheritDoc}.
84      */
85     @Override
86     public void contextDestroyed(final ServletContextEvent servletContextEvent) {
87         // Shut Apex down
88         try {
89             apexMain.shutdown();
90             apexMain = null;
91         } catch (final ApexException e) {
92             final String errorMessage = "Apex servlet stop did not execute normally";
93             LOGGER.error(errorMessage, e);
94         }
95
96         LOGGER.info("Apex Servliet has been stopped");
97     }
98 }