Mercurial > hg > thermostat-ng > agent
changeset 2467:4afe4cd04523
Changes to refactor thermostat-osgi-process-handler
- rename to get rid of 'osgi' and 'unix' in API names.
- separate out platform specific code
- (next) move os-specific code from other packages as practical
Reviewed-by: neugens,sgehwolf,jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-September/021044.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-September/021099.html
line wrap: on
line diff
--- a/agent/cli/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/agent/cli/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -93,7 +93,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/agent/core/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/agent/core/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -120,7 +120,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/client/cli/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/client/cli/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -97,7 +97,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/distribution/assembly/core-assembly-windows.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/distribution/assembly/core-assembly-windows.xml Fri Sep 30 10:15:14 2016 -0400 @@ -67,7 +67,7 @@ <include>com.redhat.thermostat:thermostat-agent-ipc-tcpsocket-client</include--> <include>com.redhat.thermostat:thermostat-common-core</include> <include>com.redhat.thermostat:thermostat-common-command</include> - <include>com.redhat.thermostat:thermostat-osgi-process-handler</include> + <include>com.redhat.thermostat:thermostat-process-handler</include> <include>com.redhat.thermostat:thermostat-storage-cli</include> <include>com.redhat.thermostat:thermostat-storage-mongodb</include> <!--include>com.redhat.thermostat:thermostat-keyring</include-->
--- a/distribution/assembly/core-assembly.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/distribution/assembly/core-assembly.xml Fri Sep 30 10:15:14 2016 -0400 @@ -65,7 +65,7 @@ <include>com.redhat.thermostat:thermostat-agent-ipc-unixsocket-client</include> <include>com.redhat.thermostat:thermostat-common-core</include> <include>com.redhat.thermostat:thermostat-common-command</include> - <include>com.redhat.thermostat:thermostat-osgi-process-handler</include> + <include>com.redhat.thermostat:thermostat-process-handler</include> <include>com.redhat.thermostat:thermostat-storage-cli</include> <include>com.redhat.thermostat:thermostat-storage-mongodb</include> <include>com.redhat.thermostat:thermostat-keyring</include>
--- a/distribution/packaging/fedora/0002_shared_osgi_spec_fixes.patch Wed Sep 28 11:35:58 2016 -0400 +++ b/distribution/packaging/fedora/0002_shared_osgi_spec_fixes.patch Fri Sep 30 10:15:14 2016 -0400 @@ -1046,9 +1046,9 @@ </dependency> <dependency> -diff --git a/unix-process-handler/pom.xml b/unix-process-handler/pom.xml ---- a/unix-process-handler/pom.xml -+++ b/unix-process-handler/pom.xml +diff --git a/process-handler/pom.xml b/process-handler/pom.xml +--- a/process-handler/pom.xml ++++ b/process-handler/pom.xml @@ -77,14 +77,8 @@ </dependency>
--- a/distribution/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/distribution/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -536,7 +536,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/killvm/agent/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/killvm/agent/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -99,7 +99,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/Activator.java Wed Sep 28 11:35:58 2016 -0400 +++ b/killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/Activator.java Fri Sep 30 10:15:14 2016 -0400 @@ -36,13 +36,13 @@ package com.redhat.thermostat.killvm.agent.internal; +import com.redhat.thermostat.service.process.ProcessHandler; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; import com.redhat.thermostat.agent.command.ReceiverRegistry; -import com.redhat.thermostat.service.process.UNIXProcessHandler; public class Activator implements BundleActivator { @@ -53,10 +53,10 @@ public void start(final BundleContext context) { registry = new ReceiverRegistry(context); - killActionTracker = new ServiceTracker(context, UNIXProcessHandler.class, null) { + killActionTracker = new ServiceTracker(context, ProcessHandler.class, null) { @Override public Object addingService(ServiceReference reference) { - UNIXProcessHandler processHandler = (UNIXProcessHandler) super.addingService(reference); + ProcessHandler processHandler = (ProcessHandler) super.addingService(reference); registry.registerReceiver(new KillVmReceiver(processHandler)); return processHandler; }
--- a/killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/KillVmReceiver.java Wed Sep 28 11:35:58 2016 -0400 +++ b/killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/KillVmReceiver.java Fri Sep 30 10:15:14 2016 -0400 @@ -44,29 +44,29 @@ import com.redhat.thermostat.common.command.Response; import com.redhat.thermostat.common.command.Response.ResponseType; import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.service.process.UNIXSignal; public class KillVmReceiver implements RequestReceiver { - private final UNIXProcessHandler unixService; + private final ProcessHandler processService; private static final Logger log = LoggingUtils.getLogger(KillVmReceiver.class); - public KillVmReceiver(UNIXProcessHandler unixService) { - this.unixService = unixService; + public KillVmReceiver(ProcessHandler theService) { + this.processService = theService; } @Override public Response receive(Request request) { - if (unixService == null) { + if (processService == null) { // no dice, should have service by now - log.severe("Unix service null!"); + log.severe("Process service is null!"); return new Response(ResponseType.ERROR); } String strPid = request.getParameter("vm-pid"); try { Integer pid = Integer.parseInt(strPid); - unixService.sendSignal(pid, UNIXSignal.TERM); + processService.sendSignal(pid, UNIXSignal.TERM); log.fine("Killed VM with PID " + pid); return new Response(ResponseType.OK); } catch (NumberFormatException e) {
--- a/killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/ActivatorTest.java Wed Sep 28 11:35:58 2016 -0400 +++ b/killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/ActivatorTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -43,7 +43,7 @@ import org.junit.Test; import com.redhat.thermostat.agent.command.RequestReceiver; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.testutils.StubBundleContext; public class ActivatorTest { @@ -66,7 +66,7 @@ public void verifyKillReciverIsRegistered() { StubBundleContext ctx = new StubBundleContext(); - ctx.registerService(UNIXProcessHandler.class, mock(UNIXProcessHandler.class), null); + ctx.registerService(ProcessHandler.class, mock(ProcessHandler.class), null); Activator activator = new Activator(); activator.start(ctx);
--- a/killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/KillVmReceiverTest.java Wed Sep 28 11:35:58 2016 -0400 +++ b/killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/KillVmReceiverTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -44,19 +44,18 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import com.redhat.thermostat.service.process.ProcessHandler; import org.junit.Test; import com.redhat.thermostat.common.command.Request; import com.redhat.thermostat.common.command.Response; import com.redhat.thermostat.common.command.Response.ResponseType; -import com.redhat.thermostat.killvm.agent.internal.KillVmReceiver; -import com.redhat.thermostat.service.process.UNIXProcessHandler; public class KillVmReceiverTest { @Test public void receiverReturnsOk() { - UNIXProcessHandler proc = mock(UNIXProcessHandler.class); + ProcessHandler proc = mock(ProcessHandler.class); KillVmReceiver receiver = new KillVmReceiver(proc); Request req = mock(Request.class); when(req.getParameter("vm-pid")).thenReturn("12345"); @@ -66,7 +65,7 @@ @Test public void receiverReturnsErrorNoPid() { - UNIXProcessHandler proc = mock(UNIXProcessHandler.class); + ProcessHandler proc = mock(ProcessHandler.class); KillVmReceiver receiver = new KillVmReceiver(proc); Request req = mock(Request.class); Response response = receiver.receive(req); @@ -75,7 +74,7 @@ @Test public void receiverReturnsErrorBadPid() { - UNIXProcessHandler proc = mock(UNIXProcessHandler.class); + ProcessHandler proc = mock(ProcessHandler.class); KillVmReceiver receiver = new KillVmReceiver(proc); Request req = mock(Request.class); when(req.getParameter("vm-pid")).thenReturn("hi"); @@ -110,8 +109,8 @@ fail("com.redhat.thermostat.agent.killvm.internal.KillVmReceiver class not found, but used by some request!"); } try { - Constructor<?> constructor = receiver.getConstructor(UNIXProcessHandler.class); - UNIXProcessHandler service = mock(UNIXProcessHandler.class); + Constructor<?> constructor = receiver.getConstructor(ProcessHandler.class); + ProcessHandler service = mock(ProcessHandler.class); Object instance = constructor.newInstance(service); Method m = receiver.getMethod("receive", Request.class); Request req = mock(Request.class);
--- a/killvm/client-swing/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/killvm/client-swing/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -135,7 +135,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -71,9 +71,6 @@ <sharedlib.prefix>lib</sharedlib.prefix> <sharedlib.suffix>.so</sharedlib.suffix> </properties> - <modules> - <module>keyring</module> - </modules> </profile> <profile> @@ -92,7 +89,6 @@ </properties> </profile> - <profile> <!-- Some thermostat code uses sun.jvmstat and com.sun.tools.attach. These are only available via tools.jar prior to JDK 9 --> @@ -347,7 +343,8 @@ <module>common</module> <module>agent</module> <module>client</module> - <module>unix-process-handler</module> + <module>keyring</module> + <module>process-handler</module> <module>thread</module> <module>killvm</module> <module>web</module>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,97 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright 2012-2016 Red Hat, Inc. + + This file is part of Thermostat. + + Thermostat is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + Thermostat is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Thermostat; see the file COPYING. If not see + <http://www.gnu.org/licenses/>. + + Linking this code with other modules is making a combined work + based on this code. Thus, the terms and conditions of the GNU + General Public License cover the whole combination. + + As a special exception, the copyright holders of this code give + you permission to link this code with independent modules to + produce an executable, regardless of the license terms of these + independent modules, and to copy and distribute the resulting + executable under terms of your choice, provided that you also + meet, for each linked independent module, the terms and conditions + of the license of that module. An independent module is a module + which is not derived from or based on this code. If you modify + this code, you may extend this exception to your version of the + library, but you are not obligated to do so. If you do not wish + to do so, delete this exception statement from your version. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <artifactId>thermostat</artifactId> + <groupId>com.redhat.thermostat</groupId> + <version>1.99.12-SNAPSHOT</version> + </parent> + + <artifactId>thermostat-process-handler</artifactId> + <packaging>bundle</packaging> + <name>Thermostat Process handler plugin</name> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Private-Package>com.redhat.thermostat.service.internal,com.redhat.thermostat.service.internal.unix,com.redhat.thermostat.service.internal.windows</Private-Package> + <Export-Package>com.redhat.thermostat.service.process</Export-Package> + <Bundle-Activator>com.redhat.thermostat.service.internal.Activator</Bundle-Activator> + <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> + <Bundle-SymbolicName>com.redhat.thermostat.process</Bundle-SymbolicName> + <!-- Do not autogenerate uses clauses in Manifests --> + <_nouses>true</_nouses> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-common-core</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/internal/Activator.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,67 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal; + +import java.util.Hashtable; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.redhat.thermostat.service.process.ProcessHandler; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +import com.redhat.thermostat.common.utils.LoggingUtils; + +public class Activator implements BundleActivator { + + private static final Logger logger = LoggingUtils.getLogger(Activator.class); + + @Override + public void start(BundleContext context) throws Exception { + logger.log(Level.INFO, "activating thermostat-process bundles"); + + Hashtable<String, String> props = new Hashtable<String, String>(); + props.put(ProcessHandler.ID, ProcessHandler.ID); + context.registerService(ProcessHandler.class.getName(), ProcessHandlerImpl.getInstance(), props); + } + + @Override + public void stop(BundleContext context) throws Exception { + /* nothing to do here */ + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/internal/ProcessHandlerImpl.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,66 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal; + +import com.redhat.thermostat.service.internal.unix.UnixProcessUtilities; +import com.redhat.thermostat.service.internal.windows.WindowsProcessUtilities; +import com.redhat.thermostat.service.process.ProcessHandler; +import com.redhat.thermostat.service.process.UNIXSignal; + +public class ProcessHandlerImpl implements ProcessHandler { + + private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); + + private static final ProcessUtilitiesBase proxy = IS_UNIX ? new UnixProcessUtilities() : new WindowsProcessUtilities(); + + public static ProcessHandler getInstance() { + return proxy; + } + + protected ProcessHandlerImpl() {} + + @Override + public void sendSignal(Integer pid, UNIXSignal signal) { + proxy.sendSignal(pid, signal); + } + + @Override + public String getProcessName(Integer pid) { + return proxy.getProcessName(pid); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/internal/ProcessUtilitiesBase.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,105 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal; + +import com.redhat.thermostat.common.tools.ApplicationException; +import com.redhat.thermostat.common.tools.ProcessStartException; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.service.process.ProcessHandler; +import com.redhat.thermostat.service.process.UNIXSignal; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +abstract public class ProcessUtilitiesBase implements ProcessHandler { + + private static final Logger logger = LoggingUtils.getLogger(ProcessUtilitiesBase.class); + + @Override + public String getProcessName(Integer pid) { + + String result = null; + + final List<String> commandLine = buildCommandLine(pid); + + try { + Process process = createAndRunProcess(commandLine); + BufferedReader reader = getProcessOutput(process); + result = processStdout(reader); + + } catch (IOException | ApplicationException e) { + logger.log(Level.WARNING, "can't run '" + commandLine.get(0) + "'!", e); + } + + return result; + } + + protected BufferedReader getProcessOutput(Process process) { + InputStream in = process.getInputStream(); + InputStreamReader isr = new InputStreamReader(in); + return new BufferedReader(isr); + } + + protected Process createAndRunProcess(List<String> args) throws IOException, ApplicationException { + ProcessBuilder builder = new ProcessBuilder(args); + Process process = null; + try { + process = builder.start(); + } catch (IOException e) { + throw new ProcessStartException(args.get(0), e); + } + return process; + } + + protected void exec(String command) { + Runtime rt = Runtime.getRuntime(); + try { + rt.exec(command); + } catch (IOException e) { + logger.log(Level.WARNING, "can't run kill!", e); + } + } + + public abstract void sendSignal(Integer pid, UNIXSignal signal); + protected abstract String processStdout(BufferedReader outStr) throws IOException; + protected abstract List<String> buildCommandLine(Integer pid); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/internal/unix/UnixProcessUtilities.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,74 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal.unix; + +import com.redhat.thermostat.service.internal.ProcessUtilitiesBase; +import com.redhat.thermostat.service.process.UNIXSignal; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class UnixProcessUtilities extends ProcessUtilitiesBase { + + @Override + public void sendSignal(Integer pid, UNIXSignal signal) { + exec("kill -s " + signal.signalName() + " " + pid); + } + + @Override + protected List<String> buildCommandLine(Integer pid) { + final List<String> commandLine = new ArrayList<>(); + commandLine.add("ps"); + commandLine.add("--no-heading"); + commandLine.add("-p"); + commandLine.add(String.valueOf(pid)); + return commandLine; + } + + @Override + protected String processStdout(final BufferedReader out) throws IOException { + final String outStr = out.readLine(); + if (outStr == null) + return null; + final String [] output = outStr.split(" "); + return output[output.length - 1]; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/internal/windows/WindowsProcessUtilities.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,81 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal.windows; + +import com.redhat.thermostat.service.internal.ProcessUtilitiesBase; +import com.redhat.thermostat.service.process.UNIXSignal; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class WindowsProcessUtilities extends ProcessUtilitiesBase { + + @Override + protected List<String> buildCommandLine(Integer pid) { + final List<String> commandLine = new ArrayList<>(); + commandLine.add("tasklist"); + commandLine.add("/FO"); + commandLine.add("csv"); + commandLine.add("/FI"); + commandLine.add("\"PID eq " + String.valueOf(pid) + "\""); + return commandLine; + } + + @Override + protected String processStdout(final BufferedReader out) throws IOException { + out.readLine(); // skip header line + final String outStr = out.readLine(); + if (outStr == null) + return null; + final String [] output = outStr.split(","); + String result = output[0]; + if (result.length() >= 2 && result.charAt(0) == '"') + result = result.replace("\"",""); + result = result.replace(".exe",""); + return result; + } + + @Override + public void sendSignal(Integer pid, UNIXSignal signal) { + // TODO - port to Windows + exec("kill -s " + signal.signalName() + " " + pid); + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/process/ProcessHandler.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,57 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.process; + +import com.redhat.thermostat.annotations.Service; + +@Service +public interface ProcessHandler { + + public static String ID = "com.redhat.thermostat.service.process.ProcessHandler"; + + /** + * Sends the given {@link UNIXSignal} to the process indicated by + * {@code pid}. + */ + void sendSignal(Integer pid, UNIXSignal signal); + + /** + * Gets the process name given its {@code pid}. + */ + String getProcessName(Integer pid); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/process/UNIXSignal.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.process; + +public enum UNIXSignal { + + TERM, + KILL; + + public String signalName() { + return name().toLowerCase(); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/process/package-info.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + + +/** + * Interact directly with processes. + * + * This package provides APIs for interacting directly with native + * proceses to provide some features not available through the standard + * Java API. + */ +package com.redhat.thermostat.service.process; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/test/java/com/redhat/thermostat/service/internal/ProcessHendlerImplTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal; + +import com.redhat.thermostat.service.internal.unix.UnixProcessUtilities; +import com.redhat.thermostat.service.internal.windows.WindowsProcessUtilities; +import com.redhat.thermostat.service.process.ProcessHandler; + +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; + +public class ProcessHendlerImplTest { + + private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); + + @Test + public void testCorrectImplementation() { + + final ProcessHandler hnd = ProcessHandlerImpl.getInstance(); + assertNotNull(hnd); + + if (IS_UNIX) { + assert(hnd instanceof UnixProcessUtilities); + } else { + assert(hnd instanceof WindowsProcessUtilities); + } + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/test/java/com/redhat/thermostat/service/internal/unix/UnixProcessUtilitiesTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,138 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal.unix; + +import com.redhat.thermostat.service.process.ProcessHandler; +import com.redhat.thermostat.service.process.UNIXSignal; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +public class UnixProcessUtilitiesTest { + + private BufferedReader reader; + private BufferedReader emptyReader; + + private List<String> processArguments = new ArrayList<>(); + private ProcessHandler process; + + @Before + public void setUp() { + + String data = "123 fluff"; + reader = new BufferedReader(new StringReader(data)); + emptyReader = new BufferedReader(new StringReader("")); + + processArguments.clear(); + + process = new UnixProcessUtilities() { + @Override + protected Process createAndRunProcess(List<String> args) + throws IOException { + processArguments.addAll(args); + return null; + } + + @Override + protected void exec(String command) { + processArguments.add(command); + } + + public BufferedReader getProcessOutput(Process process) { + return reader; + } + }; + } + + @Test + public void sendKillSignalTest() { + + process.sendSignal(12345, UNIXSignal.KILL); + + Assert.assertTrue(processArguments.contains("kill -s kill 12345")); + Assert.assertEquals(1, processArguments.size()); + } + + @Test + public void sendTermSignalTest() { + + process.sendSignal(12345, UNIXSignal.TERM); + + Assert.assertTrue(processArguments.contains("kill -s term 12345")); + Assert.assertEquals(1, processArguments.size()); + } + + @Test + public void getProcessName() { + + String result = process.getProcessName(12345); + Assert.assertEquals("fluff", result); + + Assert.assertTrue(processArguments.contains("12345")); + Assert.assertTrue(processArguments.contains("ps")); + Assert.assertTrue(processArguments.contains("--no-heading")); + Assert.assertTrue(processArguments.contains("-p")); + } + + @Test + public void getProcessNameNoOutput() { + + // redefine, since we need an empty reader + final ProcessHandler process = new UnixProcessUtilities() { + @Override + protected Process createAndRunProcess(List<String> args) + throws IOException { + processArguments.addAll(args); + return null; + } + + public BufferedReader getProcessOutput(Process process) { + return emptyReader; + } + }; + + String result = process.getProcessName(12345); + Assert.assertNull(result); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/process-handler/src/test/java/com/redhat/thermostat/service/internal/windows/WindowsProcessUtilitiesTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -0,0 +1,138 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.internal.windows; + +import com.redhat.thermostat.service.process.ProcessHandler; +import com.redhat.thermostat.service.process.UNIXSignal; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +public class WindowsProcessUtilitiesTest { + + private BufferedReader reader; + private BufferedReader emptyReader; + + private List<String> processArguments = new ArrayList<>(); + private ProcessHandler process; + + @Before + public void setUp() { + + String data = "headerline\r\n\"fluff.exe\",\"1868\",\"Console\",\"1\",\"25,952 K\""; + reader = new BufferedReader(new StringReader(data)); + emptyReader = new BufferedReader(new StringReader("")); + + processArguments.clear(); + + process = new WindowsProcessUtilities() { + @Override + protected Process createAndRunProcess(List<String> args) + throws IOException { + processArguments.addAll(args); + return null; + } + + @Override + protected void exec(String command) { + processArguments.add(command); + } + + @Override + public BufferedReader getProcessOutput(Process process) { + return reader; + } + }; + } + + @Test + public void sendKillSignalTest() { + + process.sendSignal(12345, UNIXSignal.KILL); + + Assert.assertTrue(processArguments.contains("kill -s kill 12345")); + Assert.assertEquals(1, processArguments.size()); + } + + @Test + public void sendTermSignalTest() { + + process.sendSignal(12345, UNIXSignal.TERM); + + Assert.assertTrue(processArguments.contains("kill -s term 12345")); + Assert.assertEquals(1, processArguments.size()); + } + + @Test + public void getProcessName() { + + String result = process.getProcessName(12345); + Assert.assertEquals("fluff", result); + Assert.assertTrue(processArguments.contains("\"PID eq 12345\"")); + Assert.assertTrue(processArguments.contains("tasklist")); + } + + @Test + public void getProcessNameNoOutput() { + + // redefine, since we need an empty reader + final ProcessHandler process = new WindowsProcessUtilities() { + @Override + protected Process createAndRunProcess(List<String> args) + throws IOException { + processArguments.addAll(args); + return null; + } + + @Override + public BufferedReader getProcessOutput(Process process) { + return emptyReader; + } + }; + + + String result = process.getProcessName(12345); + Assert.assertNull(result); + } +} +
--- a/setup/command/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/setup/command/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -118,7 +118,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> </dependencies>
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/Activator.java Wed Sep 28 11:35:58 2016 -0400 +++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/Activator.java Fri Sep 30 10:15:14 2016 -0400 @@ -43,7 +43,7 @@ import com.redhat.thermostat.common.cli.CommandRegistry; import com.redhat.thermostat.common.cli.CommandRegistryImpl; import com.redhat.thermostat.launcher.Launcher; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.config.CommonPaths; import org.osgi.framework.BundleActivator; @@ -68,7 +68,7 @@ CommonPaths.class, Launcher.class, Keyring.class, - UNIXProcessHandler.class, + ProcessHandler.class, }; tracker = new MultipleServiceTracker(context, deps, new MultipleServiceTracker.Action() { @Override @@ -77,7 +77,7 @@ CommonPaths paths = (CommonPaths) services.get(CommonPaths.class.getName()); Launcher launcher = (Launcher) services.get(Launcher.class.getName()); Keyring keyring = (Keyring) services.get(Keyring.class.getName()); - UNIXProcessHandler processHandler = (UNIXProcessHandler) services.get(UNIXProcessHandler.class.getName()); + ProcessHandler processHandler = (ProcessHandler) services.get(ProcessHandler.class.getName()); setupCommand.setExitStatusService(exitStatus); setupCommand.setPaths(paths); setupCommand.setLauncher(launcher); @@ -100,4 +100,4 @@ tracker.close(); setupCommand = null; } -} \ No newline at end of file +}
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupCommand.java Wed Sep 28 11:35:58 2016 -0400 +++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupCommand.java Fri Sep 30 10:15:14 2016 -0400 @@ -59,7 +59,7 @@ import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.internal.utils.laf.ThemeManager; import com.redhat.thermostat.launcher.Launcher; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.setup.command.internal.cli.CLISetup; import com.redhat.thermostat.setup.command.internal.model.ThermostatQuickSetup; import com.redhat.thermostat.setup.command.internal.model.ThermostatSetup; @@ -81,7 +81,7 @@ private Launcher launcher; private Keyring keyring; private String[] origArgsList; - private UNIXProcessHandler processHandler; + private ProcessHandler processHandler; private PrintStream out; @Override @@ -107,7 +107,7 @@ requireNonNull(launcher, t.localize(LocaleResources.SERVICE_UNAVAILABLE_MESSAGE, "Launcher")); this.keyring = dependentServices.getService(Keyring.class); requireNonNull(keyring, t.localize(LocaleResources.SERVICE_UNAVAILABLE_MESSAGE, "Keyring")); - this.processHandler = dependentServices.getService(UNIXProcessHandler.class); + this.processHandler = dependentServices.getService(ProcessHandler.class); requireNonNull(processHandler, t.localize(LocaleResources.SERVICE_UNAVAILABLE_MESSAGE, "UnixProcessHandler")); ThermostatSetup setup = createSetup(); @@ -215,15 +215,15 @@ dependentServices.addService(Keyring.class, keyring); } - public void setProcessHandler(UNIXProcessHandler processHandler) { - dependentServices.addService(UNIXProcessHandler.class, processHandler); + public void setProcessHandler(ProcessHandler processHandler) { + dependentServices.addService(ProcessHandler.class, processHandler); } public void setServicesUnavailable() { dependentServices.removeService(Launcher.class); dependentServices.removeService(CommonPaths.class); dependentServices.removeService(Keyring.class); - dependentServices.removeService(UNIXProcessHandler.class); + dependentServices.removeService(ProcessHandler.class); } public boolean isStorageRequired() {
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetup.java Wed Sep 28 11:35:58 2016 -0400 +++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetup.java Fri Sep 30 10:15:14 2016 -0400 @@ -64,7 +64,7 @@ import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.common.utils.StreamUtils; import com.redhat.thermostat.launcher.Launcher; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.config.CommonPaths; class MongodbUserSetup implements UserSetup { @@ -75,7 +75,7 @@ private static final String MONGO_PROCESS = "mongod"; private static final Logger logger = LoggingUtils.getLogger(MongodbUserSetup.class); private final UserCredsValidator validator; - private final UNIXProcessHandler processHandler; + private final ProcessHandler processHandler; private final Launcher launcher; private final CredentialFinder finder; private final CredentialsFileCreator fileCreator; @@ -89,7 +89,7 @@ private String userComment; private Integer pid; - MongodbUserSetup(UserCredsValidator validator, Launcher launcher, UNIXProcessHandler processHandler, CredentialFinder finder, CredentialsFileCreator fileCreator, CommonPaths paths, StampFiles stampFiles, StructureInformation structureInfo, AuthFileWriter authWriter, KeyringWriter keyringWriter) { + MongodbUserSetup(UserCredsValidator validator, Launcher launcher, ProcessHandler processHandler, CredentialFinder finder, CredentialsFileCreator fileCreator, CommonPaths paths, StampFiles stampFiles, StructureInformation structureInfo, AuthFileWriter authWriter, KeyringWriter keyringWriter) { this.validator = validator; this.processHandler = processHandler; this.launcher = launcher;
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetup.java Wed Sep 28 11:35:58 2016 -0400 +++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetup.java Fri Sep 30 10:15:14 2016 -0400 @@ -42,7 +42,7 @@ import com.redhat.thermostat.common.config.ClientPreferences; import com.redhat.thermostat.launcher.Launcher; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.setup.command.internal.LocaleResources; import com.redhat.thermostat.shared.config.CommonPaths; import com.redhat.thermostat.shared.locale.Translate; @@ -140,7 +140,7 @@ } } - public static ThermostatSetup create(Launcher launcher, CommonPaths paths, UNIXProcessHandler processHandler, Keyring keyring) { + public static ThermostatSetup create(Launcher launcher, CommonPaths paths, ProcessHandler processHandler, Keyring keyring) { CredentialFinder finder = new CredentialFinder(paths); CredentialsFileCreator creator = new CredentialsFileCreator(); StampFiles stampFiles = new StampFiles(paths);
--- a/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/SetupCommandTest.java Wed Sep 28 11:35:58 2016 -0400 +++ b/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/SetupCommandTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -60,6 +60,7 @@ import java.util.Objects; import com.redhat.thermostat.common.ExitStatus; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.locale.LocalizedString; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; @@ -71,8 +72,6 @@ import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.Console; import com.redhat.thermostat.launcher.Launcher; -import com.redhat.thermostat.service.process.UNIXProcessHandler; -import com.redhat.thermostat.setup.command.internal.SetupCommand; import com.redhat.thermostat.setup.command.internal.cli.CharArrayMatcher; import com.redhat.thermostat.setup.command.internal.model.ThermostatSetup; import com.redhat.thermostat.shared.config.CommonPaths; @@ -88,7 +87,7 @@ private ByteArrayOutputStream outputBaos, errorBaos; private PrintStream output, error; private CommonPaths paths; - private UNIXProcessHandler processHandler; + private ProcessHandler processHandler; private Launcher launcher; private Keyring keyring; private ExitStatus exitStatus; @@ -96,7 +95,7 @@ @Before public void setUp() { - processHandler = mock(UNIXProcessHandler.class); + processHandler = mock(ProcessHandler.class); paths = mock(CommonPaths.class); when(paths.getUserClientConfigurationFile()).thenReturn(mock(File.class)); ctxt = mock(CommandContext.class);
--- a/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetupTest.java Wed Sep 28 11:35:58 2016 -0400 +++ b/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetupTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -74,7 +74,7 @@ import com.redhat.thermostat.common.cli.AbstractStateNotifyingCommand; import com.redhat.thermostat.common.tools.ApplicationState; import com.redhat.thermostat.launcher.Launcher; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.setup.command.internal.cli.CharArrayMatcher; import com.redhat.thermostat.shared.config.CommonPaths; import com.redhat.thermostat.storage.config.FileStorageCredentials; @@ -83,7 +83,7 @@ private MongodbUserSetup mongoSetup; private StampFiles stampFiles; - private UNIXProcessHandler processHandler; + private ProcessHandler processHandler; private Launcher mockLauncher; private CredentialFinder finder; private CredentialsFileCreator fileCreator;
--- a/storage/cli/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ b/storage/cli/pom.xml Fri Sep 30 10:15:14 2016 -0400 @@ -63,7 +63,7 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-osgi-process-handler</artifactId> + <artifactId>thermostat-process-handler</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/Activator.java Wed Sep 28 11:35:58 2016 -0400 +++ b/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/Activator.java Fri Sep 30 10:15:14 2016 -0400 @@ -46,7 +46,7 @@ import com.redhat.thermostat.common.MultipleServiceTracker.Action; import com.redhat.thermostat.common.cli.CommandRegistry; import com.redhat.thermostat.common.cli.CommandRegistryImpl; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.config.CommonPaths; public class Activator implements BundleActivator { @@ -60,7 +60,7 @@ Class<?>[] deps = new Class<?>[] { ExitStatus.class, - UNIXProcessHandler.class, + ProcessHandler.class, CommonPaths.class }; tracker = new MultipleServiceTracker(context, deps, new Action() { @@ -68,7 +68,7 @@ @Override public void dependenciesAvailable(Map<String, Object> services) { ExitStatus exitStatus = (ExitStatus) services.get(ExitStatus.class.getName()); - UNIXProcessHandler processHandler = (UNIXProcessHandler) services.get(UNIXProcessHandler.class.getName()); + ProcessHandler processHandler = (ProcessHandler) services.get(ProcessHandler.class.getName()); CommonPaths paths = (CommonPaths) services.get(CommonPaths.class.getName()); reg.registerCommand("storage", new StorageCommand(exitStatus, processHandler, paths)); }
--- a/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/MongoProcessRunner.java Wed Sep 28 11:35:58 2016 -0400 +++ b/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/MongoProcessRunner.java Fri Sep 30 10:15:14 2016 -0400 @@ -53,7 +53,7 @@ import com.redhat.thermostat.common.utils.LoggedExternalProcess; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.common.utils.StringUtils; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.config.InvalidConfigurationException; import com.redhat.thermostat.shared.locale.LocalizedString; import com.redhat.thermostat.shared.locale.Translate; @@ -80,7 +80,7 @@ static final String NO_JOURNAL_FIRST_VERSION = "1.9.2"; static final String LOCALHOST_EXPTN_FIRST_VERSION = "2.4.0"; - private final UNIXProcessHandler processHandler; + private final ProcessHandler processHandler; private DBStartupConfiguration configuration; private Integer pid; private final boolean isQuiet; @@ -88,7 +88,7 @@ private final static MongoOSUtilInterface util = MongoOSUtilFactory.instance().createMongoOSUtil(); - public MongoProcessRunner(UNIXProcessHandler processHandler, DBStartupConfiguration configuration, boolean quiet, boolean permitLocalhostException) { + public MongoProcessRunner(ProcessHandler processHandler, DBStartupConfiguration configuration, boolean quiet, boolean permitLocalhostException) { this.processHandler = processHandler; this.configuration = configuration; this.isQuiet = quiet;
--- a/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/StorageCommand.java Wed Sep 28 11:35:58 2016 -0400 +++ b/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/StorageCommand.java Fri Sep 30 10:15:14 2016 -0400 @@ -49,7 +49,7 @@ import com.redhat.thermostat.common.tools.ApplicationException; import com.redhat.thermostat.common.tools.ApplicationState; import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.config.CommonPaths; import com.redhat.thermostat.shared.config.InvalidConfigurationException; import com.redhat.thermostat.shared.locale.Translate; @@ -63,12 +63,12 @@ private DBStartupConfiguration configuration; private DBOptionParser parser; private final ExitStatus exitStatus; - private final UNIXProcessHandler processHandler; + private final ProcessHandler processHandler; private final CommonPaths paths; private MongoProcessRunner runner; - public StorageCommand(ExitStatus exitStatus, UNIXProcessHandler processHandler, CommonPaths paths) { + public StorageCommand(ExitStatus exitStatus, ProcessHandler processHandler, CommonPaths paths) { this.exitStatus = exitStatus; this.processHandler = processHandler; this.paths = paths;
--- a/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/ActivatorTest.java Wed Sep 28 11:35:58 2016 -0400 +++ b/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/ActivatorTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -43,7 +43,7 @@ import org.junit.Test; import com.redhat.thermostat.common.ExitStatus; -import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.config.CommonPaths; import com.redhat.thermostat.testutils.StubBundleContext; @@ -55,10 +55,10 @@ ExitStatus exitStatus = mock(ExitStatus.class); CommonPaths paths = mock(CommonPaths.class); - UNIXProcessHandler processHandler = mock(UNIXProcessHandler.class); + ProcessHandler processHandler = mock(ProcessHandler.class); bundleContext.registerService(ExitStatus.class, exitStatus, null); bundleContext.registerService(CommonPaths.class, paths, null); - bundleContext.registerService(UNIXProcessHandler.class, processHandler, null); + bundleContext.registerService(ProcessHandler.class, processHandler, null); Activator activator = new Activator();
--- a/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/MongoProcessRunnerTest.java Wed Sep 28 11:35:58 2016 -0400 +++ b/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/MongoProcessRunnerTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -50,18 +50,18 @@ import java.io.IOException; import java.util.List; +import com.redhat.thermostat.service.process.ProcessHandler; import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.redhat.thermostat.service.process.UNIXProcessHandler; import com.redhat.thermostat.shared.config.InvalidConfigurationException; public class MongoProcessRunnerTest { private MongoProcessRunner runner; private DBStartupConfiguration config; - private UNIXProcessHandler processHandler; + private ProcessHandler processHandler; private static final String NO_JOURNAL_MONGODB_VERSION = "2.0.0"; private static final String JOURNAL_MONGODB_VERSION = "1.8.0"; @@ -85,7 +85,7 @@ when(config.getLogFile()).thenReturn(logPath); when(config.getPidFile()).thenReturn(pidFile); - processHandler = mock(UNIXProcessHandler.class); + processHandler = mock(ProcessHandler.class); runner = new MongoProcessRunner(processHandler, config, false, false); }
--- a/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/StorageCommandTest.java Wed Sep 28 11:35:58 2016 -0400 +++ b/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/StorageCommandTest.java Fri Sep 30 10:15:14 2016 -0400 @@ -47,6 +47,7 @@ import java.util.Properties; import java.util.concurrent.CountDownLatch; +import com.redhat.thermostat.service.process.ProcessHandler; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -60,14 +61,9 @@ import com.redhat.thermostat.common.cli.SimpleArguments; import com.redhat.thermostat.common.tools.ApplicationException; import com.redhat.thermostat.common.tools.ApplicationState; -import com.redhat.thermostat.service.process.UNIXProcessHandler; import com.redhat.thermostat.shared.config.CommonPaths; import com.redhat.thermostat.shared.config.InvalidConfigurationException; import com.redhat.thermostat.shared.config.internal.CommonPathsImpl; -import com.redhat.thermostat.storage.cli.internal.DBConfig; -import com.redhat.thermostat.storage.cli.internal.DBStartupConfiguration; -import com.redhat.thermostat.storage.cli.internal.MongoProcessRunner; -import com.redhat.thermostat.storage.cli.internal.StorageCommand; import com.redhat.thermostat.testutils.TestUtils; public class StorageCommandTest { @@ -78,7 +74,7 @@ private String tmpDir; private ExitStatus exitStatus; - private UNIXProcessHandler processHandler; + private ProcessHandler processHandler; private CommonPaths paths; @Before @@ -96,7 +92,7 @@ Assert.fail("cannot setup tests: " + e); } - processHandler = mock(UNIXProcessHandler.class); + processHandler = mock(ProcessHandler.class); paths = mock(CommonPathsImpl.class); File baseDir = new File(tmpDir);
--- a/unix-process-handler/pom.xml Wed Sep 28 11:35:58 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - - Copyright 2012-2016 Red Hat, Inc. - - This file is part of Thermostat. - - Thermostat is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 2, or (at your - option) any later version. - - Thermostat is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Thermostat; see the file COPYING. If not see - <http://www.gnu.org/licenses/>. - - Linking this code with other modules is making a combined work - based on this code. Thus, the terms and conditions of the GNU - General Public License cover the whole combination. - - As a special exception, the copyright holders of this code give - you permission to link this code with independent modules to - produce an executable, regardless of the license terms of these - independent modules, and to copy and distribute the resulting - executable under terms of your choice, provided that you also - meet, for each linked independent module, the terms and conditions - of the license of that module. An independent module is a module - which is not derived from or based on this code. If you modify - this code, you may extend this exception to your version of the - library, but you are not obligated to do so. If you do not wish - to do so, delete this exception statement from your version. - ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <artifactId>thermostat</artifactId> - <groupId>com.redhat.thermostat</groupId> - <version>1.99.12-SNAPSHOT</version> - </parent> - - <artifactId>thermostat-osgi-process-handler</artifactId> - <packaging>bundle</packaging> - <name>Thermostat Process handler plugin</name> - - <build> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <extensions>true</extensions> - <configuration> - <instructions> - <Private-Package>com.redhat.thermostat.service.internal</Private-Package> - <Export-Package>com.redhat.thermostat.service.process</Export-Package> - <Bundle-Activator>com.redhat.thermostat.service.internal.Activator</Bundle-Activator> - <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> - <Bundle-SymbolicName>com.redhat.thermostat.process</Bundle-SymbolicName> - <!-- Do not autogenerate uses clauses in Manifests --> - <_nouses>true</_nouses> - </instructions> - </configuration> - </plugin> - </plugins> - </build> - - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.core</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.compendium</artifactId> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-common-core</artifactId> - <version>${project.version}</version> - </dependency> - </dependencies> - -</project>
--- a/unix-process-handler/src/main/java/com/redhat/thermostat/service/internal/Activator.java Wed Sep 28 11:35:58 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -/* - * Copyright 2012-2016 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.service.internal; - -import java.util.Hashtable; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; - -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.service.process.UNIXProcessHandler; - -public class Activator implements BundleActivator { - - private static final Logger logger = LoggingUtils.getLogger(Activator.class); - - @Override - public void start(BundleContext context) throws Exception { - logger.log(Level.INFO, "activating thermostat-process bundles"); - - Hashtable<String, String> props = new Hashtable<String, String>(); - props.put(UNIXProcessHandler.ID, UNIXProcessHandler.ID); - context.registerService(UNIXProcessHandler.class.getName(), UnixProcessUtilities.getInstance(), props); - } - - @Override - public void stop(BundleContext context) throws Exception { - /* nothing to do here */ - } -} -
--- a/unix-process-handler/src/main/java/com/redhat/thermostat/service/internal/UnixProcessUtilities.java Wed Sep 28 11:35:58 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,159 +0,0 @@ -/* - * Copyright 2012-2016 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.service.internal; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.redhat.thermostat.common.tools.ApplicationException; -import com.redhat.thermostat.common.tools.ProcessStartException; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.service.process.UNIXProcessHandler; -import com.redhat.thermostat.service.process.UNIXSignal; - -class UnixProcessUtilities implements UNIXProcessHandler { - - private static final Logger logger = LoggingUtils.getLogger(UnixProcessUtilities.class); - - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - - private static final UNIXProcessHandler instance = IS_UNIX ? new UnixProcessUtilities() : new WindowsProcessUtilities(); - public static UNIXProcessHandler getInstance() { - return instance; - } - - UnixProcessUtilities() {} - - static class WindowsProcessUtilities extends UnixProcessUtilities { - public WindowsProcessUtilities() {} - - List<String> buildCommandLine(Integer pid) { - final List<String> commandLine = new ArrayList<>(); - commandLine.add("tasklist"); - commandLine.add("/FO"); - commandLine.add("csv"); - commandLine.add("/FI"); - commandLine.add("\"PID eq " + String.valueOf(pid) + "\""); - return commandLine; - } - - String processStdout(final String outStr) { - final String [] output = outStr.split(","); - String result = output[0]; - if (result.length() >= 2 && result.charAt(0) == '"') - result = result.replace("\"",""); - result = result.replace(".exe",""); - return result; - } - } - - @Override - public void sendSignal(Integer pid, UNIXSignal signal) { - exec("kill -s " + signal.signalName() + " " + pid); - } - - void exec(String command) { - Runtime rt = Runtime.getRuntime(); - try { - rt.exec(command); - } catch (IOException e) { - logger.log(Level.WARNING, "can't run kill!", e); - } - } - - List<String> buildCommandLine(Integer pid) { - final List<String> commandLine = new ArrayList<>(); - commandLine.add("ps"); - commandLine.add("--no-heading"); - commandLine.add("-p"); - commandLine.add(String.valueOf(pid)); - return commandLine; - } - - String processStdout(final String outStr) { - final String [] output = outStr.split(" "); - return output[output.length - 1]; - } - - @Override - public String getProcessName(Integer pid) { - - String result = null; - - final List<String> commandLine = buildCommandLine(pid); - - try { - Process process = createAndRunProcess(commandLine); - BufferedReader reader = getProcessOutput(process); - if (!IS_UNIX) reader.readLine(); // skip header line - result = reader.readLine(); - if (result != null) - result = processStdout(result); - - } catch (IOException | ApplicationException e) { - logger.log(Level.WARNING, "can't run '" + commandLine.get(0) + "'!", e); - } - - return result; - } - - /** package-private for testing */ - BufferedReader getProcessOutput(Process process) { - InputStream in = process.getInputStream(); - InputStreamReader isr = new InputStreamReader(in); - return new BufferedReader(isr); - } - - /** package-private for testing */ - Process createAndRunProcess(List<String> args) throws IOException, ApplicationException { - ProcessBuilder builder = new ProcessBuilder(args); - Process process = null; - try { - process = builder.start(); - } catch (IOException e) { - throw new ProcessStartException(args.get(0), e); - } - return process; - } -} -
--- a/unix-process-handler/src/main/java/com/redhat/thermostat/service/process/UNIXProcessHandler.java Wed Sep 28 11:35:58 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -/* - * Copyright 2012-2016 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.service.process; - -import com.redhat.thermostat.annotations.Service; - -@Service -public interface UNIXProcessHandler { - - public static String ID = "com.redhat.thermostat.service.process.UNIXProcessHandler"; - - /** - * Sends the given {@link UNIXSignal} to the process indicated by - * {@code pid}. - */ - public void sendSignal(Integer pid, UNIXSignal signal); - - /** - * Gets the process name given its {@code pid}. - */ - public String getProcessName(Integer pid); -} -
--- a/unix-process-handler/src/main/java/com/redhat/thermostat/service/process/UNIXSignal.java Wed Sep 28 11:35:58 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -/* - * Copyright 2012-2016 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.service.process; - -public enum UNIXSignal { - - TERM, - KILL; - - public String signalName() { - return name().toLowerCase(); - } -} -
--- a/unix-process-handler/src/main/java/com/redhat/thermostat/service/process/package-info.java Wed Sep 28 11:35:58 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -/* - * Copyright 2012-2016 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - - -/** - * Interact directly with processes. - * - * This package provides APIs for interacting directly with native - * proceses to provide some features not available through the standard - * Java API. - */ -package com.redhat.thermostat.service.process; -
--- a/unix-process-handler/src/test/java/com/redhat/thermostat/service/internal/UnixProcessUtilitiesTest.java Wed Sep 28 11:35:58 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,185 +0,0 @@ -/* - * Copyright 2012-2016 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.service.internal; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.List; - -import com.redhat.thermostat.service.process.UNIXProcessHandler; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.service.internal.UnixProcessUtilities; -import com.redhat.thermostat.service.process.UNIXSignal; - -public class UnixProcessUtilitiesTest { - - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - - private BufferedReader reader; - private BufferedReader emptyReader; - - private List<String> processArguments = new ArrayList<>(); - private UNIXProcessHandler process; - - @Before - public void setUp() { - - String data = IS_UNIX ? "123 fluff" : "headerline\r\n\"fluff.exe\",\"1868\",\"Console\",\"1\",\"25,952 K\""; - reader = new BufferedReader(new StringReader(data)); - emptyReader = new BufferedReader(new StringReader("")); - - processArguments.clear(); - - if (IS_UNIX) { - process = new UnixProcessUtilities() { - @Override - public Process createAndRunProcess(List<String> args) - throws IOException { - processArguments.addAll(args); - return null; - } - - @Override - void exec(String command) { - processArguments.add(command); - } - - public java.io.BufferedReader getProcessOutput(Process process) { - return reader; - } - }; - } else { - process = new UnixProcessUtilities.WindowsProcessUtilities() { - @Override - public Process createAndRunProcess(List<String> args) - throws IOException { - processArguments.addAll(args); - return null; - } - - @Override - void exec(String command) { - processArguments.add(command); - } - - public java.io.BufferedReader getProcessOutput(Process process) { - return reader; - } - }; - } - } - - @Test - public void sendKillSignalTest() { - - process.sendSignal(12345, UNIXSignal.KILL); - - Assert.assertTrue(processArguments.contains("kill -s kill 12345")); - Assert.assertEquals(1, processArguments.size()); - } - - @Test - public void sendTermSignalTest() { - - process.sendSignal(12345, UNIXSignal.TERM); - - Assert.assertTrue(processArguments.contains("kill -s term 12345")); - Assert.assertEquals(1, processArguments.size()); - } - - @Test - public void getProcessName() { - - String result = process.getProcessName(12345); - Assert.assertEquals("fluff", result); - - if (IS_UNIX) { - Assert.assertTrue(processArguments.contains("12345")); - Assert.assertTrue(processArguments.contains("ps")); - Assert.assertTrue(processArguments.contains("--no-heading")); - Assert.assertTrue(processArguments.contains("-p")); - } - else { - Assert.assertTrue(processArguments.contains("\"PID eq 12345\"")); - Assert.assertTrue(processArguments.contains("tasklist")); - } - } - - @Test - public void getProcessNameNoOutput() { - - // redefine, since we need an empty reader - final UNIXProcessHandler process; - if (IS_UNIX) { - process = new UnixProcessUtilities() { - @Override - public Process createAndRunProcess(List<String> args) - throws IOException { - processArguments.addAll(args); - return null; - } - - public java.io.BufferedReader getProcessOutput(Process process) { - return emptyReader; - } - }; - } - else { - process = new UnixProcessUtilities.WindowsProcessUtilities() { - @Override - public Process createAndRunProcess(List<String> args) - throws IOException { - processArguments.addAll(args); - return null; - } - - public java.io.BufferedReader getProcessOutput(Process process) { - return emptyReader; - } - }; - } - - String result = process.getProcessName(12345); - Assert.assertNull(result); - } -} -