995772f05aa17ad1fa7c3235b1c3fcb90f7ef057
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.locking;
22
23 import java.io.BufferedReader;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map.Entry;
31
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.service.AbstractParameters;
34 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
37
38 import com.google.gson.Gson;
39
40 /**
41  * The Class TestConcurrentContextThread tests concurrent use of context.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 public class ConcurrentContextJVMThread implements Runnable, Closeable {
46     // Logger for this class
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVMThread.class);
48
49     private final String testType;
50     private final int jvm;
51     private final int threadCount;
52     private final int target;
53     private Process process = null;
54
55     /**
56      * The Constructor.
57      *
58      * @param testType the test type
59      * @param jvm the jvm
60      * @param threadCount the thread count
61      * @param target the target
62      * @throws ApexException the apex exception
63      */
64     public ConcurrentContextJVMThread(final String testType, final int jvm, final int threadCount, final int target)
65             throws ApexException {
66         this.testType = testType;
67         this.jvm = jvm;
68         this.threadCount = threadCount;
69         this.target = target;
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see java.lang.Runnable#run()
76      */
77     @Override
78     public void run() {
79         final List<String> commandList = new ArrayList<>();
80         commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
81                 + System.getProperty("file.separator") + "java");
82         commandList.add("-Xms512m");
83         commandList.add("-Xmx512m");
84         commandList.add("-cp");
85         commandList.add(System.getProperty("java.class.path"));
86         commandList.add(ConcurrentContextJVM.class.getCanonicalName());
87         commandList.add(testType);
88         commandList.add(new Integer(jvm).toString());
89         commandList.add(new Integer(threadCount).toString());
90         commandList.add(new Integer(target).toString());
91         commandList.add(System.getProperty("hazelcast.config"));
92
93         for (final Entry<Class<?>, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) {
94             commandList.add(parameterServiceEntry.getKey().getCanonicalName());
95             commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
96         }
97
98         LOGGER.info("starting JVM " + jvm);
99
100         // Run the JVM
101         final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
102         processBuilder.redirectErrorStream(true);
103
104
105         try {
106             process = processBuilder.start();
107
108             final InputStream is = process.getInputStream();
109             final InputStreamReader isr = new InputStreamReader(is);
110             final BufferedReader br = new BufferedReader(isr);
111             String line;
112             LOGGER.info("JVM Output for command " + commandList + "\n");
113             while ((line = br.readLine()) != null) {
114                 LOGGER.info(line);
115             }
116
117             // Wait to get exit value
118             try {
119                 final int exitValue = process.waitFor();
120                 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
121             } catch (final InterruptedException e) {
122                 LOGGER.warn("Thread was interrupted");
123                 Thread.currentThread().interrupt();
124             }
125         } catch (final IOException ioException) {
126             LOGGER.error("Error occured while writing JVM Output for command ", ioException);
127         }
128     }
129
130
131     @Override
132     public void close() {
133         LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
134         if (process != null) {
135             LOGGER.info("Destroying process ...");
136             process.destroy();
137         }
138     }
139 }