karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [karaf] 04/05: Deduplicate the java process management code in karaf
Date Sat, 11 Nov 2017 19:25:23 GMT
This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git

commit 631e9b0ab341a68f62631ffa9eb6c0486899913a
Author: Guillaume Nodet <gnodet@gmail.com>
AuthorDate: Tue Nov 7 15:48:19 2017 +0100

    Deduplicate the java process management code in karaf
---
 .../main/java/org/apache/karaf/jpm/Process.java    |  47 -------
 .../java/org/apache/karaf/jpm/ProcessBuilder.java  |  60 ---------
 .../apache/karaf/jpm/ProcessBuilderFactory.java    |  25 ----
 .../karaf/jpm/impl/ProcessBuilderFactoryImpl.java  |  27 ----
 .../apache/karaf/jpm/impl/ProcessBuilderImpl.java  |  48 -------
 .../org/apache/karaf/jpm/impl/ProcessImpl.java     | 149 ---------------------
 .../org/apache/karaf/jpm/impl/ScriptUtils.java     | 123 -----------------
 .../main/java/org/apache/karaf/jpm/Process.java    |   0
 .../java/org/apache/karaf/jpm/ProcessBuilder.java  |   0
 .../apache/karaf/jpm/ProcessBuilderFactory.java    |   0
 .../karaf/jpm/impl/ProcessBuilderFactoryImpl.java  |   0
 .../apache/karaf/jpm/impl/ProcessBuilderImpl.java  |   0
 .../org/apache/karaf/jpm/impl/ProcessImpl.java     |   0
 .../org/apache/karaf/jpm/impl/ScriptUtils.java     |   0
 .../org/apache/karaf/jpm/impl/unix/start.sh        |   0
 .../org/apache/karaf/jpm/impl/windows/destroy.vbs  |   0
 .../org/apache/karaf/jpm/impl/windows/running.vbs  |   0
 .../org/apache/karaf/jpm/impl/windows/start.vbs    |   0
 18 files changed, 479 deletions(-)

