| 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 |
*/ |
| 19 |
// DWB20: ThreadIO should check and reset IO if something (e.g. jetty) overrides |
| 20 |
package org.apache.felix.gogo.runtime.threadio; |
| 21 |
|
| 22 |
import org.apache.felix.service.threadio.ThreadIO; |
| 23 |
|
| 24 |
import java.io.InputStream; |
| 25 |
import java.io.PrintStream; |
| 26 |
import java.util.logging.Logger; |
| 27 |
|
| 28 |
public class ThreadIOImpl implements ThreadIO |
| 29 |
{ |
| 30 |
static private final Logger log = Logger.getLogger(ThreadIOImpl.class.getName()); |
| 31 |
|
| 32 |
final Marker defaultMarker = new Marker(System.in, System.out, System.err, null); |
| 33 |
final ThreadPrintStream err = new ThreadPrintStream(this, System.err, true); |
| 34 |
final ThreadPrintStream out = new ThreadPrintStream(this, System.out, false); |
| 35 |
final ThreadInputStream in = new ThreadInputStream(this, System.in); |
| 36 |
final ThreadLocal<Marker> current = new InheritableThreadLocal<Marker>() |
| 37 |
{ |
| 38 |
@Override |
| 39 |
protected Marker initialValue() |
| 40 |
{ |
| 41 |
return defaultMarker; |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
public void start() |
| 46 |
{ |
| 47 |
if (System.out instanceof ThreadPrintStream) |
| 48 |
{ |
| 49 |
throw new IllegalStateException("Thread Print Stream already set"); |
| 50 |
} |
| 51 |
System.setOut(out); |
| 52 |
System.setIn(in); |
| 53 |
System.setErr(err); |
| 54 |
} |
| 55 |
|
| 56 |
public void stop() |
| 57 |
{ |
| 58 |
System.setErr(defaultMarker.err); |
| 59 |
System.setOut(defaultMarker.out); |
| 60 |
System.setIn(defaultMarker.in); |
| 61 |
} |
| 62 |
|
| 63 |
private void checkIO() |
| 64 |
{ // derek |
| 65 |
if (System.in != in) |
| 66 |
{ |
| 67 |
log.fine("ThreadIO: eek! who's set System.in=" + System.in); |
| 68 |
System.setIn(in); |
| 69 |
} |
| 70 |
|
| 71 |
if (System.out != out) |
| 72 |
{ |
| 73 |
log.fine("ThreadIO: eek! who's set System.out=" + System.out); |
| 74 |
System.setOut(out); |
| 75 |
} |
| 76 |
|
| 77 |
if (System.err != err) |
| 78 |
{ |
| 79 |
log.fine("ThreadIO: eek! who's set System.err=" + System.err); |
| 80 |
System.setErr(err); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
Marker current() |
| 85 |
{ |
| 86 |
Marker m = current.get(); |
| 87 |
if (m.deactivated) |
| 88 |
{ |
| 89 |
while (m.deactivated) |
| 90 |
{ |
| 91 |
m = m.previous; |
| 92 |
} |
| 93 |
current.set(m); |
| 94 |
} |
| 95 |
return m; |
| 96 |
} |
| 97 |
|
| 98 |
public void close() |
| 99 |
{ |
| 100 |
checkIO(); // derek |
| 101 |
Marker top = this.current.get(); |
| 102 |
if (top == null) |
| 103 |
{ |
| 104 |
throw new IllegalStateException("No thread io active"); |
| 105 |
} |
| 106 |
if (top != defaultMarker) |
| 107 |
{ |
| 108 |
top.deactivate(); |
| 109 |
this.current.set(top.previous); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public void setStreams(InputStream in, PrintStream out, PrintStream err) |
| 114 |
{ |
| 115 |
assert in != null; |
| 116 |
assert out != null; |
| 117 |
assert err != null; |
| 118 |
checkIO(); // derek |
| 119 |
Marker prev = current(); |
| 120 |
if (in == this.in) { |
| 121 |
in = prev.getIn(); |
| 122 |
} |
| 123 |
if (out == this.out) { |
| 124 |
out = prev.getOut(); |
| 125 |
} |
| 126 |
if (err == this.err) { |
| 127 |
err = prev.getErr(); |
| 128 |
} |
| 129 |
Marker marker = new Marker(in, out, err, prev); |
| 130 |
this.current.set(marker); |
| 131 |
} |
| 132 |
} |