View Javadoc

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;
20  import java.io.FileReader;
21  import java.io.IOException;
22  
23  import java.util.Enumeration;
24  import java.util.HashMap;
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 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 
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();
86          
87          while ( loggerEnum.hasMoreElements() )
88          {
89              Logger appLogger = (Logger) loggerEnum.nextElement();
90              Enumeration appenderEnum = appLogger.getAllAppenders();
91              String 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 fileName = data.getParameters().getString("selectedfile");
131             logger.debug("selectedfile: " + fileName);
132             if (fileName != null)
133             {
134                 String content = readFile(fileName);
135                 context.put("logfile", content);
136             }
137             else
138             {
139                 context.put("logfile", null);
140             }
141         }
142         catch (Exception 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 readFile (String filename) 
154     {
155         StringBuffer buf = new StringBuffer("");
156         try 
157         {
158             String line;
159             BufferedReader in = new BufferedReader(new FileReader(filename));
160             while ((line = in.readLine()) != null) 
161             {
162                 buf.append(line + "\n");
163             }
164             in.close();
165         } 
166         catch (IOException ioe) 
167         {
168             logger.error("Error reading file " + filename, ioe);
169         }
170         return buf.toString();
171     } // readFile
172     
173 } // class LogfileViewerAction