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