Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.remote; |
| 18 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 19 | import java.io.OutputStream; |
| 20 | import java.io.PrintStream; |
| 21 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Class implementing a <tt>TerminalPrintStream</tt>. |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 24 | */ |
| 25 | class TerminalPrintStream extends PrintStream |
| 26 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 27 | private final ServiceMediator m_services; |
| 28 | private volatile boolean m_isClosed = false; |
| 29 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 30 | /** |
| 31 | * Constructs a new instance wrapping the given <tt>OutputStream</tt>. |
| 32 | * |
| 33 | * @param tout the <tt>OutputStream</tt> to be wrapped. |
| 34 | */ |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 35 | public TerminalPrintStream(ServiceMediator services, OutputStream tout) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 36 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 37 | super(tout); |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 38 | m_services = services; |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 39 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 40 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 41 | public void print(String str) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 42 | { |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 43 | if (out != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 44 | { |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 45 | try |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 46 | { |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 47 | byte[] bytes = str.getBytes(); |
| 48 | out.write(bytes, 0, bytes.length); |
| 49 | flush(); |
| 50 | } |
| 51 | catch (Exception ex) |
| 52 | { |
| 53 | if (!m_isClosed) |
| 54 | { |
| 55 | m_services.error("TerminalPrintStream::print()", ex); |
| 56 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 57 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 58 | } |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 59 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 60 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 61 | public void println(String str) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 62 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 63 | print(str + "\r\n"); |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 64 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 65 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 66 | public void flush() |
| 67 | { |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 68 | if (out != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 69 | { |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 70 | try |
| 71 | { |
| 72 | out.flush(); |
| 73 | } |
| 74 | catch (Exception ex) |
| 75 | { |
| 76 | if (!m_isClosed) |
| 77 | { |
| 78 | m_services.error("TerminalPrintStream::println()", ex); |
| 79 | } |
| 80 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 81 | } |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 82 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 83 | |
| 84 | public void close() |
| 85 | { |
| 86 | m_isClosed = true; |
| 87 | super.close(); |
| 88 | } |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 89 | } |