1 4 package com.tc.admin; 5 6 import com.tc.admin.common.XTextPane; 7 import com.tc.management.beans.L2MBeanNames; 8 9 import java.io.PrintWriter ; 10 import java.io.StringWriter ; 11 12 import javax.management.Notification ; 13 import javax.management.NotificationListener ; 14 import javax.management.ObjectName ; 15 import javax.swing.Icon ; 16 import javax.swing.text.AttributeSet ; 17 import javax.swing.text.BadLocationException ; 18 import javax.swing.text.SimpleAttributeSet ; 19 import javax.swing.text.StyleConstants ; 20 import javax.swing.text.StyledDocument ; 21 22 public class ServerLog extends XTextPane { 23 private ConnectionContext m_cc; 24 private ObjectName m_logger; 25 private LogListener m_logListener; 26 private Icon m_errorIcon; 27 private Icon m_warnIcon; 28 private Icon m_infoIcon; 29 private Icon m_blankIcon; 30 private SimpleAttributeSet m_errorIconAttrSet; 31 private SimpleAttributeSet m_warnIconAttrSet; 32 private SimpleAttributeSet m_infoIconAttrSet; 33 private SimpleAttributeSet m_blankIconAttrSet; 34 35 private static final String LOG_ERROR = AdminClient.getContext().getMessage("log.error"); 36 37 private static final String LOG_WARN = AdminClient.getContext().getMessage("log.warn"); 38 39 private static final String LOG_INFO = AdminClient.getContext().getMessage("log.info"); 40 41 private static final int DEFAULT_MAX_LOG_LINES = 1000; 42 43 private static int MAX_LOG_LINES = Integer.getInteger("com.tc.admin.ServerLog.maxLines", 44 DEFAULT_MAX_LOG_LINES).intValue(); 45 46 public ServerLog(ConnectionContext cc) { 47 super(); 48 49 m_cc = cc; 50 m_logListener = new LogListener(); 51 m_errorIcon = LogHelper.getHelper().getErrorIcon(); 52 m_warnIcon = LogHelper.getHelper().getWarningIcon(); 53 m_infoIcon = LogHelper.getHelper().getInfoIcon(); 54 m_blankIcon = LogHelper.getHelper().getBlankIcon(); 55 56 m_errorIconAttrSet = new SimpleAttributeSet (); 57 StyleConstants.setIcon(m_errorIconAttrSet, m_errorIcon); 58 59 m_warnIconAttrSet = new SimpleAttributeSet (); 60 StyleConstants.setIcon(m_warnIconAttrSet, m_warnIcon); 61 62 m_infoIconAttrSet = new SimpleAttributeSet (); 63 StyleConstants.setIcon(m_infoIconAttrSet, m_infoIcon); 64 65 m_blankIconAttrSet = new SimpleAttributeSet (); 66 StyleConstants.setIcon(m_blankIconAttrSet, m_blankIcon); 67 68 try { 69 m_logger = m_cc.queryName(L2MBeanNames.LOGGER.getCanonicalName()); 70 m_cc.addNotificationListener(m_logger, m_logListener); 71 } catch (Exception e) { 72 AdminClient.getContext().log(e); 73 } 74 75 setEditable(false); 76 } 77 78 public ConnectionContext getConnectionContext() { 79 return m_cc; 80 } 81 82 class LogListener implements NotificationListener { 83 public void handleNotification(Notification notice, Object handback) { 84 log(notice.getMessage()); 85 } 86 } 87 88 public void log(Exception e) { 89 StringWriter sw = new StringWriter (); 90 PrintWriter pw = new PrintWriter (sw); 91 92 e.printStackTrace(pw); 93 pw.close(); 94 95 log(sw.toString()); 96 } 97 98 public void append(String s) { 99 StyledDocument doc = (StyledDocument ) getDocument(); 100 101 try { 102 int length = doc.getLength(); 103 AttributeSet iconAttrSet = m_blankIconAttrSet; 104 105 if (s.indexOf(LOG_ERROR) != -1) { 106 iconAttrSet = m_errorIconAttrSet; 107 } else if (s.indexOf(LOG_WARN) != -1) { 108 iconAttrSet = m_warnIconAttrSet; 109 } else if (s.indexOf(LOG_INFO) != -1) { 110 iconAttrSet = m_infoIconAttrSet; 111 } 112 113 appendToLog(doc, length, " ", iconAttrSet); 114 length++; 115 116 appendToLog(doc, length, s, null); 117 } catch (BadLocationException e) { 118 } 119 } 120 121 private void appendToLog(StyledDocument doc, int offset, String s, AttributeSet attrSet) throws BadLocationException { 122 doc.insertString(offset, s, attrSet); 123 124 if (MAX_LOG_LINES > 0) { 125 int lineCount; 126 int length = doc.getLength(); 127 128 s = doc.getText(0, length); 129 130 if ((lineCount = lineCount(s)) > MAX_LOG_LINES) { 131 int lines = lineCount - MAX_LOG_LINES; 132 133 offset = 0; 134 135 for (int i = 0; i < lines; i++) { 136 offset = s.indexOf(NEWLINE, offset); 137 offset++; 138 } 139 140 doc.remove(0, offset); 141 } 142 } 143 } 144 145 private static final char NEWLINE = '\n'; 146 147 private static int lineCount(String s) { 148 int result = 0; 149 int offset = 0; 150 151 if (s != null && s.length() > 0) { 152 while ((offset = s.indexOf(NEWLINE, offset)) != -1) { 153 result++; 154 offset++; 155 } 156 } 157 158 return result; 159 } 160 161 public void log(String s) { 162 append(s); 163 setCaretPosition(getDocument().getLength() - 1); 164 } 165 } 166 | Popular Tags |