* move thread local context implementation to core.

This commit is contained in:
Heng Sin Low 2010-07-18 16:01:06 +08:00
parent 043080586d
commit e49ffe9190
7 changed files with 203 additions and 357 deletions

View File

@ -1,52 +0,0 @@
/******************************************************************************
* Copyright (C) 2008 Low Heng Sin *
* 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.adempiere.webui;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.adempiere.webui.session.ServerContext;
import net.sf.cglib.proxy.InvocationHandler;
/**
* Intercaptor for Server context properties that delegate to the threadlocal instance
* @author Low Heng Sin
*
*/
public class ServerContextCallback implements InvocationHandler, Serializable {
/**
*
*/
private static final long serialVersionUID = 6708635918931322152L;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
ServerContext context = ServerContext.getCurrentInstance();
//optimize for the 2 most common access
if (method.getName().equals("getProperty")) {
Class<?>[] types = method.getParameterTypes();
if (types != null && types.length == 1 && types[0] == String.class &&
args != null && args.length == 1 && args[0] instanceof String) {
return context.getProperty((String)args[0]);
}
else if (types != null && types.length == 2 && types[0] == String.class &&
types[1] == String.class && args != null && args[0] instanceof String &&
args[1] instanceof String)
return context.getProperty((String)args[0], (String)args[1]);
}
Method m = context.getClass().getMethod(method.getName(), method.getParameterTypes());
return m.invoke(context, args);
}
}

View File

