Added org.webosinternals.optloopservice
[webos-internals:applications.git] / org.webosinternals.optloopservice / OptLoopService.java
1 package org.webosinternals.optloopservice;
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 // import java.util.Iterator;
10
11 import com.palm.luna.LSException;
12 import com.palm.luna.service.LunaServiceThread;
13 import com.palm.luna.service.ServiceMessage;
14 import com.palm.luna.message.ErrorMessage;
15
16 import org.json.JSONException;
17 import org.json.JSONObject;
18
19 public class OptLoopService extends LunaServiceThread {
20         
21         /* ============================== Variables ==============================*/
22         private Runtime runtime;
23
24         /**
25          * An object to hold the return value and stdout of the executed command.
26          */
27         private class ReturnResult {
28                 int returnValue;
29                 ArrayList<String> stdOut;
30                 public ReturnResult(int returnValue, ArrayList<String> stdOut) {
31                         this.returnValue = returnValue;
32                         this.stdOut = stdOut;
33                 }
34         }
35
36         /**
37          * Create a new OptLoopService
38          */
39         public OptLoopService() {
40                 runtime = Runtime.getRuntime();
41         }
42
43         /**
44          * A function to execute system commands.
45          * 
46          * @param command The system command to execute
47          * @return A ReturnResult object containing the return value and stdout of
48          * of the executed command.
49          */
50         private ReturnResult executeCMD(String command) {
51                 int ret = 1;
52                 ArrayList<String> output = new ArrayList<String>();
53                 if (checkArg(command)) {
54                         try {
55                                 Process p = runtime.exec(command);
56                                 InputStream in = p.getInputStream();
57                                 BufferedInputStream buf = new BufferedInputStream(in);
58                                 InputStreamReader inread = new InputStreamReader(buf);
59                                 BufferedReader bufferedreader = new BufferedReader(inread);
60
61                                 String line;
62                                 while ((line = bufferedreader.readLine()) != null) {
63                                         output.add(line);
64                                 }
65
66                                 try {
67                                         if (p.waitFor() != 0)
68                                                 System.err.println("exit value = " + p.exitValue());
69                                         else
70                                                 ret = 0;
71                                 } catch (InterruptedException e) {
72                                         System.err.println(e);
73                                 } finally {
74                                         bufferedreader.close();
75                                         inread.close();
76                                         buf.close();
77                                         in.close();
78                                 }
79                         } catch (IOException e) {
80                                 System.err.println(e.getMessage());
81                         }
82                 }
83                 return new ReturnResult(ret, output);
84         }
85
86         private JSONObject mount()
87         throws JSONException, LSException {
88                 ReturnResult ret = executeCMD("/bin/mount /opt");
89                 if (ret.returnValue==0) {
90                         JSONObject reply = new JSONObject();
91                         reply.put("return",ret.returnValue);
92                         return reply;
93                 } else {
94                         return null;
95                 }
96         }
97         
98         private JSONObject unmount()
99         throws JSONException, LSException {
100                 ReturnResult ret = executeCMD("/bin/umount /opt");
101                 if (ret.returnValue==0) {
102                         JSONObject reply = new JSONObject();
103                         reply.put("return",ret.returnValue);
104                         return reply;
105                 } else {
106                         return null;
107                 }
108         }
109         
110         /* ============================ DBUS Methods =============================*/
111         
112         @LunaServiceThread.PublicMethod
113         public void mount(ServiceMessage msg)
114         throws JSONException, LSException {
115             JSONObject reply = mount();
116             if (reply!=null)
117                 msg.respond(reply.toString());
118             else
119                 msg.respondError(ErrorMessage.ERROR_CODE_METHOD_EXCEPTION, "You fail!");
120         }
121
122         @LunaServiceThread.PublicMethod
123         public void unmount(ServiceMessage msg)
124         throws JSONException, LSException {
125             JSONObject reply = unmount();
126             if (reply!=null)
127                 msg.respond(reply.toString());
128             else
129                 msg.respondError(ErrorMessage.ERROR_CODE_METHOD_EXCEPTION, "You fail!");
130         }
131
132         /*public static void main(String[] args) {
133                 try {
134                         OptLoopService ipkg =  new OptLoopService();
135                         System.out.println(ipkg.mount().toString());
136                         System.out.println(ipkg.unmount().toString());
137                 } catch (JSONException e) {
138                         System.err.println(e);
139                 } catch (LSException e) {
140                         System.err.println(e);
141                 }
142                 
143         }*/
144
145 }