1 16 17 package org.apache.jetspeed.services.portletstats; 18 19 import org.apache.turbine.util.RunData; 21 import org.apache.turbine.services.TurbineServices; 22 import org.apache.turbine.services.TurbineBaseService; 23 import org.apache.turbine.services.InitializationException; 24 import org.apache.turbine.services.resources.ResourceService; 25 26 import org.apache.jetspeed.portal.Portlet; 28 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 29 import org.apache.jetspeed.services.logging.JetspeedLogger; 30 31 import javax.servlet.ServletConfig ; 33 import javax.servlet.http.HttpServletRequest ; 34 35 import java.text.MessageFormat ; 37 import java.text.SimpleDateFormat ; 38 import java.util.Date ; 39 40 69 public class JetspeedPortletStatsService extends TurbineBaseService 70 implements PortletStatsService 71 { 72 75 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortletStatsService.class.getName()); 76 77 92 protected static final String defaultLogFormat = "{0} {1} {2} [{3}] \"{4} {5}/{6} {7}\" {8} {9}"; 93 94 98 private boolean enabled = false; 99 100 104 protected String dateFormat = null; 105 106 107 protected SimpleDateFormat formatter = null; 108 109 110 protected boolean logLoadTime = false; 111 112 116 public void init( ServletConfig conf ) throws InitializationException 117 { 118 119 ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance()) 120 .getResources(PortletStatsService.SERVICE_NAME); 121 122 this.enabled = serviceConf.getBoolean("enabled"); 123 this.dateFormat = serviceConf.getString("dateFormat", "dd/MM/yyyy:hh:mm:ss z"); 124 this.formatter = new SimpleDateFormat (this.dateFormat); 125 this.logLoadTime = serviceConf.getBoolean("logLoadTime", false); 126 127 setInit(true); 128 129 } 130 131 134 public boolean isEnabled() 135 { 136 return this.enabled; 137 } 138 139 142 public boolean setEnabled(boolean state) 143 { 144 boolean oldState = this.enabled; 145 this.enabled = state; 146 147 return oldState; 148 } 149 150 153 public void logAccess(RunData data, Portlet portlet, String statusCode, long time) 154 { 155 156 if (!this.isEnabled()) 157 { 158 return; 159 } 160 161 try 162 { 163 logger.info(this.getLogMessage(data, portlet, statusCode, time)); 164 } 165 catch (Exception e) 166 { 167 logger.error("Exception", e); 168 } 169 } 170 171 181 protected String getLogMessage(RunData data, Portlet portlet, String statusCode, long time) 182 throws Exception 183 { 184 185 HttpServletRequest req = data.getRequest(); 186 Object [] args = { 187 req.getRemoteAddr(), 188 "-", 189 data.getUser().getUserName(), 190 this.formatter.format(new Date ()), 191 req.getMethod(), 192 req.getContextPath(), 193 portlet.getName(), 194 req.getProtocol(), 195 statusCode, 196 this.logLoadTime == true ? String.valueOf(time) : "-" 197 }; 198 199 return MessageFormat.format(defaultLogFormat, args).toString(); 200 201 } 202 203 210 public void logAccess(RunData data, Portlet portlet, String statusCode) 211 { 212 logAccess(data, portlet, statusCode, 0); 213 } 214 215 } 216 217 | Popular Tags |