1 16 package org.apache.jetspeed.modules.actions.portlets; 17 18 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 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 import org.apache.turbine.util.RunData; 35 36 import org.apache.velocity.context.Context; 38 39 import org.apache.jetspeed.portal.Portlet; 41 42 57 public class LogfileViewerAction extends GenericMVCAction 58 { 59 62 private static final Logger logger = LogManager.getLogger(LogfileViewerAction.class.getName()); 63 64 private static HashMap appenders = null; 65 66 67 public LogfileViewerAction() 68 { 69 } 70 71 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 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 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 } 173 } | Popular Tags |