diff --git a/instance/src/main/java/org/apache/karaf/jpm/Process.java b/instance/src/main/java/org/apache/karaf/jpm/Process.java
deleted file mode 100644
index 4f04d1a..0000000
--- a/instance/src/main/java/org/apache/karaf/jpm/Process.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.jpm;
-
-import java.io.IOException;
-import java.io.Serializable;
-
-/**
- * Interface representing a process
- */
-public interface Process extends Serializable {
-
-    /**
-     * Retrieves the PID of the process
-     * @return the pid
-     */
-    int getPid();
-
-    /**
-     * Check if this process is still running
-     * @return <code>true</code> if the process is running
-     * @throws IOException if an error occurs
-     */
-    boolean isRunning() throws IOException;
-
-    /**
-     * Destroy the process.
-     *
-     * @throws IOException If an error occurs.
-     */
-    void destroy() throws IOException;
-
-}
diff --git a/instance/src/main/java/org/apache/karaf/jpm/ProcessBuilder.java b/instance/src/main/java/org/apache/karaf/jpm/ProcessBuilder.java
deleted file mode 100644
index 7b81bf2..0000000
--- a/instance/src/main/java/org/apache/karaf/jpm/ProcessBuilder.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.jpm;
-
-import java.io.File;
-import java.io.IOException;
-
-/**
- * Interface used to create new processes.
- */
-public interface ProcessBuilder {
-
-    /**
-     * Specify the current directory to run the command from.
-     *
-     * @param dir The directory to run the command from.
-     * @return The {@link ProcessBuilder} instance.
-     */
-    ProcessBuilder directory(File dir);
-
-    /**
-     * Set the command to execute.
-     *
-     * @param command The command to execute.
-     * @return The {@link ProcessBuilder} instance.
-     */
-    ProcessBuilder command(String command);
-
-    /**
-     * Create and start the process.
-     *
-     * @return The process that has been started.
-     * @throws IOException If the process can not be created.
-     */
-    org.apache.karaf.jpm.Process start() throws IOException;
-
-    /**
-     * Attach to an existing process.
-     *
-     * @param pid The process PID to attach.
-     * @return The process that has been attached.
-     * @throws IOException if the process can not be attached to.
-     */
-    org.apache.karaf.jpm.Process attach(int pid) throws IOException;
-
-}
diff --git a/instance/src/main/java/org/apache/karaf/jpm/ProcessBuilderFactory.java b/instance/src/main/java/org/apache/karaf/jpm/ProcessBuilderFactory.java
deleted file mode 100644
index baa563b..0000000
--- a/instance/src/main/java/org/apache/karaf/jpm/ProcessBuilderFactory.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.jpm;
-
-
-/**
- * Factory for process builders.
- */
-public interface ProcessBuilderFactory {
-    ProcessBuilder newBuilder();
-}
diff --git a/instance/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderFactoryImpl.java
b/instance/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderFactoryImpl.java
deleted file mode 100644
index 12a6905..0000000
--- a/instance/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderFactoryImpl.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.jpm.impl;
-
-import org.apache.karaf.jpm.ProcessBuilder;
-import org.apache.karaf.jpm.ProcessBuilderFactory;
-
-public class ProcessBuilderFactoryImpl implements ProcessBuilderFactory {
-
-    public ProcessBuilder newBuilder() {
-        return new ProcessBuilderImpl();
-    }
-}
diff --git a/main/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderImpl.java b/main/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderImpl.java
deleted file mode 100644
index 4beee74..0000000
--- a/main/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderImpl.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.jpm.impl;
-
-import org.apache.karaf.jpm.Process;
-import org.apache.karaf.jpm.ProcessBuilder;
-
-import java.io.File;
-import java.io.IOException;
-
-
-public class ProcessBuilderImpl implements ProcessBuilder {
-
-    private File dir;
-    private String command;
-
-    public ProcessBuilder directory(File dir) {
-        this.dir = dir;
-        return this;
-    }
-
-    public ProcessBuilder command(String command) {
-        this.command = command;
-        return this;
-    }
-
-    public Process start() throws IOException {
-        return ProcessImpl.create(dir, command);
-    }
-
-    public Process attach(int pid) throws IOException {
-        return ProcessImpl.attach(pid);
-    }
-}
diff --git a/main/src/main/java/org/apache/karaf/jpm/impl/ProcessImpl.java b/main/src/main/java/org/apache/karaf/jpm/impl/ProcessImpl.java
deleted file mode 100644
index 2db2778..0000000
--- a/main/src/main/java/org/apache/karaf/jpm/impl/ProcessImpl.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.jpm.impl;
-
-import org.apache.karaf.jpm.Process;
-
-import java.io.*;
-import java.util.HashMap;
-import java.util.Map;
-
-public class ProcessImpl implements Process {
-
-    /**
-     * 
-     */
-    private static final long serialVersionUID = -8140632422386086507L;
-
-    private int pid;
-    //private File input;
-    //private File output;
-    //private File error;
-
-    public ProcessImpl(int pid/*, File input, File output, File error*/) {
-        this.pid = pid;
-        //this.input = input;
-        //this.output = output;
-        //this.error = error;
-    }
-
-    public int getPid() {
-        return pid;
-    }
-
-    public boolean isRunning() throws IOException {
-        if (ScriptUtils.isWindows()) {
-            Map<String, String> props = new HashMap<>();
-            props.put("${pid}", Integer.toString(pid));
-            int ret = ScriptUtils.execute("running", props);
-            return ret == 0;
-        } else {
-            try {
-                java.lang.Process process = new java.lang.ProcessBuilder("ps", "-p", Integer.toString(pid)).start();
-                BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
-                r.readLine(); // skip headers
-                String s = r.readLine();
-                boolean running = s != null && s.length() > 0;
-                process.waitFor();
-                return running;
-            } catch (InterruptedException e) {
-                throw new InterruptedIOException();
-            }
-        }
-    }
-
-    public void destroy() throws IOException {
-        int ret;
-        if (ScriptUtils.isWindows()) {
-            Map<String, String> props = new HashMap<>();
-            props.put("${pid}", Integer.toString(pid));
-            ret = ScriptUtils.execute("destroy", props);
-        } else {
-            ret = ScriptUtils.executeProcess(new ProcessBuilder("kill", "-9", Integer.toString(pid)));
-        }
-        if (ret != 0) {
-            throw new IOException("Unable to destroy process, it may already be terminated");
-        }
-    }
-
-    /*
-    public OutputStream getInputStream() throws FileNotFoundException {
-        return new FileOutputStream(input);
-    }
-
-    public InputStream getOutputStream() throws FileNotFoundException {
-        return new FileInputStream(output);
-    }
-
-    public InputStream getErrorStream() throws FileNotFoundException {
-        return new FileInputStream(error);
-    }
-    */
-
-    public int waitFor() throws InterruptedException {
-        return 0;
-    }
-
-    public int exitValue() {
-        return 0;
-    }
-
-    public static Process create(File dir, String command) throws IOException {
-        //File input = File.createTempFile("jpm.", ".input");
-        //File output = File.createTempFile("jpm.", ".output");
-        //File error = File.createTempFile("jpm.", ".error");
-        File pidFile = File.createTempFile("jpm.", ".pid");
-        try {
-            Map<String, String> props = new HashMap<>();
-            //props.put("${in.file}", input.getCanonicalPath());
-            //props.put("${out.file}", output.getCanonicalPath());
-            //props.put("${err.file}", error.getCanonicalPath());
-            props.put("${pid.file}", pidFile.getCanonicalPath());
-            props.put("${dir}", dir != null ? dir.getCanonicalPath() : "");
-            if (ScriptUtils.isWindows()) {
-                command = command.replaceAll("\"", "\"\"");
-            }
-            props.put("${command}", command);
-            int ret = ScriptUtils.execute("start", props);
-            if (ret != 0) {
-                throw new IOException("Unable to create process (error code: " + ret + ")");
-            }
-            int pid = readPid(pidFile);
-            return new ProcessImpl(pid/*, input, output, error*/);
-        } finally {
-            pidFile.delete();
-        }
-    }
-
-    public static Process attach(int pid) throws IOException {
-        return new ProcessImpl(pid);
-    }
-
-    private static int readPid(File pidFile) throws IOException {
-        InputStream is = new FileInputStream(pidFile);
-        try {
-            BufferedReader r = new BufferedReader(new InputStreamReader(is));
-            String pidString = r.readLine();
-            return Integer.valueOf(pidString);
-        } finally {
-            try {
-                is.close();
-            } catch (IOException e) {}
-        }
-    }
-
-}
diff --git a/main/src/main/java/org/apache/karaf/jpm/impl/ScriptUtils.java b/main/src/main/java/org/apache/karaf/jpm/impl/ScriptUtils.java
deleted file mode 100644
index bb58f81..0000000
--- a/main/src/main/java/org/apache/karaf/jpm/impl/ScriptUtils.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.jpm.impl;
-
-import java.io.*;
-import java.util.Map;
-import java.util.Scanner;
-
-public class ScriptUtils {
-
-    public static int execute(String name, Map<String, String> props) throws IOException
{
-        File script = File.createTempFile("jpm.", ".script");
-        try {
-            if (isWindows()) {
-                String res = "windows/" + name + ".vbs";
-                ScriptUtils.copyFilteredResource(res, script, props);
-                return executeProcess(new ProcessBuilder("cscript",
-                                                                   "/NOLOGO",
-                                                                   "//E:vbs",
-                                                                   script.getCanonicalPath()));
-            } else {
-                String res = "unix/" + name + ".sh";
-                ScriptUtils.copyFilteredResource(res, script, props);
-                return executeProcess(new ProcessBuilder("/bin/sh",
-                                                                   script.getCanonicalPath()));
-            }
-        } finally {
-            script.delete();
-        }
-    }
-
-    public static int executeProcess(ProcessBuilder builder) throws IOException {
-        try {
-            Process process = builder.start();
-            return process.waitFor();
-        } catch (InterruptedException e) {
-            throw new InterruptedIOException();
-        }
-    }
-
-    public static void copyFilteredResource(String resource, File outFile, Map<String,
String> props) throws IOException {
-        InputStream is = null;
-        try {
-            is = ScriptUtils.class.getResourceAsStream(resource);
-            // Read it line at a time so that we can use the platform line ending when we
write it out.
-            PrintStream out = new PrintStream(new FileOutputStream(outFile));
-            try {
-                Scanner scanner = new Scanner(is);
-                while (scanner.hasNextLine() ) {
-                    String line = scanner.nextLine();
-                    line = filter(line, props);
-                    out.println(line);
-                }
-                scanner.close();
-            } finally {
-                safeClose(out);
-            }
-        } finally {
-            safeClose(is);
-        }
-    }
-
-    private static void safeClose(InputStream is) throws IOException {
-        if (is == null) {
-            return;
-        }
-        try {
-            is.close();
-        } catch (Throwable ignore) {
-        }
-    }
-
-    private static void safeClose(OutputStream is) throws IOException {
-        if (is == null) {
-            return;
-        }
-        try {
-            is.close();
-        } catch (Throwable ignore) {
-        }
-    }
-
-    private static String filter(String line, Map<String, String> props) {
-        for (Map.Entry<String, String> i : props.entrySet()) {
-            int p1 = line.indexOf(i.getKey());
-            if( p1 >= 0 ) {
-                String l1 = line.substring(0, p1);
-                String l2 = line.substring(p1+i.getKey().length());
-                line = l1+i.getValue()+l2;
-            }
-        }
-        return line;
-    }
-
-    private static final boolean windows;
-
-    static {
-        windows = System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
-    }
-
-    public static boolean isWindows() {
-        return windows;
-    }
-
-    public static String getJavaCommandPath() throws IOException {
-        return new File(System.getProperty("java.home"), isWindows() ? "bin\\java.exe" :
"bin/java").getCanonicalPath();
-    }
-
-}
diff --git a/main/src/main/java/org/apache/karaf/jpm/Process.java b/util/src/main/java/org/apache/karaf/jpm/Process.java
similarity index 100%
rename from main/src/main/java/org/apache/karaf/jpm/Process.java
rename to util/src/main/java/org/apache/karaf/jpm/Process.java
diff --git a/main/src/main/java/org/apache/karaf/jpm/ProcessBuilder.java b/util/src/main/java/org/apache/karaf/jpm/ProcessBuilder.java
similarity index 100%
rename from main/src/main/java/org/apache/karaf/jpm/ProcessBuilder.java
rename to util/src/main/java/org/apache/karaf/jpm/ProcessBuilder.java
diff --git a/main/src/main/java/org/apache/karaf/jpm/ProcessBuilderFactory.java b/util/src/main/java/org/apache/karaf/jpm/ProcessBuilderFactory.java
similarity index 100%
rename from main/src/main/java/org/apache/karaf/jpm/ProcessBuilderFactory.java
rename to util/src/main/java/org/apache/karaf/jpm/ProcessBuilderFactory.java
diff --git a/main/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderFactoryImpl.java b/util/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderFactoryImpl.java
similarity index 100%
rename from main/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderFactoryImpl.java
rename to util/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderFactoryImpl.java
diff --git a/instance/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderImpl.java b/util/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderImpl.java
similarity index 100%
rename from instance/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderImpl.java
rename to util/src/main/java/org/apache/karaf/jpm/impl/ProcessBuilderImpl.java
diff --git a/instance/src/main/java/org/apache/karaf/jpm/impl/ProcessImpl.java b/util/src/main/java/org/apache/karaf/jpm/impl/ProcessImpl.java
similarity index 100%
rename from instance/src/main/java/org/apache/karaf/jpm/impl/ProcessImpl.java
rename to util/src/main/java/org/apache/karaf/jpm/impl/ProcessImpl.java
diff --git a/instance/src/main/java/org/apache/karaf/jpm/impl/ScriptUtils.java b/util/src/main/java/org/apache/karaf/jpm/impl/ScriptUtils.java
similarity index 100%
rename from instance/src/main/java/org/apache/karaf/jpm/impl/ScriptUtils.java
rename to util/src/main/java/org/apache/karaf/jpm/impl/ScriptUtils.java
diff --git a/instance/src/main/resources/org/apache/karaf/jpm/impl/unix/start.sh b/util/src/main/resources/org/apache/karaf/jpm/impl/unix/start.sh
similarity index 100%
rename from instance/src/main/resources/org/apache/karaf/jpm/impl/unix/start.sh
rename to util/src/main/resources/org/apache/karaf/jpm/impl/unix/start.sh
diff --git a/instance/src/main/resources/org/apache/karaf/jpm/impl/windows/destroy.vbs b/util/src/main/resources/org/apache/karaf/jpm/impl/windows/destroy.vbs
similarity index 100%
rename from instance/src/main/resources/org/apache/karaf/jpm/impl/windows/destroy.vbs
rename to util/src/main/resources/org/apache/karaf/jpm/impl/windows/destroy.vbs
diff --git a/instance/src/main/resources/org/apache/karaf/jpm/impl/windows/running.vbs b/util/src/main/resources/org/apache/karaf/jpm/impl/windows/running.vbs
similarity index 100%
rename from instance/src/main/resources/org/apache/karaf/jpm/impl/windows/running.vbs
rename to util/src/main/resources/org/apache/karaf/jpm/impl/windows/running.vbs
diff --git a/instance/src/main/resources/org/apache/karaf/jpm/impl/windows/start.vbs b/util/src/main/resources/org/apache/karaf/jpm/impl/windows/start.vbs
similarity index 100%
rename from instance/src/main/resources/org/apache/karaf/jpm/impl/windows/start.vbs
rename to util/src/main/resources/org/apache/karaf/jpm/impl/windows/start.vbs

-- 
To stop receiving notification emails like this one, please contact
"commits@karaf.apache.org" <commits@karaf.apache.org>.

Mime
View raw message