01: package vicazh.hyperpool.stream;
02:
03: import java.io.*;
04: import vicazh.hyperpool.Start;
05:
06: /**
07: * The dial service
08: *
09: * @author Victor Zhigunov
10: * @version 0.4.0
11: */
12: public class DialService extends Service implements DialServiceMBean {
13: private String command;
14:
15: /**
16: * Set the command
17: */
18: public void setCommand(String command) {
19: this .command = command;
20: }
21:
22: /**
23: * Get the command
24: */
25: public String getCommand() {
26: return command;
27: }
28:
29: public Connection getConnection() {
30: return new DialConnection(this );
31: }
32:
33: synchronized void dial(DialConnection connection)
34: throws IOException {
35: byte[] b = connection.astream.toByteArray();
36: try {
37: connection.getClient().outputstream.write(b);
38: } catch (IOException e) {
39: exec();
40: connection.getClient().outputstream = ((Element) getElement())
41: .get(connection.getServer().outputstream);
42: connection.getClient().outputstream.write(b);
43: }
44: }
45:
46: synchronized public OutputStream get(OutputStream outputstream)
47: throws IOException {
48: try {
49: return super .get(outputstream);
50: } catch (IOException e) {
51: exec();
52: }
53: return super .get(outputstream);
54: }
55:
56: private void exec() throws IOException {
57: Start.logger.fine(command);
58: try {
59: Runtime.getRuntime().exec(command).waitFor();
60: } catch (Exception ex) {
61: throw new IOException(ex.getMessage());
62: }
63: }
64:
65: public void setAttribute(String name, Object value)
66: throws Exception {
67: if (name.equals(DialServiceMBean.COMMAND))
68: setCommand((String) value);
69: super.setAttribute(name, value);
70: }
71: }
|