如何将 javax.mail.Session setDebug 重定向到 jTextArea
How to redirect javax.mail.Session setDebug to jTextArea
如何将 javax.mail.Session setDebug 重定向到 jTextArea?
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
Session mailSession = Session.getDefaultInstance(props, null);
logger.info("JAVAMAIL debug mode is ON");
mailSession.setDebug(true);
mailSession.setDebugOut(ps);
logger.info(os);
- 您可以创建或找到 swing hander implementation 并将其附加到 JavaMail 使用的
javax.mail
记录器命名空间。
- 您可以使用线程和 piped I/O 读取调试输出并将其写入文本区域。
您可以创建在 'autoflush' 期间写入文本区域的缓冲输出流。
public class AreaDebug {
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
test();
}
});
}
private static void test() {
assert EventQueue.isDispatchThread() : Thread.currentThread();
final Session s = Session.getInstance(new Properties());
JTextArea area = new JTextArea();
Adaptor out = new Adaptor(area.getDocument());
s.setDebugOut(new PrintStream(out, true)); //Default encoding?
s.setDebug(true);
System.out.println(area.getText());
}
private static class Adaptor extends ByteArrayOutputStream {
private final Document d;
Adaptor(final Document d) {
this.d = d;
}
@Override
public void flush() throws IOException {
final String a;
synchronized (this) {
super.flush();
a = super.toString(); //Default encoding?
super.reset();
}
Updater u = new Updater(d, a);
if (EventQueue.isDispatchThread()) {
u.run();
} else {
try {
EventQueue.invokeAndWait(u);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new InterruptedIOException();
} catch (InvocationTargetException ex) {
throw new IOException(ex);
}
}
}
}
private static class Updater implements Runnable {
private final Document d;
private final String append;
Updater(Document d, String append) {
this.d = d;
this.append = append;
}
@Override
public void run() {
try {
d.insertString(d.getLength(), append, (AttributeSet) null);
} catch (BadLocationException ex) {
Toolkit.getDefaultToolkit().beep();
}
}
}
}
您可以使用 CustomOutputStream
class http://www.codejava.net/java-se/swing/redirect-standard-output-streams-to-jtextarea 中描述的漂亮 CustomOutputStream
:
import java.io.*;
import java.util.Properties;
import javax.mail.Session;
import javax.swing.*;
public class PrintStream2TextArea {
public static void main(final String[] arguments) {
new PrintStream2TextArea().launchGui();
}
private void launchGui() {
final JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JTextArea textArea = new JTextArea(42, 28);
setupMailSession(new Properties(), textArea);
frame.getContentPane().add(textArea);
frame.setVisible(true);
}
private void setupMailSession(final Properties props, final JTextArea textArea) {
PrintStream ps = new PrintStream(new CustomOutputStream(textArea));
Session mailSession = Session.getDefaultInstance(props, null);
//logger.info("JAVAMAIL debug mode is ON");
mailSession.setDebug(true);
mailSession.setDebugOut(ps);
//logger.info(os);
}
/**
* This class extends from OutputStream to redirect output to a JTextArea.
*
* @author www.codejava.net
*/
public static class CustomOutputStream extends OutputStream {
private JTextArea textArea;
public CustomOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
textArea.append(String.valueOf((char)b));
// scrolls the text area to the end of data
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
}
如何将 javax.mail.Session setDebug 重定向到 jTextArea?
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
Session mailSession = Session.getDefaultInstance(props, null);
logger.info("JAVAMAIL debug mode is ON");
mailSession.setDebug(true);
mailSession.setDebugOut(ps);
logger.info(os);
- 您可以创建或找到 swing hander implementation 并将其附加到 JavaMail 使用的
javax.mail
记录器命名空间。 - 您可以使用线程和 piped I/O 读取调试输出并将其写入文本区域。
您可以创建在 'autoflush' 期间写入文本区域的缓冲输出流。
public class AreaDebug { public static void main(String[] args) throws Exception { EventQueue.invokeAndWait(new Runnable() { @Override public void run() { test(); } }); } private static void test() { assert EventQueue.isDispatchThread() : Thread.currentThread(); final Session s = Session.getInstance(new Properties()); JTextArea area = new JTextArea(); Adaptor out = new Adaptor(area.getDocument()); s.setDebugOut(new PrintStream(out, true)); //Default encoding? s.setDebug(true); System.out.println(area.getText()); } private static class Adaptor extends ByteArrayOutputStream { private final Document d; Adaptor(final Document d) { this.d = d; } @Override public void flush() throws IOException { final String a; synchronized (this) { super.flush(); a = super.toString(); //Default encoding? super.reset(); } Updater u = new Updater(d, a); if (EventQueue.isDispatchThread()) { u.run(); } else { try { EventQueue.invokeAndWait(u); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new InterruptedIOException(); } catch (InvocationTargetException ex) { throw new IOException(ex); } } } } private static class Updater implements Runnable { private final Document d; private final String append; Updater(Document d, String append) { this.d = d; this.append = append; } @Override public void run() { try { d.insertString(d.getLength(), append, (AttributeSet) null); } catch (BadLocationException ex) { Toolkit.getDefaultToolkit().beep(); } } } }
您可以使用 CustomOutputStream
class http://www.codejava.net/java-se/swing/redirect-standard-output-streams-to-jtextarea 中描述的漂亮 CustomOutputStream
:
import java.io.*;
import java.util.Properties;
import javax.mail.Session;
import javax.swing.*;
public class PrintStream2TextArea {
public static void main(final String[] arguments) {
new PrintStream2TextArea().launchGui();
}
private void launchGui() {
final JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JTextArea textArea = new JTextArea(42, 28);
setupMailSession(new Properties(), textArea);
frame.getContentPane().add(textArea);
frame.setVisible(true);
}
private void setupMailSession(final Properties props, final JTextArea textArea) {
PrintStream ps = new PrintStream(new CustomOutputStream(textArea));
Session mailSession = Session.getDefaultInstance(props, null);
//logger.info("JAVAMAIL debug mode is ON");
mailSession.setDebug(true);
mailSession.setDebugOut(ps);
//logger.info(os);
}
/**
* This class extends from OutputStream to redirect output to a JTextArea.
*
* @author www.codejava.net
*/
public static class CustomOutputStream extends OutputStream {
private JTextArea textArea;
public CustomOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
textArea.append(String.valueOf((char)b));
// scrolls the text area to the end of data
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
}