1 package org.exoplatform.services.portal.log.impl; 2 3 import java.util.Date ; 4 import org.exoplatform.container.configuration.ConfigurationManager; 5 import org.exoplatform.container.configuration.ServiceConfiguration; 6 import org.exoplatform.container.monitor.SessionMonitor; 7 import org.exoplatform.container.monitor.SessionMonitorListener; 8 import org.exoplatform.container.monitor.SessionMonitorListenerStack; 9 import org.exoplatform.services.organization.OrganizationService; 10 import org.exoplatform.services.organization.User; 11 import org.exoplatform.services.portal.log.PortalLogService; 12 import org.exoplatform.services.portal.log.SessionLogData; 13 import org.exoplatform.services.task.BaseTask; 14 import org.exoplatform.services.task.TaskService; 15 20 public class SessionLogPersister extends SessionMonitorListener{ 21 private PortalLogService plService_ ; 22 private OrganizationService orgService_ ; 23 private TaskService tservice_ ; 24 private String saveRule_ ; 25 26 public SessionLogPersister(SessionMonitorListenerStack stack, 27 PortalLogService service, 28 OrganizationService orgService, 29 TaskService tservice, 30 ConfigurationManager confService) throws Exception { 31 super(stack) ; 32 plService_ = service ; 33 orgService_ = orgService ; 34 tservice_ = tservice ; 35 ServiceConfiguration sconf = confService.getServiceConfiguration(getClass()) ; 36 saveRule_ = sconf.getValueParam("save.rule").getValue() ; 37 } 38 39 public void onStop(SessionMonitor monitor) { 40 if(monitor.getRemoteUser() != null) { 41 tservice_.queueTask(new UpdateUserLastLoginTask(orgService_, monitor.getSessionOwner())) ; 42 } 43 if("error.session".equals(saveRule_) && monitor.getErrorCount() == 0) return ; 44 tservice_.queueTask(new SessionLogPersisterTask(monitor, plService_)) ; 45 } 46 47 static class SessionLogPersisterTask extends BaseTask { 48 SessionMonitor monitor_ ; 49 PortalLogService service_ ; 50 51 SessionLogPersisterTask(SessionMonitor monitor, PortalLogService service) { 52 monitor_ = monitor ; 53 service_ = service ; 54 } 55 56 public void execute() throws Exception { 57 SessionLogData logData = new SessionLogDataImpl() ; 58 logData.setSessionOwner(monitor_.getSessionOwner()) ; 59 logData.setRemoteUser(monitor_.getRemoteUser()) ; 60 logData.setClientName(monitor_.getClientInfo().getClientName()) ; 61 logData.setIPAddress(monitor_.getIPAddress()) ; 62 logData.setAccessTime(new Date (monitor_.getAccessTime())) ; 63 logData.setDuration((int)monitor_.getLiveTimeInSecond()) ; 64 logData.setActionHistory(monitor_.getHistory()) ; 65 logData.setErrorCount(monitor_.getErrorCount()) ; 66 logData.setActionCount(monitor_.getActionCount()) ; 67 service_.saveSessionLogData(logData) ; 68 } 69 70 public String getName() { return "PersistSessionLog"; } 71 72 public String getDescription() { return "saving session log data" ; } 73 } 74 75 static class UpdateUserLastLoginTask extends BaseTask { 76 private OrganizationService orgService_ ; 77 private String userName_ ; 78 79 UpdateUserLastLoginTask(OrganizationService orgService, String userName) { 80 orgService_ = orgService ; 81 userName_ = userName; 82 } 83 84 public void execute() throws Exception { 85 User user = orgService_.findUserByName(userName_) ; 86 user.setLastLoginTime(new Date ()) ; 87 orgService_.saveUser(user) ; 88 } 89 90 public String getName() { return "UpdateUserLastLoginTask"; } 91 92 public String getDescription() { return "update the last login time of the user" ; } 93 } 94 } 95 | Popular Tags |