001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin;
005:
006: import com.tc.admin.common.XTextPane;
007: import com.tc.management.beans.L2MBeanNames;
008:
009: import java.io.PrintWriter;
010: import java.io.StringWriter;
011:
012: import javax.management.Notification;
013: import javax.management.NotificationListener;
014: import javax.management.ObjectName;
015: import javax.swing.Icon;
016: import javax.swing.text.AttributeSet;
017: import javax.swing.text.BadLocationException;
018: import javax.swing.text.SimpleAttributeSet;
019: import javax.swing.text.StyleConstants;
020: import javax.swing.text.StyledDocument;
021:
022: public class ServerLog extends XTextPane {
023: private ConnectionContext m_cc;
024: private ObjectName m_logger;
025: private LogListener m_logListener;
026: private Icon m_errorIcon;
027: private Icon m_warnIcon;
028: private Icon m_infoIcon;
029: private Icon m_blankIcon;
030: private SimpleAttributeSet m_errorIconAttrSet;
031: private SimpleAttributeSet m_warnIconAttrSet;
032: private SimpleAttributeSet m_infoIconAttrSet;
033: private SimpleAttributeSet m_blankIconAttrSet;
034:
035: private static final String LOG_ERROR = AdminClient.getContext()
036: .getMessage("log.error");
037:
038: private static final String LOG_WARN = AdminClient.getContext()
039: .getMessage("log.warn");
040:
041: private static final String LOG_INFO = AdminClient.getContext()
042: .getMessage("log.info");
043:
044: private static final int DEFAULT_MAX_LOG_LINES = 1000;
045:
046: private static int MAX_LOG_LINES = Integer.getInteger(
047: "com.tc.admin.ServerLog.maxLines", DEFAULT_MAX_LOG_LINES)
048: .intValue();
049:
050: public ServerLog(ConnectionContext cc) {
051: super ();
052:
053: m_cc = cc;
054: m_logListener = new LogListener();
055: m_errorIcon = LogHelper.getHelper().getErrorIcon();
056: m_warnIcon = LogHelper.getHelper().getWarningIcon();
057: m_infoIcon = LogHelper.getHelper().getInfoIcon();
058: m_blankIcon = LogHelper.getHelper().getBlankIcon();
059:
060: m_errorIconAttrSet = new SimpleAttributeSet();
061: StyleConstants.setIcon(m_errorIconAttrSet, m_errorIcon);
062:
063: m_warnIconAttrSet = new SimpleAttributeSet();
064: StyleConstants.setIcon(m_warnIconAttrSet, m_warnIcon);
065:
066: m_infoIconAttrSet = new SimpleAttributeSet();
067: StyleConstants.setIcon(m_infoIconAttrSet, m_infoIcon);
068:
069: m_blankIconAttrSet = new SimpleAttributeSet();
070: StyleConstants.setIcon(m_blankIconAttrSet, m_blankIcon);
071:
072: try {
073: m_logger = m_cc.queryName(L2MBeanNames.LOGGER
074: .getCanonicalName());
075: m_cc.addNotificationListener(m_logger, m_logListener);
076: } catch (Exception e) {
077: AdminClient.getContext().log(e);
078: }
079:
080: setEditable(false);
081: }
082:
083: public ConnectionContext getConnectionContext() {
084: return m_cc;
085: }
086:
087: class LogListener implements NotificationListener {
088: public void handleNotification(Notification notice,
089: Object handback) {
090: log(notice.getMessage());
091: }
092: }
093:
094: public void log(Exception e) {
095: StringWriter sw = new StringWriter();
096: PrintWriter pw = new PrintWriter(sw);
097:
098: e.printStackTrace(pw);
099: pw.close();
100:
101: log(sw.toString());
102: }
103:
104: public void append(String s) {
105: StyledDocument doc = (StyledDocument) getDocument();
106:
107: try {
108: int length = doc.getLength();
109: AttributeSet iconAttrSet = m_blankIconAttrSet;
110:
111: if (s.indexOf(LOG_ERROR) != -1) {
112: iconAttrSet = m_errorIconAttrSet;
113: } else if (s.indexOf(LOG_WARN) != -1) {
114: iconAttrSet = m_warnIconAttrSet;
115: } else if (s.indexOf(LOG_INFO) != -1) {
116: iconAttrSet = m_infoIconAttrSet;
117: }
118:
119: appendToLog(doc, length, " ", iconAttrSet);
120: length++;
121:
122: appendToLog(doc, length, s, null);
123: } catch (BadLocationException e) {/**/
124: }
125: }
126:
127: private void appendToLog(StyledDocument doc, int offset, String s,
128: AttributeSet attrSet) throws BadLocationException {
129: doc.insertString(offset, s, attrSet);
130:
131: if (MAX_LOG_LINES > 0) {
132: int lineCount;
133: int length = doc.getLength();
134:
135: s = doc.getText(0, length);
136:
137: if ((lineCount = lineCount(s)) > MAX_LOG_LINES) {
138: int lines = lineCount - MAX_LOG_LINES;
139:
140: offset = 0;
141:
142: for (int i = 0; i < lines; i++) {
143: offset = s.indexOf(NEWLINE, offset);
144: offset++;
145: }
146:
147: doc.remove(0, offset);
148: }
149: }
150: }
151:
152: private static final char NEWLINE = '\n';
153:
154: private static int lineCount(String s) {
155: int result = 0;
156: int offset = 0;
157:
158: if (s != null && s.length() > 0) {
159: while ((offset = s.indexOf(NEWLINE, offset)) != -1) {
160: result++;
161: offset++;
162: }
163: }
164:
165: return result;
166: }
167:
168: public void log(String s) {
169: append(s);
170: setCaretPosition(getDocument().getLength() - 1);
171: }
172: }
|