2 * Copyright 2016 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.TimeoutException;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * For execute local command
32 * (support control overtime)
37 * @author quanzhong@huawei.com
38 * @version NFVO 0.5 Sep 19, 2016
40 public class LocalComandUtils {
41 private static final Logger log = LoggerFactory.getLogger(LocalComandUtils.class);
43 private LocalComandUtils(){
53 public static String formatCommand(List<String> command) {
54 StringBuilder builder = new StringBuilder();
56 for(String cmd : command) {
57 builder.append(cmd).append(" "); // NOSONAR
60 return builder.toString();
71 public static ExeRes execute(String dir, String... command) {
72 List<String> commands = new ArrayList<>(command.length);
73 for(String arg : command) {
76 return execute(dir, commands);
81 * execute local command
84 * @param dir the command path
86 * @param timeout millis
87 * @return response msg
90 public static ExeRes execute(String dir, List<String> command,long timeout) {
91 ExeRes er = new ExeRes();
92 StringBuilder sb = new StringBuilder();
94 if(SwitchController.isDebugModel()) {
95 command.set(0, "juju.bat");
97 ProcessBuilder pb = new ProcessBuilder(command);
98 if(StringUtils.isNotBlank(dir)) {
99 pb.directory(new File(dir));
101 pb.redirectErrorStream(true);
102 Process p = pb.start();
104 // wait the process result
105 buildProcessResult(er, p, timeout);
107 InputStream in = p.getInputStream();
108 byte[] buffer = new byte[1024];
110 while((length = in.read(buffer)) > 0) {
111 sb.append(new String(buffer, 0, length));
114 er.setBody(sb.toString());
115 } catch(Exception e) {
116 er.setCode(ExeRes.FAILURE);
117 er.setBody(e.getMessage());
118 log.error("execute the command failed:{}", command, e);
124 * execute local command
127 * @param dir the command path
129 * @return response msg
132 public static ExeRes execute(String dir, List<String> command) {
133 return execute(dir,command,Constant.PROCESS_WAIT_MILLIS);
141 * @param timeout millis
142 * @throws TimeoutException
143 * @throws InterruptedException
146 private static void buildProcessResult(ExeRes er, Process p,long timeout) throws TimeoutException, InterruptedException {
147 Worker worker = new Worker(p);
150 worker.join(timeout);
151 if(worker.exitValue != null) {
152 int exit = worker.exitValue;
154 er.setCode(ExeRes.FAILURE);
155 log.warn("the process exit non-normal");
157 er.setCode(ExeRes.SUCCESS);
160 er.setCode(ExeRes.FAILURE);
161 log.warn("the process execute timeout.");
162 throw new TimeoutException();
164 } catch(InterruptedException e) {
166 Thread.currentThread().interrupt();
177 * @author quanzhong@huawei.com
178 * @version NFVO 0.5 Sep 19, 2016
180 private static class Worker extends Thread {
182 private final Process process;
184 private Integer exitValue;
186 private Worker(Process process) {
187 this.process = process;
198 exitValue = process.waitFor();
199 } catch(InterruptedException e) {
213 public static class ExeRes {
215 public static final int SUCCESS = 0;
217 public static final int FAILURE = -1;
224 * @return Returns the code.
226 public int getCode() {
231 * @param code The code to set.
233 public void setCode(int code) {
238 * @return Returns the body.
240 public String getBody() {
245 * @param body The body to set.
247 public void setBody(String body) {
252 public String toString() {
254 return EntityUtils.toString(this, this.getClass());
255 } catch(Exception e) {
256 log.error("to string error:", e);
257 return "code:" + this.getCode() + ",body:" + this.getBody();