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