commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Albert Kwong <mandr...@yahoo.com>
Subject Re: [PATCH] [Jelly] JellyServlet.java and JellyContext.java
Date Wed, 23 Apr 2003 14:16:04 GMT
Sorry guys, the cache part (part 4) of the patch
propably wouldn't work correctly in a multi-user
environment.  How about adding a release() method in
the Script interface for releasing objects?

Albert

 --- Albert Kwong <mandr_ua@yahoo.com> 的郵件內容:>
Here's a patch that deals with the JellyServlet. 
> Hope
> you guys find it useful.
> 
>     1. Make session and servlet context accessible
> from the JellyContext.
> 
>     2. Flatten request, session and servlet context
> attributes so that they are directly accessible from
> the JellyContext. (e.g. ${user} == ${session.user})
> 
>     3. Used request.getServletPath() instead of
> request.getPathInfo() to retrieve script URL, so
> that
> the servlet can be associated with an extension in
> web.xml (e.g. *.jel -> invokes JellyServlet).  This
> is
> more consistent with the Struts and Velocity model.
> 
>     4. Fixed memory leakage in JellyServlet by
> caching
> scripts in JellyContext.
> 
>     Before this fix, the JVM fails to gc the memory
> allocated by JellyServlet, possibly due to some
> cross
> references between the Script and Context objects. 
> By
> caching scripts in JellyContext, an URL is compiled
> into Script only once when it is accessed the first
> time.  It fixes the memory leakage and at the same
> time enhances performance.
> 
>     A side effect of this fix is that the servlet
> will
> no longer load scripts automatically when they are
> changed.  It may make Jelly script developers a
> little
> unhappy ;)
> 
>     Alternatively, the script caching can be
> implemented in JellyServlet, and the [cached] Script
> object can be passed to the JellyContext via a
> public
> runScript (Script, ...) method.  This alternative
> has
> less impact on the JellyContext object's behavior. 
> I
> have tried this strategy but somehow it isn't
> working,
> so I gave up.
> 
>     Yet another alternative is to implement a
> release() method in Script to clean up the mess
> after
> it is done.  I haven't looked into this alternative
> closely though. Personally I would like to retain
> the
> performance enhancement through caching.
> 
> Albert
> 
>
_________________________________________________________
>
我的驕傲(容祖兒),合久必婚(李克勤),下一站...天后(Twins)...
> Yahoo! 鈴聲下載
> http://ringtone.yahoo.com.hk> Index:
>
jelly/src/java/org/apache/commons/jelly/JellyContext.java
>
===================================================================
> RCS file:
>
/home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/JellyContext.java,v
> retrieving revision 1.42
> diff -u -r1.42 JellyContext.java
> ---
> src/java/org/apache/commons/jelly/JellyContext.java
> 7 Mar 2003 13:17:54 -0000	1.42
> +++
> src/java/org/apache/commons/jelly/JellyContext.java
> 23 Apr 2003 09:20:44 -0000
> @@ -69,6 +69,7 @@
>  import java.util.Hashtable;
>  import java.util.Iterator;
>  import java.util.Map;
> +import java.util.HashMap;
>  
>  import org.apache.commons.jelly.parser.XMLParser;
>  import org.apache.commons.logging.Log;
> @@ -78,6 +79,7 @@
>  
>  /** <p><code>JellyContext</code> represents the
> Jelly context.</p>
>    *
> +  * @author <a
> href="mailto:albert@charcoalgeneration.com">Albert
> Kwong</a>
>    * @author <a
> href="mailto:jstrachan@apache.org">James
> Strachan</a>
>    * @version $Revision: 1.10 $
>    */
> @@ -595,6 +597,9 @@
>          }
>      }
>  
> +    /** Provide script caching based on URL. */
> +    protected static Map scriptCache = new HashMap
> ();
> +
>      /** 
>       * Parses the script from the given URL then
> compiles it and runs it.
>       * 
> @@ -602,7 +607,15 @@
>       */
>      public JellyContext runScript(URL url,
> XMLOutput output,
>                            boolean export, boolean
> inherit) throws JellyException {
> -        Script script = compileScript(url);
> +
> +        /* load script from cache */
> +        Script script = (Script) scriptCache.get
> (url);
> +        
> +        if (script == null) 
> +        {
> +            script = compileScript(url);
> +            scriptCache.put (url, script); // store
> script into cache
> +        }
>          
>          URL newJellyContextURL = null;
>          try {
> Index:
>
jelly/src/java/org/apache/commons/jelly/servlet/JellyServlet.java
>
===================================================================
> RCS file:
>
/home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/servlet/JellyServlet.java,v
> retrieving revision 1.4
> diff -u -r1.4 JellyServlet.java
> ---
>
src/java/org/apache/commons/jelly/servlet/JellyServlet.java
> 24 Jan 2003 10:04:33 -0000	1.4
> +++
>
src/java/org/apache/commons/jelly/servlet/JellyServlet.java
> 23 Apr 2003 09:20:44 -0000
> @@ -69,6 +69,7 @@
>  import java.net.MalformedURLException;
>  import java.net.URL;
>  
> +import javax.servlet.ServletContext;
>  import javax.servlet.ServletException;
>  import javax.servlet.ServletOutputStream;
>  import javax.servlet.http.HttpServlet;
> @@ -78,10 +79,13 @@
>  import org.apache.commons.jelly.JellyContext;
>  import org.apache.commons.jelly.JellyException;
>  import org.apache.commons.jelly.XMLOutput;
> +import javax.servlet.http.HttpSession;
> +import java.util.Enumeration;
>  
>  /**
>   * Servlet for handling display of Jelly-fied XML
> files. Modelled after VelocityServlet.
>   * 
> + * @author <a
> href="mailto:albert@charcoalgeneration.com">Albert
> Kwong</a>
>   * @author Kelvin Tan
>   * @version $Revision: 1.4 $
>   */
> @@ -96,6 +100,16 @@
>  	 */
>  	public static final String RESPONSE = "response";
>  
> +	/**
> +	 * The HTTP response object context key.
> +	 */
> +	public static final String SESSION = "session";
> +
> +	/**
> +	 * The HTTP response object context key.
> +	 */
> +	public static final String SERVLET = "servlet";
> +
>      protected void doGet(
>  		HttpServletRequest request,
>  		HttpServletResponse response)
> @@ -133,18 +147,52 @@
>  	}
>  
>  	/**
> +     * Creates the JellyContext and store the
> various servlet objects as variables.
> +     * Attributes of the request, session and
> servlet context objects are "flattened"
> +     * so that it is directly accessible from the
> JellyContext.
> +     *
>  	 * @see
>
org.apache.velocity.servlet.VelocityServlet#createContext
>  	 * @param req
>  	 * @param res
>  	 * @return
>  	 */
>  	protected JellyContext createContext(
> -		HttpServletRequest req,
> -		HttpServletResponse res) {
> +		HttpServletRequest request,
> +		HttpServletResponse response) {
> +
> +        HttpSession    session = request.getSession
> (false);
> +        ServletContext context = getServletContext
> ();
> +
> +		JellyContext ctx = new
> JellyServletContext(context);
> +
> +		ctx.setVariable(REQUEST, request);
> +		ctx.setVariable(RESPONSE, response);
> +		ctx.setVariable(SESSION, session);
> +        ctx.setVariable(SERVLET, context);
> +
> +        /* ordering is important here.  If the
> scopes has name clashes, the latter ones overrides.
> */
> +
> +        for (Enumeration iter =
> request.getAttributeNames();
> iter.hasMoreElements();)
> +        {
> +            String name = (String) iter.nextElement
> ();
> +            ctx.setVariable (name,
> request.getAttribute (name));
> +        }
> +
> +        if (session != null)
> +        {
> +            for (Enumeration iter =
> session.getAttributeNames();
> iter.hasMoreElements();)
> +            {
> +                String name = (String)
> iter.nextElement ();
> +                ctx.setVariable (name,
> session.getAttribute (name));
> +            }
> +        }
> +
> +        for (Enumeration iter =
> context.getAttributeNames();
> iter.hasMoreElements();)
> +        {
> +            String name = (String) iter.nextElement
> ();
> +            ctx.setVariable (name,
> context.getAttribute (name));
> +        }
>  
> -		JellyContext ctx = new
> JellyServletContext(getServletContext());
> -		ctx.setVariable(REQUEST, req);
> -		ctx.setVariable(RESPONSE, res);
>  		return ctx;
>  	}
> 
=== message truncated === 

_________________________________________________________
我的驕傲(容祖兒),合久必婚(李克勤),下一站...天后(Twins)...
Yahoo! 鈴聲下載
http://ringtone.yahoo.com.hk

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message