[jboss-svn-commits] JBoss Common SVN: r3181 - in jboss-logmanager/trunk/src/main/java/org/jboss: stdio and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed May 20 16:10:07 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-05-20 16:10:06 -0400 (Wed, 20 May 2009)
New Revision: 3181

Added:
   jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContext.java
   jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java
   jboss-logmanager/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java
Removed:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WriterOutputStream.java
   jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextInputStream.java
   jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextPrintStream.java
   jboss-logmanager/trunk/src/main/java/org/jboss/stdio/Init.java
Log:
Shuffle stuff around for new stdio lib

Deleted: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java	2009-05-20 20:09:19 UTC (rev 3180)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -1,48 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.logmanager;
-
-import java.util.logging.Logger;
-import java.util.logging.Level;
-
-/**
- * A writer which writes to the current logging context's standard error logger.
- */
-public final class SystemErrLoggingWriter extends AbstractLoggingWriter {
-
-    /**
-     * Construct a new instance.
-     */
-    public SystemErrLoggingWriter() {
-    }
-
-    /** {@inheritDoc} */
-    protected Logger getLogger() {
-        return LogContext.getLogContext().getStderrLogger();
-    }
-
-    /** {@inheritDoc} */
-    protected Level getLevel() {
-        return Level.WARNING;
-    }
-}
\ No newline at end of file

Deleted: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java	2009-05-20 20:09:19 UTC (rev 3180)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -1,48 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.logmanager;
-
-import java.util.logging.Logger;
-import java.util.logging.Level;
-
-/**
- * A writer which writes to the current logging context's standard output logger.
- */
-public final class SystemOutLoggingWriter extends AbstractLoggingWriter {
-
-    /**
-     * Construct a new instance.
-     */
-    public SystemOutLoggingWriter() {
-    }
-
-    /** {@inheritDoc} */
-    protected Logger getLogger() {
-        return LogContext.getLogContext().getStdoutLogger();
-    }
-
-    /** {@inheritDoc} */
-    protected Level getLevel() {
-        return Level.INFO;
-    }
-}

Deleted: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WriterOutputStream.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WriterOutputStream.java	2009-05-20 20:09:19 UTC (rev 3180)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WriterOutputStream.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -1,146 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.logmanager;
-
-import java.io.OutputStream;
-import java.io.IOException;
-import java.io.Writer;
-import java.nio.charset.CharsetDecoder;
-import java.nio.charset.Charset;
-import java.nio.charset.CoderResult;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
-
-/**
- * An output stream which decodes into a writer.
- */
-public final class WriterOutputStream extends OutputStream {
-
-    private final Writer writer;
-    private final CharsetDecoder decoder;
-    private final ByteBuffer inputBuffer;
-    private final CharBuffer outputBuffer;
-
-    /**
-     * Construct a new instance using the default charset.
-     *
-     * @param writer the writer to write to
-     */
-    public WriterOutputStream(final Writer writer) {
-        this(writer, Charset.defaultCharset());
-    }
-
-    /**
-     * Construct a new instance using the named charset.
-     *
-     * @param writer the writer to write to
-     * @param charsetName the charset name
-     */
-    public WriterOutputStream(final Writer writer, final String charsetName) {
-        this(writer, Charset.forName(charsetName));
-    }
-
-    /**
-     * Construct a new instance using the given charset.
-     *
-     * @param writer the writer to write to
-     * @param charset the charset
-     */
-    public WriterOutputStream(final Writer writer, final Charset charset) {
-        this(writer, charset.newDecoder());
-    }
-
-    /**
-     * Construct a new instance using the given charset decoder.
-     *
-     * @param writer the writer to write to
-     * @param decoder the charset decoder
-     */
-    public WriterOutputStream(final Writer writer, final CharsetDecoder decoder) {
-        this.writer = writer;
-        this.decoder = decoder;
-        inputBuffer = ByteBuffer.allocate(256);
-        outputBuffer = CharBuffer.allocate(256);
-    }
-
-    /** {@inheritDoc} */
-    public void write(final int b) throws IOException {
-        synchronized (decoder) {
-            final ByteBuffer inputBuffer = this.inputBuffer;
-            inputBuffer.put((byte) b);
-            if (! inputBuffer.hasRemaining()) {
-                flush();
-            }
-        }
-    }
-
-    /** {@inheritDoc} */
-    public void write(final byte[] b, int off, int len) throws IOException {
-        synchronized (decoder) {
-            for (;;) {
-                final ByteBuffer inputBuffer = this.inputBuffer;
-                int cnt = Math.min(inputBuffer.remaining(), len);
-                inputBuffer.put(b, off, cnt);
-                len -= cnt;
-                off += cnt;
-                if (len == 0) {
-                    return;
-                }
-                flush();
-            }
-        }
-    }
-
-    /** {@inheritDoc} */
-    public void flush() throws IOException {
-        synchronized (decoder) {
-            final CharBuffer outputBuffer = this.outputBuffer;
-            final ByteBuffer inputBuffer = this.inputBuffer;
-            inputBuffer.flip();
-            for (;;) {
-                final CoderResult coderResult = decoder.decode(inputBuffer, outputBuffer, false);
-                if (coderResult.isOverflow()) {
-                    outputBuffer.flip();
-                    boolean ok = false;
-                    try {
-                        writer.write(outputBuffer.array(), outputBuffer.arrayOffset(), outputBuffer.remaining());
-                        ok = true;
-                    } finally {
-                        if (! ok) {
-                            inputBuffer.clear();
-                        }
-                        outputBuffer.clear();
-                    }
-                } else if (coderResult.isUnderflow()) {
-                    inputBuffer.compact();
-                    return;
-                }
-            }
-        }
-    }
-
-    /** {@inheritDoc} */
-    public void close() throws IOException {
-        flush();
-    }
-}

