View Javadoc

1   package org.rpi.plugin.lirc;
2   
3   import java.io.BufferedReader;
4   import java.io.InputStreamReader;
5   import java.util.Vector;
6   
7   import org.apache.log4j.Logger;
8   
9   
10  public class LIRCWorkQueue extends Thread {
11      private Logger log = Logger.getLogger(LIRCWorkQueue.class);
12  
13      private Vector mWorkQueue = new Vector();
14      private boolean run = true;
15  
16      public LIRCWorkQueue() {
17          this.setName("LIRCWorkQueue");
18      }
19  
20      public synchronized boolean isEmpty() {
21          return mWorkQueue.isEmpty();
22      }
23  
24      public synchronized void put(Object object) {
25          log.debug("Put Object in WorkQueue " + object.toString());
26          try {
27              mWorkQueue.addElement(object);
28          } catch (Exception e) {
29              log.error(e.getMessage(), e);
30          }
31      }
32  
33      /**
34       * Get the first object out of the queue. Return null if the queue is empty.
35       */
36      public synchronized Object get() {
37          Object object = peek();
38          if (object != null)
39              mWorkQueue.removeElementAt(0);
40          return object;
41      }
42  
43      /**
44       * Peek to see if something is available.
45       */
46      public Object peek() {
47          if (isEmpty())
48              return null;
49          return mWorkQueue.elementAt(0);
50      }
51  
52      private void sleep(int value) {
53          try {
54              Thread.sleep(value);
55          } catch (InterruptedException e) {
56              log.error(e.getMessage(), e);
57          }
58      }
59  
60      public synchronized void clear() {
61          try {
62              log.info("Clearing Work Queue. Number of Items: " + mWorkQueue.size());
63              mWorkQueue.clear();
64              log.info("WorkQueue Cleared");
65          } catch (Exception e) {
66              log.debug(e.getMessage(), e);
67          }
68      }
69  
70      private synchronized void stopRunning() {
71          log.info("Stopping WorkerQueue");
72          run = false;
73          log.info("Stopped WorkerQueue");
74          clear();
75      }
76  
77      public void run() {
78          while (run) {
79              if (!isEmpty()) {
80                  try {
81                      String command = (String) get();
82                      log.info("Pulled Command Subscription " + command);
83                      processEvent(command);
84                      log.info("Number of Commands in Queue: " + mWorkQueue.size());
85                  } catch (Exception e) {
86                      log.error(e.getMessage(), e);
87                  }
88              } else {
89                  sleep(100);
90              }
91          }
92      }
93  
94      private void processEvent(String command) {
95          if(command ==null)
96              return;
97          log.debug("Sending Command: " + command);
98          try
99          {
100             Process pa = Runtime.getRuntime().exec(command);
101             pa.waitFor();
102             BufferedReader reader = new BufferedReader(new InputStreamReader(pa.getInputStream()));
103             String line;
104             while ((line = reader.readLine()) != null) {
105                 line = line.trim();
106                 log.debug("Result of " + command + " : " + line);
107             }
108             reader.close();
109             pa.getInputStream().close();
110         }
111         catch(Exception e)
112         {
113             log.error("Error Sending Command: " + command , e);
114         }
115     }
116 
117 }