blob: 430e6d9c9a16799431c9da854b7afea67f82309f [file] [log] [blame]
Felix Meschbergerefb2d082008-08-19 13:18:47 +00001/*
Richard S. Hall59aef192009-04-25 14:50:37 +00002 * 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 Meschbergerefb2d082008-08-19 13:18:47 +000017package org.apache.felix.shell.remote;
18
Felix Meschbergerefb2d082008-08-19 13:18:47 +000019import java.io.IOException;
20import java.io.OutputStream;
21import java.io.PrintStream;
Richard S. Hall55900ec2009-04-25 16:57:58 +000022import java.net.SocketException;
Felix Meschbergerefb2d082008-08-19 13:18:47 +000023
Felix Meschbergerefb2d082008-08-19 13:18:47 +000024/**
25 * Class implementing a <tt>TerminalPrintStream</tt>.
Felix Meschbergerefb2d082008-08-19 13:18:47 +000026 */
27class TerminalPrintStream extends PrintStream
28{
Richard S. Hall55900ec2009-04-25 16:57:58 +000029 private final ServiceMediator m_services;
30 private volatile boolean m_isClosed = false;
31
Felix Meschbergerefb2d082008-08-19 13:18:47 +000032 /**
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. Hall55900ec2009-04-25 16:57:58 +000037 public TerminalPrintStream(ServiceMediator services, OutputStream tout)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000038 {
Richard S. Hall59aef192009-04-25 14:50:37 +000039 super(tout);
Richard S. Hall55900ec2009-04-25 16:57:58 +000040 m_services = services;
Felix Meschbergerefb2d082008-08-19 13:18:47 +000041 }//constructor
42
Richard S. Hall59aef192009-04-25 14:50:37 +000043 public void print(String str)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000044 {
45 try
46 {
47 byte[] bytes = str.getBytes();
Richard S. Hall59aef192009-04-25 14:50:37 +000048 out.write(bytes, 0, bytes.length);
Felix Meschbergerefb2d082008-08-19 13:18:47 +000049 flush();
50 }
Richard S. Hall55900ec2009-04-25 16:57:58 +000051 catch (Exception ex)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000052 {
Richard S. Hall55900ec2009-04-25 16:57:58 +000053 if (!m_isClosed)
54 {
55 m_services.error("TerminalPrintStream::print()", ex);
56 }
Felix Meschbergerefb2d082008-08-19 13:18:47 +000057 }
58 }//print
59
Richard S. Hall59aef192009-04-25 14:50:37 +000060 public void println(String str)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000061 {
Richard S. Hall59aef192009-04-25 14:50:37 +000062 print(str + "\r\n");
Felix Meschbergerefb2d082008-08-19 13:18:47 +000063 }//println
64
Felix Meschbergerefb2d082008-08-19 13:18:47 +000065 public void flush()
66 {
67 try
68 {
69 out.flush();
70 }
Richard S. Hall59aef192009-04-25 14:50:37 +000071 catch (IOException ex)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000072 {
Richard S. Hall55900ec2009-04-25 16:57:58 +000073 m_services.error("TerminalPrintStream::println()", ex);
Felix Meschbergerefb2d082008-08-19 13:18:47 +000074 }
75 }//flush
Richard S. Hall55900ec2009-04-25 16:57:58 +000076
77 public void close()
78 {
79 m_isClosed = true;
80 super.close();
81 }
Felix Meschbergerefb2d082008-08-19 13:18:47 +000082}//class TerminalPrintStream