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 */ 017 018 package org.apache.geronimo.console.logmanager; 019 020 import java.io.IOException; 021 import java.io.Serializable; 022 import java.io.File; 023 024 import javax.portlet.PortletConfig; 025 import javax.portlet.PortletContext; 026 import javax.portlet.PortletException; 027 import javax.portlet.PortletRequestDispatcher; 028 import javax.portlet.RenderRequest; 029 import javax.portlet.RenderResponse; 030 import javax.portlet.WindowState; 031 import javax.portlet.PortletSession; 032 033 import org.apache.geronimo.console.BasePortlet; 034 import org.apache.geronimo.console.util.PortletManager; 035 import org.apache.geronimo.system.logging.SystemLog; 036 037 public class LogViewerPortlet extends BasePortlet { 038 private final static String CRITERIA_KEY = "org.apache.geronimo.console.log.CRITERIA"; 039 040 protected PortletRequestDispatcher searchView; 041 042 protected PortletRequestDispatcher helpView; 043 044 protected void doHelp(RenderRequest renderRequest, 045 RenderResponse renderRespose) throws PortletException, IOException { 046 helpView.include(renderRequest, renderRespose); 047 } 048 049 protected void doView(RenderRequest renderRequest, 050 RenderResponse renderRespose) throws PortletException, IOException { 051 if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) { 052 return; 053 } 054 String action = renderRequest.getParameter("action"); 055 056 SystemLog log = PortletManager.getCurrentSystemLog(renderRequest); 057 String[] logFiles = log.getLogFileNames(); 058 LogFile[] files = new LogFile[logFiles.length]; 059 for (int i = 0; i < files.length; i++) { 060 files[i] = new LogFile(logFiles[i]); 061 } 062 Criteria criteria = (Criteria) renderRequest.getPortletSession(true).getAttribute(CRITERIA_KEY, PortletSession.PORTLET_SCOPE); 063 064 if (criteria == null || (action != null && !"refresh".equals(action))) { 065 if(criteria == null) 066 criteria = new Criteria(); 067 String startPos = renderRequest.getParameter("startPos"); 068 String endPos = renderRequest.getParameter("endPos"); 069 String maxRows = renderRequest.getParameter("maxRows"); 070 String logLevel = renderRequest.getParameter("logLevel"); 071 String searchString = renderRequest.getParameter("searchString"); 072 String stackTraces = renderRequest.getParameter("stackTraces"); 073 String logFile = renderRequest.getParameter("logFile"); 074 if(logFile == null || logFile.equals("")) { 075 logFile = logFiles[0]; 076 } 077 078 criteria.level = logLevel == null || logLevel.equals("") ? criteria.level : logLevel; 079 try{ 080 criteria.max = maxRows == null || maxRows.equals("") ? criteria.max : Integer.parseInt(maxRows); 081 }catch(NumberFormatException e){ 082 //ignore 083 } 084 try{ 085 criteria.start = startPos == null || startPos.equals("") ? null : new Integer(startPos); 086 }catch(NumberFormatException e){ 087 //ignore 088 } 089 try{ 090 criteria.stop = endPos == null || endPos.equals("") ? null : new Integer(endPos); 091 }catch(NumberFormatException e){ 092 //ignore 093 } 094 criteria.logFile = logFile; 095 criteria.stackTraces = stackTraces != null && !stackTraces.equals(""); 096 097 criteria.text = searchString == null || searchString.equals("") ? null : searchString; 098 renderRequest.getPortletSession(true).setAttribute(CRITERIA_KEY, criteria, PortletSession.PORTLET_SCOPE); 099 } 100 101 SystemLog.SearchResults results = log.getMatchingItems(criteria.logFile, criteria.start, criteria.stop, 102 criteria.level, criteria.text, criteria.max, criteria.stackTraces); 103 renderRequest.setAttribute("searchResults", results.getResults()); 104 renderRequest.setAttribute("lineCount", new Integer(results.getLineCount())); 105 renderRequest.setAttribute("startPos", criteria.start); 106 renderRequest.setAttribute("endPos", criteria.stop); 107 renderRequest.setAttribute("logLevel", criteria.level); 108 renderRequest.setAttribute("searchString", criteria.text); 109 renderRequest.setAttribute("maxRows", Integer.toString(criteria.max)); 110 renderRequest.setAttribute("logFile", criteria.logFile); 111 renderRequest.setAttribute("logFiles", files); 112 if(criteria.stackTraces) { 113 renderRequest.setAttribute("stackTraces", Boolean.TRUE); 114 } 115 if(results.isCapped()) { 116 renderRequest.setAttribute("capped", Boolean.TRUE); 117 } 118 119 searchView.include(renderRequest, renderRespose); 120 } 121 122 public void init(PortletConfig portletConfig) throws PortletException { 123 PortletContext pc = portletConfig.getPortletContext(); 124 searchView = pc 125 .getRequestDispatcher("/WEB-INF/view/logmanager/search.jsp"); 126 helpView = pc 127 .getRequestDispatcher("/WEB-INF/view/logmanager/viewhelp.jsp"); 128 super.init(portletConfig); 129 } 130 131 private static class Criteria implements Serializable { 132 int max = 10; 133 Integer start; 134 Integer stop; 135 String text; 136 String level = "WARN"; 137 String logFile; 138 boolean stackTraces; 139 } 140 141 public static class LogFile { 142 private String fullName; 143 private String name; 144 145 public LogFile(String fullName) { 146 this.fullName = fullName; 147 //todo: what if portla JVM has different separator than server JVM? 148 int pos = fullName.lastIndexOf(File.separatorChar); 149 if(pos > -1) { 150 name = fullName.substring(pos+1); 151 } else { 152 name = fullName; 153 } 154 } 155 156 public String getFullName() { 157 return fullName; 158 } 159 160 public String getName() { 161 return name; 162 } 163 } 164 }