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.full.rest;
24 import java.io.PrintStream;
25 import org.slf4j.ext.XLogger;
26 import org.slf4j.ext.XLoggerFactory;
29 * This class is the main class that is used to launch the Apex editor from the command line.
32 public class ApexServicesRestMain {
33 // Logger for this class
34 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServicesRestMain.class);
36 // Recurring string constants
37 private static final String REST_ENDPOINT_PREFIX = "Apex Editor REST endpoint (";
40 * The Enum EditorState holds the current state of the editor.
43 public enum EditorState {
44 /** The editor is stopped. */
46 /** The editor is ready to run. */
48 /** The editor is getting ready to run. */
50 /** The editor is running. */
54 private static final int EDITOR_RNNING_CHECK_TIMEOUT = 1000;
56 private EditorState state = EditorState.STOPPED;
58 // The Apex editor this class is running
59 private ApexServicesRest apexServices = null;
61 // The parameters for the editor
62 private ApexServicesRestParameters parameters = null;
64 // Output and error streams for messages
65 private final PrintStream outStream;
68 * Constructor, kicks off the editor.
70 * @param args The command line arguments for the editor
71 * @param outStream The stream for output messages
73 public ApexServicesRestMain(final String[] args, final PrintStream outStream) {
74 // Save the streams for output and error
75 this.outStream = outStream;
77 // Editor parameter parsing
78 final ApexServicesRestParameterParser parser = new ApexServicesRestParameterParser();
81 // Get and check the parameters
82 parameters = parser.parse(args);
83 } catch (final ApexServicesRestParameterException e) {
84 throw new ApexServicesRestParameterException(REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, "
85 + e.getMessage() + '\n' + parser.getHelp(ApexServicesRestMain.class.getName()), e);
88 if (parameters.isHelpSet()) {
89 throw new ApexServicesRestParameterException(parser.getHelp(ApexServicesRestMain.class.getName()));
92 // Validate the parameters
93 final String validationMessage = parameters.validate();
94 if (validationMessage.length() > 0) {
95 throw new ApexServicesRestParameterException(
96 REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, " + validationMessage + '\n'
97 + parser.getHelp(ApexServicesRestMain.class.getName()));
100 state = EditorState.READY;
104 * Initialize the Apex editor.
107 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at " + parameters.getBaseUri().toString()
111 state = EditorState.INITIALIZING;
114 apexServices = new ApexServicesRest(parameters);
116 // Add a shutdown hook to shut down the editor when the process is exiting
117 Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesRestShutdownHook()));
119 state = EditorState.RUNNING;
121 if (parameters.getTimeToLive() == ApexServicesRestParameters.INFINITY_TIME_TO_LIVE) {
122 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at "
123 + parameters.getBaseUri().toString());
125 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started");
128 // Find out how long is left to wait
129 long timeRemaining = parameters.getTimeToLive();
130 while (timeRemaining == ApexServicesRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
131 // decrement the time to live in the non-infinity case
132 if (timeRemaining > 0) {
137 Thread.sleep(EDITOR_RNNING_CHECK_TIMEOUT);
139 } catch (final Exception e) {
140 String message = REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage();
141 outStream.println(message);
142 LOGGER.warn(message, e);
144 if (apexServices != null) {
145 apexServices.shutdown();
148 state = EditorState.STOPPED;
153 * Get the editor state.
157 public EditorState getState() {
165 public String toString() {
166 final StringBuilder ret = new StringBuilder();
167 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(parameters).append("], State=")
168 .append(this.getState());
169 return ret.toString();
173 * Explicitly shut down the editor.
175 public void shutdown() {
176 if (apexServices != null) {
177 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
178 apexServices.shutdown();
180 state = EditorState.STOPPED;
181 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
185 * This class is a shutdown hook for the Apex editor command.
187 private class ApexServicesRestShutdownHook implements Runnable {
193 if (apexServices != null) {
194 apexServices.shutdown();
200 * Main method, main entry point for command.
202 * @param args The command line arguments for the editor
204 public static void main(final String[] args) {
206 final ApexServicesRestMain editorMain = new ApexServicesRestMain(args, System.out);
208 } catch (final Exception e) {
209 LOGGER.error("error starting REST client", e);