--- a/src/main/java/net/timbusproject/extractors/modules/debiansoftwareextractor/Engine.java
+++ b/src/main/java/net/timbusproject/extractors/modules/debiansoftwareextractor/local/CommandManager.java
@@ -16,57 +16,61 @@
* See the License for the specific language governing permissions and limitation under the License.
*/
-package net.timbusproject.extractors.modules.debiansoftwareextractor;
+package net.timbusproject.extractors.modules.debiansoftwareextractor.local;
-import com.jcraft.jsch.JSchException;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
-import org.osgi.service.log.LogService;
-import org.springframework.beans.factory.annotation.Autowired;
-import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
import java.util.Scanner;
-public class Engine {
+/**
+ * Created by miguel on 14-01-2014.
+ */
+public class CommandManager {
- @Autowired
- private LogService log;
- public JSONArray run(SSHManager instance) throws JSchException, IOException, JSONException, ParseException {
- String dpkg = "dpkg -l | grep ^ii | awk '{print $2;}'";
- String dpkgStatus = "dpkg --status ";
+ public String run(String command) throws IOException, JSONException, ParseException {
+ return doCommand(command);
+ }
- // call sendCommand for each command and the output (without prompts) is returned
- instance.connect();
-// log.log(LogService.LOG_INFO, "connection: " + instance.connect());
- String pkgList = instance.sendCommand(dpkg);
- Scanner scanner = new Scanner(pkgList);
+ public String doCommand(String command) throws IOException {
+ StringBuilder outputBuffer = new StringBuilder();
+ InputStream commandOutput;
+ OutputStream commandInput;
+ ProcessBuilder processBuilder;
+ if (command == null)
+ processBuilder = new ProcessBuilder("/usr/bin/lshw", "-json");
+ else
+ processBuilder = new ProcessBuilder("/bin/sh", "-c", command);
- JSONArray jsonArray = new JSONArray();
- while (scanner.hasNextLine()) {
-// log.log(LogService.LOG_INFO, "extracting " + pkg);
- String pkg = scanner.nextLine();
- jsonArray.put(parser(instance.sendCommand(dpkgStatus + pkg)));
-// jsonArray.put(parser(instance.sendCommand(dpkgStatus + "bash")));
+ Process p = processBuilder.start();
+ commandInput = p.getOutputStream();
+// BufferedWriter buffered = new BufferedWriter(new OutputStreamWriter(commandInput));
+// buffered.write(pass);
+ commandOutput = p.getInputStream();
- // line for testing purposes. delete it to retrieve more packages
-// break;
+ int readByte = commandOutput.read();
+ String line;
+ while (readByte != 0xffffffff) {
+ outputBuffer.append((char) readByte);
+ readByte = commandOutput.read();
+ }
+ commandOutput.close();
+ commandInput.close();
+
+ try {
+ p.waitFor();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
}
- // close only after all commands are sent
- instance.close();
- //put the date in JSOn array
-
-// System.out.println(jsonArray.toString(2));
-// writeToFile(jsonArray);
- return jsonArray;
+ p.destroy();
+ return outputBuffer.toString();
}
public JSONObject parser(String pkg) throws JSONException {
@@ -79,41 +83,34 @@
if (tmp.endsWith(":")) {
key = tmp.replace(':', ' ').trim();
}
- switch (key.toLowerCase()) {
- case "description":
- StringBuilder stringBuilder = new StringBuilder();
- while (scanner.hasNextLine() && !tmp.contains(key)) {
- String str = scanner.nextLine();
- if (str.length() == 0)
- System.out.println(pkg);
- else
- if (!(Character.isUpperCase(str.charAt(0)) && str.contains(":"))) {
- stringBuilder.append(str);
- }
+ key = key.toLowerCase();
+ String s = key.toLowerCase();
+ if (s.equals("description")) {
+ StringBuilder stringBuilder = new StringBuilder();
+ while (scanner.hasNextLine() && !tmp.contains(key)) {
+ String str = scanner.nextLine();
+ if (!(Character.isUpperCase(str.charAt(0)) && str.contains(":"))) {
+ stringBuilder.append(str);
}
- jsonObject.put(key, stringBuilder);
- break;
- case "conffiles":
- getConffiles(scanner, jsonObject, tmp, key);
- break;
- case "provides":
- case "pre-depends":
- case "recommends":
- case "suggests":
- case "conflicts":
- case "replaces":
- case "depends":
- JSONArray jsonArrayAnd = getDepends(scanner);
- jsonObject.put(key, jsonArrayAnd);
- break;
- default:
- jsonObject.put(key, scanner.nextLine().trim());
- break;
+ }
+ jsonObject.put(key, stringBuilder);
+
+ } else if (s.equals("conffiles")) {
+ getConffiles(scanner, jsonObject, tmp, key);
+
+ } else if (s.equals("provides") || s.equals("pre-depends") || s.equals("recommends") || s.equals("suggests") || s.equals("conflicts") || s.equals("replaces") || s.equals("depends")) {
+ JSONArray jsonArrayAnd = getDepends(scanner);
+ jsonObject.put(key, jsonArrayAnd);
+
+ } else {
+ jsonObject.put(key, scanner.nextLine().trim());
+
}
}
}
return jsonObject;
}
+
private JSONArray getDepends(Scanner scanner) throws JSONException {
String[] depends = scanner.nextLine().trim().split(",");
JSONArray jsonArrayAnd = new JSONArray();