From 2cc8299ea4f28ad94e4e9cc9749c0751c320b42a Mon Sep 17 00:00:00 2001 From: Sergii Kabashniuk Date: Mon, 20 Dec 2021 13:07:22 +0200 Subject: [PATCH] build: Removed unused jna based code (#214) * build: Removed unused jna based code Signed-off-by: Sergii Kabashniuk --- .../.deps/EXCLUDED/prod.md | 9 +- assembly/assembly-wsmaster-war/.deps/dev.md | 6 +- .../assembly-wsmaster-war/.deps/problems.md | 2 - assembly/assembly-wsmaster-war/.deps/prod.md | 23 +- core/che-core-api-core/pom.xml | 4 - .../core/util/CancellableProcessWrapper.java | 39 ---- .../api/core/util/DefaultProcessManager.java | 51 ----- .../che/api/core/util/ProcessManager.java | 28 --- .../che/api/core/util/ProcessUtil.java | 160 -------------- .../che/api/core/util/UnixProcessManager.java | 202 ------------------ .../che/api/core/util/ProcessUtilTest.java | 83 ------- pom.xml | 5 - 12 files changed, 20 insertions(+), 592 deletions(-) delete mode 100644 core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/CancellableProcessWrapper.java delete mode 100644 core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/DefaultProcessManager.java delete mode 100644 core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessManager.java delete mode 100644 core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessUtil.java delete mode 100644 core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/UnixProcessManager.java delete mode 100644 core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java diff --git a/assembly/assembly-wsmaster-war/.deps/EXCLUDED/prod.md b/assembly/assembly-wsmaster-war/.deps/EXCLUDED/prod.md index 6dcff53600..7147c851b6 100644 --- a/assembly/assembly-wsmaster-war/.deps/EXCLUDED/prod.md +++ b/assembly/assembly-wsmaster-war/.deps/EXCLUDED/prod.md @@ -8,5 +8,10 @@ This file lists dependencies that do not need CQs or auto-detection does not wor | `org.jgroups.kubernetes/jgroups-kubernetes@1.0.13.Final` | [CQ20984](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=20984) | | `org.flywaydb/flyway-core@4.2.0` | [CQ14689](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=14689) | | `com.auth0/jwks-rsa@0.19.0` | [CQ23769](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23769) | -| `org.apache.tomcat/tomcat-jni@10.0.14` | [CQ23765](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23765) | - +| `io.swagger.core.v3/swagger-annotations-jakarta@2.1.11` | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-core-jakarta@2.1.11` | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-integration-jakarta@2.1.11` | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swaqqgger-jaxrs2-jakarta@2.1.11` | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-models-jakarta@2.1.11` | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-jaxrs2-jakarta@2.1.11` | Apache-2.0 clearlydefined | + diff --git a/assembly/assembly-wsmaster-war/.deps/dev.md b/assembly/assembly-wsmaster-war/.deps/dev.md index c7c134bd00..0945c38fc3 100644 --- a/assembly/assembly-wsmaster-war/.deps/dev.md +++ b/assembly/assembly-wsmaster-war/.deps/dev.md @@ -2,14 +2,12 @@ | Packages | License | Resolved CQs | | --- | --- | --- | -| `antlr/antlr@2.7.7` | BSD-3-Clause | #148 | | `com.beust/jcommander@1.78` | Apache-2.0 | [CQ22738](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=22738) | | `com.h2database/h2@1.4.196` | EPL-1.0 | [CQ11693](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=11693) | | `net.bytebuddy/byte-buddy-agent@1.11.3` | Apache-2.0 | clearlydefined | | `net.bytebuddy/byte-buddy@1.11.3` | Apache-2.0 | clearlydefined | -| `org.antlr/ST4@4.0.7` | NOASSERTION | [CQ14504](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=14504) | -| `org.antlr/antlr-runtime@3.5` | BSD-3-Clause | clearlydefined | -| `org.antlr/stringtemplate@3.2.1` | BSD-2-Clause | [CQ6759](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=6759) | +| `org.antlr/ST4@4.3.1` | BSD-3-Clause | #1495 | +| `org.antlr/antlr-runtime@3.5.2` | BSD-3-Clause | clearlydefined | | `org.eclipse.che.core/che-core-commons-test@7.41.0-SNAPSHOT` | EPL-2.0 | ecd.che | | `org.mockito/mockito-core@3.11.2` | MIT | clearlydefined | | `org.objenesis/objenesis@3.2` | Apache-2.0 | clearlydefined | diff --git a/assembly/assembly-wsmaster-war/.deps/problems.md b/assembly/assembly-wsmaster-war/.deps/problems.md index 49290b57c5..f00baf852b 100644 --- a/assembly/assembly-wsmaster-war/.deps/problems.md +++ b/assembly/assembly-wsmaster-war/.deps/problems.md @@ -1,5 +1,3 @@ # Dependency analysis ## UNRESOLVED Production dependencies - -1. `org.apache.tomcat/tomcat-jni@10.0.14` diff --git a/assembly/assembly-wsmaster-war/.deps/prod.md b/assembly/assembly-wsmaster-war/.deps/prod.md index a144cf111e..2a4064ef06 100644 --- a/assembly/assembly-wsmaster-war/.deps/prod.md +++ b/assembly/assembly-wsmaster-war/.deps/prod.md @@ -3,8 +3,8 @@ | Packages | License | Resolved CQs | | --- | --- | --- | | `aopalliance/aopalliance@1.0` | LicenseRef-Public-Domain | [CQ2918](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=2918) | -| `ch.qos.logback/logback-classic@1.2.8` | EPL-1.0 | [CQ13636](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=13636) | -| `ch.qos.logback/logback-core@1.2.8` | EPL-1.0 | [CQ13635](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=13635) | +| `ch.qos.logback/logback-classic@1.2.9` | EPL-1.0 | [CQ13636](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=13636) | +| `ch.qos.logback/logback-core@1.2.9` | EPL-1.0 | [CQ13635](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=13635) | | `com.auth0/jwks-rsa@0.19.0` | NOASSERTION | [CQ23769](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23769) | | `com.fasterxml.jackson.core/jackson-annotations@2.11.2` | Apache-2.0 | [CQ23491](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23491) | | `com.fasterxml.jackson.core/jackson-core@2.11.2` | Apache-2.0 | [CQ23092](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23092) | @@ -25,7 +25,7 @@ | `com.google.j2objc/j2objc-annotations@1.3` | Apache-2.0 | [CQ21195](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=21195) | | `com.google.oauth-client/google-oauth-client@1.32.1` | Apache-2.0 | clearlydefined | | `com.ibm.icu/icu4j@68.1` | NOASSERTION | [CQ23669](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23654) | -| `com.jcraft/jsch@0.1.54` | BSD-2-Clause | [CQ12480](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=12480) | +| `com.jcraft/jsch@0.1.55` | BSD-3-Clause | [CQ19435](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=19435) | | `com.squareup.okhttp3/logging-interceptor@3.12.12` | Apache-2.0 | clearlydefined | | `com.squareup.okhttp3/okhttp@3.12.12` | Apache-2.0 | [CQ19549](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=19549) | | `com.squareup.okio/okio@1.15.0` | Apache-2.0 | [CQ20187](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=20187) | @@ -95,21 +95,20 @@ | `io.prometheus/simpleclient@0.7.0` | Apache-2.0 | clearlydefined | | `io.prometheus/simpleclient_common@0.7.0` | Apache-2.0 | clearlydefined | | `io.prometheus/simpleclient_httpserver@0.7.0` | Apache-2.0 | clearlydefined | -| `io.swagger.core.v3/swagger-annotations-jakarta@2.1.10` | Apache-2.0 | clearlydefined | -| `io.swagger.core.v3/swagger-core-jakarta@2.1.10` | Apache-2.0 | clearlydefined | -| `io.swagger.core.v3/swagger-integration-jakarta@2.1.10` | Apache-2.0 | clearlydefined | -| `io.swagger.core.v3/swagger-jaxrs2-jakarta@2.1.10` | Apache-2.0 | clearlydefined | -| `io.swagger.core.v3/swagger-models-jakarta@2.1.10` | Apache-2.0 | clearlydefined | +| `io.swagger.core.v3/swagger-annotations-jakarta@2.1.11` | - | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-core-jakarta@2.1.11` | - | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-integration-jakarta@2.1.11` | - | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-jaxrs2-jakarta@2.1.11` | - | Apache-2.0 clearlydefined | +| `io.swagger.core.v3/swagger-models-jakarta@2.1.11` | - | Apache-2.0 clearlydefined | | `jakarta.activation/jakarta.activation-api@2.0.1` | EPL-2.0 OR BSD-3-Clause OR GPL-2.0-with-classpath-exception | ee4j.jaf | | `jakarta.annotation/jakarta.annotation-api@2.0.0` | EPL-2.0 OR GPL-2.0-with-classpath-exception | ee4j.ca | -| `jakarta.inject/jakarta.inject-api@1.0.3` | - | eclipse | +| `jakarta.inject/jakarta.inject-api@1.0.5` | - | eclipse | | `jakarta.servlet/jakarta.servlet-api@5.0.0` | EPL-2.0 OR GPL-2.0-with-classpath-exception | ee4j.servlet | | `jakarta.validation/jakarta.validation-api@3.0.0` | EPL-1.0 | modeling.emf.validation | | `jakarta.websocket/jakarta.websocket-api@2.0.0` | EPL-2.0 OR GPL-2.0-with-classpath-exception | ee4j.websocket | | `jakarta.ws.rs/jakarta.ws.rs-api@3.0.0` | EPL-2.0 OR GPL-2.0-with-classpath-exception | ee4j.jaxrs | | `jakarta.xml.bind/jakarta.xml.bind-api@3.0.0` | BSD-3-Clause | ee4j.jaxb | -| `net.java.dev.jna/jna@4.1.0` | Apache-2.0 | [CQ9291](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=9291) | -| `net.logstash.logback/logstash-logback-encoder@6.6` | Apache-2.0 | clearlydefined | +| `net.logstash.logback/logstash-logback-encoder@7.0.1` | Apache-2.0 | clearlydefined | | `org.apache.commons/commons-lang3@3.12.0` | Apache-2.0 | clearlydefined | | `org.apache.httpcomponents/httpcore@4.4.14` | Apache-2.0 | [CQ23528](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23528) | | `org.apache.thrift/libthrift@0.12.0` | Apache-2.0 | clearlydefined | @@ -120,7 +119,7 @@ | `org.apache.tomcat/tomcat-dbcp@10.0.14` | Apache-2.0 | clearlydefined | | `org.apache.tomcat/tomcat-el-api@10.0.14` | Apache-2.0 | clearlydefined | | `org.apache.tomcat/tomcat-jaspic-api@10.0.14` | Apache-2.0 | clearlydefined | -| `org.apache.tomcat/tomcat-jni@10.0.14` | - | [CQ23765](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=23765) | +| `org.apache.tomcat/tomcat-jni@10.0.14` | Apache-2.0 | clearlydefined | | `org.apache.tomcat/tomcat-jsp-api@10.0.14` | Apache-2.0 | clearlydefined | | `org.apache.tomcat/tomcat-juli@10.0.14` | Apache-2.0 | clearlydefined | | `org.apache.tomcat/tomcat-servlet-api@10.0.14` | Apache-2.0 AND (EPL-2.0 OR GPL-2.0-with-classpath-exception) | #921 | diff --git a/core/che-core-api-core/pom.xml b/core/che-core-api-core/pom.xml index 2ecfe37825..48d88a5135 100644 --- a/core/che-core-api-core/pom.xml +++ b/core/che-core-api-core/pom.xml @@ -63,10 +63,6 @@ jakarta.ws.rs jakarta.ws.rs-api - - net.java.dev.jna - jna - org.eclipse.che.core che-core-api-dto diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/CancellableProcessWrapper.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/CancellableProcessWrapper.java deleted file mode 100644 index f00c1fabc5..0000000000 --- a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/CancellableProcessWrapper.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2012-2018 Red Hat, Inc. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - */ -package org.eclipse.che.api.core.util; - -/** - * Cancellable wrapper of {@code Process}. - * - * @author andrew00x - */ -public final class CancellableProcessWrapper implements Cancellable { - private final Process process; - private final Callback callback; - - public CancellableProcessWrapper(Process process) { - this(process, null); - } - - public CancellableProcessWrapper(Process process, Callback callback) { - this.process = process; - this.callback = callback; - } - - @Override - public void cancel() { - ProcessUtil.kill(process); - if (callback != null) { - callback.cancelled(this); - } - } -} diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/DefaultProcessManager.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/DefaultProcessManager.java deleted file mode 100644 index bd31528d8a..0000000000 --- a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/DefaultProcessManager.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2012-2018 Red Hat, Inc. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - */ -package org.eclipse.che.api.core.util; - -/** - * Typically may be in use for windows systems only. For *nix like system UnixProcessManager is in - * use. - * - * @author andrew00x - */ -class DefaultProcessManager extends ProcessManager { - /* - NOTE: some methods are not implemented for other system than unix like system. - */ - - @Override - public void kill(Process process) { - if (isAlive(process)) { - process.destroy(); - try { - process.waitFor(); // wait for process death - } catch (InterruptedException e) { - Thread.interrupted(); - } - } - } - - @Override - public boolean isAlive(Process process) { - try { - process.exitValue(); - return false; - } catch (IllegalThreadStateException e) { - return true; - } - } - - @Override - int system(String command) { - throw new UnsupportedOperationException(); - } -} diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessManager.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessManager.java deleted file mode 100644 index b150dca980..0000000000 --- a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessManager.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2012-2018 Red Hat, Inc. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - */ -package org.eclipse.che.api.core.util; - -/** @author andrew00x */ -abstract class ProcessManager { - static ProcessManager newInstance() { - if (SystemInfo.isUnix()) { - return new UnixProcessManager(); - } - return new DefaultProcessManager(); - } - - abstract void kill(Process process); - - abstract boolean isAlive(Process process); - - abstract int system(String command); -} diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessUtil.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessUtil.java deleted file mode 100644 index ea34b5a66c..0000000000 --- a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/ProcessUtil.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright (c) 2012-2018 Red Hat, Inc. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - */ -package org.eclipse.che.api.core.util; - -import static java.lang.String.format; - -import com.google.common.base.Joiner; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Helpers to manage system processes. - * - * @author andrew00x - * @author Alexander Garagatyi - */ -public final class ProcessUtil { - - private static final Logger LOG = LoggerFactory.getLogger(ProcessUtil.class); - private static final ProcessManager PROCESS_MANAGER = ProcessManager.newInstance(); - - /** - * Writes stdout and stderr of the process to consumers.
- * Supposes that stderr of the process is redirected to stdout. - * - * @param p process to read output from - * @param stdout a consumer where stdout will be redirected - * @param stderr a consumer where stderr will be redirected - * @throws IOException - */ - public static void process(Process p, LineConsumer stdout, LineConsumer stderr) - throws IOException { - try (BufferedReader inputReader = - new BufferedReader(new InputStreamReader(p.getInputStream())); - BufferedReader errorReader = - new BufferedReader(new InputStreamReader(p.getErrorStream()))) { - String line; - while ((line = inputReader.readLine()) != null) { - stdout.writeLine(line); - } - while ((line = errorReader.readLine()) != null) { - stderr.writeLine(line); - } - } - } - - /** - * Writes stdout of the process to consumer.
- * Supposes that stderr of the process is redirected to stdout. - * - * @param p process to read output from - * @param stdout a consumer where stdout will be redirected - * @throws IOException - */ - public static void process(Process p, LineConsumer stdout) throws IOException { - try (BufferedReader inputReader = - new BufferedReader(new InputStreamReader(p.getInputStream()))) { - String line; - while ((line = inputReader.readLine()) != null) { - stdout.writeLine(line); - } - } - } - - /** - * Start the process, writing the stdout and stderr to {@code outputConsumer} and terminate - * process by {@code timeout}.
- * - * @param commandLine arguments of process command - * @param timeout timeout for process. If process duration > {@code timeout} than kill process and - * throw {@link TimeoutException}. - * @param timeUnit timeUnit of the {@code timeout}. - * @param outputConsumer a consumer where stdout and stderr will be redirected - * @return the started process - * @throws InterruptedException in case terminate process - * @throws IOException in case I/O error - * @throws TimeoutException if process gets more time then defined by {@code timeout} - */ - public static Process executeAndWait( - String[] commandLine, int timeout, TimeUnit timeUnit, LineConsumer outputConsumer) - throws TimeoutException, IOException, InterruptedException { - ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true); - - Process process = pb.start(); - - CompletableFuture.runAsync( - () -> { - try { - // consume logs until process ends - process(process, outputConsumer); - } catch (IOException e) { - LOG.error( - format( - "Failed to complete reading of the process '%s' output due to occurred error", - Joiner.on(" ").join(commandLine)), - e); - } - }); - - if (!process.waitFor(timeout, timeUnit)) { - try { - ProcessUtil.kill(process); - } catch (RuntimeException x) { - LOG.error("An error occurred while killing process '{}'", Joiner.on(" ").join(commandLine)); - } - throw new TimeoutException( - format( - "Process '%s' was terminated by timeout %s %s.", - Joiner.on(" ").join(commandLine), timeout, timeUnit.name().toLowerCase())); - } - - return process; - } - - /** - * Start the process, writing the stdout and stderr to consumer. - * - * @param pb process builder to start - * @param consumer a consumer where stdout and stderr will be redirected - * @return the started process - * @throws IOException - */ - public static Process execute(ProcessBuilder pb, LineConsumer consumer) throws IOException { - pb.redirectErrorStream(true); - Process process = pb.start(); - - process(process, consumer); - - return process; - } - - public static boolean isAlive(Process process) { - return PROCESS_MANAGER.isAlive(process); - } - - public static void kill(Process process) { - PROCESS_MANAGER.kill(process); - } - - public static int system(String command) { - return PROCESS_MANAGER.system(command); - } - - private ProcessUtil() {} -} diff --git a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/UnixProcessManager.java b/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/UnixProcessManager.java deleted file mode 100644 index ca22f5b229..0000000000 --- a/core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/UnixProcessManager.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (c) 2012-2021 Red Hat, Inc. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - */ -package org.eclipse.che.api.core.util; - -import com.sun.jna.Library; -import com.sun.jna.Native; -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Process manager for *nix like system. - * - * @author andrew00x - */ -class UnixProcessManager extends ProcessManager { - /* - At the moment tested on linux only. - */ - - private static final Logger LOG = LoggerFactory.getLogger(UnixProcessManager.class); - - private static final CLibrary C_LIBRARY; - - private static final Field PID_FIELD; - private static final Method PID_METHOD; - - static { - CLibrary lib = null; - Field pidField = null; - Method pidMethod = null; - if (SystemInfo.isUnix()) { - try { - lib = ((CLibrary) Native.loadLibrary("c", CLibrary.class)); - } catch (Exception e) { - LOG.error("Cannot load native library", e); - } - try { - pidField = - Thread.currentThread() - .getContextClassLoader() - .loadClass("java.lang.UNIXProcess") - .getDeclaredField("pid"); - pidField.setAccessible(true); - } catch (Exception e) { - // try with Java9 - try { - pidMethod = Process.class.getDeclaredMethod("pid"); - } catch (NoSuchMethodException e1) { - LOG.error(e1.getMessage(), e1); - } - } - } - C_LIBRARY = lib; - PID_FIELD = pidField; - PID_METHOD = pidMethod; - } - - private static interface CLibrary extends Library { - // kill -l - int SIGKILL = 9; - int SIGTERM = 15; - - int kill(int pid, int signal); - - String strerror(int errno); - - int system(String cmd); - } - - private static final Pattern UNIX_PS_TABLE_PATTERN = Pattern.compile("\\s+"); - - @Override - public void kill(Process process) { - if (C_LIBRARY != null) { - killTree(getPid(process)); - } else { - throw new IllegalStateException("Cannot kill process. Not a UNIX system?"); - } - } - - private void killTree(int pid) { - final int[] children = getChildProcesses(pid); - LOG.debug("PID: {}, child PIDs: {}", pid, children); - if (children.length > 0) { - for (int cpid : children) { - killTree(cpid); // kill process tree recursively - } - } - int r = C_LIBRARY.kill(pid, CLibrary.SIGKILL); // kill origin process - LOG.debug("kill {}", pid); - if (r != 0) { - if (LOG.isDebugEnabled()) { - LOG.debug("kill for {} returns {}, strerror '{}'", pid, r, C_LIBRARY.strerror(r)); - } - } - } - - private int[] getChildProcesses(final int myPid) { - final String ps = "ps -e -o ppid,pid,comm"; /* PPID, PID, COMMAND */ - final List children = new ArrayList<>(); - final StringBuilder error = new StringBuilder(); - final LineConsumer stdout = - new LineConsumer() { - @Override - public void writeLine(String line) throws IOException { - if (line != null && !line.isEmpty()) { - final String[] tokens = UNIX_PS_TABLE_PATTERN.split(line.trim()); - if (tokens.length == 3 /* PPID, PID, COMMAND */) { - int ppid; - try { - ppid = Integer.parseInt(tokens[0]); - } catch (NumberFormatException e) { - // May be first line from process table: 'PPID PID COMMAND'. Skip it. - return; - } - if (ppid == myPid) { - int pid = Integer.parseInt(tokens[1]); - children.add(pid); - } - } - } - } - - @Override - public void close() throws IOException {} - }; - - final LineConsumer stderr = - new LineConsumer() { - @Override - public void writeLine(String line) throws IOException { - if (error.length() > 0) { - error.append('\n'); - } - error.append(line); - } - - @Override - public void close() throws IOException {} - }; - - try { - ProcessUtil.process(Runtime.getRuntime().exec(ps), stdout, stderr); - } catch (IOException e) { - throw new IllegalStateException(e); - } - - if (error.length() > 0) { - throw new IllegalStateException("Cannot get child processes: " + error.toString()); - } - final int size = children.size(); - final int[] result = new int[size]; - for (int i = 0; i < size; i++) { - result[i] = children.get(i); - } - return result; - } - - @Override - public boolean isAlive(Process process) { - return process.isAlive(); - } - - int getPid(Process process) { - if (PID_FIELD != null) { - try { - return ((Number) PID_FIELD.get(process)).intValue(); - } catch (IllegalAccessException e) { - throw new IllegalStateException("Cannot get process ID. Not a UNIX system?", e); - } - } else if (PID_METHOD != null) { - try { - return ((Long) PID_METHOD.invoke(process)).intValue(); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new IllegalStateException("Cannot get process ID. Not a UNIX system?", e); - } - } else { - throw new IllegalStateException("Cannot get process ID. Not a UNIX system?"); - } - } - - @Override - int system(String command) { - return C_LIBRARY.system(command); - } -} diff --git a/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java b/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java deleted file mode 100644 index d887198afe..0000000000 --- a/core/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2012-2018 Red Hat, Inc. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - */ -package org.eclipse.che.api.core.util; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import org.testng.Assert; -import org.testng.annotations.Test; - -/** @author andrew00x */ -public class ProcessUtilTest { - - @Test - public void testKill() throws Exception { - final Process p = Runtime.getRuntime().exec(new String[] {"ping", "google.com"}); - final List stdout = new ArrayList<>(); - final List stderr = new ArrayList<>(); - final IOException[] processError = new IOException[1]; - final CountDownLatch latch = new CountDownLatch(1); - final long start = System.currentTimeMillis(); - new Thread() { - public void run() { - try { - ProcessUtil.process( - p, - new LineConsumer() { - @Override - public void writeLine(String line) throws IOException { - stdout.add(line); - } - - @Override - public void close() throws IOException {} - }, - new LineConsumer() { - @Override - public void writeLine(String line) throws IOException { - stderr.add(line); - } - - @Override - public void close() throws IOException {} - }); - } catch (IOException e) { - processError[0] = e; // throw when kill process - } finally { - latch.countDown(); - } - } - }.start(); - - Thread.sleep(1000); // give time to start process - Assert.assertTrue(ProcessUtil.isAlive(p), "Process is not started."); - - ProcessUtil.kill(p); // kill process - - latch.await(15, TimeUnit.SECONDS); // should not stop here if process killed - final long end = System.currentTimeMillis(); - - Thread.sleep(200); - - // System process sleeps 10 seconds. It is safety to check we done in less then 3 sec. - Assert.assertFalse(ProcessUtil.isAlive(p)); - Assert.assertTrue((end - start) < 3000, "Fail kill process"); - - System.out.println(processError[0]); - // processError[0].printStackTrace(); - System.out.println(stdout); - System.out.println(stderr); - } -} diff --git a/pom.xml b/pom.xml index 1d61121fd8..f96b7fc853 100644 --- a/pom.xml +++ b/pom.xml @@ -515,11 +515,6 @@ mysql-connector-java ${mysql.connector.version}
- - net.java.dev.jna - jna - ${net.java.dev.jna.version} - net.logstash.logback logstash-logback-encoder