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;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
30 * This class is the main class that is used to launch the Apex editor from the command line.
33 public class ApexServicesRestMain {
34 // Logger for this class
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServicesRestMain.class);
37 // Recurring string constants
38 private static final String REST_ENDPOINT_PREFIX = "Apex Editor REST endpoint (";
41 * The Enum EditorState holds the current state of the editor.
44 public enum EditorState {
45 /** The editor is stopped. */
47 /** The editor is ready to run. */
49 /** The editor is getting ready to run. */
51 /** The editor is running. */
55 private static final int EDITOR_RNNING_CHECK_TIMEOUT = 1000;
57 private EditorState state = EditorState.STOPPED;
59 // The Apex editor this class is running
60 private ApexServicesRest apexServices = null;
62 // The parameters for the editor
63 private ApexServicesRestParameters parameters = null;
65 // Output and error streams for messages
66 private final PrintStream outStream;
69 * Constructor, kicks off the editor.
71 * @param args The command line arguments for the editor
72 * @param outStream The stream for output messages
74 public ApexServicesRestMain(final String[] args, final PrintStream outStream) {
75 // Save the streams for output and error
76 this.outStream = outStream;
78 // Editor parameter parsing
79 final ApexServicesRestParameterParser parser = new ApexServicesRestParameterParser();
82 // Get and check the parameters
83 parameters = parser.parse(args);
84 } catch (final ApexServicesRestParameterException e) {
85 throw new ApexServicesRestParameterException(REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, "
86 + e.getMessage() + '\n' + parser.getHelp(ApexServicesRestMain.class.getName()), e);
89 if (parameters.isHelpSet()) {
90 throw new ApexServicesRestParameterException(parser.getHelp(ApexServicesRestMain.class.getName()));
93 // Validate the parameters
94 final String validationMessage = parameters.validate();
95 if (validationMessage.length() > 0) {
96 throw new ApexServicesRestParameterException(
97 REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, " + validationMessage + '\n'
98 + parser.getHelp(ApexServicesRestMain.class.getName()));
101 state = EditorState.READY;
105 * Initialize the Apex editor.
108 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at " + parameters.getBaseUri().toString()
112 state = EditorState.INITIALIZING;
115 apexServices = new ApexServicesRest(parameters);
117 // Add a shutdown hook to shut down the editor when the process is exiting
118 Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesRestShutdownHook()));
120 state = EditorState.RUNNING;
122 if (parameters.getTimeToLive() == ApexServicesRestParameters.INFINITY_TIME_TO_LIVE) {
123 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at "
124 + parameters.getBaseUri().toString());
126 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started");
129 // Find out how long is left to wait
130 long timeRemaining = parameters.getTimeToLive();
131 while (timeRemaining == ApexServicesRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
132 // decrement the time to live in the non-infinity case
133 if (timeRemaining > 0) {
138 Thread.sleep(EDITOR_RNNING_CHECK_TIMEOUT);
140 } catch (final Exception e) {
141 String message = REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage();
142 outStream.println(message);
143 LOGGER.warn(message, e);
146 if (apexServices != null) {
147 apexServices.shutdown();
150 state = EditorState.STOPPED;
155 * Get the editor state.
159 public EditorState getState() {
167 public String toString() {
168 final StringBuilder ret = new StringBuilder();
169 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(parameters).append("], State=")
170 .append(this.getState());
171 return ret.toString();
175 * Explicitly shut down the editor.
177 public void shutdown() {
178 if (apexServices != null) {
179 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
180 apexServices.shutdown();
182 state = EditorState.STOPPED;
183 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
187 * This class is a shutdown hook for the Apex editor command.
189 private class ApexServicesRestShutdownHook implements Runnable {
195 if (apexServices != null) {
196 apexServices.shutdown();
202 * Main method, main entry point for command.
204 * @param args The command line arguments for the editor
206 public static void main(final String[] args) {
208 final ApexServicesRestMain editorMain = new ApexServicesRestMain(args, System.out);
210 } catch (final Exception e) {
211 LOGGER.error("error starting REST client", e);