From: Marc P. <ma...@an...> - 2003-07-26 11:03:19
|
Guys, Just to make sure we are all reading from the same sheet now - I'm going to give some quick pseudo code for all the files involved. This is no doubt CONTENTIOUS! This is because the impl I am showing allows ContextAware detection for all variables placed into the context as well as "tools" loaded by the new factory/provider - something that has not yet been decided. Don't dismiss these changes just on that basis - this is to illustrate/confirm the general plan outlined by Lane (and earlier by myself). We can easily not do the instanceof ContextAware stuff in Context.put, but in that case we should definitely NOT do the same for tools returned from the provider. See comments inline. However all these changes really are relatively trivial - moving loadTools from Broker is the "biggest" job. I'm happy to do all this if you guys agree on the plan, if Lane isn't fussed about doing it. It would seem fitting that I have to do is, since I was the main protagonist in all this rucus. --- New file: ContextAware.java public interface ContextAware { void init( Context c); } --- ContextTool.java public interface ContextTool extends ContextAware { } --- New file: LazyVariableProvider.java I think this is the name that fits the functionality best, getting away from the ContextTool terminology, using WM provider terminology public interface LazyVariableProvider extends Provider { Object getVariable( String name, Context context); } --- New file: DefaultContextToolProvider.java The default impl providing existing CT functionality by excised from core of WM public class DefaultContextToolProvider implements LazyVariableProvider { //... impl Provider methods of course, including loading //existing CT definitions ... public Object getVariable( String name, Context context) { if (toolNamesMap.contains( name)) { return newToolInstance(name); // Note that without Context doing instance of in put for vars, we should then // have the instanceof ContextTool code in here somewhere and init tools ourselves. // instead of having Context do it. } else { return null; } } } --- Context.java Modifications.... public class Context implements Map, Cloneable { //...existing context stuff skipped... /** * Get the named object/property from the Context. If the Object * does not exist and there is a tool of the same name then the * Object will be instantiated and managed by the tool. * If there's no such variable, it throws. */ protected Object internalGet (Object name) throws PropertyException { Object ret = _variables.get(name); if (ret == null && !_variables.containsKey(name)) { // **** some modifications to remove CT dependency here Object tool = null; if (lazyVariableProvider != null) tool = lazyVariableProvider.getVariable( name, this); if (tool != null) { // Remove this code if we decide Context will not handle this kind of thing if (tool instanceof ContextAware) ((ContextAware)tool).init(this); } 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; } /** * Add an object to the context returning the object that was * there previously under the same name, if any. */ public final Object put (Object name, Object value) { // Remove this code if we decide Context will not handle this kind of thing if (value instanceof ContextAware) ((ContextAware)value).init( this); return _variables.put(name, value); } } -- Marc Palmer Contract Java Consultant/Developer w a n g j a m m e r s java and web software design experts with an ethical outlook http://www.wangjammers.org |