1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package hello;
  27 
  28 import jdk.packager.services.UserJvmOptionsService;
  29 
  30 import java.awt.BorderLayout;
  31 import java.awt.Dimension;
  32 import java.awt.Toolkit;
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.lang.management.ManagementFactory;
  36 import java.lang.management.RuntimeMXBean;
  37 import java.util.List;
  38 import java.util.Map;
  39 import javax.swing.*;
  40 import javax.swing.table.DefaultTableModel;
  41 import javax.swing.event.TableModelEvent;
  42 import javax.swing.event.TableModelListener;
  43 import javax.swing.JOptionPane;
  44 
  45 public class TestPackager {
  46 
  47     private static String[] args;
  48 
  49     private static void createAndShowGUI() {
  50         //Create and set up the window.
  51         UserJvmOptionsService ujo = UserJvmOptionsService.getUserJVMDefaults();
  52         Map<String, String> userOptions = ujo.getUserJVMOptions();
  53 
  54         for (Map.Entry <String, String> entry : userOptions.entrySet()) {
  55             System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
  56         }
  57         if (!userOptions.containsKey("-DfirstRunMs=")) {
  58             userOptions.put("-DfirstRunMs=", Long.toString(System.currentTimeMillis()));
  59         }
  60         userOptions.put("-DlastRunMs=", Long.toString(System.currentTimeMillis()));
  61         ujo.setUserJVMOptions(userOptions);
  62 
  63         JFrame frame = new JFrame("Display Parameters");
  64         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  65 
  66         Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  67         frame.setBounds(0, 0, dim.width / 4, dim.height / 4);
  68 
  69 
  70         long v = Runtime.getRuntime().maxMemory();
  71         Long value = v / 1048576;
  72         long t = Runtime.getRuntime().totalMemory();
  73         Long total = t / 1048576;
  74 
  75         String canonicalPath;
  76         try {
  77             canonicalPath = new File(".").getCanonicalPath();
  78         } catch (IOException ioe) {
  79             canonicalPath = ioe.getMessage();
  80         }
  81         JLabel label = new JLabel("<html>Memory Max: " + value.toString() + " MiB"
  82                                   + "<br>Memory Current: " + total.toString() + " MiB"
  83                                  + "<br>Current Working Dir: " + canonicalPath + "");
  84 
  85         RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
  86         List<String> arguments = RuntimemxBean.getInputArguments();
  87 
  88         JList<String> list = new JList<>(arguments.toArray(new String[arguments.size()]));
  89         list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  90         list.setLayoutOrientation(JList.VERTICAL);
  91         JScrollPane listScroller = new JScrollPane(list);
  92         listScroller.setPreferredSize(new Dimension(400, 200));
  93 
  94         DefaultTableModel model = new DefaultTableModel();
  95         model.addColumn("Key");
  96         model.addColumn("Effective");
  97         model.addColumn("Default");
  98         model.addTableModelListener(new TableModelListener() {
  99           public void tableChanged(TableModelEvent e) {
 100              System.out.println(e);
 101 
 102              switch (e.getType()) {
 103                case TableModelEvent.UPDATE:
 104                  int column = e.getColumn();
 105                  int row = e.getFirstRow();
 106 
 107                  if (column == 1) {
 108                    String key = model.getValueAt(row, 0).toString();
 109                    String value = model.getValueAt(row, column).toString();
 110                    JOptionPane.showMessageDialog(null, key + "=" + value + " column=" + String.valueOf(column) + " row=" + String.valueOf(row), "Changed", JOptionPane.INFORMATION_MESSAGE);
 111                    UserJvmOptionsService ujo = UserJvmOptionsService.getUserJVMDefaults();
 112                    Map<String, String> userOptions = ujo.getUserJVMOptions();
 113                    userOptions.put(key, value);
 114                    ujo.setUserJVMOptions(userOptions);
 115                  }
 116                  break;
 117              }
 118           }
 119         });
 120 
 121         Map<String, String> defaults = ujo.getUserJVMOptionDefaults();
 122         for (Map.Entry <String, String> entry : userOptions.entrySet()) {
 123             String def = defaults.get(entry.getKey());
 124             model.addRow(new Object[] {entry.getKey(), entry.getValue(), def == null ? "<no default>" : def});
 125         }
 126         JTable prefs = new JTable(model);
 127         JScrollPane prefsScroller = new JScrollPane(prefs);
 128         prefsScroller.setPreferredSize(new Dimension(400, 100));
 129 
 130         JList<String> argList = new JList<>(args);
 131         argList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
 132         argList.setLayoutOrientation(JList.VERTICAL);
 133         JScrollPane argListScroller = new JScrollPane(argList);
 134         argListScroller.setPreferredSize(new Dimension(400, 100));
 135 
 136 
 137         Box box = Box.createVerticalBox();
 138 
 139         box.add(new JLabel("JVM Arguments (user, options, and properties"));
 140         box.add(Box.createVerticalStrut(5));
 141         box.add(listScroller);
 142 
 143         box.add(Box.createVerticalStrut(10));
 144         box.add(new JLabel("User JVM Options, as set and with defaults"));
 145         box.add(Box.createVerticalStrut(5));
 146         box.add(prefsScroller);
 147 
 148         box.add(Box.createVerticalStrut(10));
 149         box.add(new JLabel("Command Line Arguments"));
 150         box.add(Box.createVerticalStrut(5));
 151         box.add(argListScroller);
 152 
 153         JPanel panel = new JPanel();
 154         panel.setLayout(new BorderLayout());
 155         panel.setBorder(BorderFactory.createEmptyBorder(10, 30, 30, 30));
 156         label.setBorder(BorderFactory.createEmptyBorder(0, 0, 20, 0));
 157         panel.add(label, BorderLayout.NORTH);
 158 
 159         DefaultTableModel propsModel = new DefaultTableModel();
 160         propsModel.addColumn("Key");
 161         propsModel.addColumn("Value");
 162 
 163         for (Map.Entry <Object, Object> entry : System.getProperties().entrySet()) {
 164             propsModel.addRow(new Object[] {entry.getKey(), entry.getValue()});
 165         }
 166         JTable props = new JTable(propsModel);
 167         props.setAutoCreateRowSorter(true);
 168         JScrollPane propsScroller = new JScrollPane(props);
 169         propsScroller.setPreferredSize(new Dimension(400, 100));
 170 
 171 
 172         JTabbedPane tabbedPane = new JTabbedPane();
 173         tabbedPane.addTab("Launch", box);
 174         tabbedPane.addTab("System", propsScroller);
 175 
 176         panel.add(tabbedPane, BorderLayout.CENTER);
 177         panel.setSize(panel.getPreferredSize());
 178 
 179         frame.getContentPane().add(panel);
 180         frame.setLocationRelativeTo(null);
 181         frame.setSize(frame.getPreferredSize());
 182 
 183         //Display the window.
 184         frame.setVisible(true);
 185         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 186     }
 187 
 188     public static void main(String[] args) {
 189         TestPackager.args = args;
 190         //Schedule a job for the event-dispatching thread:
 191         //creating and showing this application's GUI.
 192         try {
 193             javax.swing.SwingUtilities.invokeAndWait(TestPackager::createAndShowGUI);
 194         }
 195         catch (Exception e) {
 196             e.printStackTrace();
 197         }
 198     }
 199 }