-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExecIdler.java
More file actions
199 lines (174 loc) · 4.7 KB
/
Copy pathExecIdler.java
File metadata and controls
199 lines (174 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package org.lcdproc.lcdjava.idler;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import org.lcdproc.lcdjava.LCDException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
/**
* Display the results of executing a program.
* <p>Copyright (c) 2004-2005 Darren Greaves.
* @author Darren Greaves
* @version $Id: ExecIdler.java,v 1.2 2005-03-03 14:13:16 boncey Exp $
*/
public class ExecIdler extends AbstractIdler
{
/**
* Version details.
*/
public static final String CVSID =
"$Id: ExecIdler.java,v 1.2 2005-03-03 14:13:16 boncey Exp $";
private static Logger _log = LoggerFactory.getLogger(ExecIdler.class);
/**
* The size of the buffer the program's output is read into.
*/
private static final int BUFF_SIZE = 4000;
/**
* Pad the display to enhance readability.
*/
private static final String PADDING = " *** ";
/**
* The text to display.
*/
private final StringBuffer _text = new StringBuffer();
/**
* The name of the program to execute.
*/
private String _program;
/**
* Does this Idler have anything to display?
*/
private boolean _valid;
/**
* How often to update this feed in minutes.
*/
private int _update;
/**
* The Runtime object.
*/
private static Runtime _runtime = Runtime.getRuntime();
/**
* Public constructor, read the text from the config.
* @param config the XML element for this Idler.
* @param name the name of the Idler.
* @throws LCDException if there was a problem.
*/
public ExecIdler(Element config, String name)
throws LCDException
{
super(config, name);
_program = config.getAttribute("program");
if (_program == null)
{
throw new LCDException("Expected 'program' parameter");
}
String update = config.getAttribute("update");
_update = Integer.parseInt(update) *
IdlerLoader.ONE_SECOND *
IdlerLoader.ONE_MINUTE;
ExecUpdater updater = new ExecUpdater();
Thread thread = new Thread(updater);
thread.start();
}
/**
* Re-read the feed from the given URL.
* @return <code>true</code> if success, <code>false</code> otherwise.
*/
private boolean execute()
{
_log.info("Executing " + _program);
boolean success = false;
try
{
synchronized (_text)
{
Process proc = _runtime.exec(_program);
BufferedReader stdout = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
String line = stdout.readLine();
while (line != null)
{
_text.append(PADDING + line);
line = stdout.readLine();
}
stdout.close();
}
if (_text.length() > 0)
{
success = true;
}
}
catch (IOException e)
{
// temporary error.
_log.warn("Failed executing " + _program, e);
}
return success;
}
/**
* Is there any text to display?
* @return <code>true</code> if there is anything to display,
* <code>false</code> otherwise.
*/
public boolean isValid()
{
return _valid;
}
/**
* Get the text to display.
* @return the text to display.
*/
public synchronized String getDisplayText()
{
return _text.toString();
}
/**
* Idler is being destroyed, do nothing.
*/
public void destroy()
{
}
/**
* Return a String representing this object.
* @return a String representing this object.
*/
public String toString()
{
return "Program = " + _program + "; Valid = " + _valid;
}
/**
* Thread for handling periodic updates.
* @author Darren Greaves
*/
private class ExecUpdater implements Runnable
{
/**
* Public constructor.
*/
public ExecUpdater()
{
}
/**
* Update the feed.
*/
public void run()
{
while (true)
{
synchronized (this)
{
try
{
_valid = execute();
Thread.sleep(_update);
}
catch (InterruptedException e)
{
// Do nothing
}
}
}
}
}
}