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.Writer;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Can be set with a listener that is called back right after the flush method is called.
28   */
29  public class WebConsoleWriterOutputStream extends org.eclipse.jetty.io.WriterOutputStream
30  {
31      
32      /**
33       * Interface called back after the outputstream is flushed.
34       */
35      public interface OnFlushListener
36      {
37          /**
38           * Called right after the flush method on the output stream has been executed.
39           */
40          public void onFlush();
41  
42      }
43      
44      public interface MessageBroadcaster
45      {
46          public void broadcast();
47      }
48  
49      private List<OnFlushListener> _callBacks;
50  
51      public WebConsoleWriterOutputStream(Writer writer, String encoding)
52      {
53          super(writer, encoding);
54      }
55      
56      @Override
57      public synchronized void flush() throws IOException
58      {
59          super.flush();
60          if (_callBacks != null)
61          {
62              for (OnFlushListener listener : _callBacks)
63              {
64                  listener.onFlush();
65              }
66          }
67      }
68      
69      public synchronized void addOnFlushListener(OnFlushListener callback)
70      {
71          if (_callBacks == null)
72          {
73              _callBacks = new ArrayList<WebConsoleWriterOutputStream.OnFlushListener>();
74          }
75          if (!_callBacks.contains(callback))
76          {
77              _callBacks.add(callback);
78          }
79      }
80      public synchronized boolean removeOnFlushListener(OnFlushListener callback)
81      {
82          if (_callBacks != null)
83          {
84              return _callBacks.remove(callback);
85          }
86          return false;
87      }
88      public synchronized List<OnFlushListener> getOnFlushListeners()
89      {
90          return _callBacks;
91      }
92      
93  }