blob: 3cf20996d27d7df67cf6cabedee712744b504fbd [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.OutputStream;
20import java.io.PrintStream;
21
Felix Meschbergerefb2d082008-08-19 13:18:47 +000022/**
23 * Class implementing a <tt>TerminalPrintStream</tt>.
Felix Meschbergerefb2d082008-08-19 13:18:47 +000024 */
25class TerminalPrintStream extends PrintStream
26{
Richard S. Hall55900ec2009-04-25 16:57:58 +000027 private final ServiceMediator m_services;
28 private volatile boolean m_isClosed = false;
29
Felix Meschbergerefb2d082008-08-19 13:18:47 +000030 /**
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. Hall55900ec2009-04-25 16:57:58 +000035 public TerminalPrintStream(ServiceMediator services, OutputStream tout)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000036 {
Richard S. Hall59aef192009-04-25 14:50:37 +000037 super(tout);
Richard S. Hall55900ec2009-04-25 16:57:58 +000038 m_services = services;
Richard S. Hallfd9c8832010-09-20 19:28:38 +000039 }
Felix Meschbergerefb2d082008-08-19 13:18:47 +000040
Richard S. Hall59aef192009-04-25 14:50:37 +000041 public void print(String str)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000042 {
Richard S. Hallfd9c8832010-09-20 19:28:38 +000043 if (out != null)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000044 {
Richard S. Hallfd9c8832010-09-20 19:28:38 +000045 try
Richard S. Hall55900ec2009-04-25 16:57:58 +000046 {
Richard S. Hallfd9c8832010-09-20 19:28:38 +000047 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. Hall55900ec2009-04-25 16:57:58 +000057 }
Felix Meschbergerefb2d082008-08-19 13:18:47 +000058 }
Richard S. Hallfd9c8832010-09-20 19:28:38 +000059 }
Felix Meschbergerefb2d082008-08-19 13:18:47 +000060
Richard S. Hall59aef192009-04-25 14:50:37 +000061 public void println(String str)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000062 {
Richard S. Hall59aef192009-04-25 14:50:37 +000063 print(str + "\r\n");
Richard S. Hallfd9c8832010-09-20 19:28:38 +000064 }
Felix Meschbergerefb2d082008-08-19 13:18:47 +000065
Felix Meschbergerefb2d082008-08-19 13:18:47 +000066 public void flush()
67 {
Richard S. Hallfd9c8832010-09-20 19:28:38 +000068 if (out != null)
Felix Meschbergerefb2d082008-08-19 13:18:47 +000069 {
Richard S. Hallfd9c8832010-09-20 19:28:38 +000070 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 Meschbergerefb2d082008-08-19 13:18:47 +000081 }
Richard S. Hallfd9c8832010-09-20 19:28:38 +000082 }
Richard S. Hall55900ec2009-04-25 16:57:58 +000083
84 public void close()
85 {
86 m_isClosed = true;
87 super.close();
88 }
Richard S. Hallfd9c8832010-09-20 19:28:38 +000089}