Deleted: jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextInputStream.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextInputStream.java	2009-05-20 20:09:19 UTC (rev 3180)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextInputStream.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -1,98 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.stdio;
-
-import java.io.InputStream;
-import java.io.IOException;
-
-public final class ContextInputStream extends InputStream {
-    private static final class StreamHolder extends InheritableThreadLocal<InputStream> {
-        private final InputStream initialValue;
-
-        private StreamHolder(final InputStream initialValue) {
-            this.initialValue = initialValue;
-        }
-
-        protected InputStream initialValue() {
-            return initialValue;
-        }
-    }
-
-    private final ThreadLocal<InputStream> delegateHolder;
-
-    public ContextInputStream(final InputStream defaultDelegate) {
-        delegateHolder = new StreamHolder(defaultDelegate);
-    }
-
-    public InputStream swapDelegate(final InputStream newDelegate) {
-        try {
-            return delegateHolder.get();
-        } finally {
-            delegateHolder.set(newDelegate);
-        }
-    }
-
-    public static InputStream getAndSetSystemIn(final InputStream newSystemIn) {
-        return ((ContextInputStream)System.in).swapDelegate(newSystemIn);
-    }
-
-    private InputStream getDelegate() {
-        return delegateHolder.get();
-    }
-
-    public int read() throws IOException {
-        return getDelegate().read();
-    }
-
-    public int read(final byte[] b) throws IOException {
-        return getDelegate().read(b);
-    }
-
-    public int read(final byte[] b, final int off, final int len) throws IOException {
-        return getDelegate().read(b, off, len);
-    }
-
-    public long skip(final long n) throws IOException {
-        return getDelegate().skip(n);
-    }
-
-    public int available() throws IOException {
-        return getDelegate().available();
-    }
-
-    public void close() throws IOException {
-        getDelegate().close();
-    }
-
-    public void mark(final int readlimit) {
-        getDelegate().mark(readlimit);
-    }
-
-    public void reset() throws IOException {
-        getDelegate().reset();
-    }
-
-    public boolean markSupported() {
-        return getDelegate().markSupported();
-    }
-}

