rename IPKGService to org.webosinternals.ipkgservice
[webos-internals:applications.git] / org.webosinternals.ipkgservice / IPKGService.java
1 package org.webosinternals.ipkgservice;
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 import java.util.ArrayList;
9
10 import com.palm.luna.LSException;
11 import com.palm.luna.service.LunaServiceThread;
12 import com.palm.luna.service.ServiceMessage;
13 import com.palm.luna.message.ErrorMessage;
14 import org.json.JSONException;
15 import org.json.JSONObject;
16
17 public class IPKGService extends LunaServiceThread {
18
19         private class ReturnResult {
20                 int returnValue;
21                 ArrayList<String> stdOut;
22                 public ReturnResult(int returnValue, ArrayList<String> stdOut) {
23                         this.returnValue = returnValue;
24                         this.stdOut = stdOut;
25                 }
26         }
27
28         private static final String ipkgBaseCommand = "/usr/bin/ipkg -o /var ";
29
30         Runtime runtime;
31
32         public IPKGService() {
33                 runtime = Runtime.getRuntime();
34         }
35
36         private boolean checkArg(String arg) {
37                 boolean ret = true;
38                 if ( arg.contains("&") || arg.contains(";") )
39                         ret = false;
40                 return ret;
41         }
42
43         private ReturnResult executeCMD(String command) {
44                 int ret = 1;
45                 ArrayList<String> output = new ArrayList<String>();
46                 if (checkArg(command)) {
47                         try {
48                                 Process p = runtime.exec(command);
49                                 InputStream in = p.getInputStream();
50                                 BufferedInputStream buf = new BufferedInputStream(in);
51                                 InputStreamReader inread = new InputStreamReader(buf);
52                                 BufferedReader bufferedreader = new BufferedReader(inread);
53
54                                 String line;
55                                 while ((line = bufferedreader.readLine()) != null) {
56                                         output.add(line);
57                                 }
58
59                                 try {
60                                         if (p.waitFor() != 0) {
61                                                 ret = 0;
62                                                 System.err.println("exit value = " + p.exitValue());
63                                         }
64                                 } catch (InterruptedException e) {
65                                         System.err.println(e);
66                                 } finally {
67                                         bufferedreader.close();
68                                         inread.close();
69                                         buf.close();
70                                         in.close();
71                                 }
72                         } catch (IOException e) {
73                                 System.err.println(e.getMessage());
74                         }
75                 }
76                 return new ReturnResult(ret, output);
77         }
78
79         @LunaServiceThread.PublicMethod
80         public void update(ServiceMessage msg)
81         throws JSONException, LSException {
82                 ReturnResult ret = executeCMD(ipkgBaseCommand + "update");
83                 JSONObject reply = new JSONObject();
84                 if (ret.returnValue==0) {
85                         reply.put("return",ret.returnValue);
86                         msg.respond(reply.toString());
87                 } else {
88                         msg.respondError(ErrorMessage.ERROR_CODE_METHOD_EXCEPTION, "You fail!");
89                 }
90         }
91
92         @LunaServiceThread.PublicMethod
93         public void list(ServiceMessage msg)
94         throws JSONException, LSException {
95                 ReturnResult ret = executeCMD(ipkgBaseCommand + "list");
96                 JSONObject reply = new JSONObject();
97                 if (ret.returnValue==0) {
98                         for (int i=0; i<ret.stdOut.size()-1;i++) {
99                                 String[] item = ret.stdOut.get(i).split("-");
100                                 JSONObject info = new JSONObject();
101                                 info.put("version", item[1].trim());
102                                 info.put("description", item[2].trim());
103                                 reply.put(item[0].trim(), info);
104                         }
105                         msg.respond(reply.toString());
106                 } else {
107                         msg.respondError(ErrorMessage.ERROR_CODE_METHOD_EXCEPTION, "You fail!");
108                 }
109         }
110
111         @LunaServiceThread.PublicMethod
112         public void list_installed(ServiceMessage msg)
113         throws JSONException, LSException {
114                 ReturnResult ret = executeCMD(ipkgBaseCommand + "list_installed");
115                 JSONObject reply = new JSONObject();
116                 if (ret.returnValue==0) {
117                         for (int i=0; i<ret.stdOut.size()-1;i++) {
118                                 String[] item = ret.stdOut.get(i).split("-");
119                                 JSONObject info = new JSONObject();
120                                 info.put("version", item[1].trim());
121                                 reply.put(item[0].trim(), info);
122                         }
123                         msg.respond(reply.toString());
124                 } else {
125                         msg.respondError(ErrorMessage.ERROR_CODE_METHOD_EXCEPTION, "You fail!");
126                 }
127         }
128
129         @LunaServiceThread.PublicMethod
130         public void list_upgrades(ServiceMessage msg)
131         throws JSONException, LSException {
132                 ReturnResult ret = executeCMD(ipkgBaseCommand + "list_installed");
133                 JSONObject reply = new JSONObject();
134                 if (ret.returnValue==0) {
135                         for (int i=0; i<ret.stdOut.size()-1;i++) {
136                                 String[] item = ret.stdOut.get(i).split("-");
137                                 JSONObject info = new JSONObject();
138                                 info.put("version", item[1].trim());
139                                 reply.put(item[0].trim(), info);
140                         }
141                         msg.respond(reply.toString());
142                 } else {
143                         msg.respondError(ErrorMessage.ERROR_CODE_METHOD_EXCEPTION, "You fail!");
144                 }
145         }
146
147         @LunaServiceThread.PublicMethod
148         public void remove(ServiceMessage msg)
149         throws JSONException, LSException {
150                 if (msg.getJSONPayload().has("package")) {
151                         String pkg = msg.getJSONPayload().getString("package").trim();
152                         ReturnResult ret = executeCMD(ipkgBaseCommand + "remove " + pkg);
153                         JSONObject reply = new JSONObject();
154                         if (ret.returnValue==0) {
155                                 reply.put("return",ret.returnValue);
156                                 msg.respond(reply.toString());
157                                 return;
158                         }
159                 }
160                 msg.respondError(ErrorMessage.ERROR_CODE_INVALID_PARAMETER, "You fail!");
161         }
162
163         @LunaServiceThread.PublicMethod
164         public void remove_recursive(ServiceMessage msg)
165         throws JSONException, LSException {
166                 if (msg.getJSONPayload().has("package")) {
167                         String pkg = msg.getJSONPayload().getString("package").trim();
168                         ReturnResult ret = executeCMD(ipkgBaseCommand + "-recursive remove " + pkg);
169                         JSONObject reply = new JSONObject();
170                         if (ret.returnValue==0) {
171                                 reply.put("return",ret.returnValue);
172                                 msg.respond(reply.toString());
173                                 return;
174                         }
175                 }
176                 msg.respondError(ErrorMessage.ERROR_CODE_INVALID_PARAMETER, "You fail!");
177         }
178
179         @LunaServiceThread.PublicMethod
180         public void install(ServiceMessage msg)
181         throws JSONException, LSException {
182                 if (msg.getJSONPayload().has("package")) {
183                         String pkg = msg.getJSONPayload().getString("package").trim();
184                         ReturnResult ret = executeCMD(ipkgBaseCommand + "install " + pkg);
185                         JSONObject reply = new JSONObject();
186                         if (ret.returnValue==0) {
187                                 reply.put("return",ret.returnValue);
188                                 msg.respond(reply.toString());
189                                 return;
190                         }
191                 }
192                 msg.respondError(ErrorMessage.ERROR_CODE_INVALID_PARAMETER, "You fail!");
193         }
194
195 }