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()), e);
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 + '\n'
102 + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
105 state = ServicesState.READY;
109 * Initialize the rest service.
112 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at " + 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) {
145 String message = REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage();
146 outStream.println(message);
147 LOGGER.warn(message, e);
149 if (apexMonitoringRest != null) {
150 apexMonitoringRest.shutdown();
151 apexMonitoringRest = null;
153 state = ServicesState.STOPPED;
159 * Get services state.
161 * @return the service state
163 public ServicesState getState() {
168 public String toString() {
169 final StringBuilder ret = new StringBuilder();
170 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
171 .append(this.getState());
172 return ret.toString();
176 * Explicitly shut down the services.
178 public void shutdown() {
179 if (apexMonitoringRest != null) {
180 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
181 apexMonitoringRest.shutdown();
183 state = ServicesState.STOPPED;
184 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
188 * This class is a shutdown hook for the Apex services command.
190 private class ApexServicesShutdownHook implements Runnable {
194 * @see java.lang.Runnable#run()
198 if (apexMonitoringRest != null) {
199 apexMonitoringRest.shutdown();