2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.distribution.main.rest;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.distribution.main.parameters.RestServerParameters;
34 * Class to manage life cycle of distribution rest server.
36 * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
38 public class DistributionRestServer implements Startable {
40 private static final String SEPARATOR = ".";
41 private static final String HTTP_SERVER_SERVICES = "http.server.services";
42 private static final Logger LOGGER = FlexLogger.getLogger(DistributionRestServer.class);
44 private List<HttpServletServer> servers = new ArrayList<>();
46 private RestServerParameters restServerParameters;
49 * Constructor for instantiating DistributionRestServer.
51 * @param restServerParameters the rest server parameters
53 public DistributionRestServer(final RestServerParameters restServerParameters) {
54 this.restServerParameters = restServerParameters;
61 public boolean start() {
63 servers = HttpServletServer.factory.build(getServerProperties());
64 for (final HttpServletServer server : servers) {
67 } catch (final Exception exp) {
68 LOGGER.error("Failed to start distribution http server", exp);
75 * Creates the server properties object using restServerParameters.
77 * @return the properties object
79 private Properties getServerProperties() {
80 final Properties props = new Properties();
81 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
82 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
83 restServerParameters.getHost());
84 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
85 Integer.toString(restServerParameters.getPort()));
86 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
87 DistributionRestController.class.getCanonicalName());
88 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
89 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
90 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
91 restServerParameters.getUserName());
92 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
93 restServerParameters.getPassword());
94 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
95 String.valueOf(restServerParameters.isHttps()));
103 public boolean stop() {
104 for (final HttpServletServer server : servers) {
107 } catch (final Exception exp) {
108 LOGGER.error("Failed to stop distribution http server", exp);
118 public void shutdown() {
126 public boolean isAlive() {
127 return !servers.isEmpty();
134 public String toString() {
135 final StringBuilder builder = new StringBuilder();
136 builder.append("DistributionRestServer [servers=");
137 builder.append(servers);
139 return builder.toString();