* move thread local context implementation to core.

This commit is contained in:
Heng Sin Low 2010-07-18 16:00:25 +08:00
parent 1a9a979dcc
commit 043080586d
8 changed files with 274 additions and 87 deletions

View File

@ -47,5 +47,6 @@ Require-Bundle: org.adempiere.tools;bundle-version="1.0.0",
com.springsource.javax.jms;bundle-version="1.1.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Eclipse-ExtensibleAPI: true
Import-Package: org.eclipse.core.runtime;version="3.4.0"
Import-Package: net.sf.cglib.proxy,
org.eclipse.core.runtime;version="3.4.0"

View File

@ -0,0 +1,76 @@
/******************************************************************************
* 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.util;
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 $
*/
public final class ServerContext
{
/**
* generated serial version Id
*/
private static final long serialVersionUID = -8274580404204046413L;
private ServerContext()
{
}
private static InheritableThreadLocal<Properties> context = new InheritableThreadLocal<Properties>() {
protected Properties initialValue()
{
Properties ctx = new Properties();
ctx.put(Env.LANGUAGE, Language.getBaseAD_Language());
return ctx;
}
};
/**
* Get server context for current thread
* @return Properties
*/
public static Properties getCurrentInstance()
{
return (Properties)context.get();
}
/**
* dispose server context for current thread
*/
public static void dispose()
{
context.remove();
}
/**
* Set server context for current thread
* @param ctx
*/
public static void setCurrentInstance(Properties ctx)
{
context.set(ctx);
}
}

View File

@ -0,0 +1,49 @@
/******************************************************************************
* 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.util;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Properties;
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 = 1L;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Properties 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

@ -0,0 +1,53 @@
/******************************************************************************
* 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.util;
import java.util.Properties;
import net.sf.cglib.proxy.Enhancer;
import org.compiere.util.ContextProvider;
/**
*
* @author Low Heng Sin
*
*/
public class ServerContextProvider implements ContextProvider {
private final static ServerContextCallback callback = new ServerContextCallback();
private final static Properties context = (Properties) Enhancer.create(Properties.class, callback);
public final static ServerContextProvider INSTANCE = new ServerContextProvider();
private ServerContextProvider() {}
/**
* Get server context proxy
*/
public Properties getContext() {
return context;
}
/**
* Show url at zk desktop
*/
public void showURL(String url) {
ServerContextURLHandler handler = (ServerContextURLHandler) getContext().get(ServerContextURLHandler.SERVER_CONTEXT_URL_HANDLER);
if (handler != null)
handler.showURL(url);
}
}

View File

@ -0,0 +1,6 @@
package org.adempiere.util;
public interface ServerContextURLHandler {
public final static String SERVER_CONTEXT_URL_HANDLER = "SERVER_CONTEXT_URL_HANDLER";
public void showURL(String url);
}

View File

@ -22,7 +22,6 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
@ -32,8 +31,6 @@ import javax.jnlp.UnavailableServiceException;
import javax.swing.ImageIcon;
import org.adempiere.base.Core;
import org.adempiere.base.IResourceFinder;
import org.adempiere.base.Service;
import org.compiere.db.CConnection;
import org.compiere.model.MClient;
import org.compiere.model.MSystem;
@ -47,7 +44,6 @@ import org.compiere.util.Ini;
import org.compiere.util.Login;
import org.compiere.util.SecureEngine;
import org.compiere.util.SecureInterface;
//import org.compiere.util.Splash;
import org.compiere.util.Util;
/**
@ -80,7 +76,6 @@ public final class Adempiere
static private final String s_file32x32 = "images/AD32.gif";
/** 100*30 Product Image. */
static private final String s_file100x30 = "images/AD10030.png";
// static private final String s_file100x30HR = "images/AD10030HR.png";
/** 48*15 Product Image. */
static private final String s_file48x15 = "images/Adempiere.png";
static private final String s_file48x15HR = "images/AdempiereHR.png";

View File

@ -48,6 +48,7 @@ import javax.swing.SwingUtilities;
import org.adempiere.base.Core;
import org.adempiere.base.IResourceFinder;
import org.adempiere.util.ServerContextProvider;
import org.compiere.db.CConnection;
import org.compiere.model.MClient;
import org.compiere.model.MLookupCache;
@ -72,12 +73,14 @@ public final class Env
/** Logging */
private static CLogger s_log = CLogger.getCLogger(Env.class);
private static ContextProvider contextProvider = new DefaultContextProvider();
private final static ContextProvider clientContextProvider = new DefaultContextProvider();
/**
* @param provider
* @deprecated
*/
public static void setContextProvider(ContextProvider provider)
{
contextProvider = provider;
getCtx().put(LANGUAGE, Language.getBaseAD_Language());
}
/**
@ -208,9 +211,16 @@ public final class Env
*/
public static final Properties getCtx()
{
return contextProvider.getContext();
return getContextProvider().getContext();
} // getCtx
public static ContextProvider getContextProvider() {
if (Ini.isClient())
return clientContextProvider;
else
return ServerContextProvider.INSTANCE;
}
/**
* Set Context
* @param ctx context
@ -1562,7 +1572,7 @@ public final class Env
public static void startBrowser (String url)
{
s_log.info(url);
contextProvider.showURL(url);
getContextProvider().showURL(url);
} // startBrowser
/**

View File

@ -234,7 +234,7 @@ public final class Ini implements Serializable
private static String s_propertyFileName = null;
/** Logger */
private static Logger log = null;
private static Logger log = Logger.getLogger(Ini.class.getName());
/**
* Save INI parameters to disk
@ -670,7 +670,7 @@ public final class Ini implements Serializable
public static String getAsString()
{
StringBuffer buf = new StringBuffer ("Ini[");
Enumeration e = s_prop.keys();
Enumeration<?> e = s_prop.keys();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
@ -711,11 +711,8 @@ public final class Ini implements Serializable
*/
public static void setClient (boolean client)
{
if (log != null) //already initialized
return;
s_client = client;
CLogMgt.initialize(client);
log = Logger.getLogger(Ini.class.getName());
} // setClient
/**