2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-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.client.monitoring.rest;
23 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 {
42 STOPPED, READY, INITIALIZING, RUNNING
45 private ServicesState state = ServicesState.STOPPED;
47 // The parameters for the client
48 private ApexMonitoringRestParameters parameters = null;
50 // Output and error streams for messages
51 private final PrintStream outStream;
53 // The Apex services client this class is running
54 private ApexMonitoringRest apexMonitoringRest = null;
57 * Main method, main entry point for command.
59 * @param args The command line arguments for the client
61 public static void main(final String[] args) {
63 final ApexMonitoringRestMain restMain = new ApexMonitoringRestMain(args, System.out);
65 } catch (final Exception e) {
66 LOGGER.error("start failed", e);
71 * Constructor, kicks off the rest service.
73 * @param args The command line arguments for the RESTful service
74 * @param outStream The stream for output messages
76 public ApexMonitoringRestMain(final String[] args, final PrintStream outStream) {
77 // Save the streams for output and error
78 this.outStream = outStream;
80 // Client parameter parsing
81 final ApexMonitoringRestParameterParser parser = new ApexMonitoringRestParameterParser();
84 // Get and check the parameters
85 parameters = parser.parse(args);
86 } catch (final ApexMonitoringRestParameterException e) {
87 throw new ApexMonitoringRestParameterException(
88 REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, " + e.getMessage() + '\n'
89 + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
92 if (parameters.isHelpSet()) {
93 throw new ApexMonitoringRestParameterException(
94 parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
97 // Validate the parameters
98 final String validationMessage = parameters.validate();
99 if (validationMessage.length() > 0) {
100 throw new ApexMonitoringRestParameterException(
101 REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, " + validationMessage
102 + '\n' + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
105 state = ServicesState.READY;
109 * Initialize the rest service.
112 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at "
113 + parameters.getBaseUri().toString() + " . . .");
116 state = ServicesState.INITIALIZING;
118 // Start the REST service
119 apexMonitoringRest = new ApexMonitoringRest(parameters);
121 // Add a shutdown hook to shut down the rest services when the process is exiting
122 Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesShutdownHook()));
124 state = ServicesState.RUNNING;
126 if (parameters.getTimeToLive() == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE) {
127 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at "
128 + parameters.getBaseUri().toString());
130 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started");
133 // Find out how long is left to wait
134 long timeRemaining = parameters.getTimeToLive();
135 while (timeRemaining == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
136 // decrement the time to live in the non-infinity case
137 if (timeRemaining > 0) {
144 } catch (final Exception e) {
146 REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage());
148 if (apexMonitoringRest != null) {
149 apexMonitoringRest.shutdown();
150 apexMonitoringRest = null;
152 state = ServicesState.STOPPED;
158 * Get services state.
160 * @return the service state
162 public ServicesState getState() {
167 public String toString() {
168 final StringBuilder ret = new StringBuilder();
169 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
170 .append(this.getState());
171 return ret.toString();
175 * Explicitly shut down the services.
177 public void shutdown() {
178 if (apexMonitoringRest != null) {
179 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
180 apexMonitoringRest.shutdown();
182 state = ServicesState.STOPPED;
183 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
187 * This class is a shutdown hook for the Apex services command.
189 private class ApexServicesShutdownHook implements Runnable {
193 * @see java.lang.Runnable#run()
197 if (apexMonitoringRest != null) {
198 apexMonitoringRest.shutdown();