diff --git a/pi4j-core/src/main/java/com/pi4j/system/NetworkInfo.java b/pi4j-core/src/main/java/com/pi4j/system/NetworkInfo.java index 1bf739c47..905b9a70e 100644 --- a/pi4j-core/src/main/java/com/pi4j/system/NetworkInfo.java +++ b/pi4j-core/src/main/java/com/pi4j/system/NetworkInfo.java @@ -28,12 +28,12 @@ */ -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; +import com.pi4j.util.ExecUtil; + public class NetworkInfo { // private constructor @@ -41,61 +41,28 @@ private NetworkInfo() { // forbid object construction } - private static String[] executeCommand(String command) throws IOException, InterruptedException { - return executeCommand(command, null); - } - - private static String[] executeCommand(String command, String split) throws IOException, InterruptedException { - List result = new ArrayList(); - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line = reader.readLine(); - while (line != null) { - if (!line.isEmpty()) { - if (split == null || split.isEmpty()) { - result.add(line.trim()); - } else { - String[] parts = line.trim().split(split); - for(String part : parts) { - if (part != null && !part.isEmpty()) { - result.add(part.trim()); - } - } - } - } - line = reader.readLine(); - } - - if (result.size() > 0) { - return (String[])result.toArray(new String[0]); - } else { - return new String[0]; - } - } - public static String getHostname() throws IOException, InterruptedException { - return executeCommand("hostname --short")[0]; + return ExecUtil.execute("hostname --short")[0]; } public static String getFQDN() throws IOException, InterruptedException { - return executeCommand("hostname --fqdn")[0]; + return ExecUtil.execute("hostname --fqdn")[0]; } public static String[] getIPAddresses() throws IOException, InterruptedException { - return executeCommand("hostname --all-ip-addresses", " "); + return ExecUtil.execute("hostname --all-ip-addresses", " "); } public static String getIPAddress() throws IOException, InterruptedException { - return executeCommand("hostname --ip-address")[0]; + return ExecUtil.execute("hostname --ip-address")[0]; } public static String[] getFQDNs() throws IOException, InterruptedException { - return executeCommand("hostname --all-fqdns", " "); + return ExecUtil.execute("hostname --all-fqdns", " "); } public static String[] getNameservers() throws IOException, InterruptedException { - String[] nameservers = executeCommand("cat /etc/resolv.conf"); + String[] nameservers = ExecUtil.execute("cat /etc/resolv.conf"); List result = new ArrayList(); for (String nameserver : nameservers) { if (nameserver.startsWith("nameserver")) { diff --git a/pi4j-core/src/main/java/com/pi4j/system/SystemInfo.java b/pi4j-core/src/main/java/com/pi4j/system/SystemInfo.java index 76f2bef0b..e785b385c 100644 --- a/pi4j-core/src/main/java/com/pi4j/system/SystemInfo.java +++ b/pi4j-core/src/main/java/com/pi4j/system/SystemInfo.java @@ -28,14 +28,13 @@ */ -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.HashMap; import java.util.Map; +import com.pi4j.util.ExecUtil; import com.pi4j.util.StringUtil; public class SystemInfo { @@ -51,16 +50,14 @@ private static String getCpuInfo(String target) throws IOException, InterruptedE // if the CPU data has not been previously acquired, then acquire it now if (cpuInfo == null) { cpuInfo = new HashMap(); - Process p = Runtime.getRuntime().exec("cat /proc/cpuinfo"); - p.waitFor(); - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line = reader.readLine(); - while (line != null) { - String parts[] = line.split(":", 2); - if (parts.length >= 2 && !parts[0].trim().isEmpty() && !parts[1].trim().isEmpty()) { - cpuInfo.put(parts[0].trim(), parts[1].trim()); + String result[] = ExecUtil.execute("cat /proc/cpuinfo"); + if(result != null){ + for(String line : result) { + String parts[] = line.split(":", 2); + if (parts.length >= 2 && !parts[0].trim().isEmpty() && !parts[1].trim().isEmpty()) { + cpuInfo.put(parts[0].trim(), parts[1].trim()); + } } - line = reader.readLine(); } } @@ -179,19 +176,11 @@ public Boolean run() { private static String getBashVersionInfo() { String versionInfo = ""; try { - - String cmd = "bash --version"; - Process p = Runtime.getRuntime().exec(cmd); - p.waitFor(); - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line = reader.readLine(); - if(p.exitValue() == 0) { - while(line != null) { - if(!line.isEmpty()) { - versionInfo = line; // return only first output line of version info - break; - } - line = reader.readLine(); + String result[] = ExecUtil.execute("bash --version"); + for(String line : result) { + if(!line.isEmpty()) { + versionInfo = line; // return only first output line of version info + break; } } } @@ -218,13 +207,9 @@ private static boolean hasReadElfTag(String tag) { private static String getReadElfTag(String tag) { String tagValue = null; try { - String cmd = "/usr/bin/readelf -A /proc/self/exe"; - Process p = Runtime.getRuntime().exec(cmd); - p.waitFor(); - if(p.exitValue() == 0) { - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line = reader.readLine(); - while(line != null) { + String result[] = ExecUtil.execute("/usr/bin/readelf -A /proc/self/exe"); + if(result != null){ + for(String line : result) { line = line.trim(); if (line.startsWith(tag) && line.contains(":")) { String lineParts[] = line.split(":", 2); @@ -232,14 +217,11 @@ private static String getReadElfTag(String tag) { tagValue = lineParts[1].trim(); break; } - line = reader.readLine(); } - } + } } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException ie) { ie.printStackTrace(); } return tagValue; } - - } diff --git a/pi4j-core/src/main/java/com/pi4j/util/ExecUtil.java b/pi4j-core/src/main/java/com/pi4j/util/ExecUtil.java new file mode 100644 index 000000000..667b7de84 --- /dev/null +++ b/pi4j-core/src/main/java/com/pi4j/util/ExecUtil.java @@ -0,0 +1,81 @@ +package com.pi4j.util; + +/* + * #%L + * ********************************************************************** + * ORGANIZATION : Pi4J + * PROJECT : Pi4J :: Java Library (Core) + * FILENAME : ExecUtil.java + * + * This file is part of the Pi4J project. More information about + * this project can be found here: http://www.pi4j.com/ + * ********************************************************************** + * %% + * Copyright (C) 2012 - 2013 Pi4J + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +public class ExecUtil +{ + public static String[] execute(String command) throws IOException, InterruptedException { + return execute(command, null); + } + + public static String[] execute(String command, String split) throws IOException, InterruptedException { + List result = new ArrayList(); + Process p = Runtime.getRuntime().exec(command); + p.waitFor(); + + if(p.exitValue() != 0) + return null; + + InputStreamReader isr = new InputStreamReader(p.getInputStream()); + BufferedReader reader = new BufferedReader(isr); + String line = reader.readLine(); + while (line != null) { + if (!line.isEmpty()) { + if (split == null || split.isEmpty()) { + result.add(line.trim()); + } else { + String[] parts = line.trim().split(split); + for(String part : parts) { + if (part != null && !part.isEmpty()) { + result.add(part.trim()); + } + } + } + } + line = reader.readLine(); + } + + // close readers and stream + reader.close(); + isr.close(); + p.getInputStream().close(); + + if (result.size() > 0) { + return (String[])result.toArray(new String[0]); + } else { + return new String[0]; + } + } +}