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.deployment.rest;
23 import java.io.PrintStream;
25 import org.slf4j.ext.XLogger;
26 import org.slf4j.ext.XLoggerFactory;
29 * The main class for ApexDeploymentRest.
31 * @author Michael Watkins (michael.watkins@ericsson.com)
33 public class ApexDeploymentRestMain {
34 // Logger for this class
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDeploymentRestMain.class);
37 // Recurring string constants
38 private static final String REST_ENDPOINT_PREFIX = "Apex Services REST endpoint (";
41 public enum ServicesState {
42 STOPPED, READY, INITIALIZING, RUNNING
45 private ServicesState state = ServicesState.STOPPED;
47 // The parameters for the client
48 private ApexDeploymentRestParameters parameters = null;
50 // Output and error streams for messages
51 private final PrintStream outStream;
53 // The Apex services client this class is running
54 private ApexDeploymentRest apexDeploymentRest = null;
57 * Constructor, kicks off the rest service.
59 * @param args The command line arguments for the RESTful service
60 * @param outStream The stream for output messages
62 public ApexDeploymentRestMain(final String[] args, final PrintStream outStream) {
63 // Save the streams for output and error
64 this.outStream = outStream;
66 // Client parameter parsing
67 final ApexDeploymentRestParameterParser parser = new ApexDeploymentRestParameterParser();
70 // Get and check the parameters
71 parameters = parser.parse(args);
72 } catch (final ApexDeploymentRestParameterException e) {
73 throw new ApexDeploymentRestParameterException(
74 REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, " + e.getMessage() + '\n'
75 + parser.getHelp(ApexDeploymentRestMain.class.getCanonicalName()), e);
78 if (parameters.isHelpSet()) {
79 throw new ApexDeploymentRestParameterException(
80 parser.getHelp(ApexDeploymentRestMain.class.getCanonicalName()));
83 // Validate the parameters
84 final String validationMessage = parameters.validate();
85 if (validationMessage.length() > 0) {
86 throw new ApexDeploymentRestParameterException(
87 REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, " + validationMessage + '\n'
88 + parser.getHelp(ApexDeploymentRestMain.class.getCanonicalName()));
91 state = ServicesState.READY;
95 * Initialize the rest service.
98 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at " + parameters.getBaseUri().toString()
102 state = ServicesState.INITIALIZING;
104 // Start the REST service
105 apexDeploymentRest = new ApexDeploymentRest(parameters);
107 // Add a shutdown hook to shut down the rest services when the process is exiting
108 Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesShutdownHook()));
110 state = ServicesState.RUNNING;
112 if (parameters.getTimeToLive() == ApexDeploymentRestParameters.INFINITY_TIME_TO_LIVE) {
113 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at "
114 + parameters.getBaseUri().toString());
116 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started");
119 // Find out how long is left to wait
120 long timeRemaining = parameters.getTimeToLive();
121 while (timeRemaining == ApexDeploymentRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
122 // decrement the time to live in the non-infinity case
123 if (timeRemaining > 0) {
130 } catch (final Exception e) {
131 String message = REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage();
132 outStream.println(message);
133 LOGGER.warn(message, e);
135 if (apexDeploymentRest != null) {
136 apexDeploymentRest.shutdown();
137 apexDeploymentRest = null;
139 state = ServicesState.STOPPED;
145 * Get services state.
147 * @return the service state
149 public ServicesState getState() {
154 public String toString() {
155 final StringBuilder ret = new StringBuilder();
156 ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
157 .append(this.getState());
158 return ret.toString();
162 * Explicitly shut down the services.
164 public void shutdown() {
165 if (apexDeploymentRest != null) {
166 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down");
167 apexDeploymentRest.shutdown();
169 state = ServicesState.STOPPED;
170 outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down");
174 * This class is a shutdown hook for the Apex services command.
176 private class ApexServicesShutdownHook implements Runnable {
182 if (apexDeploymentRest != null) {
183 apexDeploymentRest.shutdown();
189 * Main method, main entry point for command.
191 * @param args The command line arguments for the client
193 public static void main(final String[] args) {
195 final ApexDeploymentRestMain restMain = new ApexDeploymentRestMain(args, System.out);
197 } catch (final Exception e) {
198 LOGGER.error("start failed", e);