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.editor.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 ApexEditorMain {
34 // Logger for this class
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorMain.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 ApexEditor apexEditor = null;
62 // The parameters for the editor
63 private ApexEditorParameters 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 ApexEditorMain(final String[] args, final PrintStream outStream) {
75 // Save the streams for output and error
76 this.outStream = outStream;
78 // Editor parameter parsing
79 final ApexEditorParameterParser parser = new ApexEditorParameterParser();
82 // Get and check the parameters
83 parameters = parser.parse(args);
84 } catch (final ApexEditorParameterException e) {
85 throw new ApexEditorParameterException(REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, "
86 + e.getMessage() + '\n' + parser.getHelp(ApexEditorMain.class.getName()), e);
89 if (parameters.isHelpSet()) {
90 throw new ApexEditorParameterException(parser.getHelp(ApexEditorMain.class.getName()));
93 // Validate the parameters
94 final String validationMessage = parameters.validate();
95 if (validationMessage.length() > 0) {
96 throw new ApexEditorParameterException(REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, "
97 + validationMessage + '\n' + parser.getHelp(ApexEditorMain.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 apexEditor = new ApexEditor(parameters);
116 // Add a shutdown hook to shut down the editor when the process is exiting
117 Runtime.getRuntime().addShutdownHook(new Thread(new ApexEditorShutdownHook()));
119 state = EditorState.RUNNING;
121 if (parameters.getTimeToLive() == ApexEditorParameters.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 == ApexEditorParameters.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);
145 if (apexEditor != null) {
146 apexEditor.shutdown();
149 state = EditorState.STOPPED;
154 * Get the editor state.
158 public EditorState getState() {
166 public String toString() {
167 final StringBuilder ret = new StringBuilder();
168 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(parameters).append("], State=")
169 .append(this.getState());
170 return ret.toString();
174 * Explicitly shut down the editor.
176 public void shutdown() {
177 if (apexEditor != null) {
178 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
179 apexEditor.shutdown();
181 state = EditorState.STOPPED;
182 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
186 * This class is a shutdown hook for the Apex editor command.
188 private class ApexEditorShutdownHook implements Runnable {
194 if (apexEditor != null) {
195 apexEditor.shutdown();
201 * Main method, main entry point for command.
203 * @param args The command line arguments for the editor
205 public static void main(final String[] args) {
207 final ApexEditorMain editorMain = new ApexEditorMain(args, System.out);
209 } catch (final Exception e) {
210 LOGGER.error("start failed", e);