Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 1 | /* |
| 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 Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 19 | // DWB20: ThreadIO should check and reset IO if something (e.g. jetty) overrides |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 20 | package aQute.threadio; |
| 21 | |
| 22 | import java.io.*; |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 23 | import java.util.logging.Logger; |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 24 | |
| 25 | import org.osgi.service.component.*; |
| 26 | import org.osgi.service.threadio.*; |
| 27 | |
| 28 | public class ThreadIOImpl implements ThreadIO { |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 29 | static private final Logger log = Logger.getLogger(ThreadIOImpl.class.getName()); |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 30 | 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 Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 57 | 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. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 74 | public void close() { |
Guillaume Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 75 | checkIO(); // derek |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 76 | 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 Nodet | 4d9d13e | 2009-06-22 15:21:30 +0000 | [diff] [blame] | 95 | checkIO(); // derek |
Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 96 | Marker marker = new Marker(this,in,out,err, current.get()); |
| 97 | this.current.set(marker); |
| 98 | marker.activate(); |
| 99 | } |
| 100 | } |