📄 stafjvmlogviewer.java
字号:
fJVMLogDirName = "{STAF/DataDir}{STAF/Config/Sep/File}lang" + "{STAF/Config/Sep/File}java{STAF/Config/Sep/File}jvm" + "{STAF/Config/Sep/File}" + fJVMName + "{STAF/Config/Sep/File}"; // Resolve fJVMLogDirName value so that it looks better when displayed // (e.g. in the frame title, etc) res = fHandle.submit2( machine, "VAR", "RESOLVE STRING " + fJVMLogDirName); if (res.rc == 0) fJVMLogDirName = res.result; fJVMLogFileName = fJVMLogDirName + fJVMLogName; String frameTitle = "JVM Log for Service " + serviceName + " - STAF " + machine + " FS GET FILE " + fJVMLogFileName; // Determine the number of JVM Logs that exist for the JVMName String request = "LIST DIRECTORY " + STAFUtil.wrapData(fJVMLogDirName) + " NAME JVMLog SORTBYNAME"; res = fHandle.submit2(machine, "FS", request); if (res.rc != 0) { String errorMsg = "Error submitting request: STAF " + fMachine + " " + request + "\nRC: " + res.rc + "\nResult: " + res.result; JOptionPane.showMessageDialog( parent, errorMsg, "Error Determining the Number of JVM Logs", JOptionPane.INFORMATION_MESSAGE); if (! fSystemExit) return; else System.exit(0); } // Unmarshall the result to get a list of the JVM Logs and determine // the size of that list (which is the number of JVM Logs for this JVM) try { STAFMarshallingContext mc = STAFMarshallingContext.unmarshall( res.result); java.util.List jvmLogList = (java.util.List)mc.getRootObject(); fMaxJVMLogs = jvmLogList.size(); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog( parent, "Error determining the number of JVM Logs that " + "exist\nfor service " + serviceName + " on machine " + machine, "Error Determining the number of JVM Logs Available", JOptionPane.INFORMATION_MESSAGE); if (! fSystemExit) return; else System.exit(0); } // Create the Log Frame and populate it with data from the JVM Log fLogFrame = new STAFLogFrame(frameTitle); Vector logLines = refreshTable(); if (logLines == null) { if (! fSystemExit) return; else System.exit(0); } if (logLines.size() == 0) { JOptionPane.showMessageDialog( parent, "Log + has no entries\n\n" + fLogFrame.getTitle(), "No Log Entries", JOptionPane.INFORMATION_MESSAGE); if (! fSystemExit) return; else System.exit(0); } fLogFrame.setSize(800, 400); fLogFrame.show(); String osName = System.getProperties().getProperty("os.name"); if (osName.equals("Windows 2000")) { fLogFrame.setState(JFrame.ICONIFIED); fLogFrame.setState(JFrame.NORMAL); } else { fLogFrame.toFront(); } } public Vector getLogLines() { STAFResult stafResult = fHandle.submit2( fMachine, "FS", "GET FILE " + STAFUtil.wrapData(fJVMLogFileName)); if (stafResult.rc != 0) { String errorMsg = "Error submitting request: STAF " + fMachine + " FS GET FILE " + fJVMLogFileName + "\nRC: " + stafResult.rc + "\nResult: " + stafResult.result; JOptionPane.showMessageDialog( parent, errorMsg, "Error getting JVMLog", JOptionPane.INFORMATION_MESSAGE); return null; } String logContents = stafResult.result; // If displayAll == false, search for the start of the last log within // the JVMLog by searching for the last occurrence of: String beginNewLogString = "***************************************************************" + "****************\n" + "*** nnnnnnnn-nn:nn:nn"; // - Start of Log for JVMName: xxxx String startOfLogString = " - Start of Log for JVMName: "; if (!fDisplayAll) { // Only show the last log contained in the JVMLog file int index = logContents.lastIndexOf(startOfLogString); if (index - beginNewLogString.length() >= 0) { index = index - beginNewLogString.length(); logContents = logContents.substring(index); } } Vector logLines = new Vector(); Vector thisLogData = new Vector(); thisLogData.add(logContents); logLines.add(thisLogData); return logLines; } public void showErrorDialog(Component parent, String message) { showErrorDialog(parent, message, "STAFJVMLogViwer Error"); } public void showErrorDialog(Component parent, String message, String title) { JTextPane messagePane = new JTextPane(); messagePane.setFont(new Font("Dialog", Font.BOLD, 12)); messagePane.setEditable(false); messagePane.setText(message); messagePane.select(0,0); JScrollPane scrollPane = new JScrollPane(messagePane); // Calculate the height for the scrollPane based on the number // of characters in the message int minHeight = 180; // Minimum height for scrollPane int maxHeight = 650; // Maximum height for scrollPane int avgCharsPerLine = 40; // Avg characters per line int lineHeight = 16; // Approximate height of a line int msgLength = message.length(); int numLines = 0; int height = minHeight; if (msgLength > 0) { numLines = msgLength / avgCharsPerLine; if (numLines > 0) height = numLines * lineHeight; } if (height > maxHeight) height = maxHeight; else if (height < minHeight) height = minHeight; scrollPane.setPreferredSize(new Dimension(510, height)); JOptionPane.showMessageDialog(parent, scrollPane, title, JOptionPane.ERROR_MESSAGE); } public Vector refreshTable() { Vector logLines = getLogLines(); if (logLines == null) return null; fLogTable.setRowHeight(30); fLogTable.setModel(new STAFTableModel(logLines, columnNames)); updateLogTableRenderers(); updateRowHeights(fLogTable, 0, fFontName); sizeColumnsToFitText(fLogTable); // Scroll to bottom fLogVerticalScrollBar.setValue(fLogVerticalScrollBar.getMaximum()); return logLines; } public void updateLogTableRenderers() { fLogTable.getColumnModel().getColumn(0).setCellRenderer( new STAFLogTableCellRenderer(false)); } public static void updateRowHeights(JTable table, int multiLineColumn, String fontName) { int numLines = 1; for (int i = 0 ; i < table.getRowCount() ; i++) { JTextArea textarea = new JTextArea( (String)table.getValueAt(i, multiLineColumn)); textarea.setFont(new Font(fontName, Font.PLAIN, 12)); int height = textarea.getPreferredSize().height + 5; table.setRowHeight(i, height); } } public static void sizeColumnsToFitText(JTable table) { int tableWidth = 0; FontMetrics metrics = table.getFontMetrics(table.getFont()); for (int i = 0; i < table.getColumnCount(); i++) { int width = 0; int maxWidth = 0; Vector data = new Vector(); data.addElement(table.getColumnModel().getColumn(i). getHeaderValue()); for (int j = 0; j < table.getRowCount(); j++) { try { Object obj = table.getValueAt(j,i); String cellText = ""; if (obj != null) { cellText = table.getValueAt(j,i).toString(); } BufferedReader reader = new BufferedReader(new StringReader(cellText)); String line; try { while ((line = reader.readLine()) != null) { data.addElement(line); } } catch(IOException ex) { ex.printStackTrace(); } finally { try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } catch(Exception ex) { ex.printStackTrace(); } } Enumeration e = data.elements(); while (e.hasMoreElements()) { width = metrics.stringWidth((String)e.nextElement()); if (width > maxWidth) maxWidth = width; } Insets insets = ((JComponent)table.getCellRenderer(0,i)). getInsets(); // Need to pad a little extra for everything to look right maxWidth += insets.left + insets.right + (maxWidth*.15); table.getColumnModel().getColumn(i).setPreferredWidth(maxWidth); tableWidth += maxWidth; } Dimension d = table.getSize(); d.width = tableWidth; table.setSize(d); } public class STAFLogFrame extends JFrame implements ActionListener { public STAFLogFrame(String title) { super(title); JMenuBar mainMenuBar = new JMenuBar(); setJMenuBar(mainMenuBar); fFileMenu = new JMenu("File"); mainMenuBar.add(fFileMenu); fViewMenu = new JMenu("View"); mainMenuBar.add(fViewMenu); fFileExit = new JMenuItem("Exit"); fFileExit.addActionListener(this); fFileMenu.add(fFileExit); fViewRefresh = new JMenuItem("Refresh"); fViewRefresh.addActionListener(this); fViewMenu.add(fViewRefresh); fViewMenu.insertSeparator(1); fViewShowCurrent = new JMenuItem("Show Current"); fViewShowCurrent.addActionListener(this); fViewMenu.add(fViewShowCurrent); fViewShowAll = new JMenuItem("Show All"); fViewShowAll.addActionListener(this); fViewMenu.add(fViewShowAll); fViewMenu.insertSeparator(4); fViewSelectJVMLogName = new JMenuItem( "Select JVM Log Name..."); fViewSelectJVMLogName.addActionListener(this); fViewMenu.add(fViewSelectJVMLogName); fViewMenu.insertSeparator(6); fViewChangeFont = new JMenuItem("Change Font..."); fViewChangeFont.addActionListener(this); fViewMenu.add(fViewChangeFont); // Set up Change JVM Log Name panels fSelectJVMLogNameDialog = new JDialog( this, "Select JVM Log Name", true); JPanel selectJVMLogNamePanel = new JPanel(); selectJVMLogNamePanel.setLayout(new BorderLayout()); String selectLogNameTitle = "Select JVM Log Name in Directory " + fJVMLogDirName + " "; selectJVMLogNamePanel.setBorder(new TitledBorder( selectLogNameTitle)); // Calculate the width for the "Select Log Name" panel based on // the title length which varies based on the length of the // JVM Log Directory name FontMetrics metrics = selectJVMLogNamePanel.getFontMetrics( selectJVMLogNamePanel.getFont()); int panelWidth = metrics.stringWidth(selectLogNameTitle); panelWidth += (int)(panelWidth * .15); // Need to pad a little
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -