KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > ServerLog


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
10 import java.io.StringWriter JavaDoc;
11
12 import javax.management.Notification JavaDoc;
13 import javax.management.NotificationListener JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.swing.Icon JavaDoc;
16 import javax.swing.text.AttributeSet JavaDoc;
17 import javax.swing.text.BadLocationException JavaDoc;
18 import javax.swing.text.SimpleAttributeSet JavaDoc;
19 import javax.swing.text.StyleConstants JavaDoc;
20 import javax.swing.text.StyledDocument JavaDoc;
21
22 public class ServerLog extends XTextPane {
23   private ConnectionContext m_cc;
24   private ObjectName JavaDoc m_logger;
25   private LogListener m_logListener;
26   private Icon JavaDoc m_errorIcon;
27   private Icon JavaDoc m_warnIcon;
28   private Icon JavaDoc m_infoIcon;
29   private Icon JavaDoc m_blankIcon;
30   private SimpleAttributeSet JavaDoc m_errorIconAttrSet;
31   private SimpleAttributeSet JavaDoc m_warnIconAttrSet;
32   private SimpleAttributeSet JavaDoc m_infoIconAttrSet;
33   private SimpleAttributeSet JavaDoc m_blankIconAttrSet;
34
35   private static final String JavaDoc LOG_ERROR = AdminClient.getContext().getMessage("log.error");
36
37   private static final String JavaDoc LOG_WARN = AdminClient.getContext().getMessage("log.warn");
38
39   private static final String JavaDoc 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 JavaDoc();
57     StyleConstants.setIcon(m_errorIconAttrSet, m_errorIcon);
58
59     m_warnIconAttrSet = new SimpleAttributeSet JavaDoc();
60     StyleConstants.setIcon(m_warnIconAttrSet, m_warnIcon);
61
62     m_infoIconAttrSet = new SimpleAttributeSet JavaDoc();
63     StyleConstants.setIcon(m_infoIconAttrSet, m_infoIcon);
64
65     m_blankIconAttrSet = new SimpleAttributeSet JavaDoc();
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 JavaDoc 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 JavaDoc {
83     public void handleNotification(Notification JavaDoc notice, Object JavaDoc handback) {
84       log(notice.getMessage());
85     }
86   }
87
88   public void log(Exception JavaDoc e) {
89     StringWriter JavaDoc sw = new StringWriter JavaDoc();
90     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
91
92     e.printStackTrace(pw);
93     pw.close();
94
95     log(sw.toString());
96   }
97
98   public void append(String JavaDoc s) {
99     StyledDocument JavaDoc doc = (StyledDocument JavaDoc) getDocument();
100
101     try {
102       int length = doc.getLength();
103       AttributeSet JavaDoc 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 JavaDoc e) {/**/
118     }
119   }
120
121   private void appendToLog(StyledDocument JavaDoc doc, int offset, String JavaDoc s, AttributeSet JavaDoc attrSet) throws BadLocationException JavaDoc {
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 JavaDoc 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 JavaDoc s) {
162     append(s);
163     setCaretPosition(getDocument().getLength() - 1);
164   }
165 }
166
Popular Tags