1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.osgi.equinoxtools.console;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.PipedInputStream;
25 import java.io.PipedOutputStream;
26 import java.io.PrintStream;
27 import java.io.StringWriter;
28 import java.util.List;
29
30 import org.eclipse.jetty.osgi.equinoxtools.console.WebConsoleWriterOutputStream.OnFlushListener;
31 import org.eclipse.osgi.framework.console.ConsoleSession;
32
33
34
35
36
37
38 public class WebConsoleSession extends ConsoleSession
39 {
40
41 private OutputStream _out;
42 private StringWriter _outWriter;
43 private PrintStream _source;
44 private InputStream _in;
45
46 public WebConsoleSession()
47 {
48 _outWriter = new StringWriter();
49 _out = new WebConsoleWriterOutputStream(_outWriter,"UTF-8");
50 try
51 {
52 PipedOutputStream source = new PipedOutputStream();
53 PipedInputStream sink = new PipedInputStream(source);
54 _in = sink;
55 _source = new PrintStream(source);
56 }
57 catch (IOException e)
58 {
59
60 e.printStackTrace();
61 }
62 }
63
64 @Override
65 protected void doClose()
66 {
67 if (_out != null) try { _out.close(); } catch (IOException e) {}
68 if (_in != null) try { _in.close(); } catch (IOException ioe) {}
69 }
70
71 @Override
72 public InputStream getInput()
73 {
74 return _in;
75 }
76
77 @Override
78 public OutputStream getOutput()
79 {
80 return _out;
81 }
82
83
84
85
86
87 public StringWriter getOutputAsWriter()
88 {
89 return _outWriter;
90 }
91
92
93
94
95 public PrintStream getSource()
96 {
97 return _source;
98 }
99
100
101
102
103
104 public void issueCommand(String cmd)
105 {
106 if (cmd != null)
107 {
108 getSource().println(cmd);
109 }
110 }
111
112
113
114
115 public void addOnFlushListener(OnFlushListener flushListener)
116 {
117 ((WebConsoleWriterOutputStream)_out).addOnFlushListener(flushListener);
118 }
119
120
121
122 public boolean removeOnFlushListener(OnFlushListener flushListener)
123 {
124 return ((WebConsoleWriterOutputStream)_out).removeOnFlushListener(flushListener);
125 }
126
127
128
129
130
131 public void processCommand(String cmd, boolean wait)
132 {
133 cmd = cmd.trim();
134 while (cmd.startsWith("osgi>"))
135 {
136 cmd = cmd.substring("osgi>".length()).trim();
137 }
138 if (cmd.equals("clear"))
139 {
140 clearOutput();
141 }
142 else
143 {
144 getOutputAsWriter().append(cmd + "\n");
145 int originalOutputLength = getOutputAsWriter().getBuffer().length();
146 issueCommand(cmd);
147
148 if (wait)
149 {
150
151
152
153 int waitLoopNumber = 0;
154 int lastWaitOutputLength = -1;
155 while (waitLoopNumber < 10)
156 {
157 waitLoopNumber++;
158 try
159 {
160 Thread.sleep(100);
161 }
162 catch (InterruptedException e)
163 {
164 break;
165 }
166 int newOutputLength = getOutputAsWriter().getBuffer().length();
167 if (newOutputLength > originalOutputLength && newOutputLength == lastWaitOutputLength)
168 {
169 break;
170 }
171 lastWaitOutputLength = newOutputLength;
172 }
173 }
174 }
175
176 }
177
178 public void clearOutput()
179 {
180 StringBuffer buf = getOutputAsWriter().getBuffer();
181 if (buf.length() > 0) buf.delete(0,buf.length()-1);
182 }
183
184 public List<OnFlushListener> getOnFlushListeners()
185 {
186 return ((WebConsoleWriterOutputStream)_out).getOnFlushListeners();
187 }
188
189 }