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.File;
021    import java.io.IOException;
022    import java.io.Serializable;
023    import java.util.Enumeration;
024    import java.util.Map;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletConfig;
029    import javax.portlet.PortletContext;
030    import javax.portlet.PortletException;
031    import javax.portlet.PortletRequestDispatcher;
032    import javax.portlet.PortletSession;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    import javax.portlet.WindowState;
036    
037    import org.apache.geronimo.console.BasePortlet;
038    import org.apache.geronimo.console.util.PortletManager;
039    import org.apache.geronimo.system.logging.SystemLog;
040    
041    /**
042     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
043     */
044    public class LogViewerPortlet extends BasePortlet {
045        private final static String CRITERIA_KEY = "org.apache.geronimo.console.log.CRITERIA";
046    
047        protected PortletRequestDispatcher searchView;
048    
049        protected PortletRequestDispatcher helpView;
050    
051        protected void doHelp(RenderRequest renderRequest,
052                RenderResponse renderRespose) throws PortletException, IOException {
053            helpView.include(renderRequest, renderRespose);
054        }
055    
056        @Override
057        public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {
058            
059            //Add all the parameters to the actionResponse Attributes so we can get the back
060            actionResponse.setRenderParameters(actionRequest.getParameterMap());
061         
062        }
063        
064        protected void doView(RenderRequest renderRequest,
065                RenderResponse renderRespose) throws PortletException, IOException {
066            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
067                return;
068            }
069            String action = renderRequest.getParameter("action");
070    
071            SystemLog log = PortletManager.getCurrentSystemLog(renderRequest);
072            String[] logFiles = log.getLogFileNames();
073            LogFile[] files = new LogFile[logFiles.length];
074            for (int i = 0; i < files.length; i++) {
075                files[i] = new LogFile(logFiles[i]);
076            }
077            Criteria criteria = (Criteria) renderRequest.getPortletSession(true).getAttribute(CRITERIA_KEY, PortletSession.PORTLET_SCOPE);
078            
079            if(criteria != null) {
080                // Check if criteria.logFile is in the logFileNames of current logging configuration
081                boolean found = false;
082                for(String logFile: logFiles) {
083                    if(criteria.logFile.equals(new File(logFile))) {
084                        found = true;
085                        break;
086                    }
087                }
088                if(!found) {
089                    // This arises when log4j properties file is changed dynamically using LogManagerPortlet
090                    // and the earlier log file is no longer in the current logging configuration.
091                    // Change the log file to any one in the current logging configuration so that LogViewer
092                    // won't run into errors.
093                    criteria.logFile = logFiles[0];
094                }
095            }
096            if (criteria == null || (action != null && !"refresh".equals(action))) {
097                if(criteria == null)
098                    criteria = new Criteria();
099                String startPos = renderRequest.getParameter("startPos");
100                String endPos = renderRequest.getParameter("endPos");
101                String maxRows = renderRequest.getParameter("maxRows");
102                String logLevel = renderRequest.getParameter("logLevel");
103                String searchString = renderRequest.getParameter("searchString");
104                String stackTraces = renderRequest.getParameter("stackTraces");
105                String logFile = renderRequest.getParameter("logFile");
106                if(logFile == null || logFile.equals("")) {
107                    logFile = logFiles[0];
108                }
109                
110                criteria.level = logLevel == null || logLevel.equals("") ? criteria.level : logLevel;
111                try{
112                    criteria.max = maxRows == null || maxRows.equals("") ? criteria.max : Integer.parseInt(maxRows);
113                }catch(NumberFormatException e){
114                    //ignore
115                }
116                try{
117                    criteria.start = startPos == null || startPos.equals("") ? null : new Integer(startPos);
118                }catch(NumberFormatException e){
119                //ignore
120                }
121                try{
122                    criteria.stop = endPos == null || endPos.equals("") ? null : new Integer(endPos);
123                }catch(NumberFormatException e){
124                    //ignore
125                    }
126                criteria.logFile = logFile;
127                criteria.stackTraces = stackTraces != null && !stackTraces.equals("");
128                
129                criteria.text = searchString == null || searchString.equals("") ? null : searchString;
130                renderRequest.getPortletSession(true).setAttribute(CRITERIA_KEY, criteria, PortletSession.PORTLET_SCOPE);
131            }
132    
133            SystemLog.SearchResults results = log.getMatchingItems(criteria.logFile, criteria.start, criteria.stop,
134                            criteria.level, criteria.text, criteria.max, criteria.stackTraces);
135            renderRequest.setAttribute("searchResults", results.getResults());
136            renderRequest.setAttribute("lineCount", new Integer(results.getLineCount()));
137            renderRequest.setAttribute("startPos", criteria.start);
138            renderRequest.setAttribute("endPos", criteria.stop);
139            renderRequest.setAttribute("logLevel", criteria.level);
140            renderRequest.setAttribute("searchString", criteria.text);
141            renderRequest.setAttribute("maxRows", Integer.toString(criteria.max));
142            renderRequest.setAttribute("logFile", criteria.logFile);
143            renderRequest.setAttribute("logFiles", files);
144            if(criteria.stackTraces) {
145                renderRequest.setAttribute("stackTraces", Boolean.TRUE);
146            }
147            if(results.isCapped()) {
148                renderRequest.setAttribute("capped", Boolean.TRUE);
149            }
150    
151            searchView.include(renderRequest, renderRespose);
152        }
153    
154        public void init(PortletConfig portletConfig) throws PortletException {
155            PortletContext pc = portletConfig.getPortletContext();
156            searchView = pc
157                    .getRequestDispatcher("/WEB-INF/view/logmanager/search.jsp");
158            helpView = pc
159                    .getRequestDispatcher("/WEB-INF/view/logmanager/viewhelp.jsp");
160            super.init(portletConfig);
161        }
162    
163        private static class Criteria implements Serializable {
164            int max = 10;
165            Integer start;
166            Integer stop;
167            String text;
168            String level = "WARN";
169            String logFile;
170            boolean stackTraces;
171        }
172    
173        public static class LogFile {
174            private String fullName;
175            private String name;
176    
177            public LogFile(String fullName) {
178                this.fullName = fullName;
179                this.name = (new File(fullName)).getName();
180            }
181    
182            public String getFullName() {
183                return fullName;
184            }
185    
186            public String getName() {
187                return name;
188            }
189        }
190    }