Mercurial > hg > release > thermostat-0.4
changeset 304:04906155db36
Process plugin
reviewed-by: rkennke
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-May/001335.html
PR 979
line wrap: on
line diff
--- a/common/pom.xml Tue May 15 16:09:46 2012 +0200 +++ b/common/pom.xml Tue May 15 16:09:46 2012 +0200 @@ -46,7 +46,7 @@ </parent> <artifactId>thermostat-common</artifactId> - <packaging>bundle</packaging> + <packaging>jar</packaging> <name>Thermostat Common</name> <url>${project.parent.url}</url> @@ -58,25 +58,6 @@ <filtering>true</filtering> </resource> </resources> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <version>1.4.0</version> - <extensions>true</extensions> - <configuration> - <instructions> - <Embed-Dependency>*;scope=compile|runtime;artifactId=!org.apache.felix.framework|osgi_R4_core</Embed-Dependency> - <Embed-Transitive>true</Embed-Transitive> - <Import-Package>org.osgi.framework,!android.dalvik,!com.apple.*,!dalvik.system,!javax.mail.*,!javax.servlet.*,!junit.*,!org.testng.*,*</Import-Package> - <Private-Package>com.redhat.thermostat.*</Private-Package> - <Bundle-Activator>com.redhat.thermostat.osgi.OSGiActivator</Bundle-Activator> - <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> - <Export-Package>com.redhat.thermostat.service.process</Export-Package> - </instructions> - </configuration> - </plugin> - </plugins> </build> <dependencies> @@ -114,6 +95,7 @@ <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.framework</artifactId> </dependency> + </dependencies>
--- a/common/src/main/java/com/redhat/thermostat/osgi/OSGiActivator.java Tue May 15 16:09:46 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -/* - * Copyright 2012 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.osgi; - -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; -import com.redhat.thermostat.tools.unix.UnixProcessUtilities; - -public class OSGiActivator implements BundleActivator { - - private static final Logger logger = LoggingUtils.getLogger(OSGiActivator.class); - - @Override - public void start(BundleContext context) throws Exception { - logger.log(Level.INFO, "activating thermostat-common 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 { - // TODO Auto-generated method stub - - } -}
--- a/common/src/main/java/com/redhat/thermostat/service/process/UNIXProcessHandler.java Tue May 15 16:09:46 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -/* - * Copyright 2012 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 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(String pid, UNIXSignal signal); - - /** - * Gets the process name given its {@code pid}. - */ - public String getProcessName(String pid); -}
--- a/common/src/main/java/com/redhat/thermostat/service/process/UNIXSignal.java Tue May 15 16:09:46 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -/* - * Copyright 2012 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 { - - KILL; - - public String signalName() { - return name().toLowerCase(); - } -}
--- a/common/src/main/java/com/redhat/thermostat/tools/unix/UnixProcessUtilities.java Tue May 15 16:09:46 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,107 +0,0 @@ -/* - * Copyright 2012 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.tools.unix; - -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.utils.LoggedExternalProcess; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.service.process.UNIXProcessHandler; -import com.redhat.thermostat.service.process.UNIXSignal; -import com.redhat.thermostat.tools.ApplicationException; - -public class UnixProcessUtilities implements UNIXProcessHandler { - - private static final Logger logger = LoggingUtils.getLogger(UnixProcessUtilities.class); - - private static final UnixProcessUtilities instance = new UnixProcessUtilities(); - public static UnixProcessUtilities getInstance() { - return instance; - } - - UnixProcessUtilities() {} - - @Override - public void sendSignal(String pid, UNIXSignal signal) { - System.err.println("yeah!"); - } - - @Override - public String getProcessName(String pid) { - - String result = null; - - List<String> commandLine = new ArrayList<>(); - commandLine.add("ps"); - commandLine.add("--no-heading"); - commandLine.add("-p"); - commandLine.add(pid); - - try { - Process process = createAndRunProcess(commandLine); - BufferedReader reader = getProcessOutput(process); - result = reader.readLine(); - if (result != null) { - String [] output = result.split(" "); - result = output[output.length - 1]; - } - - } catch (IOException | ApplicationException e) { - logger.log(Level.WARNING, "can't run ps!", e); - } - - return result; - } - - public BufferedReader getProcessOutput(Process process) { - InputStream in = process.getInputStream(); - InputStreamReader isr = new InputStreamReader(in); - return new BufferedReader(isr); - } - - public Process createAndRunProcess(List<String> args) throws IOException, ApplicationException { - LoggedExternalProcess process = new LoggedExternalProcess(args); - return process.runAndReturnProcess(); - } -}
--- a/common/src/test/java/com/redhat/thermostat/tools/unix/UnixProcessUtilitiesTest.java Tue May 15 16:09:46 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,110 +0,0 @@ -/* - * Copyright 2012 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.tools.unix; - - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.List; - -import junit.framework.Assert; - -import org.junit.Before; -import org.junit.Test; - -public class UnixProcessUtilitiesTest { - - private BufferedReader reader; - private BufferedReader emptyReader; - - @Before - public void setUp() { - - String data = "123 fluff"; - reader = new BufferedReader(new StringReader(data)); - emptyReader = new BufferedReader(new StringReader("")); - } - - @Test - public void getProcessName() { - - final List<String> processArguments = new ArrayList<>(); - UnixProcessUtilities 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 reader; - }; - }; - - 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() { - - final List<String> processArguments = new ArrayList<>(); - UnixProcessUtilities 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; - }; - }; - - String result = process.getProcessName("12345"); - Assert.assertNull(result); - } -}
--- a/distribution/pom.xml Tue May 15 16:09:46 2012 +0200 +++ b/distribution/pom.xml Tue May 15 16:09:46 2012 +0200 @@ -204,5 +204,11 @@ <artifactId>thermostat-client-launcher</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-process-handler</artifactId> + <version>${project.version}</version> + <type>bundle</type> + </dependency> </dependencies> </project>
--- a/pom.xml Tue May 15 16:09:46 2012 +0200 +++ b/pom.xml Tue May 15 16:09:46 2012 +0200 @@ -78,6 +78,7 @@ <module>client</module> <module>distribution</module> <module>tools</module> + <module>unix-process-handler</module> </modules> <build>
--- a/tools/pom.xml Tue May 15 16:09:46 2012 +0200 +++ b/tools/pom.xml Tue May 15 16:09:46 2012 +0200 @@ -87,6 +87,12 @@ <artifactId>thermostat-agent</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-process-handler</artifactId> + <version>${project.version}</version> + <type>bundle</type> + </dependency> </dependencies> </project>
--- a/tools/src/main/java/com/redhat/thermostat/tools/db/MongoProcessRunner.java Tue May 15 16:09:46 2012 +0200 +++ b/tools/src/main/java/com/redhat/thermostat/tools/db/MongoProcessRunner.java Tue May 15 16:09:46 2012 +0200 @@ -51,8 +51,8 @@ import com.redhat.thermostat.common.utils.LoggedExternalProcess; import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.service.process.impl.UnixProcessUtilities; import com.redhat.thermostat.tools.ApplicationException; -import com.redhat.thermostat.tools.unix.UnixProcessUtilities; class MongoProcessRunner {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unix-process-handler/pom.xml Tue May 15 16:09:46 2012 +0200 @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + <parent> + <artifactId>thermostat</artifactId> + <groupId>com.redhat.thermostat</groupId> + <version>0.3-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> + <version>1.4.0</version> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package>com.redhat.thermostat.service.process</Export-Package> + <Bundle-Activator>com.redhat.thermostat.service.process.Activator</Bundle-Activator> + <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi_R4_core</artifactId> + <version>1.0</version> + </dependency> + + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-common</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unix-process-handler/src/main/java/com/redhat/thermostat/service/process/Activator.java Tue May 15 16:09:46 2012 +0200 @@ -0,0 +1,66 @@ +/* + * Copyright 2012 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 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.service.process.impl.UnixProcessUtilities; + +public class Activator implements BundleActivator { + + private static final Logger logger = Logger.getLogger(Activator.class.getSimpleName()); + + @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 */ + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unix-process-handler/src/main/java/com/redhat/thermostat/service/process/UNIXProcessHandler.java Tue May 15 16:09:46 2012 +0200 @@ -0,0 +1,53 @@ +/* + * Copyright 2012 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 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(String pid, UNIXSignal signal); + + /** + * Gets the process name given its {@code pid}. + */ + public String getProcessName(String pid); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unix-process-handler/src/main/java/com/redhat/thermostat/service/process/UNIXSignal.java Tue May 15 16:09:46 2012 +0200 @@ -0,0 +1,46 @@ +/* + * Copyright 2012 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 { + + KILL; + + public String signalName() { + return name().toLowerCase(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unix-process-handler/src/main/java/com/redhat/thermostat/service/process/impl/UnixProcessUtilities.java Tue May 15 16:09:46 2012 +0200 @@ -0,0 +1,116 @@ +/* + * Copyright 2012 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.impl; + +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.utils.LoggedExternalProcess; +import com.redhat.thermostat.service.process.UNIXProcessHandler; +import com.redhat.thermostat.service.process.UNIXSignal; +import com.redhat.thermostat.tools.ApplicationException; + +public class UnixProcessUtilities implements UNIXProcessHandler { + + private static final Logger logger = Logger.getLogger(UnixProcessUtilities.class.getSimpleName()); + + private static final UnixProcessUtilities instance = new UnixProcessUtilities(); + public static UnixProcessUtilities getInstance() { + return instance; + } + + UnixProcessUtilities() {} + + @Override + public void sendSignal(String pid, UNIXSignal signal) { + List<String> commandLine = new ArrayList<>(); + commandLine.add("kill"); + + commandLine.add("-s " + signal.signalName()); + commandLine.add(pid); + + try { + createAndRunProcess(commandLine); + } catch (IOException | ApplicationException e) { + logger.log(Level.WARNING, "can't run kill!", e); + } + } + + @Override + public String getProcessName(String pid) { + + String result = null; + + List<String> commandLine = new ArrayList<>(); + commandLine.add("ps"); + commandLine.add("--no-heading"); + commandLine.add("-p"); + commandLine.add(pid); + + try { + Process process = createAndRunProcess(commandLine); + BufferedReader reader = getProcessOutput(process); + result = reader.readLine(); + if (result != null) { + String [] output = result.split(" "); + result = output[output.length - 1]; + } + + } catch (IOException | ApplicationException e) { + logger.log(Level.WARNING, "can't run ps!", e); + } + + return result; + } + + public BufferedReader getProcessOutput(Process process) { + InputStream in = process.getInputStream(); + InputStreamReader isr = new InputStreamReader(in); + return new BufferedReader(isr); + } + + public Process createAndRunProcess(List<String> args) throws IOException, ApplicationException { + LoggedExternalProcess process = new LoggedExternalProcess(args); + return process.runAndReturnProcess(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/unix-process-handler/src/test/java/com/redhat/thermostat/service/process/impl/UnixProcessUtilitiesTest.java Tue May 15 16:09:46 2012 +0200 @@ -0,0 +1,126 @@ +/* + * Copyright 2012 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.impl; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.service.process.UNIXSignal; + +public class UnixProcessUtilitiesTest { + + private BufferedReader reader; + private BufferedReader emptyReader; + + private List<String> processArguments = new ArrayList<>(); + private UnixProcessUtilities 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 + public Process createAndRunProcess(List<String> args) + throws IOException { + processArguments.addAll(args); + return null; + } + + public java.io.BufferedReader getProcessOutput(Process process) { + return reader; + }; + }; + } + + @Test + public void sendSignalTest() { + + process.sendSignal("12345", UNIXSignal.KILL); + + Assert.assertTrue(processArguments.contains("kill")); + Assert.assertTrue(processArguments.contains("-s kill")); + + // we want exactly the last argument to be the pid + Assert.assertEquals("12345", processArguments.get(processArguments.size() - 1)); + } + + @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 + UnixProcessUtilities 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; + }; + }; + + String result = process.getProcessName("12345"); + Assert.assertNull(result); + } +}