Fix logged session listener initialized before server startup

This commit is contained in:
Elaine Tan 2012-11-07 13:15:46 +08:00
parent 9a87cca423
commit d7aa548502
4 changed files with 106 additions and 2 deletions

View File

@ -32,12 +32,15 @@ import javax.jnlp.BasicService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
import javax.swing.ImageIcon;
import javax.swing.event.EventListenerList;
import org.adempiere.base.Core;
import org.compiere.db.CConnection;
import org.compiere.model.MClient;
import org.compiere.model.MSystem;
import org.compiere.model.ModelValidationEngine;
import org.compiere.model.ServerStateChangeEvent;
import org.compiere.model.ServerStateChangeListener;
import org.compiere.util.CLogFile;
import org.compiere.util.CLogMgt;
import org.compiere.util.CLogger;
@ -109,6 +112,9 @@ public final class Adempiere
/** Thread pool **/
private static ThreadPoolExecutor threadPoolExecutor = null;
/** A list of event listeners for this component. */
private static EventListenerList m_listenerList = new EventListenerList();
static {
ClassLoader loader = Adempiere.class.getClassLoader();
@ -538,6 +544,8 @@ public final class Adempiere
createThreadPool();
fireServerStateChanged(new ServerStateChangeEvent(new Object(), ServerStateChangeEvent.SERVER_START));
if (isClient) // don't test connection
return false; // need to call
@ -655,4 +663,26 @@ public final class Adempiere
public static ThreadPoolExecutor getThreadPoolExecutor() {
return threadPoolExecutor;
}
/**
* @param l listener
*/
public static synchronized void removeServerStateChangeListener(ServerStateChangeListener l)
{
m_listenerList.remove(ServerStateChangeListener.class, l);
}
/**
* @param l listener
*/
public static synchronized void addServerStateChangeListener(ServerStateChangeListener l)
{
m_listenerList.add(ServerStateChangeListener.class, l);
}
private static void fireServerStateChanged(ServerStateChangeEvent e)
{
ServerStateChangeListener[] listeners = m_listenerList.getListeners(ServerStateChangeListener.class);
for (int i = 0; i < listeners.length; i++)
listeners[i].stateChange(e);
}
} // Adempiere

View File

@ -0,0 +1,28 @@
package org.compiere.model;
import java.util.EventObject;
public class ServerStateChangeEvent extends EventObject {
/**
*
*/
private static final long serialVersionUID = -7744346736197438379L;
public final static int SERVER_START = 0;
public final static int SERVER_STOP = 1;
private int eventType;
public ServerStateChangeEvent(Object source, int eventType) {
super(source);
this.eventType = eventType;
}
public int getEventType() {
return eventType;
}
public void setEventType(int eventType) {
this.eventType = eventType;
}
}

View File

@ -0,0 +1,31 @@
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 2007 Adempiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.compiere.model;
import java.util.EventListener;
/**
*
* @author Low Heng Sin
*
*/
public interface ServerStateChangeListener extends EventListener {
/**
*
* @param event
*/
public void stateChange(ServerStateChangeEvent event);
}

View File

@ -10,12 +10,15 @@ import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.compiere.Adempiere;
import org.compiere.model.MSession;
import org.compiere.model.ServerStateChangeEvent;
import org.compiere.model.ServerStateChangeListener;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.WebUtil;
public class LoggedSessionListener implements HttpSessionListener, ServletContextListener{
public class LoggedSessionListener implements HttpSessionListener, ServletContextListener, ServerStateChangeListener{
private static Hashtable<String, HttpSession> AD_SessionList = new Hashtable<String, HttpSession>();
@Override
@ -45,6 +48,12 @@ public class LoggedSessionListener implements HttpSessionListener, ServletContex
}
public void DestroyAllSession() {
if (!Adempiere.isStarted())
{
Adempiere.addServerStateChangeListener(this);
return;
}
String serverName = WebUtil.getServerName();
String sql = "UPDATE AD_Session SET processed = 'Y' WHERE processed ='N' AND servername = '"+serverName+"'";
Statement stmt = DB.createStatement();
@ -53,7 +62,8 @@ public class LoggedSessionListener implements HttpSessionListener, ServletContex
}catch (Exception e) {
System.out.println("UpdateSession: "+e);
}
Adempiere.removeServerStateChangeListener(this);
}
public void removeADSession(String sessionID, String serverName) {
@ -73,4 +83,9 @@ public class LoggedSessionListener implements HttpSessionListener, ServletContex
System.out.println("UpdateSession: "+e);
}
}
@Override
public void stateChange(ServerStateChangeEvent event) {
DestroyAllSession();
}
}