KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > LogfileViewerAction


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 at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * 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 and
14  * limitations under the License.
15  */

16 package org.apache.jetspeed.modules.actions.portlets;
17
18 // Java classes
19
import java.io.BufferedReader JavaDoc;
20 import java.io.FileReader JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 // Log4J classes
27
import org.apache.log4j.Logger;
28 import org.apache.log4j.LogManager;
29 import org.apache.log4j.Appender;
30 import org.apache.log4j.FileAppender;
31 import org.apache.log4j.spi.LoggerRepository;
32
33 // Turbine classes
34
import org.apache.turbine.util.RunData;
35
36 // Velocity classes
37
import org.apache.velocity.context.Context;
38
39 // Jetspeed classes
40
import org.apache.jetspeed.portal.Portlet;
41
42 /**
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  */

57 public class LogfileViewerAction extends GenericMVCAction
58 {
59     /**
60      * Static initialization of the logger for this class
61      */

62      private static final Logger logger = LogManager.getLogger(LogfileViewerAction.class.getName());
63
64      private static HashMap JavaDoc appenders = null;
65
66     /** Creates a new instance of LogFileViewerAction */
67     public LogfileViewerAction()
68     {
69     }
70     
71     /**
72      * Lists the current logfiles
73      * @param portlet The current portlet
74      * @param context the current portlet context
75      * @paran rundata the Turbine rundata
76      *
77      */

78     protected void buildNormalContext(Portlet portlet, Context context, RunData rundata) throws Exception JavaDoc
79     {
80         String JavaDoc tempName;
81         LoggerRepository repos = logger.getLoggerRepository();
82         Enumeration JavaDoc loggerEnum = repos.getCurrentLoggers();
83         HashMap JavaDoc files = new HashMap JavaDoc();
84         HashMap JavaDoc fileNames = new HashMap JavaDoc();
85         appenders = new HashMap JavaDoc();
86         
87         while ( loggerEnum.hasMoreElements() )
88         {
89             Logger appLogger = (Logger) loggerEnum.nextElement();
90             Enumeration JavaDoc appenderEnum = appLogger.getAllAppenders();
91             String JavaDoc name;
92
93             while ( appenderEnum.hasMoreElements() )
94             {
95                 Appender appender = (Appender) appenderEnum.nextElement();
96                 if (appender instanceof FileAppender)
97                 {
98                     name = appender.getName();
99                     tempName = ((FileAppender)appender).getFile();
100                     tempName = tempName.substring(tempName.lastIndexOf(System.getProperty("file.separator")) + 1);
101                     if (name == null)
102                     {
103                         name = tempName;
104                         appender.setName(name);
105                     }
106  
107                     if (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     }
119     
120     /**
121      * If a file is selected, it's contents is put in "logfile"
122      * @paran rundata the Turbine rundata
123      * @param context the current portlet context
124      *
125      */

126     public void doUpdate(RunData data, Context context)
127     {
128         try
129         {
130             String JavaDoc fileName = data.getParameters().getString("selectedfile");
131             logger.debug("selectedfile: " + fileName);
132             if (fileName != null)
133             {
134                 String JavaDoc content = readFile(fileName);
135                 context.put("logfile", content);
136             }
137             else
138             {
139                 context.put("logfile", null);
140             }
141         }
142         catch (Exception JavaDoc ex)
143         {
144             logger.error("Exception in viewing logfile: ", ex);
145         }
146     }
147
148     /**
149      * Reads the contents of a file and returns in \n separated lines.
150      * @paran filename Name of file to read
151      *
152      */

153     private String JavaDoc readFile (String JavaDoc filename)
154     {
155         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("");
156         try
157         {
158             String JavaDoc line;
159             BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(filename));
160             while ((line = in.readLine()) != null)
161             {
162                 buf.append(line + "\n");
163             }
164             in.close();
165         }
166         catch (IOException JavaDoc ioe)
167         {
168             logger.error("Error reading file " + filename, ioe);
169         }
170         return buf.toString();
171     } // readFile
172

173 } // class LogfileViewerAction
174
Popular Tags