View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2011 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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   * A simple console session for equinox.
29   * 
30   * @author hmalphettes
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              //this never happens?
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       * For the output we are using a string writer in fact.
79       * @return
80       */
81      public StringWriter getOutputAsWriter()
82      {
83          return _outWriter;
84      }
85      
86      /**
87       * @return The print stream where commands can be written.
88       */
89      public PrintStream getSource()
90      {
91          return _source;
92      }
93      
94      /**
95       * Issue a command on the console.
96       * @param cmd
97       */
98      public void issueCommand(String cmd)
99      {
100         if (cmd != null)
101         {
102             getSource().println(cmd);
103         }
104     }
105     
106     /**
107      * @param flushListener Object that is called back when the outputstream is flushed.
108      */
109     public void addOnFlushListener(OnFlushListener flushListener)
110     {
111         ((WebConsoleWriterOutputStream)_out).addOnFlushListener(flushListener);
112     }
113     /**
114      * @param flushListener Object that is called back when the outputstream is flushed.
115      */
116     public boolean removeOnFlushListener(OnFlushListener flushListener)
117     {
118         return ((WebConsoleWriterOutputStream)_out).removeOnFlushListener(flushListener);
119     }
120     
121     /**
122      * Process command comming from a web UI
123      * @param cmd
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                 //it does not get uglier than this:
145                 //give a little time to equinox to interpret the command so we see the response
146                 //we could do a lot better, but we might as well use the async servlets anyways.
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 }