2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.editors.apex.rest;
25 import java.io.PrintStream;
26 import java.util.concurrent.atomic.AtomicReference;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
31 * This class is the main class that is used to launch the Apex editor from the command line.
34 public class ApexEditorMain {
35 // Logger for this class
36 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorMain.class);
38 // Recurring string constants
39 private static final String REST_ENDPOINT_PREFIX = "Apex Editor REST endpoint (";
42 * The Enum EditorState holds the current state of the editor.
45 public enum EditorState {
46 /** The editor is stopped. */
48 /** The editor is ready to run. */
50 /** The editor is getting ready to run. */
52 /** The editor is running. */
56 private static final int EDITOR_RNNING_CHECK_TIMEOUT = 1000;
58 private EditorState state = EditorState.STOPPED;
60 // The Apex editor this class is running
61 private ApexEditor apexEditor = null;
63 // The parameters for the editor
64 private static AtomicReference<ApexEditorParameters> parameters = new AtomicReference<>();
66 // Output and error streams for messages
67 private final PrintStream outStream;
70 * Constructor, kicks off the editor.
72 * @param args The command line arguments for the editor
73 * @param outStream The stream for output messages
75 public ApexEditorMain(final String[] args, final PrintStream outStream) {
76 // Save the streams for output and error
77 this.outStream = outStream;
79 // Editor parameter parsing
80 final ApexEditorParameterParser parser = new ApexEditorParameterParser();
83 // Get and check the parameters
84 parameters.set(parser.parse(args));
85 } catch (final ApexEditorParameterException e) {
86 throw new ApexEditorParameterException(REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, "
87 + e.getMessage() + '\n' + parser.getHelp(ApexEditorMain.class.getName()), e);
89 if (parameters.get().isHelp()) {
90 throw new ApexEditorParameterException(parser.getHelp(ApexEditorMain.class.getName()));
93 // Validate the parameters
94 final String validationMessage = parameters.get().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 "
108 + parameters.get().getBaseUri().toString() + " . . .");
111 state = EditorState.INITIALIZING;
114 apexEditor = new ApexEditor(parameters.get());
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.get().getTimeToLive() == ApexEditorParameters.INFINITY_TIME_TO_LIVE) {
122 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at "
123 + parameters.get().getBaseUri().toString());
125 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started");
128 // Find out how long is left to wait
129 long timeRemaining = parameters.get().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);
144 if (apexEditor != null) {
145 apexEditor.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 (apexEditor != null) {
177 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
178 apexEditor.shutdown();
180 state = EditorState.STOPPED;
181 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
185 * Get the editor parameters.
187 * @return the parameters
189 public static ApexEditorParameters getParameters() {
190 return parameters.get();
194 * This class is a shutdown hook for the Apex editor command.
196 private class ApexEditorShutdownHook implements Runnable {
202 if (apexEditor != null) {
203 apexEditor.shutdown();
209 * Main method, main entry point for command.
211 * @param args The command line arguments for the editor
213 public static void main(final String[] args) {
215 final ApexEditorMain editorMain = new ApexEditorMain(args, null);
217 } catch (final Exception e) {
218 LOGGER.error("start failed", e);