From 8f59c1b0bf5fed4fce80ebcc0bc588e62dde706d Mon Sep 17 00:00:00 2001 From: Jiri Vanek Date: Tue, 5 Nov 2013 20:07:27 +0100 Subject: Java console resurrected and connected to new logging. --- plugin/icedteanp/java/sun/applet/JavaConsole.java | 371 --------------------- .../sun/applet/PluginAppletSecurityContext.java | 6 +- plugin/icedteanp/java/sun/applet/PluginMain.java | 12 +- .../java/sun/applet/PluginStreamHandler.java | 32 +- 4 files changed, 26 insertions(+), 395 deletions(-) delete mode 100644 plugin/icedteanp/java/sun/applet/JavaConsole.java (limited to 'plugin') diff --git a/plugin/icedteanp/java/sun/applet/JavaConsole.java b/plugin/icedteanp/java/sun/applet/JavaConsole.java deleted file mode 100644 index a7c7aab..0000000 --- a/plugin/icedteanp/java/sun/applet/JavaConsole.java +++ /dev/null @@ -1,371 +0,0 @@ -/* JavaConsole -- A java console for the plugin - Copyright (C) 2009 Red Hat - -This file is part of IcedTea. - -IcedTea is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -IcedTea 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 -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with IcedTea; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - -package sun.applet; - -import java.awt.Dimension; -import java.awt.Font; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JSplitPane; -import javax.swing.JTextArea; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import javax.swing.border.EmptyBorder; -import javax.swing.border.TitledBorder; - -import net.sourceforge.jnlp.config.DeploymentConfiguration; -import net.sourceforge.jnlp.runtime.JNLPRuntime; -import net.sourceforge.jnlp.util.ImageResources; -import net.sourceforge.jnlp.util.logging.OutputController; - -/** - * A simple Java console for IcedTeaPlugin - * - */ -public class JavaConsole { - - private boolean initialized = false; - - JFrame consoleWindow; - JTextArea stdErrText; - JTextArea stdOutText; - - /** - * Initialize the console - */ - public void initialize() { - - try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (Exception e) { - OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e); - } - - final String logDir = JNLPRuntime.getConfiguration().getProperty(DeploymentConfiguration.KEY_USER_LOG_DIR); - - consoleWindow = new JFrame("Java Console"); - consoleWindow.setIconImages(ImageResources.INSTANCE.getApplicationImages()); - - JPanel contentPanel = new JPanel(); - contentPanel.setLayout(new GridBagLayout()); - - GridBagConstraints c; - - Font monoSpace = new Font("Monospaced", Font.PLAIN, 12); - - /* std out */ - - stdOutText = new JTextArea(); - JScrollPane stdOutScrollPane = new JScrollPane(stdOutText); - stdOutScrollPane.setBorder(new TitledBorder( - new EmptyBorder(5, 5, 5, 5), "System.out")); - stdOutText.setEditable(false); - stdOutText.setFont(monoSpace); - - TextAreaUpdater stdOutUpdater = new TextAreaUpdater(new File(logDir, - PluginMain.PLUGIN_STDOUT_FILE), stdOutText); - stdOutUpdater.setName("IcedteaPlugin Console Thread(System.out)"); - - /* std err */ - - stdErrText = new JTextArea(); - JScrollPane stdErrScrollPane = new JScrollPane(stdErrText); - stdErrScrollPane.setBorder(new TitledBorder( - new EmptyBorder(5, 5, 5, 5), "System.err")); - stdErrText.setEditable(false); - stdErrText.setFont(monoSpace); - - TextAreaUpdater stdErrUpdater = new TextAreaUpdater(new File(logDir, - PluginMain.PLUGIN_STDERR_FILE), stdErrText); - stdErrUpdater.setName("IcedteaPlugin Console Thread(System.err)"); - - JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, - stdOutScrollPane, stdErrScrollPane); - - c = new GridBagConstraints(); - c.fill = GridBagConstraints.BOTH; - c.gridheight = 10; - c.weighty = 1; - - contentPanel.add(splitPane, c); - - /* buttons */ - - c = new GridBagConstraints(); - c.gridy = 10; - c.gridheight = 1; - c.weightx = 0.5; - c.weighty = 0; - - JPanel buttonPanel = new JPanel(); - contentPanel.add(buttonPanel, c); - - JButton gcButton = new JButton("Run GC"); - buttonPanel.add(gcButton); - gcButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - printMemoryInfo(); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "Performing Garbage Collection...."); - System.gc(); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "Done"); - printMemoryInfo(); - } - - }); - - JButton finalizersButton = new JButton("Run Finalizers"); - buttonPanel.add(finalizersButton); - finalizersButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - printMemoryInfo(); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "Running finalization...."); - Runtime.getRuntime().runFinalization(); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "Done"); - printMemoryInfo(); - } - }); - - JButton memoryButton = new JButton("Memory Info"); - buttonPanel.add(memoryButton); - memoryButton.addActionListener(new ActionListener() { - - public void actionPerformed(ActionEvent e) { - printMemoryInfo(); - } - - }); - - JButton systemPropertiesButton = new JButton("System Properties"); - buttonPanel.add(systemPropertiesButton); - systemPropertiesButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - printSystemProperties(); - } - - }); - - JButton classloadersButton = new JButton("Classloaders"); - buttonPanel.add(classloadersButton); - classloadersButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - printClassLoaders(); - } - - }); - - JButton threadListButton = new JButton("Thread List"); - buttonPanel.add(threadListButton); - threadListButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - printThreadInfo(); - } - - }); - - JButton closeButton = new JButton("Close"); - buttonPanel.add(closeButton); - closeButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - hideConsole(); - } - }); - } - }); - - stdOutUpdater.start(); - stdErrUpdater.start(); - - consoleWindow.add(contentPanel); - consoleWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - consoleWindow.pack(); - consoleWindow.setSize(new Dimension(900, 600)); - consoleWindow.setMinimumSize(new Dimension(900, 300)); - - initialized = true; - - splitPane.setDividerLocation(0.5); - splitPane.setResizeWeight(0.5); - } - - public void showConsole() { - - if (!initialized) { - initialize(); - } - - consoleWindow.setVisible(true); - } - - public void hideConsole() { - consoleWindow.setVisible(false); - } - - protected void printSystemProperties() { - - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " ----"); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "System Properties:"); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, ""); - Properties p = System.getProperties(); - Set keys = p.keySet(); - for (Object key : keys) { - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, key.toString() + ": " + p.get(key)); - } - - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " ----"); - } - - private void printClassLoaders() { - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " ----"); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "Available Classloaders: "); - Set loaders = PluginAppletSecurityContext.getLoaderInfo().keySet(); - for (String loader : loaders) { - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, loader + "\n" - + " codebase = " - + PluginAppletSecurityContext.getLoaderInfo().get(loader)); - } - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " ----"); - } - - private void printMemoryInfo() { - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " ----- "); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " Memory Info:"); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " Max Memory: " - + String.format("%1$10d", Runtime.getRuntime().maxMemory())); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " Total Memory: " - + String.format("%1$10d", Runtime.getRuntime().totalMemory())); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " Free Memory: " - + String.format("%1$10d", Runtime.getRuntime().freeMemory())); - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " ----"); - - } - - private void printThreadInfo() { - Map map = Thread.getAllStackTraces(); - Set keys = map.keySet(); - for (Thread key : keys) { - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "Thread " + key.getId() + ": " + key.getName()); - for (StackTraceElement element : map.get(key)) { - OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, " " + element); - } - - } - } - - public static void main(String[] args) { - - final JavaConsole console = new JavaConsole(); - - boolean toShowConsole = false; - - for (String arg : args) { - if ("--show-console".equals(arg)) { - toShowConsole = true; - } - } - - if (toShowConsole) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - console.showConsole(); - } - }); - } - - } - - /** - * This thread updates the text on a JTextArea based on the text in a file - */ - class TextAreaUpdater extends Thread { - - File fileToRead; - JTextArea outputTextArea; - - public TextAreaUpdater(File file, JTextArea textArea) { - fileToRead = file; - outputTextArea = textArea; - setDaemon(true); - } - - public void run() { - - try { - BufferedReader reader = new BufferedReader(new FileReader( - fileToRead)); - String line; - while (true) { - while ((line = reader.readLine()) != null) { - outputTextArea.insert(line + "\n", outputTextArea - .getDocument().getLength()); - outputTextArea.setCaretPosition(outputTextArea - .getText().length()); - } - Thread.sleep(1000); - } - - } catch (FileNotFoundException e) { - OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e); - } catch (IOException e) { - OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e); - } catch (InterruptedException e) { - OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e); - Thread.currentThread().interrupt(); - } - - } - - } - -} diff --git a/plugin/icedteanp/java/sun/applet/PluginAppletSecurityContext.java b/plugin/icedteanp/java/sun/applet/PluginAppletSecurityContext.java index fb343f3..2fc42d8 100644 --- a/plugin/icedteanp/java/sun/applet/PluginAppletSecurityContext.java +++ b/plugin/icedteanp/java/sun/applet/PluginAppletSecurityContext.java @@ -235,7 +235,7 @@ public class PluginAppletSecurityContext { private ClassLoader liveconnectLoader = ClassLoader.getSystemClassLoader(); int identifier = 0; - public static PluginStreamHandler streamhandler; + private static PluginStreamHandler streamhandler; long startTime = 0; @@ -321,6 +321,10 @@ public class PluginAppletSecurityContext { streamhandler = sh; } + public static PluginStreamHandler getStreamhandler() { + return streamhandler; + } + public static Map getLoaderInfo() { Hashtable map = new Hashtable(); diff --git a/plugin/icedteanp/java/sun/applet/PluginMain.java b/plugin/icedteanp/java/sun/applet/PluginMain.java index cef5313..e6ba080 100644 --- a/plugin/icedteanp/java/sun/applet/PluginMain.java +++ b/plugin/icedteanp/java/sun/applet/PluginMain.java @@ -75,6 +75,7 @@ import java.net.URL; import java.net.URLStreamHandler; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Map; import java.util.Properties; import sun.awt.AppContext; import sun.awt.SunToolkit; @@ -82,15 +83,13 @@ import sun.awt.SunToolkit; import net.sourceforge.jnlp.config.DeploymentConfiguration; import net.sourceforge.jnlp.runtime.JNLPRuntime; import net.sourceforge.jnlp.security.JNLPAuthenticator; +import net.sourceforge.jnlp.util.logging.JavaConsole; import net.sourceforge.jnlp.util.logging.OutputController; /** * The main entry point into PluginAppletViewer. */ public class PluginMain { - // the files where stdout/stderr are sent to - public static final String PLUGIN_STDERR_FILE = "java.stderr"; - public static final String PLUGIN_STDOUT_FILE = "java.stdout"; // This is used in init(). Getting rid of this is desirable but depends // on whether the property that uses it is necessary/standard. @@ -147,6 +146,13 @@ public class PluginMain { streamHandler.startProcessing(); setCookieHandler(streamHandler); + JavaConsole.getConsole().setClassLoaderInfoProvider(new JavaConsole.ClassLoaderInfoProvider() { + + @Override + public Map getLoaderInfo() { + return PluginAppletSecurityContext.getLoaderInfo(); + } + }); } catch (Exception e) { OutputController.getLogger().log(e); diff --git a/plugin/icedteanp/java/sun/applet/PluginStreamHandler.java b/plugin/icedteanp/java/sun/applet/PluginStreamHandler.java index fb23755..e21b243 100644 --- a/plugin/icedteanp/java/sun/applet/PluginStreamHandler.java +++ b/plugin/icedteanp/java/sun/applet/PluginStreamHandler.java @@ -44,11 +44,12 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.net.MalformedURLException; import java.nio.charset.Charset; import javax.swing.SwingUtilities; import net.sourceforge.jnlp.runtime.JNLPRuntime; +import net.sourceforge.jnlp.runtime.Translator; +import net.sourceforge.jnlp.util.logging.JavaConsole; import net.sourceforge.jnlp.util.logging.OutputController; public class PluginStreamHandler { @@ -58,7 +59,6 @@ public class PluginStreamHandler { private RequestQueue queue = new RequestQueue(); - private JavaConsole console = new JavaConsole(); private PluginMessageConsumer consumer; private volatile boolean shuttingDown = false; @@ -242,9 +242,17 @@ public class PluginStreamHandler { private void handlePluginMessage(String message) { if (message.equals("plugin showconsole")) { - showConsole(); + if (JavaConsole.isEnabled()){ + JavaConsole.getConsole().showConsoleLater(); + } else { + OutputController.getLogger().log(OutputController.Level.ERROR_ALL, Translator.R("DPJavaConsoleDisabledHint")); + } } else if (message.equals("plugin hideconsole")) { - hideConsole(); + if (JavaConsole.isEnabled()){ + JavaConsole.getConsole().hideConsoleLater(); + } else { + OutputController.getLogger().log(OutputController.Level.ERROR_ALL, Translator.R("DPJavaConsoleDisabledHint")); + } } else { // else this is something that was specifically requested finishCallRequest(message); @@ -374,20 +382,4 @@ public class PluginStreamHandler { return; } - - private void showConsole() { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - console.showConsole(); - } - }); - } - - private void hideConsole() { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - console.hideConsole(); - } - }); - } } -- cgit v1.2.3