blob: 55b5ae3c69f185dfb69623348f0504deafde9f49 [file] [log] [blame]
Richard S. Hallfbd735b2009-06-11 16:07:20 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Guillaume Nodet4d9d13e2009-06-22 15:21:30 +000019// DWB20: ThreadIO should check and reset IO if something (e.g. jetty) overrides
Richard S. Hallfbd735b2009-06-11 16:07:20 +000020package aQute.threadio;
21
22import java.io.*;
Guillaume Nodet4d9d13e2009-06-22 15:21:30 +000023import java.util.logging.Logger;
Richard S. Hallfbd735b2009-06-11 16:07:20 +000024
25import org.osgi.service.component.*;
26import org.osgi.service.threadio.*;
27
28public class ThreadIOImpl implements ThreadIO {
Guillaume Nodet4d9d13e2009-06-22 15:21:30 +000029 static private final Logger log = Logger.getLogger(ThreadIOImpl.class.getName());
Richard S. Hallfbd735b2009-06-11 16:07:20 +000030 ThreadPrintStream err = new ThreadPrintStream(System.err);
31 ThreadPrintStream out = new ThreadPrintStream(System.out);
32 ThreadInputStream in = new ThreadInputStream(System.in);
33 ThreadLocal<Marker> current = new ThreadLocal<Marker>();
34
35 protected void activate(ComponentContext context) {
36 start();
37 }
38
39 protected void deactivate() {
40 stop();
41 }
42
43 public void stop() {
44 System.setErr(err.dflt);
45 System.setOut(out.dflt);
46 System.setIn(in.dflt);
47 }
48
49 public void start() {
50 if ( System.out instanceof ThreadPrintStream )
51 throw new IllegalStateException("Thread Print Stream already set");
52 System.setOut(out);
53 System.setIn(in);
54 System.setErr(err);
55 }
56
Guillaume Nodet4d9d13e2009-06-22 15:21:30 +000057 private void checkIO() { // derek
58 if (System.in != in) {
59 log.fine("ThreadIO: eek! who's set System.in=" + System.in);
60 System.setIn(in);
61 }
62
63 if (System.out != out) {
64 log.fine("ThreadIO: eek! who's set System.out=" + System.out);
65 System.setOut(out);
66 }
67
68 if (System.err != err) {
69 log.fine("ThreadIO: eek! who's set System.err=" + System.err);
70 System.setErr(err);
71 }
72 }
73
Richard S. Hallfbd735b2009-06-11 16:07:20 +000074 public void close() {
Guillaume Nodet4d9d13e2009-06-22 15:21:30 +000075 checkIO(); // derek
Richard S. Hallfbd735b2009-06-11 16:07:20 +000076 Marker top = this.current.get();
77 if ( top == null )
78 throw new IllegalStateException("No thread io active");
79
80 Marker previous = top.previous;
81 if (previous==null) {
82 in.end();
83 out.end();
84 err.end();
85 } else {
86 this.current.set(previous);
87 previous.activate();
88 }
89 }
90
91 public void setStreams(InputStream in, PrintStream out, PrintStream err) {
92 assert in != null;
93 assert out != null;
94 assert err != null;
Guillaume Nodet4d9d13e2009-06-22 15:21:30 +000095 checkIO(); // derek
Richard S. Hallfbd735b2009-06-11 16:07:20 +000096 Marker marker = new Marker(this,in,out,err, current.get());
97 this.current.set(marker);
98 marker.activate();
99 }
100}