001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.logmanager;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.util.Enumeration;
022: import java.util.Map;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletConfig;
027: import javax.portlet.PortletContext;
028: import javax.portlet.PortletException;
029: import javax.portlet.PortletRequestDispatcher;
030: import javax.portlet.PortletSession;
031: import javax.portlet.RenderRequest;
032: import javax.portlet.RenderResponse;
033: import javax.portlet.WindowState;
034:
035: import org.apache.geronimo.console.BasePortlet;
036: import org.apache.geronimo.console.util.PortletManager;
037: import org.apache.geronimo.system.logging.SystemLog;
038:
039: /**
040: * @version $Rev: 617213 $ $Date: 2008-01-31 12:20:33 -0800 (Thu, 31 Jan 2008) $
041: */
042: public class LogViewerPortlet extends BasePortlet {
043: private final static String CRITERIA_KEY = "org.apache.geronimo.console.log.CRITERIA";
044:
045: protected PortletRequestDispatcher searchView;
046:
047: protected PortletRequestDispatcher helpView;
048:
049: protected void doHelp(RenderRequest renderRequest,
050: RenderResponse renderRespose) throws PortletException,
051: IOException {
052: helpView.include(renderRequest, renderRespose);
053: }
054:
055: @Override
056: public void processAction(ActionRequest actionRequest,
057: ActionResponse actionResponse) throws PortletException,
058: IOException {
059:
060: //Add all the parameters to the actionResponse Attributes so we can get the back
061: actionResponse.setRenderParameters(actionRequest
062: .getParameterMap());
063:
064: }
065:
066: protected void doView(RenderRequest renderRequest,
067: RenderResponse renderRespose) throws PortletException,
068: IOException {
069: if (WindowState.MINIMIZED
070: .equals(renderRequest.getWindowState())) {
071: return;
072: }
073: String action = renderRequest.getParameter("action");
074:
075: SystemLog log = PortletManager
076: .getCurrentSystemLog(renderRequest);
077: String[] logFiles = log.getLogFileNames();
078: LogFile[] files = new LogFile[logFiles.length];
079: for (int i = 0; i < files.length; i++) {
080: files[i] = new LogFile(logFiles[i]);
081: }
082: Criteria criteria = (Criteria) renderRequest.getPortletSession(
083: true).getAttribute(CRITERIA_KEY,
084: PortletSession.PORTLET_SCOPE);
085:
086: if (criteria != null) {
087: // Check if criteria.logFile is in the logFileNames of current logging configuration
088: boolean found = false;
089: for (String logFile : logFiles) {
090: if (criteria.logFile.equals(new File(logFile))) {
091: found = true;
092: break;
093: }
094: }
095: if (!found) {
096: // This arises when log4j properties file is changed dynamically using LogManagerPortlet
097: // and the earlier log file is no longer in the current logging configuration.
098: // Change the log file to any one in the current logging configuration so that LogViewer
099: // won't run into errors.
100: criteria.logFile = logFiles[0];
101: }
102: }
103: if (criteria == null
104: || (action != null && !"refresh".equals(action))) {
105: if (criteria == null)
106: criteria = new Criteria();
107: String startPos = renderRequest.getParameter("startPos");
108: String endPos = renderRequest.getParameter("endPos");
109: String maxRows = renderRequest.getParameter("maxRows");
110: String logLevel = renderRequest.getParameter("logLevel");
111: String searchString = renderRequest
112: .getParameter("searchString");
113: String stackTraces = renderRequest
114: .getParameter("stackTraces");
115: String logFile = renderRequest.getParameter("logFile");
116: if (logFile == null || logFile.equals("")) {
117: logFile = logFiles[0];
118: }
119:
120: criteria.level = logLevel == null || logLevel.equals("") ? criteria.level
121: : logLevel;
122: try {
123: criteria.max = maxRows == null || maxRows.equals("") ? criteria.max
124: : Integer.parseInt(maxRows);
125: } catch (NumberFormatException e) {
126: //ignore
127: }
128: try {
129: criteria.start = startPos == null
130: || startPos.equals("") ? null : new Integer(
131: startPos);
132: } catch (NumberFormatException e) {
133: //ignore
134: }
135: try {
136: criteria.stop = endPos == null || endPos.equals("") ? null
137: : new Integer(endPos);
138: } catch (NumberFormatException e) {
139: //ignore
140: }
141: criteria.logFile = logFile;
142: criteria.stackTraces = stackTraces != null
143: && !stackTraces.equals("");
144:
145: criteria.text = searchString == null
146: || searchString.equals("") ? null : searchString;
147: renderRequest.getPortletSession(true).setAttribute(
148: CRITERIA_KEY, criteria,
149: PortletSession.PORTLET_SCOPE);
150: }
151:
152: SystemLog.SearchResults results = log.getMatchingItems(
153: criteria.logFile, criteria.start, criteria.stop,
154: criteria.level, criteria.text, criteria.max,
155: criteria.stackTraces);
156: renderRequest.setAttribute("searchResults", results
157: .getResults());
158: renderRequest.setAttribute("lineCount", new Integer(results
159: .getLineCount()));
160: renderRequest.setAttribute("startPos", criteria.start);
161: renderRequest.setAttribute("endPos", criteria.stop);
162: renderRequest.setAttribute("logLevel", criteria.level);
163: renderRequest.setAttribute("searchString", criteria.text);
164: renderRequest.setAttribute("maxRows", Integer
165: .toString(criteria.max));
166: renderRequest.setAttribute("logFile", criteria.logFile);
167: renderRequest.setAttribute("logFiles", files);
168: if (criteria.stackTraces) {
169: renderRequest.setAttribute("stackTraces", Boolean.TRUE);
170: }
171: if (results.isCapped()) {
172: renderRequest.setAttribute("capped", Boolean.TRUE);
173: }
174:
175: searchView.include(renderRequest, renderRespose);
176: }
177:
178: public void init(PortletConfig portletConfig)
179: throws PortletException {
180: PortletContext pc = portletConfig.getPortletContext();
181: searchView = pc
182: .getRequestDispatcher("/WEB-INF/view/logmanager/search.jsp");
183: helpView = pc
184: .getRequestDispatcher("/WEB-INF/view/logmanager/viewhelp.jsp");
185: super .init(portletConfig);
186: }
187:
188: private static class Criteria implements Serializable {
189: int max = 10;
190: Integer start;
191: Integer stop;
192: String text;
193: String level = "WARN";
194: String logFile;
195: boolean stackTraces;
196: }
197:
198: public static class LogFile {
199: private String fullName;
200: private String name;
201:
202: public LogFile(String fullName) {
203: this .fullName = fullName;
204: //todo: what if portla JVM has different separator than server JVM?
205: int pos = fullName.lastIndexOf(File.separatorChar);
206: if (pos > -1) {
207: name = fullName.substring(pos + 1);
208: } else {
209: name = fullName;
210: }
211: }
212:
213: public String getFullName() {
214: return fullName;
215: }
216:
217: public String getName() {
218: return name;
219: }
220: }
221: }
|