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 * User: ewatkmi Date: 31 Jul 2017
28 public class ApexMonitoringRestMain {
30 public enum ServicesState {
31 STOPPED, READY, INITIALIZING, RUNNING
34 private ServicesState state = ServicesState.STOPPED;
36 // The parameters for the client
37 private ApexMonitoringRestParameters parameters = null;
39 // Output and error streams for messages
40 private final PrintStream outStream;
42 // The Apex services client this class is running
43 private ApexMonitoringRest apexMonitoringRest = null;
46 * Main method, main entry point for command
48 * @param args The command line arguments for the client
50 public static void main(final String[] args) {
52 final ApexMonitoringRestMain restMain = new ApexMonitoringRestMain(args, System.out);
54 } catch (final Exception e) {
55 System.err.println(e.getMessage());
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 "Apex Services REST endpoint (" + this.toString() + ") parameter error, " + e.getMessage() + '\n'
78 + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
81 if (parameters.isHelpSet()) {
82 throw new ApexMonitoringRestParameterException(
83 parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
86 // Validate the parameters
87 final String validationMessage = parameters.validate();
88 if (validationMessage.length() > 0) {
89 throw new ApexMonitoringRestParameterException(
90 "Apex Services REST endpoint (" + this.toString() + ") parameters invalid, " + validationMessage
91 + '\n' + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
94 state = ServicesState.READY;
98 * Initialize the rest service
101 outStream.println("Apex Services REST endpoint (" + this.toString() + ") starting at "
102 + 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("Apex Services REST endpoint (" + this.toString() + ") started at "
117 + parameters.getBaseURI().toString());
119 outStream.println("Apex Services REST endpoint (" + 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) {
135 "Apex Services REST endpoint (" + this.toString() + ") failed at with error: " + e.getMessage());
137 if (apexMonitoringRest != null) {
138 apexMonitoringRest.shutdown();
139 apexMonitoringRest = null;
141 state = ServicesState.STOPPED;
147 * Get services state.
149 * @return the service state
151 public ServicesState getState() {
156 public String toString() {
157 final StringBuilder ret = new StringBuilder();
158 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
159 .append(this.getState());
160 return ret.toString();
164 * Explicitly shut down the services
166 public void shutdown() {
167 if (apexMonitoringRest != null) {
168 outStream.println("Apex Services REST endpoint (" + this.toString() + ") shutting down");
169 apexMonitoringRest.shutdown();
171 state = ServicesState.STOPPED;
172 outStream.println("Apex Services REST endpoint (" + this.toString() + ") shut down");
176 * This class is a shutdown hook for the Apex services command
178 private class ApexServicesShutdownHook implements Runnable {
182 * @see java.lang.Runnable#run()
186 if (apexMonitoringRest != null) {
187 apexMonitoringRest.shutdown();