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.IOException; |
| 20 | import java.io.OutputStream; |
| 21 | import java.io.PrintStream; |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 22 | import java.net.SocketException; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 23 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 24 | /** |
| 25 | * Class implementing a <tt>TerminalPrintStream</tt>. |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 26 | */ |
| 27 | class TerminalPrintStream extends PrintStream |
| 28 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 29 | private final ServiceMediator m_services; |
| 30 | private volatile boolean m_isClosed = false; |
| 31 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Constructs a new instance wrapping the given <tt>OutputStream</tt>. |
| 34 | * |
| 35 | * @param tout the <tt>OutputStream</tt> to be wrapped. |
| 36 | */ |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 37 | public TerminalPrintStream(ServiceMediator services, OutputStream tout) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 38 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 39 | super(tout); |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 40 | m_services = services; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 41 | }//constructor |
| 42 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 43 | public void print(String str) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 44 | { |
| 45 | try |
| 46 | { |
| 47 | byte[] bytes = str.getBytes(); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 48 | out.write(bytes, 0, bytes.length); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 49 | flush(); |
| 50 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 51 | catch (Exception ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 52 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 53 | if (!m_isClosed) |
| 54 | { |
| 55 | m_services.error("TerminalPrintStream::print()", ex); |
| 56 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 57 | } |
| 58 | }//print |
| 59 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 60 | public void println(String str) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 61 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 62 | print(str + "\r\n"); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 63 | }//println |
| 64 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 65 | public void flush() |
| 66 | { |
| 67 | try |
| 68 | { |
| 69 | out.flush(); |
| 70 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 71 | catch (IOException ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 72 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 73 | m_services.error("TerminalPrintStream::println()", ex); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 74 | } |
| 75 | }//flush |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 76 | |
| 77 | public void close() |
| 78 | { |
| 79 | m_isClosed = true; |
| 80 | super.close(); |
| 81 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 82 | }//class TerminalPrintStream |