View Javadoc

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