Deleted: jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextPrintStream.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextPrintStream.java	2009-05-20 20:09:19 UTC (rev 3180)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/stdio/ContextPrintStream.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -1,200 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.stdio;
-
-import java.io.OutputStream;
-import java.io.PrintStream;
-import java.io.IOException;
-import java.util.Locale;
-
-public final class ContextPrintStream extends PrintStream {
-
-    private static final class StreamHolder extends InheritableThreadLocal<PrintStream> {
-        private final PrintStream initialValue;
-
-        private StreamHolder(final PrintStream initialValue) {
-            this.initialValue = initialValue;
-        }
-
-        protected PrintStream initialValue() {
-            return initialValue;
-        }
-    }
-
-    private final ThreadLocal<PrintStream> delegateHolder;
-
-    public ContextPrintStream(PrintStream defaultDelegate) {
-        super((OutputStream)null);
-        delegateHolder = new StreamHolder(defaultDelegate);
-    }
-
-    public PrintStream swapDelegate(final PrintStream newDelegate) {
-        try {
-            return delegateHolder.get();
-        } finally {
-            delegateHolder.set(newDelegate);
-        }
-    }
-
-    public static PrintStream getAndSetContextSystemOut(final PrintStream newSystemOut) {
-        return ((ContextPrintStream)System.out).swapDelegate(newSystemOut);
-    }
-
-    public static PrintStream getAndSetContextSystemErr(final PrintStream newSystemOut) {
-        return ((ContextPrintStream)System.err).swapDelegate(newSystemOut);
-    }
-
-    public void close() {
-        delegateHolder.get().close();
-    }
-
-    public void flush() {
-        delegateHolder.get().flush();
-    }
-
-    public boolean checkError() {
-        return delegateHolder.get().checkError();
-    }
-
-    public void write(final int b) {
-        delegateHolder.get().write(b);
-    }
-
-    public void write(final byte[] buf, final int off, final int len) {
-        delegateHolder.get().write(buf, off, len);
-    }
-
-    public void print(final boolean b) {
-        delegateHolder.get().print(b);
-    }
-
-    public void print(final char c) {
-        delegateHolder.get().print(c);
-    }
-
-    public void print(final int i) {
-        delegateHolder.get().print(i);
-    }
-
-    public void print(final long l) {
-        delegateHolder.get().print(l);
-    }
-
-    public void print(final float f) {
-        delegateHolder.get().print(f);
-    }
-
-    public void print(final double d) {
-        delegateHolder.get().print(d);
-    }
-
-    public void print(final char[] s) {
-        delegateHolder.get().print(s);
-    }
-
-    public void print(final String s) {
-        delegateHolder.get().print(s);
-    }
-
-    public void print(final Object obj) {
-        delegateHolder.get().print(obj);
-    }
-
-    public void println() {
-        delegateHolder.get().println();
-    }
-
-    public void println(final boolean x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final char x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final int x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final long x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final float x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final double x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final char[] x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final String x) {
-        delegateHolder.get().println(x);
-    }
-
-    public void println(final Object x) {
-        delegateHolder.get().println(x);
-    }
-
-    public PrintStream printf(final String format, final Object... args) {
-        return delegateHolder.get().printf(format, args);
-    }
-
-    public PrintStream printf(final Locale l, final String format, final Object... args) {
-        return delegateHolder.get().printf(l, format, args);
-    }
-
-    public PrintStream format(final String format, final Object... args) {
-        return delegateHolder.get().format(format, args);
-    }
-
-    public PrintStream format(final Locale l, final String format, final Object... args) {
-        return delegateHolder.get().format(l, format, args);
-    }
-
-    public PrintStream append(final CharSequence csq) {
-        return delegateHolder.get().append(csq);
-    }
-
-    public PrintStream append(final CharSequence csq, final int start, final int end) {
-        return delegateHolder.get().append(csq, start, end);
-    }
-
-    public PrintStream append(final char c) {
-        return delegateHolder.get().append(c);
-    }
-
-    public void write(final byte[] b) throws IOException {
-        delegateHolder.get().write(b);
-    }
-
-    protected void setError() {
-    }
-
-    protected void clearError() {
-    }
-}

