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