add IPKGService.... update function implemented (not tested)
[webos-internals:applications.git] / IPGKService / IPKGService.java
1 package org.webosinternals.service.ipkg;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8
9 import com.palm.luna.LSException;
10 import com.palm.luna.service.LunaServiceThread;
11 import com.palm.luna.service.ServiceMessage;
12 import com.palm.luna.message.ErrorMessage;
13 import org.json.JSONException;
14 import org.json.JSONObject;
15
16 public class IPKGService extends LunaServiceThread {
17         
18         private class ReturnResult {
19                 int returnValue;
20                 String stdOut;
21                 public ReturnResult(int returnValue, String stdOut) {
22                         this.returnValue = returnValue;
23                         this.stdOut = stdOut;
24                 }
25         }
26         
27         private static final String ipkgBaseCommand = "/usr/bin/ipkg -o /var ";
28
29         Runtime runtime;
30
31         public IPKGService() {
32                 runtime = Runtime.getRuntime();
33         }
34
35         private ReturnResult executeCMD(String command) {
36                 StringBuffer output = new StringBuffer();
37                 int ret = 0;
38                 try {
39                         Process p = runtime.exec(command);
40                         InputStream in = p.getInputStream();
41                         BufferedInputStream buf = new BufferedInputStream(in);
42                         InputStreamReader inread = new InputStreamReader(buf);
43                         BufferedReader bufferedreader = new BufferedReader(inread);
44
45                         String line;
46                         while ((line = bufferedreader.readLine()) != null) {
47                                 output.append(line+"\n");
48                         }
49
50                         try {
51                                 if (p.waitFor() != 0) {
52                                         System.err.println("exit value = " + p.exitValue());
53                                 }
54                         } catch (InterruptedException e) {
55                                 System.err.println(e);
56                                 ret = 1;
57                         } finally {
58                                 bufferedreader.close();
59                                 inread.close();
60                                 buf.close();
61                                 in.close();
62                         }
63                 } catch (IOException e) {
64                         System.err.println(e.getMessage());
65                 }
66
67                 return new ReturnResult(ret, output.toString());
68         }
69
70         @LunaServiceThread.PublicMethod
71         public void update(ServiceMessage msg)
72         throws JSONException, LSException {
73                 ReturnResult ret = executeCMD(ipkgBaseCommand + "update");
74                 JSONObject reply = new JSONObject();
75                 if (ret.returnValue==0) {
76                         reply.put("output",ret.stdOut);
77                         msg.respond(reply.toString());
78                 } else {
79                         msg.respondError(ErrorMessage.ERROR_CODE_METHOD_EXCEPTION, "You fail!");
80                 }
81         }
82
83 }