From: <bri...@us...> - 2003-07-30 04:51:20
|
Update of /cvsroot/webmacro/webmacro/src/org/webmacro In directory sc8-pr-cvs1:/tmp/cvs-serv23890/src/org/webmacro Modified Files: Broker.java Context.java ContextTool.java Log Message: Refactor ContextTool to be a special case of ContextAutoLoader Index: Broker.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/Broker.java,v retrieving revision 1.40 retrieving revision 1.41 diff -C2 -d -r1.40 -r1.41 *** Broker.java 30 Jul 2003 02:28:13 -0000 1.40 --- Broker.java 30 Jul 2003 04:51:17 -0000 1.41 *************** *** 22,30 **** import java.io.*; import java.lang.ref.WeakReference; - import java.lang.reflect.Constructor; import java.net.MalformedURLException; import java.net.URL; import java.util.*; import org.webmacro.engine.DefaultEvaluationExceptionHandler; import org.webmacro.engine.EvaluationExceptionHandler; --- 22,31 ---- import java.io.*; import java.lang.ref.WeakReference; import java.net.MalformedURLException; import java.net.URL; import java.util.*; + import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; + import org.webmacro.broker.ContextAutoLoader; import org.webmacro.engine.DefaultEvaluationExceptionHandler; import org.webmacro.engine.EvaluationExceptionHandler; *************** *** 38,42 **** import org.webmacro.util.Settings; import org.webmacro.util.SubSettings; - import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; /** --- 39,42 ---- *************** *** 89,94 **** private Map _functionMap = new ConcurrentHashMap(); ! /** a local map for context tools */ ! private Map _tools = new ConcurrentHashMap(); --- 89,94 ---- private Map _functionMap = new ConcurrentHashMap(); ! /** a local map for context tools and other automatic context goodies */ ! private Map _toolLoader = new ConcurrentHashMap(); *************** *** 237,241 **** private class ProviderSettingHandler extends Settings.ListSettingHandler { - public void processSetting (String settingKey, String settingValue) { --- 237,240 ---- *************** *** 253,257 **** } ! private class ToolsSettingHandler extends Settings.ListSettingHandler { public void processSetting (String settingKey, String settingValue) --- 252,256 ---- } ! private class AutoLoaderSettingHandler extends Settings.ListSettingHandler { public void processSetting (String settingKey, String settingValue) *************** *** 259,267 **** try { ! addTool(settingKey, settingValue, "Tool"); } catch (Exception e) { ! _log.error("Tool (" + settingValue + ") failed to load", e); } } --- 258,268 ---- try { ! Class pClass = classForName(settingValue); ! ContextAutoLoader instance = (ContextAutoLoader) pClass.newInstance(); ! instance.init(Broker.this, settingKey); } catch (Exception e) { ! _log.error("ContextAutoLoader (" + settingValue + ") failed to load", e); } } *************** *** 300,306 **** } ! // load tools ! loadTools("ContextTools"); ! loadTools("WebContextTools"); eehClass = _config.getSetting("ExceptionHandler"); --- 301,306 ---- } ! _config.processListSetting("ContextAutoLoaders", new AutoLoaderSettingHandler()); ! // @@@ load autoloaders eehClass = _config.getSetting("ExceptionHandler"); *************** *** 826,929 **** } ! /** ! * Attempts to instantiate the tool using two different constructors ! * until one succeeds, in the following order: ! * <ul> ! * <li>new MyTool(String key)</li> ! * <li>new MyTool()</li> ! * </ul> ! * The key is generally the unqualified class name of the tool minus the ! * "Tool" suffix, e.g., "My" in the example above ! * The settings are any configured settings for this tool, i.e, settings ! * prefixed with the tool's key. ! * <br> ! * NOTE: keats - 25 May 2002, no tools are known to use the settings mechanism. ! * We should create an example of this and test it, or abolish this capability! ! * Abolished -- BG ! */ ! private void addTool(String toolName, String className, String suffix) { ! Class c; ! try ! { ! c = classForName(className); ! } ! catch (ClassNotFoundException e) ! { ! _log.warning("Context: Could not locate class for context tool " ! + className); ! return; ! } ! if (toolName == null || toolName.equals("")) ! { ! toolName = className; ! int start = 0; ! int end = toolName.length(); ! int lastDot = toolName.lastIndexOf('.'); ! if (lastDot != -1) ! { ! start = lastDot + 1; ! } ! if (toolName.endsWith(suffix)) ! { ! end -= suffix.length(); ! } ! toolName = toolName.substring(start, end); ! } ! ! Constructor ctor = null; ! Constructor[] ctors = c.getConstructors(); ! Class[] parmTypes = null; ! Object instance = null; ! ! // check for 1 arg (String) constructor ! for (int i = 0; i < ctors.length; i++) ! { ! parmTypes = ctors[i].getParameterTypes(); ! if (parmTypes.length == 1 && parmTypes[0].equals(String.class)) ! { ! ctor = ctors[i]; ! Object[] args = {toolName}; ! try ! { ! instance = ctor.newInstance(args); ! } ! catch (Exception e) ! { ! _log.error("Failed to instantiate tool " ! + toolName + " of class " + className + " using constructor " ! + ctor.toString(), e); ! } ! } } ! if (instance == null) ! { ! // try no-arg constructor ! try ! { ! instance = c.newInstance(); ! } ! catch (Exception e) ! { ! _log.error("Unable to construct tool " + toolName + " of class " + className, e); ! return; ! } } - _tools.put(toolName, instance); - _log.info("Registered ContextTool " + toolName); - } - - /** Fetch a tool */ - public ContextTool getTool(Object toolName) { - return (ContextTool) _tools.get(toolName); } ! /** ! * Load the context tools listed in the supplied string. See ! * the ComponentMap class for a description of the format of ! * this string. ! */ ! protected final void loadTools (String keyName) ! { ! getSettings().processListSetting(keyName, new ToolsSettingHandler()); } --- 826,845 ---- } ! /** Fetch a tool */ ! public Object getAutoContextVariable(String variableName, Context context) { ! ContextAutoLoader loader = (ContextAutoLoader) _toolLoader.get(variableName); ! try { ! if (loader == null) ! return null; ! else ! return loader.get(variableName, context); } ! catch (PropertyException e) { ! return null; } } ! public void registerAutoContextVariable(String variableName, ContextAutoLoader loader) { ! _toolLoader.put(variableName, loader); } Index: Context.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/Context.java,v retrieving revision 1.63 retrieving revision 1.64 diff -C2 -d -r1.63 -r1.64 *** Context.java 17 Jul 2003 05:25:06 -0000 1.63 --- Context.java 30 Jul 2003 04:51:17 -0000 1.64 *************** *** 219,269 **** { Object ret = _variables.get(name); ! if (ret == null && !_variables.containsKey(name)) { ! Object tool = _broker.getTool(name); ! if (tool != null) { ! try ! { ! ContextTool ct = (ContextTool) tool; ! ret = ct.init(this); ! put(name, ret); ! } ! catch (PropertyException e) ! { ! _log.error("Unable to initialize ContextTool: " + name, e); ! } } ! else if (name instanceof FunctionCall) { ! FunctionCall fc = (FunctionCall) name; ! String fname = fc.getName(); ! MethodWrapper func = null; ! if (_funcs != null) { ! func = (MethodWrapper) _funcs.get(fname); ! } ! if (func == null) ! { ! func = _broker.getFunction(fname); ! } ! if (func != null) ! { ! Object[] args = fc.getArguments(this); ! ret = func.invoke(args); ! } ! else ! { ! _log.error("Function " + fname + " was not loaded!"); ! } } else { ! // changed by Keats 30-Nov-01 ! return UNDEF; } ! //throw new ! // PropertyException.NoSuchVariableException(name.toString()); } - return ret; } --- 219,262 ---- { Object ret = _variables.get(name); ! if (ret != null || _variables.containsKey(name)) ! return ret; ! ! if (name instanceof String) { ! Object var = _broker.getAutoContextVariable((String) name, this); ! if (var != null) { ! put(name, var); ! return var; ! } ! else ! return UNDEF; ! } ! else if (name instanceof FunctionCall) { ! FunctionCall fc = (FunctionCall) name; ! String fname = fc.getName(); ! MethodWrapper func = null; ! if (_funcs != null) { ! func = (MethodWrapper) _funcs.get(fname); ! } ! if (func == null) { ! func = _broker.getFunction(fname); } ! if (func != null) { ! Object[] args = fc.getArguments(this); ! ret = func.invoke(args); } else { ! _log.error("Function " + fname + " was not loaded!"); } ! return ret; ! } ! else ! { ! // changed by Keats 30-Nov-01 ! return UNDEF; } } Index: ContextTool.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/ContextTool.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** ContextTool.java 16 Jul 2003 06:44:59 -0000 1.9 --- ContextTool.java 30 Jul 2003 04:51:17 -0000 1.10 *************** *** 24,32 **** package org.webmacro; /** ! * This interface is used to attach utilities to a context to assist ! * with the generation of views. */ ! public interface ContextTool { --- 24,34 ---- package org.webmacro; + import org.webmacro.broker.ContextObjectFactory; + /** ! * This class is used as a base class for legacy context tools so they can fit into the ContextObjectFactory ! * framework. */ ! public abstract class ContextTool implements ContextObjectFactory { *************** *** 38,42 **** * to hold the per-request state. */ ! public Object init (Context c) throws PropertyException; } --- 40,47 ---- * to hold the per-request state. */ ! public abstract Object init (Context c) throws PropertyException; + public Object get(Context c) throws PropertyException { + return init(c); + } } |