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;
26 * The main class for Apex Restful Monitoring.
28 * @author Michael Watkins (michael.watkins@ericsson.com)
30 public class ApexMonitoringRestMain {
32 public enum ServicesState {
33 STOPPED, READY, INITIALIZING, RUNNING
36 private ServicesState state = ServicesState.STOPPED;
38 // The parameters for the client
39 private ApexMonitoringRestParameters parameters = null;
41 // Output and error streams for messages
42 private final PrintStream outStream;
44 // The Apex services client this class is running
45 private ApexMonitoringRest apexMonitoringRest = null;
48 * Main method, main entry point for command.
50 * @param args The command line arguments for the client
52 public static void main(final String[] args) {
54 final ApexMonitoringRestMain restMain = new ApexMonitoringRestMain(args, System.out);
56 } catch (final Exception e) {
57 System.err.println(e.getMessage());
62 * Constructor, kicks off the rest service.
64 * @param args The command line arguments for the RESTful service
65 * @param outStream The stream for output messages
67 public ApexMonitoringRestMain(final String[] args, final PrintStream outStream) {
68 // Save the streams for output and error
69 this.outStream = outStream;
71 // Client parameter parsing
72 final ApexMonitoringRestParameterParser parser = new ApexMonitoringRestParameterParser();
75 // Get and check the parameters
76 parameters = parser.parse(args);
77 } catch (final ApexMonitoringRestParameterException e) {
78 throw new ApexMonitoringRestParameterException(
79 "Apex Services REST endpoint (" + this.toString() + ") parameter error, " + e.getMessage() + '\n'
80 + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
83 if (parameters.isHelpSet()) {
84 throw new ApexMonitoringRestParameterException(
85 parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
88 // Validate the parameters
89 final String validationMessage = parameters.validate();
90 if (validationMessage.length() > 0) {
91 throw new ApexMonitoringRestParameterException(
92 "Apex Services REST endpoint (" + this.toString() + ") parameters invalid, " + validationMessage
93 + '\n' + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
96 state = ServicesState.READY;
100 * Initialize the rest service.
103 outStream.println("Apex Services REST endpoint (" + this.toString() + ") starting at "
104 + parameters.getBaseURI().toString() + " . . .");
107 state = ServicesState.INITIALIZING;
109 // Start the REST service
110 apexMonitoringRest = new ApexMonitoringRest(parameters);
112 // Add a shutdown hook to shut down the rest services when the process is exiting
113 Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesShutdownHook()));
115 state = ServicesState.RUNNING;
117 if (parameters.getTimeToLive() == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE) {
118 outStream.println("Apex Services REST endpoint (" + this.toString() + ") started at "
119 + parameters.getBaseURI().toString());
121 outStream.println("Apex Services REST endpoint (" + this.toString() + ") started");
124 // Find out how long is left to wait
125 long timeRemaining = parameters.getTimeToLive();
126 while (timeRemaining == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
127 // decrement the time to live in the non-infinity case
128 if (timeRemaining > 0) {
135 } catch (final Exception e) {
137 "Apex Services REST endpoint (" + this.toString() + ") failed at with error: " + e.getMessage());
139 if (apexMonitoringRest != null) {
140 apexMonitoringRest.shutdown();
141 apexMonitoringRest = null;
143 state = ServicesState.STOPPED;
149 * Get services state.
151 * @return the service state
153 public ServicesState getState() {
158 public String toString() {
159 final StringBuilder ret = new StringBuilder();
160 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
161 .append(this.getState());
162 return ret.toString();
166 * Explicitly shut down the services.
168 public void shutdown() {
169 if (apexMonitoringRest != null) {
170 outStream.println("Apex Services REST endpoint (" + this.toString() + ") shutting down");
171 apexMonitoringRest.shutdown();
173 state = ServicesState.STOPPED;
174 outStream.println("Apex Services REST endpoint (" + this.toString() + ") shut down");
178 * This class is a shutdown hook for the Apex services command.
180 private class ApexServicesShutdownHook implements Runnable {
184 * @see java.lang.Runnable#run()
188 if (apexMonitoringRest != null) {
189 apexMonitoringRest.shutdown();