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.service.engine.main;
23 import java.util.Arrays;
24 import java.util.Base64;
25 import java.util.Map.Entry;
27 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
28 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
29 import org.onap.policy.apex.service.parameters.ApexParameters;
30 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * This class initiates Apex as a complete service from the command line.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class ApexMain {
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexMain.class);
42 // The Apex Activator that activates the Apex engine
43 private ApexActivator activator;
45 // The parameters read in from JSON
46 private ApexParameters parameters;
49 * Instantiates the Apex service.
51 * @param args the commaind line arguments
53 public ApexMain(final String[] args) {
54 LOGGER.entry("Starting Apex service with parameters " + Arrays.toString(args) + " . . .");
56 // Check the arguments
57 final ApexCommandLineArguments arguments = new ApexCommandLineArguments();
59 // The arguments return a string if there is a message to print and we should exit
60 final String argumentMessage = arguments.parse(args);
61 if (argumentMessage != null) {
62 LOGGER.info(argumentMessage);
66 // Validate that the arguments are sane
68 } catch (final ApexException e) {
69 LOGGER.error("start of Apex service failed", e);
73 // Read the parameters
75 parameters = new ApexParameterHandler().getParameters(arguments);
76 } catch (final Exception e) {
77 LOGGER.error("start of Apex service failed", e);
81 // Set incoming Java properties
82 setJavaProperties(parameters);
84 // Set the name of the event handler parameters for producers and consumers
85 for (final Entry<String, EventHandlerParameters> ehParameterEntry : parameters.getEventOutputParameters()
87 if (!ehParameterEntry.getValue().checkSetName()) {
88 ehParameterEntry.getValue().setName(ehParameterEntry.getKey());
91 for (final Entry<String, EventHandlerParameters> ehParameterEntry : parameters.getEventInputParameters()
93 if (!ehParameterEntry.getValue().checkSetName()) {
94 ehParameterEntry.getValue().setName(ehParameterEntry.getKey());
98 // Now, create the activator for the Apex service
99 activator = new ApexActivator(parameters);
101 // Start the activator
103 activator.initialize();
104 } catch (final ApexActivatorException e) {
105 LOGGER.error("start of Apex service failed, used parameters are " + Arrays.toString(args), e);
109 // Add a shutdown hook to shut everything down in an orderly manner
110 Runtime.getRuntime().addShutdownHook(new ApexMainShutdownHookClass());
111 LOGGER.exit("Started Apex");
115 * Get the parameters specified in JSON.
117 * @return the parameters
119 public ApexParameters getParameters() {
124 * Shut down Execution.
126 * @throws ApexException on shutdown errors
128 public void shutdown() throws ApexException {
129 if (activator != null) {
130 activator.terminate();
135 * The Class ApexMainShutdownHookClass terminates the Apex engine for the Apex service when its run method is
138 private class ApexMainShutdownHookClass extends Thread {
142 * @see java.lang.Runnable#run()
147 // Shutdown the Apex engine and wait for everything to stop
148 activator.terminate();
149 } catch (final ApexException e) {
150 LOGGER.warn("error occured during shut down of the Apex service", e);
156 * Set the Java properties specified in the parameters.
158 * @param parameters The incoming parameters
160 private void setJavaProperties(final ApexParameters parameters) {
161 if (!parameters.checkJavaPropertiesSet()) {
165 // Set each Java property
166 for (String[] javaProperty : parameters.getJavaProperties()) {
167 String javaPropertyName = javaProperty[0];
168 String javaPropertyValue = javaProperty[1];
170 // Passwords are encoded using base64, better than sending passwords in the clear
171 if (javaPropertyName.toLowerCase().contains("password")) {
172 javaPropertyValue = new String(Base64.getDecoder().decode(javaPropertyValue.getBytes()));
175 // Set the Java property
176 System.setProperty(javaPropertyName, javaPropertyValue);
183 * @param args the arguments
185 public static void main(final String[] args) {