001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.admin;
007:
008: import java.net.URL;
009: import java.util.*;
010:
011: import java.io.*;
012: import javax.servlet.*;
013: import javax.servlet.http.*;
014:
015: import com.sun.portal.search.admin.CSViewBeanBase;
016: import com.sun.portal.search.util.SearchConfig;
017: import javax.servlet.http.HttpServletRequest;
018: import com.iplanet.jato.RequestContext;
019: import com.iplanet.jato.view.event.DisplayEvent;
020: import com.iplanet.jato.view.event.RequestInvocationEvent;
021:
022: import com.iplanet.jato.view.html.StaticTextField;
023: import com.iplanet.jato.view.html.TextField;
024: import com.iplanet.jato.view.html.CheckBox;
025: import com.iplanet.jato.view.html.Button;
026: import com.iplanet.jato.view.html.ComboBox;
027: import com.iplanet.jato.view.html.RadioButtonGroup;
028: import com.iplanet.jato.util.HtmlUtil;
029:
030: import com.iplanet.jato.view.html.Option;
031: import com.iplanet.jato.view.html.OptionList;
032:
033: import com.iplanet.jato.view.View;
034: import com.iplanet.jato.view.ViewBean;
035: import com.iplanet.jato.view.ViewBeanBase;
036:
037: import com.iplanet.jato.ViewBeanManager;
038: import com.iplanet.jato.model.ModelControlException;
039: import com.iplanet.am.console.components.view.html.IPlanetButton;
040:
041: /**
042: * iPS admin console view bean: TODO
043: */
044: public class LogsViewBean extends CSViewBeanBase {
045: public static final String DEFAULT_DISPLAY_URL = "/ps/searchadmin/Logs.jsp";
046: public static final String PAGE_NAME = "Logs";
047: //
048: // Logs
049: //
050: public static final String VLOG_COMBO = "VLogCombo";
051: public static final String NLINES_TEXT = "NLinesText";
052: public static final String LOG_RESULT = "LogResult";
053: public static final String LOG_BUTTON = "LOGButton";
054:
055: private static final int AVG_BYTES_PER_LINE = 256; // initial try average 256 bytes per line
056: private static String[] logFNs = { null, null, null, null, null,
057: null };
058: private static final String[] logIndex = { "0", "1", "2", "3", "4",
059: "5" };
060:
061: /**
062: * constructor
063: *
064: * @param PageName of this view bean
065: * @param displayURL default display URL
066: */
067: public LogsViewBean() {
068: super (PAGE_NAME);
069: setDefaultDisplayURL(DEFAULT_DISPLAY_URL);
070: registerChildren();
071: }
072:
073: /**
074: * register child component
075: */
076: protected void registerChildren() {
077: registerChild(VLOG_COMBO, ComboBox.class);
078: registerChild(NLINES_TEXT, TextField.class);
079: registerChild(LOG_RESULT, StaticTextField.class);
080: registerChild(LOG_BUTTON, IPlanetButton.class);
081: }
082:
083: /**
084: * create child component
085: *
086: * @param name of component
087: * @return child component
088: */
089: protected View createChild(String name) {
090: View Headerchild = super .createChild(name);
091: if (Headerchild != null)
092: return Headerchild;
093: if (name.equals(VLOG_COMBO)) {
094: ComboBox child = new ComboBox(this , VLOG_COMBO, "");
095: OptionList VLogOptions = new OptionList(
096: getLocalizedStringArray("log.viewer.loglistlabel",
097: ","), logIndex);
098: child.setOptions(VLogOptions);
099: return child;
100: }
101: if (name.equals(NLINES_TEXT)) {
102: return new TextField(this , NLINES_TEXT, "25");
103: }
104: if (name.equals(LOG_RESULT)) {
105: return new StaticTextField(this , LOG_RESULT,
106: getLocalizedString("log.viewer.defaultmsg"));
107: }
108: if (name.equals(LOG_BUTTON)) {
109: return new IPlanetButton(this , LOG_BUTTON, "");
110: }
111:
112: throw new IllegalArgumentException("Invalid child name ["
113: + name + "]");
114: }
115:
116: private void getLogFNs() throws Exception {
117: String logDir = CSConfig.getServerRoot() + File.separator
118: + "logs" + File.separator;
119: if (logFNs[0] == null) {
120: logFNs[0] = logDir + "filter.log";
121: }
122: if (logFNs[1] == null) {
123: logFNs[1] = SearchConfig.getValue(SearchConfig.RDMGR_LOGFN);
124: }
125: if (logFNs[2] == null) {
126: logFNs[2] = SearchConfig.getValue(SearchConfig.DEBUG_LOGFN);
127: }
128: if (logFNs[3] == null) {
129: logFNs[3] = logDir + "robot.log";
130: }
131: if (logFNs[4] == null) {
132: logFNs[4] = SearchConfig
133: .getValue(SearchConfig.SENGINE_LOGFN);
134: }
135: if (logFNs[5] == null) {
136: logFNs[5] = SearchConfig.getValue(SearchConfig.RDM_LOGFN);
137: }
138: }
139:
140: public void beginDisplay(DisplayEvent event) {
141: setPageEncoding();
142: setDisplayFieldValue(LOG_BUTTON,
143: getLocalizedString("log.viewer.buttontext"));
144: }
145:
146: public String getLastLines(String logFileName, int showLines)
147: throws Exception {
148: if (showLines <= 0) {
149: return null;
150: }
151: RandomAccessFile raf = new RandomAccessFile(logFileName, "r");
152: if (raf.length() == 0) {
153: return "";
154: }
155: int readSize = showLines * AVG_BYTES_PER_LINE;
156: byte[] buffer;
157: int startPosition = 0;
158: while (true) {
159: long tryPosition = raf.length() - readSize;
160: if (tryPosition < 0) {
161: tryPosition = 0;
162: readSize = (int) raf.length();
163: }
164: raf.seek(tryPosition);
165:
166: buffer = new byte[readSize];
167:
168: raf.readFully(buffer);
169: int line = -1;
170: int i;
171: for (i = readSize - 1; i >= 0 && line < showLines; i--) {
172: if (buffer[i] == '\n') {
173: /* test for \r\n */
174: if (i > 0 && buffer[i - 1] == '\r') {
175: i--;
176: }
177: line++;
178: }
179: }
180: if (line == showLines) {
181: startPosition = i + 2;
182: break;
183: }
184: if (tryPosition == 0) {
185: startPosition = 0;
186: break;
187: }
188: readSize += 10000; /* add 10 k more */
189: }
190:
191: return new String(buffer, startPosition, buffer.length
192: - startPosition, "UTF-8");
193:
194: }
195:
196: public void handleLOGButtonRequest(RequestInvocationEvent event) {
197: int logNdx = getDisplayFieldIntValue(VLOG_COMBO);
198: try {
199: getLogFNs();
200: } catch (Exception e) {
201: }
202:
203: String logFileName = logFNs[logNdx];
204: int showLines = 25;
205: try {
206: showLines = getDisplayFieldIntValue(NLINES_TEXT);
207: if (showLines <= 0) {
208: this .errorMessage = getLocalizedString("log.viewer.numberformatexception");
209: forwardTo();
210: return;
211: }
212: } catch (Exception e) {
213: this .errorMessage = getLocalizedString("log.viewer.numberformatexception");
214: forwardTo();
215: return;
216: }
217:
218: try {
219:
220: String lastLines = getLastLines(logFileName, showLines);
221:
222: if (lastLines != null) {
223: StringBuffer outBuf = new StringBuffer();
224: int lineCount = 0;
225: StringTokenizer st = new StringTokenizer(lastLines,
226: "\r\n");
227: while (st.hasMoreTokens()) {
228: String token = st.nextToken();
229: if (token.trim().length() > 0) {
230: outBuf.append("<Font color=gray>"
231: + (++lineCount) + " : </font>"
232: + HtmlUtil.escape(token) + "<br>\n");
233: }
234: }
235: if (lineCount > 0) {
236: setDisplayFieldValue(LOG_RESULT, outBuf);
237: } else {
238: setDisplayFieldValue(
239: LOG_RESULT,
240: "<font color=red>"
241: + getLocalizedString("log.viewer.emptylogmsg")
242: + "</font>");
243: }
244: }
245: } catch (Exception e) {
246: String[] args = { logFileName };
247: setDisplayFieldValue(LOG_RESULT, "<font color=red>"
248: + this .getLocalizedMessageFormat(
249: "log.viewer.noexistlogmsg", args)
250: + "</font>");
251: }
252:
253: forwardTo();
254: }
255:
256: }
|