Update of /cvsroot/webmacro/webmacro/src/org/webmacro In directory sc8-pr-cvs1:/tmp/cvs-serv32064/src/org/webmacro Modified Files: Broker.java Context.java ContextTool.java FastWriter.java Template.java WM.java WebMacro.java Removed Files: FilterTool.java Log Message: More ripping out -- eliminate reference counting of Brokers, pooling of context and fastwriter; streamline Context; eliminate Clock thread hack; eliminate ascii fastwriter hack Index: Broker.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/Broker.java,v retrieving revision 1.36 retrieving revision 1.37 diff -C2 -d -r1.36 -r1.37 *** Broker.java 8 Jul 2003 19:48:44 -0000 1.36 --- Broker.java 16 Jul 2003 06:44:59 -0000 1.37 *************** *** 28,31 **** --- 28,32 ---- import java.io.*; import java.lang.ref.WeakReference; + import java.lang.reflect.Constructor; import java.net.MalformedURLException; import java.net.URL; *************** *** 82,89 **** private Map _functionMap = Collections.synchronizedMap(new HashMap()); ! /** Reference _count to detect unused brokers */ ! private int _count; ! /** our _key in the cache of brokers */ ! private Object _key; /* --- 83,89 ---- private Map _functionMap = Collections.synchronizedMap(new HashMap()); ! /** a local map for context tools */ ! private Map _tools = Collections.synchronizedMap(new HashMap()); ! /* *************** *** 173,177 **** { _name = name; ! _ls = LogSystem.getInstance(_name); _log = _ls.getLog("broker", "general object loader and configuration"); } --- 173,177 ---- { _name = name; ! _ls = new LogSystem(_name); _log = _ls.getLog("broker", "general object loader and configuration"); } *************** *** 229,233 **** } ! private class SettingHandler extends Settings.ListSettingHandler { --- 229,233 ---- } ! private class ProviderSettingHandler extends Settings.ListSettingHandler { *************** *** 247,250 **** --- 247,266 ---- } + private class ToolsSettingHandler extends Settings.ListSettingHandler + { + public void processSetting (String settingKey, String settingValue) + { + try + { + addTool(settingKey, settingValue, "Tool"); + } + catch (Exception e) + { + _log.error("Tool (" + settingValue + ") failed to load", e); + } + } + } + + /** * Constructors should call this after they've set up the properties *************** *** 296,300 **** // set up providers ! _config.processListSetting("Providers", new SettingHandler()); if (_providers.size() == 0) { --- 312,316 ---- // set up providers ! _config.processListSetting("Providers", new ProviderSettingHandler()); if (_providers.size() == 0) { *************** *** 303,306 **** --- 319,326 ---- } + // load tools + loadTools("Tools"); + loadTools("WebContextTools"); + eehClass = _config.getSetting("ExceptionHandler"); if (eehClass != null && !eehClass.equals("")) *************** *** 366,370 **** try { ! tmpl.getString(argContext); } catch (Exception e) --- 386,390 ---- try { ! tmpl.evaluateAsString(argContext); } catch (Exception e) *************** *** 422,426 **** register(WEBMACRO_PROPERTIES, b); } - b.startClient(); return b; } --- 442,445 ---- *************** *** 443,447 **** register(p, b); } - b.startClient(); return b; } --- 462,465 ---- *************** *** 465,469 **** } - b.startClient(); return b; } --- 483,486 ---- *************** *** 549,553 **** { BROKERS.put(key, new WeakReference(broker)); - broker._key = key; } --- 566,569 ---- *************** *** 562,566 **** if (ref != null) { ! return (Broker) ref.get(); } else --- 578,585 ---- if (ref != null) { ! Broker broker = (Broker) ref.get(); ! if (broker == null) ! BROKERS.remove(key); ! return broker; } else *************** *** 660,664 **** public Log getLog (String type, String description) { ! return _ls.getLog(type); } --- 679,683 ---- public Log getLog (String type, String description) { ! return _ls.getLog(type, description); } *************** *** 837,890 **** /** ! * Backwards compatible, calls get(String,String) ! * @deprecated call get(String,String) instead */ ! public final Object getValue (String type, String query) ! throws ResourceException ! { ! return get(type, query); ! } ! ! public synchronized void startClient () ! { ! if (_count++ == 0) { ! _log.info("starting clock"); ! Clock.startClient(); // start clock } ! } ! ! public synchronized void stopClient () ! { ! if (--_count == 0) { ! shutdown(); ! // make sure to cleanup the logging system ! // if no more references to this Broker are around ! LogSystem.removeInstance(_ls); } } /** ! * Shut down the broker */ ! public synchronized void shutdown () { ! _log.notice("shutting down"); ! ! Enumeration e = _providers.elements(); ! while (e.hasMoreElements()) ! { ! Provider pr = (Provider) e.nextElement(); ! _log.info("stopping: " + pr); ! pr.destroy(); ! } ! _providers.clear(); ! _log.info("stopping clock"); ! Clock.stopClient(); ! _ls.flush(); ! BROKERS.remove(this._key); } --- 856,968 ---- /** ! * 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()); ! } ! /** ! * Backwards compatible, calls get(String,String) ! * @deprecated call get(String,String) instead ! */ ! public final Object getValue (String type, String query) ! throws ResourceException ! { ! return get(type, query); } Index: Context.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/Context.java,v retrieving revision 1.61 retrieving revision 1.62 diff -C2 -d -r1.61 -r1.62 *** Context.java 12 Jun 2003 00:47:43 -0000 1.61 --- Context.java 16 Jul 2003 06:44:59 -0000 1.62 *************** *** 24,36 **** package org.webmacro; import org.webmacro.engine.EvaluationExceptionHandler; import org.webmacro.engine.FunctionCall; import org.webmacro.engine.MethodWrapper; - import org.webmacro.util.Pool; - import org.webmacro.util.Settings; - import org.webmacro.util.SubSettings; - - import java.lang.reflect.Constructor; - import java.util.*; /** --- 24,32 ---- package org.webmacro; + import java.util.*; + import org.webmacro.engine.EvaluationExceptionHandler; import org.webmacro.engine.FunctionCall; import org.webmacro.engine.MethodWrapper; /** *************** *** 59,72 **** public class Context implements Map, Cloneable { ! private Broker _broker; ! private HashMap _tools = new HashMap(); ! private HashMap _funcs = new HashMap(); ! private Log _log; - private HashMap _initializedTools = new HashMap(); private EvaluationExceptionHandler _eeHandler; private Map _variables = new HashMap(); - private Pool _contextPool = null; private TemplateEvaluationContext _teContext --- 55,65 ---- public class Context implements Map, Cloneable { ! private final Broker _broker; ! private final Log _log; ! private HashMap _funcs = null; // lazy initialization private EvaluationExceptionHandler _eeHandler; private Map _variables = new HashMap(); private TemplateEvaluationContext _teContext *************** *** 76,97 **** = org.webmacro.engine.UndefinedMacro.getInstance(); - // Adding new tools to the context - private static final Class[] _ctorArgs1 = { - java.lang.String.class, - org.webmacro.util.Settings.class - }; - private static final Class[] _ctorArgs2 = {java.lang.String.class}; - private org.webmacro.profile.Profile _prof = null; /** ! * Create a new Context relative to the supplied broker */ ! public Context (Broker broker) ! { ! this(broker, true); } ! protected Context (Broker broker, boolean loadTools) { _prof = broker.newProfile(); --- 69,85 ---- = org.webmacro.engine.UndefinedMacro.getInstance(); private org.webmacro.profile.Profile _prof = null; /** ! * Create a new Context relative to the default WM instance */ ! public Context() throws InitException { ! this(Broker.getBroker()); } ! /** ! * Create a new Context relative to the supplied broker ! */ ! public Context (Broker broker) { _prof = broker.newProfile(); *************** *** 106,111 **** _broker = broker; _log = broker.getLog("context", "property and evaluation errors"); - if (loadTools) - loadTools("ContextTools"); if (_prof != null) { --- 94,97 ---- *************** *** 123,151 **** } - private class SettingHandler extends Settings.ListSettingHandler - { - public void processSetting (String settingKey, String settingValue) - { - try - { - addTool(settingKey, settingValue, "Tool"); - } - catch (Exception e) - { - _log.error("Provider (" + settingValue + ") failed to load", e); - } - } - } - - /** - * 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) - { - _broker.getSettings().processListSetting(keyName, new SettingHandler()); - } - /** * See cloneContext(). Subclasses should override cloneContext() --- 109,112 ---- *************** *** 179,183 **** c._prof = _broker.newProfile(); c.startTiming("Context life"); // stops in clear() - c._initializedTools = (HashMap) _initializedTools.clone(); c._teContext = new TemplateEvaluationContext(); if (_variables instanceof HashMap) --- 140,143 ---- *************** *** 198,205 **** /** * Clear the context so that it can be used for another request. - * This does not meant hat the context is completely empty: it - * may have been configured with some initial state, such as - * a collection of tools, that are to be re-used. But all local - * variables and other local structures will be cleared. * <p> * Subclasses may override the clear method and add functionality --- 158,161 ---- *************** *** 209,220 **** { _variables.clear(); - Iterator i = _initializedTools.entrySet().iterator(); - while (i.hasNext()) - { - Map.Entry m = (Map.Entry) i.next(); - ContextTool ct = (ContextTool) m.getKey(); - ct.destroy(m.getValue()); - } - _initializedTools.clear(); _eeHandler = null; if (_prof != null) --- 165,168 ---- *************** *** 303,307 **** if (ret == null && !_variables.containsKey(name)) { ! Object tool = _tools.get(name); if (tool != null) { --- 251,255 ---- if (ret == null && !_variables.containsKey(name)) { ! Object tool = _broker.getTool(name); if (tool != null) { *************** *** 311,315 **** ret = ct.init(this); put(name, ret); - _initializedTools.put(ct, ret); } catch (PropertyException e) --- 259,262 ---- *************** *** 322,326 **** FunctionCall fc = (FunctionCall) name; String fname = fc.getName(); ! MethodWrapper func = (MethodWrapper) _funcs.get(fname); if (func == null) { --- 269,276 ---- FunctionCall fc = (FunctionCall) name; String fname = fc.getName(); ! MethodWrapper func = null; ! if (_funcs != null) { ! func = (MethodWrapper) _funcs.get(fname); ! } if (func == null) { *************** *** 356,360 **** try { - //return internalGet(name); Object o = internalGet(name); if (o == UNDEF) --- 306,309 ---- *************** *** 395,398 **** --- 344,349 ---- { MethodWrapper func = wrapMethod(instance, methodName); + if (_funcs == null) + _funcs = new HashMap(); _funcs.put(name, func); } *************** *** 541,588 **** } - private static String makeName (Object[] names) - { - StringBuffer buf = new StringBuffer(); - buf.append("$("); - for (int i = 0; i < names.length; i++) - { - if (i != 0) - { - buf.append("."); - } - buf.append((names[i] != null) ? names[i] : "NULL"); - } - buf.append(")"); - return buf.toString(); - } - - /** - * Assign the object pool that this context should return to - * when its recycle() method is called. - */ - public final void setPool (Pool contextPool) - { - _contextPool = contextPool; - } - - public final Pool getPool () - { - return _contextPool; - } - - /** - * Return the context to the object pool assigned via setPool(), - * if any. This method implicitly calls clear(). - */ - public final void recycle () - { - clear(); - if (_contextPool != null) - { - _contextPool.put(this); - } - } - - /** * Set the underlying Map object. The supplied Map will subsequently --- 492,495 ---- *************** *** 674,791 **** } - /** - * Attempts to instantiate the tool using three different constructors - * until one succeeds, in the following order: - * <ul> - * <li>new MyContextTool(String key, Settings settings)</li> - * <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! - */ - private void addTool (String key, String className, String suffix) - { - Class c; - try - { - c = _broker.classForName(className); - } - catch (ClassNotFoundException e) - { - _log.warning("Context: Could not locate class for context tool " - + className); - return; - } - if (key == null || key.equals("")) - { - key = className; - int start = 0; - int end = key.length(); - int lastDot = key.lastIndexOf('.'); - if (lastDot != -1) - { - start = lastDot + 1; - } - if (key.endsWith(suffix)) - { - end -= suffix.length(); - } - key = key.substring(start, end); - } - - Constructor ctor = null; - Constructor[] ctors = c.getConstructors(); - Class[] parmTypes = null; - Object instance = null; - - // check for 2 arg constructor - for (int i = 0; i < ctors.length; i++) - { - parmTypes = ctors[i].getParameterTypes(); - if (parmTypes.length == 2 - && parmTypes[0].equals(_ctorArgs1[0]) - && parmTypes[1].equals(_ctorArgs1[1])) - { - ctor = ctors[i]; - Object[] args = {key, new SubSettings(_broker.getSettings(), key)}; - try - { - instance = ctor.newInstance(args); - } - catch (Exception e) - { - _log.error("Failed to instantiate tool " - + key + " of class " + className + " using constructor " - + ctor.toString(), e); - } - } - } - if (instance == null) - { - // check for 1 arg constructor - for (int i = 0; i < ctors.length; i++) - { - parmTypes = ctors[i].getParameterTypes(); - if (parmTypes.length == 1 && parmTypes[0].equals(_ctorArgs1[0])) - { - ctor = ctors[i]; - Object[] args = {key}; - try - { - instance = ctor.newInstance(args); - } - catch (Exception e) - { - _log.error("Failed to instantiate tool " - + key + " 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 " + key + " of class " + className, e); - return; - } - } - _tools.put(key, instance); - _log.info("Registered ContextTool " + key); - } - ////////////////////////////////////////////////////////////// --- 581,585 ---- Index: ContextTool.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/ContextTool.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** ContextTool.java 12 Jun 2003 00:47:44 -0000 1.8 --- ContextTool.java 16 Jul 2003 06:44:59 -0000 1.9 *************** *** 40,49 **** public Object init (Context c) throws PropertyException; - /** - * At the end of processing this method will be called to - * return the object generated by init(), in case it needs - * to be recycled or otherwise cleaned up. - */ - public void destroy (Object o); - } --- 40,42 ---- Index: FastWriter.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/FastWriter.java,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** FastWriter.java 8 Jul 2003 19:48:44 -0000 1.30 --- FastWriter.java 16 Jul 2003 06:44:59 -0000 1.31 *************** *** 24,36 **** package org.webmacro; import org.webmacro.util.ByteBufferOutputStream; import org.webmacro.util.Encoder; import org.webmacro.util.EncoderProvider; - import org.webmacro.util.Pool; - - import java.io.*; - import java.util.ArrayList; - import java.util.Hashtable; - import java.util.List; --- 24,32 ---- package org.webmacro; + import java.io.*; + import org.webmacro.util.ByteBufferOutputStream; import org.webmacro.util.Encoder; import org.webmacro.util.EncoderProvider; *************** *** 42,59 **** * call reset(). You can access the output by one of several * methods: toString(), toByteArray(), or writeTo(OutputStream) - * <li> you can turn off unicode conversion by calling setAsciiHack() * <li> you can use a unicode conversion cache by calling writeStatic() * <li> you can get the contents written to the FastWriter back * as an array of bytes INSTEAD of writing to the output stream * </ul> - * Note that if you turn on the asciiHack and then write non-ASCII - * data the output will be mangled. * <p> * <b>Note that the FastWriter requires an explicit flush</b> * <p> - * If you re-use a FastWriter you must re-use it in a context which - * uses the SAME unicode conversion. The caches and internal data - * structures which the FastWriter allocates are tied to the - * encoding it was created with. * * @author Marcel Huijkman --- 38,48 ---- *************** *** 89,93 **** private final int DEFAULT_BUFFER_SIZE; - private final int MAX_POOL_SIZE; private final String _encoding; // what encoding we use private final Writer _bwriter; --- 78,81 ---- *************** *** 97,114 **** private OutputStream _out; - /** _open is true iff the FW has been dispensed to a user and not - * returned to the pool. */ - private boolean _open = true; - - private byte[] _buf = new byte[512]; - private char[] _cbuf = new char[512]; - - private boolean _encodeProperly; // are we in fast mode? private boolean _buffered; - private static final Hashtable WRITERCACHE = new Hashtable(); - private Pool _myPool = null; - /** * Create a FastWriter to the target outputstream. You must specify --- 85,91 ---- *************** *** 119,123 **** throws UnsupportedEncodingException { - MAX_POOL_SIZE = broker.getSettings().getIntegerSetting("FastWriter.MaxPoolSize", 10); DEFAULT_BUFFER_SIZE = broker.getSettings().getIntegerSetting("FastWriter.DefaultBufferSize", 4096); _encoding = hackEncoding(encoding); --- 96,99 ---- *************** *** 135,139 **** } - _encodeProperly = true; _buffered = false; --- 111,114 ---- *************** *** 199,228 **** /** - * Ordinarily an expensive char-to-byte routine is used to convert - * strings and char[]'s to byte format. If you know that your data - * is going to be ASCII only for some number of writes, turn on - * this AsciiHack and then write the ASCII data. It's much faster. - * Remember to turn the AsciiHack off before writing true Unicode - * characters, otherwise they'll be mangled. - */ - public void setAsciiHack (boolean on) - { - if (_buffered) - { - bflush(); - } - _encodeProperly = !on; - } - - /** - * Returns true if we are mangling the unicode conversion in an - * attempt to eek out a bit of extra efficiency. - */ - public boolean getAsciiHack () - { - return !_encodeProperly; - } - - /** * Write characters to the output stream performing slow unicode * conversion unless AsciiHack is on. --- 174,177 ---- *************** *** 230,294 **** public void write (char[] cbuf) throws java.io.IOException { ! if (_encodeProperly) ! { ! _bwriter.write(cbuf, 0, cbuf.length); ! _buffered = true; ! } ! else ! { ! int len = cbuf.length; ! if (_buf.length < len) ! { ! _buf = new byte[len]; ! } ! for (int i = 0; i < len; i++) ! { ! _buf[i] = (byte) cbuf[i]; ! } ! _bstream.write(_buf, 0, len); ! } } /** * Write characters to to the output stream performing slow unicode ! * conversion unless the AsciiHack is on. */ public void write (char[] cbuf, int offset, int len) throws java.io.IOException { ! if (_encodeProperly) ! { ! _bwriter.write(cbuf, offset, len); ! _buffered = true; ! } ! else ! { ! if (_buf.length < len) ! { ! _buf = new byte[len]; ! } ! int end = offset + len; ! for (int i = offset; i < end; i++) ! { ! _buf[i] = (byte) cbuf[i]; ! } ! _bstream.write(_buf, 0, len); ! } } /** * Write a single character, performing slow unicode conversion - * unless AsciiHack is on. */ public void write (int c) throws java.io.IOException { ! if (_encodeProperly) ! { ! _bwriter.write(c); ! _buffered = true; ! } ! else ! { ! _bstream.write((byte) c); ! } } --- 179,203 ---- public void write (char[] cbuf) throws java.io.IOException { ! _bwriter.write(cbuf, 0, cbuf.length); ! _buffered = true; } /** * Write characters to to the output stream performing slow unicode ! * conversion. */ public void write (char[] cbuf, int offset, int len) throws java.io.IOException { ! _bwriter.write(cbuf, offset, len); ! _buffered = true; } /** * Write a single character, performing slow unicode conversion */ public void write (int c) throws java.io.IOException { ! _bwriter.write(c); ! _buffered = true; } *************** *** 310,330 **** } ! if (_encodeProperly) ! { ! _bwriter.write(_cbuf, 0, len); ! _buffered = true; ! } ! else ! { ! if (_buf.length < len) ! { ! _buf = new byte[len]; ! } ! for (int i = 0; i < len; i++) ! { ! _buf[i] = (byte) _cbuf[i]; ! } ! _bstream.write(_buf, 0, len); ! } } --- 219,224 ---- } ! _bwriter.write(_cbuf, 0, len); ! _buffered = true; } *************** *** 345,365 **** } ! if (_encodeProperly) ! { ! _bwriter.write(_cbuf, 0, len); ! _buffered = true; ! } ! else ! { ! if (_buf.length < len) ! { ! _buf = new byte[len]; ! } ! for (int i = 0; i < len; i++) ! { ! _buf[i] = (byte) _cbuf[i]; ! } ! _bstream.write(_buf, 0, len); ! } } --- 239,244 ---- } ! _bwriter.write(_cbuf, 0, len); ! _buffered = true; } *************** *** 520,524 **** _bstream.reset(); _out = out; - _open = true; } --- 399,402 ---- *************** *** 531,545 **** throws UnsupportedEncodingException { - FastWriter fw; - Pool p = (Pool) WRITERCACHE.get(encoding); - if (p != null) - { - fw = (FastWriter) p.get(); - if (fw != null) - { - fw.reset(out); - return fw; - } - } return new FastWriter(broker, out, encoding); } --- 409,412 ---- *************** *** 580,589 **** } - /** - * Return the FastWriter to the queue for later re-use. You must - * not use the FastWriter after this call. Calling close() - * returns the FastWriter to the pool. If you don't want to - * return it to the pool just discard it without a close(). - */ public void close () throws IOException { --- 447,450 ---- *************** *** 594,655 **** _out = null; } - if (_open) - { - _open = false; - if (_myPool == null) - { - // get/create the pool this FW should be using - _myPool = (Pool) WRITERCACHE.get(_encoding); - if (_myPool == null) - { - _myPool = new FWPool(MAX_POOL_SIZE); - WRITERCACHE.put(_encoding, _myPool); - } - } - _myPool.put(this); - } } } - /** - * A simple <b>synchronized</b> pool used only by FastWriter - */ - class FWPool implements Pool - { - - private final List _pool; - private final int _maxSize; - private volatile int _size = 0; - - FWPool (int maxSize) - { - _maxSize = maxSize; - _pool = new ArrayList(maxSize); - } - - public Object get () - { - Object obj = null; - synchronized (_pool) - { - if (_size > 0) - { - obj = _pool.remove(0); - _size--; - } - } - return obj; - } - - public void put (Object fw) - { - if (_size < _maxSize) - { // not rqrd to be t.s. perfect - synchronized (_pool) - { - _pool.add(fw); - _size++; - } - } - } - } --- 455,459 ---- Index: Template.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/Template.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** Template.java 8 Jul 2003 19:48:44 -0000 1.15 --- Template.java 16 Jul 2003 06:44:59 -0000 1.16 *************** *** 99,106 **** throws PropertyException, IOException; ! public String getString(Context context) throws PropertyException; ! public byte[] getBytes(String encoding, Context context) throws PropertyException; } --- 99,106 ---- throws PropertyException, IOException; ! public String evaluateAsString(Context context) throws PropertyException; ! public byte[] evaluateAsBytes(String encoding, Context context) throws PropertyException; } Index: WM.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/WM.java,v retrieving revision 1.42 retrieving revision 1.43 diff -C2 -d -r1.42 -r1.43 *** WM.java 8 Jul 2003 19:48:44 -0000 1.42 --- WM.java 16 Jul 2003 06:44:59 -0000 1.43 *************** *** 24,38 **** package org.webmacro; ! import org.webmacro.servlet.ServletBroker; ! import org.webmacro.servlet.WebContext; ! import org.webmacro.util.Pool; ! import org.webmacro.util.ScalablePool; ! import javax.servlet.Servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; ! import java.io.OutputStream; ! import java.io.UnsupportedEncodingException; ! import java.util.Properties; --- 24,35 ---- package org.webmacro; ! import java.io.*; ! import java.util.*; import javax.servlet.Servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; ! ! import org.webmacro.servlet.ServletBroker; ! import org.webmacro.servlet.WebContext; *************** *** 49,68 **** { - final private Context _context; - private WebContext _webContext = null; - // INIT METHODS--MANAGE ACCESS TO THE BROKER final private Broker _broker; // cache for rapid access - private boolean _alive = false; // so we don't unload twice - final private Provider _tmplProvider; final private Provider _urlProvider; final private Log _log; - final private ThreadLocal _contextCache; - final private ThreadLocal _webContextCache; - /** --- 46,57 ---- *************** *** 130,153 **** _broker = broker; - _alive = true; _log = _broker.getLog("wm", "WebMacro instance lifecycle"); ! _log.notice("new " + this ! + " v" + WebMacro.VERSION); ! _context = new Context(_broker); ! _contextCache = new ThreadLocal() ! { ! public Object initialValue () ! { ! return new ScalablePool(); ! } ! }; ! _webContext = new WebContext(_broker); ! _webContextCache = new ThreadLocal() ! { ! public Object initialValue () ! { ! return new ScalablePool(); ! } ! }; try --- 119,124 ---- _broker = broker; _log = _broker.getLog("wm", "WebMacro instance lifecycle"); ! _log.notice("new " + this + " v" + WebMacro.VERSION); try *************** *** 168,193 **** } - /** - * Call this method when you are finished with WebMacro. If you - * don't call this method, the Broker and all of WebMacro's caches - * may not be properly shut down, potentially resulting in loss of - * data, and wasted memory. This method is called in the - * finalizer, but it is best to call it as soon as you know you - * are done with WebMacro. - * <p> - * After a call to destroy() attempts to use this object may yield - * unpredicatble results. - */ - final public void destroy () - { - if (_alive) - { - _alive = false; - _webContext = null; - _log.info("shutdown " + this); - _broker.stopClient(); - } - } - public String toString () { --- 139,142 ---- *************** *** 195,226 **** } - /** - * This message returns false until you destroy() this object, - * subsequently it returns true. Do not attempt to use this object - * after it has been destroyed. */ - final public boolean isDestroyed () - { - return !_alive; - } - - - /** - * You should never call this method, on any object. Leave it up - * to the garbage collector. If you want to shut this object down, - * call destroy() instead. If you subclass this message, be sure - * to call super.finalize() since this is one of the cases where - * it matters. */ - protected void finalize () throws Throwable - { - try - { - destroy(); - } - finally - { - super.finalize(); - } - } - /** --- 144,147 ---- *************** *** 260,301 **** /** ! * Instantiate a new context from a pool. This method is more ! * efficient, in terms of object creation, than creating a ! * Context directly. The Context will return to the pool ! * when Context.recycle() is called. */ final public Context getContext () { ! Pool cpool = (Pool) _contextCache.get(); ! Context c = (Context) cpool.get(); ! if (c == null) ! { ! c = (Context) _context.clone(); ! c.setPool(cpool); ! } ! else ! c.clear(); ! return c; } /** ! * Instantiate a new webcontext from a pool. This method is more ! * efficient, in terms of object creation, than creating a ! * WebContext object directly. The WebContext will return to ! * the pool when WebContext.recycle() is called. */ final public WebContext getWebContext (HttpServletRequest req, HttpServletResponse resp) { ! Pool cpool = (Pool) _webContextCache.get(); ! WebContext c = (WebContext) cpool.get(); ! if (c == null) ! { ! c = _webContext.newInstance(req, resp); ! c.setPool(cpool); ! } ! else ! c.reinitialize(req, resp); ! return c; } --- 181,198 ---- /** ! * Instantiate a new context. */ final public Context getContext () { ! return new Context(_broker); } /** ! * Instantiate a new webcontext. */ final public WebContext getWebContext (HttpServletRequest req, HttpServletResponse resp) { ! return new WebContext(_broker, req, resp); } Index: WebMacro.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/WebMacro.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** WebMacro.java 12 Jun 2003 00:47:44 -0000 1.15 --- WebMacro.java 16 Jul 2003 06:44:59 -0000 1.16 *************** *** 50,71 **** /** - * Call this method when you are finished with WebMacro. If you don't - * call this method, the Broker and all of WebMacro's caches may not - * be properly shut down, potentially resulting in loss of data, and - * wasted memory. This method is called in the finalizer, but it is - * best to call it as soon as you know you are done with WebMacro. - * <p> After a call to destroy() attempts to use this object may yield - * unpredicatble results. - */ - public void destroy (); - - /** - * This message returns false until you destroy() this object, - * subsequently it returns true. Do not attempt to use this object - * after it has been destroyed. - */ - public boolean isDestroyed (); - - /** * This object is used to access components that have been plugged * into WebMacro; it is shared between all instances of this class and --- 50,53 ---- *************** *** 92,95 **** --- 74,78 ---- * @throws java.io.UnsupportedEncodingException if the encoding type * specified is not supported by your JVM. + * @deprecated */ public FastWriter getFastWriter (OutputStream out, String enctype) --- FilterTool.java DELETED --- |