2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.client.monitoring.rest;
24 import java.io.PrintStream;
25 import org.slf4j.ext.XLogger;
26 import org.slf4j.ext.XLoggerFactory;
29 * The main class for Apex Restful Monitoring.
31 * @author Michael Watkins (michael.watkins@ericsson.com)
33 public class ApexMonitoringRestMain {
34 // Logger for this class
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexMonitoringRestMain.class);
37 // Recurring string constants
38 private static final String REST_ENDPOINT_PREFIX = "Apex Services REST endpoint (";
41 public enum ServicesState {
48 private ServicesState state = ServicesState.STOPPED;
50 // The parameters for the client
51 private ApexMonitoringRestParameters parameters = null;
53 // Output and error streams for messages
54 private final PrintStream outStream;
56 // The Apex services client this class is running
57 private ApexMonitoringRest apexMonitoringRest = null;
60 * Constructor, kicks off the rest service.
62 * @param args The command line arguments for the RESTful service
63 * @param outStream The stream for output messages
65 public ApexMonitoringRestMain(final String[] args, final PrintStream outStream) {
66 // Save the streams for output and error
67 this.outStream = outStream;
69 // Client parameter parsing
70 final ApexMonitoringRestParameterParser parser = new ApexMonitoringRestParameterParser();
73 // Get and check the parameters
74 parameters = parser.parse(args);
75 } catch (final ApexMonitoringRestParameterException e) {
76 throw new ApexMonitoringRestParameterException(
77 REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, " + e.getMessage() + '\n'
78 + parser.getHelp(ApexMonitoringRestMain.class.getName()),
82 if (parameters.isHelpSet()) {
83 throw new ApexMonitoringRestParameterException(parser.getHelp(ApexMonitoringRestMain.class.getName()));
86 // Validate the parameters
87 final String validationMessage = parameters.validate();
88 if (validationMessage.length() > 0) {
89 throw new ApexMonitoringRestParameterException(
90 REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, " + validationMessage + '\n'
91 + parser.getHelp(ApexMonitoringRestMain.class.getName()));
94 state = ServicesState.READY;
98 * Initialize the rest service.
101 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at " + parameters.getBaseUri().toString()
105 state = ServicesState.INITIALIZING;
107 // Start the REST service
108 apexMonitoringRest = new ApexMonitoringRest(parameters);
110 // Add a shutdown hook to shut down the rest services when the process is exiting
111 Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesShutdownHook()));
113 state = ServicesState.RUNNING;
115 if (parameters.getTimeToLive() == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE) {
116 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at "
117 + parameters.getBaseUri().toString());
119 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started");
122 // Find out how long is left to wait
123 long timeRemaining = parameters.getTimeToLive();
124 while (timeRemaining == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
125 // decrement the time to live in the non-infinity case
126 if (timeRemaining > 0) {
133 } catch (final Exception e) {
134 String message = REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage();
135 outStream.println(message);
136 LOGGER.warn(message, e);
138 if (apexMonitoringRest != null) {
139 apexMonitoringRest.shutdown();
140 apexMonitoringRest = null;
142 state = ServicesState.STOPPED;
148 * Get services state.
150 * @return the service state
152 public ServicesState getState() {
157 public String toString() {
158 final StringBuilder ret = new StringBuilder();
159 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
160 .append(this.getState());
161 return ret.toString();
165 * Explicitly shut down the services.
167 public void shutdown() {
168 if (apexMonitoringRest != null) {
169 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
170 apexMonitoringRest.shutdown();
172 state = ServicesState.STOPPED;
173 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
177 * This class is a shutdown hook for the Apex services command.
179 private class ApexServicesShutdownHook implements Runnable {
185 if (apexMonitoringRest != null) {
186 apexMonitoringRest.shutdown();
192 * Main method, main entry point for command.
194 * @param args The command line arguments for the client
196 public static void main(final String[] args) {
198 final ApexMonitoringRestMain restMain = new ApexMonitoringRestMain(args, System.out);
200 } catch (final Exception e) {
201 LOGGER.error("start failed", e);