KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > ReTraceRunnable


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.gui;
22
23 import java.awt.Cursor JavaDoc;
24 import java.io.*;
25
26 import javax.swing.*;
27
28 import proguard.retrace.ReTrace;
29
30
31 /**
32  * This <code>Runnable</code> runs ReTrace, sending console output to a text
33  * area and any exceptions to message dialogs.
34  *
35  * @see ReTrace
36  * @author Eric Lafortune
37  */

38 class ReTraceRunnable implements Runnable JavaDoc
39 {
40     private JTextArea consoleTextArea;
41     private boolean verbose;
42     private File mappingFile;
43     private String JavaDoc stackTrace;
44
45
46     /**
47      * Creates a new ProGuardRunnable object.
48      * @param consoleTextArea the text area to send the console output to.
49      * @param verbose specifies whether the de-obfuscated stack trace
50      * should be verbose.
51      * @param mappingFile the mapping file that was written out by ProGuard.
52      */

53     public ReTraceRunnable(JTextArea consoleTextArea,
54                            boolean verbose,
55                            File mappingFile,
56                            String JavaDoc stackTrace)
57     {
58         this.consoleTextArea = consoleTextArea;
59         this.verbose = verbose;
60         this.mappingFile = mappingFile;
61         this.stackTrace = stackTrace;
62     }
63
64
65     // Implementation for Runnable.
66

67     public void run()
68     {
69         consoleTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
70         consoleTextArea.setText("");
71
72         // Redirect the stack trace string to the System's in stream, and the
73
// out and err streams to the console text area.
74
InputStream oldIn = System.in;
75         PrintStream oldOut = System.out;
76         PrintStream oldErr = System.err;
77
78         ByteArrayInputStream inputStream =
79            new ByteArrayInputStream(stackTrace.getBytes());
80
81         PrintStream printStream =
82             new PrintStream(new TextAreaOutputStream(consoleTextArea), true);
83
84         System.setIn(inputStream);
85         System.setOut(printStream);
86         System.setErr(printStream);
87
88         try
89         {
90             // Create a new ProGuard object with the GUI's configuration.
91
ReTrace reTrace = new ReTrace(verbose,
92                                           mappingFile);
93
94             // Run it.
95
reTrace.execute();
96         }
97         catch (Exception JavaDoc ex)
98         {
99             // Print out the exception message.
100
System.out.println(ex.getMessage());
101
102             // Show a dialog as well.
103
MessageDialogRunnable.showMessageDialog(consoleTextArea,
104                                                     ex.getMessage(),
105                                                     msg("errorReTracing"),
106                                                     JOptionPane.ERROR_MESSAGE);
107         }
108         catch (OutOfMemoryError JavaDoc er)
109         {
110             // Forget about the ProGuard object as quickly as possible.
111
System.gc();
112
113             // Print out a message suggesting what to do next.
114
System.out.println(msg("outOfMemory"));
115
116             // Show a dialog as well.
117
MessageDialogRunnable.showMessageDialog(consoleTextArea,
118                                                     msg("outOfMemory"),
119                                                     msg("errorReTracing"),
120                                                     JOptionPane.ERROR_MESSAGE);
121         }
122
123         // Make sure all output has been sent to the console text area.
124
printStream.flush();
125
126         // Restore the old System's in, out, and err streams.
127
System.setIn(oldIn);
128         System.setOut(oldOut);
129         System.setErr(oldErr);
130
131         consoleTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
132         consoleTextArea.setCaretPosition(0);
133
134         // Reset the global static redirection lock.
135
ProGuardGUI.systemOutRedirected = false;
136     }
137
138
139     // Small utility methods.
140

141     /**
142      * Returns the message from the GUI resources that corresponds to the given
143      * key.
144      */

145     private String JavaDoc msg(String JavaDoc messageKey)
146     {
147          return GUIResources.getMessage(messageKey);
148     }
149 }
150
Popular Tags