1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16packageorg.apache.jetspeed.modules.actions.portlets;
1718// Java classes19import java.io.BufferedReader;
20import java.io.FileReader;
21import java.io.IOException;
2223import java.util.Enumeration;
24import java.util.HashMap;
2526// Log4J classes27import org.apache.log4j.Logger;
28import org.apache.log4j.LogManager;
29import org.apache.log4j.Appender;
30import org.apache.log4j.FileAppender;
31import org.apache.log4j.spi.LoggerRepository;
3233// Turbine classes34import org.apache.turbine.util.RunData;
3536// Velocity classes37import org.apache.velocity.context.Context;
3839// Jetspeed classes40import org.apache.jetspeed.portal.Portlet;
4142/***43 * This class is the action class for a portlet that lets you view the Log4J 44 * logfiles defined in your Jetspeed installation.<br/>45 * the portlet iterates through the Log4J appender defined that are of type 46 * FileAppender or its subclasses, and lists the filenames in a listbox.<br/>47 *48 * The portlet puts the following in the context:<br/>49 * <code>appenders</code> - a HashMap with the appenders found<br/>50 * <code>files</code> - a HashMap with the filenames without path<br/>51 * <code>logfile</code> - the content of the file indicated by <code>selectedfile</code><br/>52 * 53 * 54 * @author <a href="mailto:harald@ommang.com">Harald Ommang</a>55 * @version $Id: LogfileViewerAction.java,v 1.3 2004/02/23 02:56:58 jford Exp $56 */57publicclassLogfileViewerActionextendsGenericMVCAction58 {
59/***60 * Static initialization of the logger for this class61 */62privatestaticfinal Logger logger = LogManager.getLogger(LogfileViewerAction.class.getName());
6364privatestatic HashMap appenders = null;
6566/*** Creates a new instance of LogFileViewerAction */67publicLogfileViewerAction()
68 {
69 }
7071/*** 72 * Lists the current logfiles73 * @param portlet The current portlet74 * @param context the current portlet context75 * @paran rundata the Turbine rundata76 *77 */78protectedvoid buildNormalContext(Portlet portlet, Context context, RunData rundata) throws Exception
79 {
80 String tempName;
81 LoggerRepository repos = logger.getLoggerRepository();
82 Enumeration loggerEnum = repos.getCurrentLoggers();
83 HashMap files = new HashMap();
84 HashMap fileNames = new HashMap();
85 appenders = new HashMap();
8687while ( loggerEnum.hasMoreElements() )
88 {
89 Logger appLogger = (Logger) loggerEnum.nextElement();
90 Enumeration appenderEnum = appLogger.getAllAppenders();
91 String name;
9293while ( appenderEnum.hasMoreElements() )
94 {
95 Appender appender = (Appender) appenderEnum.nextElement();
96if (appender instanceof FileAppender)
97 {
98 name = appender.getName();
99 tempName = ((FileAppender)appender).getFile();
100 tempName = tempName.substring(tempName.lastIndexOf(System.getProperty("file.separator")) + 1);
101if (name == null)
102 {
103 name = tempName;
104 appender.setName(name);
105 }
106107if (logger.isDebugEnabled())
108 {
109 logger.debug("AppenderName " + name);
110 }
111 appenders.put(name, appender);
112 files.put(name, tempName);
113 }
114 }
115 }
116 context.put("appenders", appenders.values());
117 context.put("files", files);
118 }
119120/*** 121 * If a file is selected, it's contents is put in "logfile"122 * @paran rundata the Turbine rundata123 * @param context the current portlet context124 *125 */126publicvoid doUpdate(RunData data, Context context)
127 {
128try129 {
130 String fileName = data.getParameters().getString("selectedfile");
131 logger.debug("selectedfile: " + fileName);
132if (fileName != null)
133 {
134 String content = readFile(fileName);
135 context.put("logfile", content);
136 }
137else138 {
139 context.put("logfile", null);
140 }
141 }
142catch (Exception ex)
143 {
144 logger.error("Exception in viewing logfile: ", ex);
145 }
146 }
147148/*** 149 * Reads the contents of a file and returns in \n separated lines.150 * @paran filename Name of file to read151 *152 */153private String readFile (String filename)
154 {
155 StringBuffer buf = new StringBuffer("");
156try157 {
158 String line;
159 BufferedReader in = new BufferedReader(new FileReader(filename));
160while ((line = in.readLine()) != null)
161 {
162 buf.append(line + "\n");
163 }
164 in.close();
165 }
166catch (IOException ioe)
167 {
168 logger.error("Error reading file " + filename, ioe);
169 }
170return buf.toString();
171 } // readFile172173 } // class LogfileViewerAction