@ -16,8 +16,8 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.adempiere.util.ServerContext;
import org.adempiere.webui.component.ZkCssHelper;
import org.adempiere.webui.session.ServerContext;
import org.adempiere.webui.session.SessionContextListener;
import org.compiere.model.MAssignmentSlot;
import org.compiere.model.ScheduleUtil;
@ -29,7 +29,7 @@ import org.zkoss.xml.XMLs;
public class TimelineEventFeed extends HttpServlet {
/**
*
* generated serial version Id
*/
private static final long serialVersionUID = 5507229085571123451L;
@ -42,13 +42,16 @@ public class TimelineEventFeed extends HttpServlet {
return;
}
ServerContext serverContext = ServerContext.getCurrentInstance();
if (serverContext == null) {
serverContext = ServerContext.newInstance();
ServerContext.setCurrentInstance(ctx);
try {
doGet0(req, resp);
} finally {
ServerContext.dispose();
}
serverContext.clear();
serverContext.putAll(ctx);
}
private void doGet0(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
int resourceId = 0;
String resourceIdParam = req.getParameter("S_Resource_ID");
if (resourceIdParam != null && resourceIdParam.trim().length() > 0) {

View File

@ -1,49 +0,0 @@
/******************************************************************************
* 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. *
*
* Copyright (C) 2007 Low Heng Sin hengsin@avantz.com
* _____________________________________________
*****************************************************************************/
package org.adempiere.webui;
import java.util.Properties;
import net.sf.cglib.proxy.Enhancer;
import org.adempiere.webui.session.SessionManager;
import org.compiere.util.ContextProvider;
/**
*
* @author Low Heng Sin
*
*/
public class ZkContextProvider implements ContextProvider {
private final static ServerContextCallback callback = new ServerContextCallback();
private final static Properties context = (Properties) Enhancer.create(Properties.class, callback);
/**
* Get server context proxy
*/
public Properties getContext() {
return context;
}
/**
* Show url at zk desktop
*/
public void showURL(String url) {
SessionManager.getAppDesktop().showURL(url,true);
}
}

View File

@ -20,9 +20,9 @@ import java.util.Locale;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.util.ServerContext;
import org.adempiere.webui.AdempiereWebUI;
import org.adempiere.webui.desktop.IDesktop;
import org.adempiere.webui.session.ServerContext;
import org.adempiere.webui.session.SessionContextListener;
import org.adempiere.webui.util.ServerPushTemplate;
import org.compiere.model.MSysConfig;
@ -30,6 +30,7 @@ import org.compiere.util.CLogger;
import org.zkoss.util.Locales;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.DesktopUnavailableException;
import org.zkoss.zk.ui.event.Events;
/**
*
@ -154,44 +155,35 @@ public class DashboardRunnable implements Runnable, Serializable
{
ServerPushTemplate template = new ServerPushTemplate(desktop);
for(int i = 0; i < dashboardPanels.size(); i++)
{
//make sure context is correct
Properties ctx = (Properties)template.getDesktop().getSession().getAttribute(SessionContextListener.SESSION_CTX);
if (ctx != null)
{
ServerContext serverContext = ServerContext.getCurrentInstance();
if (serverContext == null) {
serverContext = ServerContext.newInstance();
serverContext.putAll(ctx);
} else {
String id = ctx.getProperty(SessionContextListener.SERVLET_SESSION_ID);
if (id == null || !id.equals(serverContext.getProperty(SessionContextListener.SERVLET_SESSION_ID))) {
serverContext.clear();
serverContext.putAll(ctx);
}
}
}
dashboardPanels.get(i).refresh(template);
}
//set thread local context if not in event thread
Properties ctx = null;
boolean isEventThread = Events.inEventListener();
if (!isEventThread)
{
ctx = (Properties)template.getDesktop().getSession().getAttribute(SessionContextListener.SESSION_CTX);
if (ctx == null)
return;
}
try
{
if (!isEventThread)
{
ServerContext.setCurrentInstance(ctx);
}
for(int i = 0; i < dashboardPanels.size(); i++)
{
dashboardPanels.get(i).refresh(template);
}
//make sure context is correct
Properties ctx = (Properties)template.getDesktop().getSession().getAttribute(SessionContextListener.SESSION_CTX);
if (ctx != null)
{
ServerContext serverContext = ServerContext.getCurrentInstance();
if (serverContext == null) {
serverContext = ServerContext.newInstance();
serverContext.putAll(ctx);
} else {
String id = ctx.getProperty(SessionContextListener.SERVLET_SESSION_ID);
if (id == null || !id.equals(serverContext.getProperty(SessionContextListener.SERVLET_SESSION_ID))) {
serverContext.clear();
serverContext.putAll(ctx);
}
}
}
appDesktop.onServerPush(template);
appDesktop.onServerPush(template);
}
finally
{
if (!isEventThread)
{
ServerContext.dispose();
}
}
}
public void stop() {

View File

@ -1,85 +0,0 @@
/******************************************************************************
* Product: Posterita Ajax UI *
* Copyright (C) 2007 Posterita Ltd. 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. *
* For the text or an alternative of this public license, you may reach us *
* Posterita Ltd., 3, Draper Avenue, Quatre Bornes, Mauritius *
* or via info@posterita.org or http://www.posterita.org/ *
*****************************************************************************/
package org.adempiere.webui.session;
import java.util.Properties;
import org.compiere.util.Env;
import org.compiere.util.Language;
/**
*
* @author <a href="mailto:agramdass@gmail.com">Ashley G Ramdass</a>
* @date Feb 25, 2007
* @version $Revision: 0.10 $
*/
@SuppressWarnings("serial")
public final class ServerContext extends Properties
{
private ServerContext()
{
super();
/**
* Set english as default language
*/
this.put(Env.LANGUAGE, Language.getBaseAD_Language());
}
private static InheritableThreadLocal<ServerContext> context = new InheritableThreadLocal<ServerContext>() {
protected ServerContext initialValue()
{
return new ServerContext();
}
};
/**
* Get server context for current thread
* @return ServerContext
*/
public static ServerContext getCurrentInstance()
{
return (ServerContext)context.get();
}
/**
* dispose server context for current thread
*/
public static void dispose()
{
context.remove();
}
/**
* Allocate new server context for current thread
* @return ServerContext
*/
public static ServerContext newInstance()
{
dispose();
return getCurrentInstance();
}
/**
* Set server context for current thread
* @param ctx
*/
public static void setCurrentInstance(ServerContext ctx)
{
context.set(ctx);
}
}

View File

@ -27,13 +27,18 @@ import org.zkoss.util.Locales;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventThreadCleanup;
import org.zkoss.zk.ui.event.EventThreadInit;
import org.zkoss.zk.ui.event.EventThreadResume;
import org.zkoss.zk.ui.event.EventThreadSuspend;
import org.zkoss.zk.ui.util.ExecutionCleanup;
import org.zkoss.zk.ui.util.ExecutionInit;
import org.adempiere.util.ServerContextURLHandler;
import org.adempiere.util.ServerContext;
/**
*
* @author <a href="mailto:agramdass@gmail.com">Ashley G Ramdass</a>
@ -41,11 +46,47 @@ import org.zkoss.zk.ui.util.ExecutionInit;
* @version $Revision: 0.10 $
*/
public class SessionContextListener implements ExecutionInit,
ExecutionCleanup, EventThreadInit, EventThreadResume, EventThreadCleanup
ExecutionCleanup, EventThreadInit, EventThreadResume, EventThreadCleanup, EventThreadSuspend
{
public static final String SERVLET_SESSION_ID = "servlet.sessionId";
public static final String SESSION_CTX = "WebUISessionContext";
private Properties _ctx = null;
/**
* get servlet thread local context from session
* @param exec
* @param createNew
*/
private void setupExecutionContextFromSession(Execution exec) {
Session session = exec.getDesktop().getSession();
Properties ctx = (Properties)session.getAttribute(SESSION_CTX);
HttpSession httpSession = (HttpSession)session.getNativeSession();
if (ctx != null)
{
//verify ctx
String cacheId = ctx.getProperty(SERVLET_SESSION_ID);
if (cacheId == null || !cacheId.equals(httpSession.getId()) )
{
ctx = null;
session.removeAttribute(SESSION_CTX);
}
}
if (ctx == null)
{
ctx = new Properties();
ctx.put(ServerContextURLHandler.SERVER_CONTEXT_URL_HANDLER, new ServerContextURLHandler() {
public void showURL(String url) {
SessionManager.getAppDesktop().showURL(url, true);
}
});
ctx.setProperty(SERVLET_SESSION_ID, httpSession.getId());
session.setAttribute(SESSION_CTX, ctx);
}
ServerContext.setCurrentInstance(ctx);
}
/**
* @param exec
* @param parent
@ -54,29 +95,13 @@ public class SessionContextListener implements ExecutionInit,
*/
public void init(Execution exec, Execution parent)
{
if (parent == null)
{
Properties ctx = (Properties)exec.getDesktop().getSession().getAttribute(SESSION_CTX);
if (ctx == null)
{
ctx = new Properties();
HttpSession session = (HttpSession)exec.getDesktop().getSession().getNativeSession();
ctx.setProperty(SERVLET_SESSION_ID, session.getId());
exec.getDesktop().getSession().setAttribute(SESSION_CTX, ctx);
}
ServerContext serverContext = ServerContext.getCurrentInstance();
if (serverContext == null)
{
serverContext = ServerContext.newInstance();
}
serverContext.clear();
serverContext.putAll(ctx);
exec.setAttribute(SESSION_CTX, serverContext);
//set locale
Locales.setThreadLocal(Env.getLanguage(ctx).getLocale());
}
if (parent == null)
{
//in servlet thread
setupExecutionContextFromSession(Executions.getCurrent());
//set locale
Locales.setThreadLocal(Env.getLanguage(ServerContext.getCurrentInstance()).getLocale());
}
}
/**
@ -87,83 +112,51 @@ public class SessionContextListener implements ExecutionInit,
*/
public void cleanup(Execution exec, Execution parent, List errs)
{
//in servlet thread
if (parent == null)
{
Properties ctx = (Properties)exec.getDesktop().getSession().getAttribute(SESSION_CTX);
if (ctx != null)
{
ServerContext serverContext = (ServerContext) Executions.getCurrent().getAttribute(SESSION_CTX);
if (serverContext != null)
{
ctx.clear();
ctx.putAll(serverContext);
}
}
ServerContext.dispose();
exec.removeAttribute(SESSION_CTX);
}
}
/**
* get from servlet thread's ThreadLocal
* @param comp
* @param evt
* @see EventThreadInit#prepare(Component, Event)
*/
public void prepare(Component comp, Event evt)
{
//in servlet thread
_ctx = ServerContext.getCurrentInstance();
}
/**
* copy event thread's ThreadLocal to servlet thread
* @param comp
* @param evt
* @see EventThreadInit#init(Component, Event)
*/
public boolean init(Component comp, Event evt)
{
ServerContext serverContext = (ServerContext) Executions.getCurrent().getAttribute(SESSION_CTX);
if (serverContext == null)
{
serverContext = ServerContext.newInstance();
}
else
{
ServerContext.setCurrentInstance(serverContext);
}
//set locale
Locales.setThreadLocal(Env.getLanguage(serverContext).getLocale());
return true;
}
* @param evt
* @throws Exception
* @see {@link EventThreadSuspend#afterSuspend(Component, Event)}
*/
public void afterSuspend(Component comp, Event evt) throws Exception
{
//in servlet thread
Properties ctx = new Properties();
ctx.putAll(_ctx);
ServerContext.setCurrentInstance(ctx);
Executions.getCurrent().getDesktop().getSession().setAttribute(SESSION_CTX, ctx);
}
/**
* get from servlet thread's ThreadLocal
* @param comp
* @param evt
* @see EventThreadResume#beforeResume(Component, Event)
*/
public void beforeResume(Component comp, Event evt)
{
}
/**
* @param comp
* @param evt
* @see EventThreadResume#afterResume(Component, Event)
*/
public void afterResume(Component comp, Event evt)
{
ServerContext serverContext = (ServerContext) Executions.getCurrent().getAttribute(SESSION_CTX);
if (serverContext == null)
{
serverContext = ServerContext.newInstance();
}
else
{
ServerContext.setCurrentInstance(serverContext);
}
//set locale
Locales.setThreadLocal(Env.getLanguage(serverContext).getLocale());
//in servlet thread
_ctx = ServerContext.getCurrentInstance();
}
/**
@ -173,9 +166,77 @@ public class SessionContextListener implements ExecutionInit,
*/
public void abortResume(Component comp, Event evt)
{
//in servlet thread
}
/**
* copy event thread's ThreadLocal to servlet thread's ThreadLocal
* @param comp
* @param evt
* @see EventThreadCleanup#complete(Component, Event)
*/
public void complete(Component comp, Event evt) throws Exception
{
//in servlet thread
Properties ctx = new Properties();
ctx.putAll(_ctx);
ServerContext.setCurrentInstance(ctx);
Executions.getCurrent().getDesktop().getSession().setAttribute(SESSION_CTX, ctx);
}
/**
* copy servlet thread's ThreadLocal to event thread's ThreadLocal
* @param comp
* @param evt
* @see EventThreadInit#init(Component, Event)
*/
public boolean init(Component comp, Event evt)
{
//in event processing thread
Properties ctx = new Properties();
ctx.putAll(_ctx);
ServerContext.setCurrentInstance(ctx);
//set locale
Locales.setThreadLocal(Env.getLanguage(ServerContext.getCurrentInstance()).getLocale());
return true;
}
/**
* get from event thread's ThreadLocal
* @param comp
* @param evt
* @param obj
* @throws Exception
* @see {@link EventThreadSuspend#beforeSuspend(Component, Event, Object)}
*/
public void beforeSuspend(Component comp, Event evt, Object obj)
throws Exception
{
//in event processing thread
_ctx = ServerContext.getCurrentInstance();
}
/**
* copy servlet thread's ThreadLocal to event thread's ThreadLocal
* @param comp
* @param evt
* @see EventThreadResume#afterResume(Component, Event)
*/
public void afterResume(Component comp, Event evt)
{
//in event processing thread
Properties ctx = new Properties();
ctx.putAll(_ctx);
ServerContext.setCurrentInstance(ctx);
//set locale
Locales.setThreadLocal(Env.getLanguage(ServerContext.getCurrentInstance()).getLocale());
}
/**
* get from event thread's ThreadLocal
* @param comp
* @param evt
* @param errs
@ -183,15 +244,8 @@ public class SessionContextListener implements ExecutionInit,
*/
public void cleanup(Component comp, Event evt, List errs) throws Exception
{
ServerContext.dispose();
}
/**
* @param comp
* @param evt
* @see EventThreadCleanup#complete(Component, Event)
*/
public void complete(Component comp, Event evt) throws Exception
{
//in event processing thread
_ctx = ServerContext.getCurrentInstance();
// ServerContext.dispose();
}
}

View File

@ -18,6 +18,7 @@
package org.adempiere.webui.session;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import javax.servlet.ServletConfig;
@ -27,18 +28,18 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.adempiere.webui.ZkContextProvider;
import org.adempiere.webui.window.ZkJRViewerProvider;
import org.adempiere.webui.window.ZkReportViewerProvider;
import org.compiere.Adempiere;
import org.compiere.print.ReportCtl;
import org.compiere.report.ReportStarter;
import org.compiere.util.CLogMgt;
import org.compiere.util.CLogger;
import org.compiere.util.Env;
import org.compiere.util.Ini;
import org.zkoss.zk.ui.http.DHtmlLayoutServlet;
import org.adempiere.util.ServerContext;
import org.adempiere.util.ServerContextURLHandler;
/**
*
* @author <a href="mailto:agramdass@gmail.com">Ashley G Ramdass</a>
@ -47,45 +48,28 @@ import org.zkoss.zk.ui.http.DHtmlLayoutServlet;
*/
public class WebUIServlet extends DHtmlLayoutServlet
{
/**
*
*/
private static final long serialVersionUID = 261899419681731L;
private static final long serialVersionUID = 1L;
/** Logger for the class * */
private static final CLogger logger;
/** Logger for the class * */
private static CLogger logger;
static
{
logger = CLogger.getCLogger(WebUIServlet.class);
}
private ServletConfig servletConfig;
public void init(ServletConfig servletConfig) throws ServletException
{
System.out.println("WebUIServlet init ...");
super.init(servletConfig);
// HttpBrigde requires config
this.servletConfig = servletConfig;
/** Initialise context for the current thread*/
ServerContext.newInstance();
Env.setContextProvider(new ZkContextProvider());
/**
* Start ADempiere
*/
logger.info("Starting ADempiere...");
try
{
CLogMgt.initialize(false);
}
catch(Exception ex)
{
logger.log(Level.SEVERE, "Could not initialize ADempiere logging Management", ex);
}
Properties serverContext = new Properties();
serverContext.put(ServerContextURLHandler.SERVER_CONTEXT_URL_HANDLER, new ServerContextURLHandler() {
public void showURL(String url) {
SessionManager.getAppDesktop().showURL(url, true);
}
});
ServerContext.setCurrentInstance(serverContext);
boolean started = Adempiere.startup(false);
if(!started)
@ -97,7 +81,7 @@ public class WebUIServlet extends DHtmlLayoutServlet
Ini.setProperty(Ini.P_ADEMPIERESYS, false);
ReportCtl.setReportViewerProvider(new ZkReportViewerProvider());
ReportStarter.setReportViewerProvider(new ZkJRViewerProvider());
logger.info("ADempiere started successfully");
logger.log(Level.OFF, "ADempiere started successfully");
/**
* End ADempiere Start
*/
@ -124,8 +108,7 @@ public class WebUIServlet extends DHtmlLayoutServlet
public ServletConfig getServletConfig()
{
return servletConfig;
// return super.getServletConfig();
return super.getServletConfig();
}
public String getServletInfo()