You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2004/02/12 19:13:21 UTC

cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/servlet ServletToolboxManager.java

nbubna      2004/02/12 10:13:21

  Modified:    src/java/org/apache/velocity/tools/view/servlet
                        ServletToolboxManager.java
  Log:
  fix session tool init synchronization problems (http://marc.theaimsgroup.com/?t=107541485900001&r=1&w=2)
  
  Revision  Changes    Path
  1.10      +36 -14    jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java
  
  Index: ServletToolboxManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ServletToolboxManager.java	6 Nov 2003 00:26:54 -0000	1.9
  +++ ServletToolboxManager.java	12 Feb 2004 18:13:21 -0000	1.10
  @@ -359,30 +359,26 @@
           if (!sessionToolInfo.isEmpty())
           {
               HttpSession session = ctx.getRequest().getSession(createSession);
  -
  -            if (session != null) {
  -                //synchronize session tool initialization to avoid potential
  -                //conflicts from multiple simultaneous requests in the same session
  -                synchronized(session)
  +            if (session != null)
  +            {
  +                // allow only one thread per session at a time
  +                synchronized(getMutex(session))
                   {
  -                    //get the initialized session tools
  +                    // get the session tools
                       Map stmap = (Map)session.getAttribute(SESSION_TOOLS_KEY);
  -
  -                    //if session tools aren't initialized,
  -                    //do so and store them in the session
                       if (stmap == null)
                       {
  +                        // init and store session tools map
                           stmap = new HashMap(sessionToolInfo.size());
                           Iterator i = sessionToolInfo.iterator();
                           while(i.hasNext())
                           {
  -                            ToolInfo info = (ToolInfo)i.next();
  -                            stmap.put(info.getKey(), info.getInstance(ctx));
  +                            ToolInfo ti = (ToolInfo)i.next();
  +                            stmap.put(ti.getKey(), ti.getInstance(ctx));
                           }
                           session.setAttribute(SESSION_TOOLS_KEY, stmap);
                       }
  -
  -                    //add the initialized session tools to the toolbox
  +                    // add them to the toolbox
                       toolbox.putAll(stmap);
                   }
               }
  @@ -399,5 +395,31 @@
           return new ToolboxContext(toolbox);
       }
   
  +
  +    /**
  +     * Returns a mutex (lock object) unique to the specified session 
  +     * to allow for reliable synchronization on the session.
  +     */
  +    protected Object getMutex(HttpSession session)
  +    {
  +        // yes, this uses double-checked locking, but it is safe here
  +        // since partial initialization of the lock is not an issue
  +        Object lock = session.getAttribute("session.mutex");
  +        if (lock == null)
  +        {
  +            // one thread per toolbox manager at a time
  +            synchronized(this)
  +            {
  +                // in case another thread already came thru
  +                lock = session.getAttribute("session.mutex");
  +                if (lock == null)
  +                {
  +                    lock = new Object();
  +                    session.setAttribute("session.mutex", lock);
  +                }
  +            }
  +        }
  +        return lock;
  +    }
   
   }
  
  
  

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