Deleted: jboss-logmanager/trunk/src/main/java/org/jboss/stdio/Init.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/stdio/Init.java	2009-05-20 20:09:19 UTC (rev 3180)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/stdio/Init.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -1,36 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.stdio;
-
-import java.io.PrintStream;
-import java.io.InputStream;
-
-public final class Init {
-    private Init() {}
-
-    public static void initSystemStreams(final PrintStream defaultSystemOut, final PrintStream defaultSystemErr, final InputStream defaultSystemIn) {
-        System.setOut(new ContextPrintStream(defaultSystemOut));
-        System.setErr(new ContextPrintStream(defaultSystemErr));
-        System.setIn(new ContextInputStream(defaultSystemIn));
-    }
-}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContext.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContext.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContext.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -0,0 +1,356 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.IOException;
+import java.security.Permission;
+import java.util.Locale;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A context for console input and output.
+ */
+public final class StdioContext {
+    private static final StdioContext SYSTEM_STDIO_CONTEXT = new StdioContext(System.in, System.out, System.err);
+
+    private static final Permission CREATE_CONTEXT_PERMISSION = new RuntimePermission("createStdioContext", null);
+    private static final Permission SET_CONTEXT_SELECTOR_PERMISSION = new RuntimePermission("setStdioContextSelector", null);
+    private static final Permission INSTALL_PERMISSION = new RuntimePermission("installStdioContextSelector", null);
+
+    private enum State {
+        UNINSTALLED,
+        INSTALLING,
+        INSTALLED,
+        UNINSTALLING,
+    }
+
+    private static final AtomicReference<State> state = new AtomicReference<State>(State.UNINSTALLED);
+
+    private final InputStream in;
+    private final PrintStream out;
+    private final PrintStream err;
+
+    StdioContext(final InputStream in, final PrintStream out, final PrintStream err) {
+        this.in = in;
+        this.out = out;
+        this.err = err;
+    }
+
+    /**
+     * Create a console I/O context.
+     *
+     * @param in the input stream for this context
+     * @param out the output stream for this context
+     * @param err the error stream for this context
+     * @return the new context
+     * @throws SecurityException if the caller does not have the {@code createStdioContext} {@link RuntimePermission}
+     */
+    public static StdioContext create(final InputStream in, final PrintStream out, final PrintStream err) throws SecurityException {
+        final SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            sm.checkPermission(CREATE_CONTEXT_PERMISSION);
+        }
+        return new StdioContext(in, out, err);
+    }
+
+    /**
+     * Get the current console I/O context.
+     *
+     * @return the current context
+     */
+    public static StdioContext getStdioContext() {
+        return stdioContextSelector.getStdioContext();
+    }
+
+    /**
+     * Get the input stream for this context.
+     *
+     * @return the input stream
+     */
+    public InputStream getIn() {
+        return in;
+    }
+
+    /**
+     * Get the output stream for this context.
+     *
+     * @return the output stream
+     */
+    public PrintStream getOut() {
+        return out;
+    }
+
+    /**
+     * Get the error stream for this context.
+     *
+     * @return the error stream
+     */
+    public PrintStream getErr() {
+        return err;
+    }
+
+    private static volatile StdioContextSelector stdioContextSelector = new StdioContextSelector() {
+        public StdioContext getStdioContext() {
+            return SYSTEM_STDIO_CONTEXT;
+        }
+    };
+
+    /**
+     * Install the StdioContext streams.
+     *
+     * @throws SecurityException if the caller does not have the {@code installStdioContextSelector} {@link RuntimePermission}
+     * @throws IllegalStateException if the streams are already installed
+     */
+    public static void install() throws SecurityException, IllegalStateException {
+        final SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            sm.checkPermission(INSTALL_PERMISSION);
+        }
+        if (! state.compareAndSet(State.UNINSTALLED, State.INSTALLING)) {
+            throw new IllegalStateException("Already installed");
+        }
+        System.setOut(new DelegatingPrintStream() {
+            PrintStream getDelegate() {
+                return stdioContextSelector.getStdioContext().out;
+            }
+        });
+        System.setErr(new DelegatingPrintStream() {
+            PrintStream getDelegate() {
+                return stdioContextSelector.getStdioContext().err;
+            }
+        });
+        System.setIn(new DelegatingInputStream() {
+            InputStream getDelegate() {
+                return stdioContextSelector.getStdioContext().in;
+            }
+        });
+        state.set(State.INSTALLED);
+    }
+
+    /**
+     * Uninstall the StdioContext streams.
+     *
+     * @throws SecurityException if the caller does not have the {@code installStdioContextSelector} {@link RuntimePermission}
+     * @throws IllegalStateException if the streams are already uninstalled
+     */
+    public static void uninstall() throws SecurityException, IllegalStateException {
+        final SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            sm.checkPermission(INSTALL_PERMISSION);
+        }
+        if (! state.compareAndSet(State.INSTALLED, State.UNINSTALLING)) {
+            throw new IllegalStateException("Already uninstalled");
+        }
+        System.setOut(SYSTEM_STDIO_CONTEXT.out);
+        System.setErr(SYSTEM_STDIO_CONTEXT.err);
+        System.setIn(SYSTEM_STDIO_CONTEXT.in);
+        state.set(State.UNINSTALLED);
+    }
+
+    public static void setStdioContextSelector(final StdioContextSelector stdioContextSelector) {
+        if (stdioContextSelector == null) {
+            throw new NullPointerException("stdioContextSelector is null");
+        }
+        final SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            sm.checkPermission(SET_CONTEXT_SELECTOR_PERMISSION);
+        }
+        StdioContext.stdioContextSelector = stdioContextSelector;
+    }
+
+    private static abstract class DelegatingPrintStream extends PrintStream {
+
+        protected DelegatingPrintStream() {
+            super((OutputStream) null);
+        }
+
+        abstract PrintStream getDelegate();
+
+        public void flush() {
+            getDelegate().flush();
+        }
+
+        public void close() {
+            getDelegate().close();
+        }
+
+        public boolean checkError() {
+            return getDelegate().checkError();
+        }
+
+        public void write(final int b) {
+            getDelegate().write(b);
+        }
+
+        public void write(final byte[] buf, final int off, final int len) {
+            getDelegate().write(buf, off, len);
+        }
+
+        public void print(final boolean b) {
+            getDelegate().print(b);
+        }
+
+        public void print(final char c) {
+            getDelegate().print(c);
+        }
+
+        public void print(final int i) {
+            getDelegate().print(i);
+        }
+
+        public void print(final long l) {
+            getDelegate().print(l);
+        }
+
+        public void print(final float f) {
+            getDelegate().print(f);
+        }
+
+        public void print(final double d) {
+            getDelegate().print(d);
+        }
+
+        public void print(final char[] s) {
+            getDelegate().print(s);
+        }
+
+        public void print(final String s) {
+            getDelegate().print(s);
+        }
+
+        public void print(final Object obj) {
+            getDelegate().print(obj);
+        }
+
+        public void println() {
+            getDelegate().println();
+        }
+
+        public void println(final boolean x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final char x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final int x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final long x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final float x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final double x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final char[] x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final String x) {
+            getDelegate().println(x);
+        }
+
+        public void println(final Object x) {
+            getDelegate().println(x);
+        }
+
+        public PrintStream printf(final String format, final Object... args) {
+            return getDelegate().printf(format, args);
+        }
+
+        public PrintStream printf(final Locale l, final String format, final Object... args) {
+            return getDelegate().printf(l, format, args);
+        }
+
+        public PrintStream format(final String format, final Object... args) {
+            return getDelegate().format(format, args);
+        }
+
+        public PrintStream format(final Locale l, final String format, final Object... args) {
+            return getDelegate().format(l, format, args);
+        }
+
+        public PrintStream append(final CharSequence csq) {
+            return getDelegate().append(csq);
+        }
+
+        public PrintStream append(final CharSequence csq, final int start, final int end) {
+            return getDelegate().append(csq, start, end);
+        }
+
+        public PrintStream append(final char c) {
+            return getDelegate().append(c);
+        }
+    }
+
+    private static abstract class DelegatingInputStream extends InputStream {
+        abstract InputStream getDelegate();
+
+        public int read() throws IOException {
+            return getDelegate().read();
+        }
+
+        public int read(final byte[] b) throws IOException {
+            return getDelegate().read(b);
+        }
+
+        public int read(final byte[] b, final int off, final int len) throws IOException {
+            return getDelegate().read(b, off, len);
+        }
+
+        public long skip(final long n) throws IOException {
+            return getDelegate().skip(n);
+        }
+
+        public int available() throws IOException {
+            return getDelegate().available();
+        }
+
+        public void close() throws IOException {
+            getDelegate().close();
+        }
+
+        public void mark(final int readlimit) {
+            getDelegate().mark(readlimit);
+        }
+
+        public void reset() throws IOException {
+            getDelegate().reset();
+        }
+
+        public boolean markSupported() {
+            return getDelegate().markSupported();
+        }
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/stdio/StdioContextSelector.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+/**
+ * A mechanism for determining what the current standard I/O context is.  All I/O to {@link System#in}, {@link System#out},
+ * and {@link System#err} are handled by the current standard I/O context
+ */
+public interface StdioContextSelector {
+    StdioContext getStdioContext();
+}

Copied: jboss-logmanager/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java (from rev 3176, jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WriterOutputStream.java)
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/stdio/WriterOutputStream.java	2009-05-20 20:10:06 UTC (rev 3181)
@@ -0,0 +1,146 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stdio;
+
+import java.io.OutputStream;
+import java.io.IOException;
+import java.io.Writer;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.Charset;
+import java.nio.charset.CoderResult;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+
+/**
+ * An output stream which decodes into a writer.
+ */
+public final class WriterOutputStream extends OutputStream {
+
+    private final Writer writer;
+    private final CharsetDecoder decoder;
+    private final ByteBuffer inputBuffer;
+    private final CharBuffer outputBuffer;
+
+    /**
+     * Construct a new instance using the default charset.
+     *
+     * @param writer the writer to write to
+     */
+    public WriterOutputStream(final Writer writer) {
+        this(writer, Charset.defaultCharset());
+    }
+
+    /**
+     * Construct a new instance using the named charset.
+     *
+     * @param writer the writer to write to
+     * @param charsetName the charset name
+     */
+    public WriterOutputStream(final Writer writer, final String charsetName) {
+        this(writer, Charset.forName(charsetName));
+    }
+
+    /**
+     * Construct a new instance using the given charset.
+     *
+     * @param writer the writer to write to
+     * @param charset the charset
+     */
+    public WriterOutputStream(final Writer writer, final Charset charset) {
+        this(writer, charset.newDecoder());
+    }
+
+    /**
+     * Construct a new instance using the given charset decoder.
+     *
+     * @param writer the writer to write to
+     * @param decoder the charset decoder
+     */
+    public WriterOutputStream(final Writer writer, final CharsetDecoder decoder) {
+        this.writer = writer;
+        this.decoder = decoder;
+        inputBuffer = ByteBuffer.allocate(256);
+        outputBuffer = CharBuffer.allocate(256);
+    }
+
+    /** {@inheritDoc} */
+    public void write(final int b) throws IOException {
+        synchronized (decoder) {
+            final ByteBuffer inputBuffer = this.inputBuffer;
+            inputBuffer.put((byte) b);
+            if (! inputBuffer.hasRemaining()) {
+                flush();
+            }
+        }
+    }
+
+    /** {@inheritDoc} */
+    public void write(final byte[] b, int off, int len) throws IOException {
+        synchronized (decoder) {
+            for (;;) {
+                final ByteBuffer inputBuffer = this.inputBuffer;
+                int cnt = Math.min(inputBuffer.remaining(), len);
+                inputBuffer.put(b, off, cnt);
+                len -= cnt;
+                off += cnt;
+                if (len == 0) {
+                    return;
+                }
+                flush();
+            }
+        }
+    }
+
+    /** {@inheritDoc} */
+    public void flush() throws IOException {
+        synchronized (decoder) {
+            final CharBuffer outputBuffer = this.outputBuffer;
+            final ByteBuffer inputBuffer = this.inputBuffer;
+            inputBuffer.flip();
+            for (;;) {
+                final CoderResult coderResult = decoder.decode(inputBuffer, outputBuffer, false);
+                if (coderResult.isOverflow()) {
+                    outputBuffer.flip();
+                    boolean ok = false;
+                    try {
+                        writer.write(outputBuffer.array(), outputBuffer.arrayOffset(), outputBuffer.remaining());
+                        ok = true;
+                    } finally {
+                        if (! ok) {
+                            inputBuffer.clear();
+                        }
+                        outputBuffer.clear();
+                    }
+                } else if (coderResult.isUnderflow()) {
+                    inputBuffer.compact();
+                    return;
+                }
+            }
+        }
+    }
+
+    /** {@inheritDoc} */
+    public void close() throws IOException {
+        flush();
+    }
+}




More information about the jboss-svn-commits mailing list