nbubna      2003/01/23 21:04:51

  Modified:    view/src/java/org/apache/velocity/tools/view
                        XMLToolboxManager.java
               view/src/java/org/apache/velocity/tools/view/servlet
                        ServletToolboxManager.java
  Log:
  allow developers to configure whether or not the toolbox manager creates a session 
on its own to store session tools
  
  Revision  Changes    Path
  1.2       +34 -19    
jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/XMLToolboxManager.java
  
  Index: XMLToolboxManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/XMLToolboxManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLToolboxManager.java    10 May 2002 05:42:18 -0000      1.1
  +++ XMLToolboxManager.java    24 Jan 2003 05:04:51 -0000      1.2
  @@ -204,32 +204,47 @@
           Document document = new SAXReader().read(input);
           List elements = document.selectNodes("//"+BASE_NODE+"/*");
   
  +        int elementsRead = 0;
           Iterator i = elements.iterator();
           while(i.hasNext())
           {
               Element e = (Element)i.next();
  -            String name = e.getName();
  -            
  -            ToolInfo info;
  -
  -            if (name.equalsIgnoreCase(ELEMENT_TOOL))
  -            {
  -                info = readToolInfo(e);
  -            }
  -            else if (name.equalsIgnoreCase(ELEMENT_DATA)) 
  -            {
  -                info = readDataInfo(e);
  -            }
  -            else 
  -            {
  -                throw new InvalidSchemaException("Unknown element: "+name);
  +            if (readElement(e)) {
  +                elementsRead++;
               }
  +        }
  +
  +        log("Toolbox loaded.  Read "+elementsRead+" elements.");
  +    }
  +
  +
  +    /**
  +     * Delegates the reading of an element's ToolInfo
  +     * and adds the returned instance to the tool list.
  +     */
  +    protected boolean readElement(Element e) throws Exception
  +    {
  +        String name = e.getName();
   
  -            addTool(info);
  -            log("Added "+info.getClassname()+" as "+info.getKey());
  +        ToolInfo info = null;
  +
  +        if (name.equalsIgnoreCase(ELEMENT_TOOL))
  +        {
  +            info = readToolInfo(e);
  +        }
  +        else if (name.equalsIgnoreCase(ELEMENT_DATA)) 
  +        {
  +            info = readDataInfo(e);
  +        }
  +        else 
  +        {
  +            log("Could not read element: "+name);
  +            return false;
           }
   
  -        log("Toolbox loaded.");
  +        addTool(info);
  +        log("Added "+info.getClassname()+" as "+info.getKey());
  +        return true;
       }
   
   
  
  
  
  1.4       +112 -21   
jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java
  
  Index: ServletToolboxManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/view/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletToolboxManager.java        10 May 2002 05:42:18 -0000      1.3
  +++ ServletToolboxManager.java        24 Jan 2003 05:04:51 -0000      1.4
  @@ -135,6 +135,10 @@
       // --------------------------------------------------- Properties ---------
   
       public static final String ELEMENT_SCOPE = "scope";
  +    public static final String ELEMENT_CREATE_SESSION = "create-session";
  +
  +    public static final String VALUE_YES  = "yes";
  +    public static final String VALUE_NO   = "no";
   
       public static final String SESSION_TOOLS_KEY = 
"org.apache.velocity.tools.view.tools.ServletToolboxManager.SessionTools";
   
  @@ -142,6 +146,7 @@
       private Map appTools;
       private ArrayList sessionToolInfo;
       private ArrayList requestToolInfo;
  +    private boolean createSession;
   
   
   
  @@ -156,6 +161,7 @@
           appTools = new HashMap();
           sessionToolInfo = new ArrayList();
           requestToolInfo = new ArrayList();
  +        createSession = true;
       }
   
   
  @@ -163,7 +169,30 @@
       // --------------------------------------------------- Methods ------------
   
       /**
  +     * Sets whether or not to create a new session when none exists for the
  +     * current request and session-scoped tools have been defined for this
  +     * toolbox.
  +     *
  +     * If true, then a call to {@link getToolboxContext} will 
  +     * create a new session if none currently exists for this request and
  +     * the toolbox has one or more session-scoped tools designed.
  +     *
  +     * If false, then a call to {@link getToolboxContext} will never
  +     * create a new session for the current request.
  +     * This effectively means that no session-scoped tools will be added to 
  +     * the ToolboxContext for a request that does not have a session object.
  +     *
  +     * The default value is true.
  +     */
  +    public void setCreateSession(boolean b)
  +    {
  +        createSession = b;
  +    }
  +
  +
  +    /**
        * Overrides XMLToolboxManager to log to the servlet context
  +     * and to Velocity's main log
        */
       protected void log(String s) 
       {
  @@ -172,6 +201,66 @@
   
   
       /**
  +     * Overrides XMLToolboxManager to handle the create-session element.
  +     */
  +    protected boolean readElement(Element e) throws Exception
  +    {
  +        String name = e.getName();
  +
  +        ToolInfo info = null;
  +
  +        if (name.equalsIgnoreCase(ELEMENT_TOOL))
  +        {
  +            info = readToolInfo(e);
  +        }
  +        else if (name.equalsIgnoreCase(ELEMENT_DATA)) 
  +        {
  +            info = readDataInfo(e);
  +        }
  +        else if (name.equalsIgnoreCase(ELEMENT_CREATE_SESSION))
  +        {
  +            readCreateSession(e);
  +            return true;
  +        }
  +        else
  +        {
  +            log("Could not read element: "+name);
  +            return false;
  +        }
  +
  +        addTool(info);
  +        log("Added "+info.getClassname()+" as "+info.getKey());
  +        return true;
  +    }
  +
  +
  +    /**
  +     * Reads the value for create-session.
  +     *
  +     * @see setCreateSession(boolean)
  +     */
  +    protected boolean readCreateSession(Element e) throws Exception
  +    {
  +        String csValue = e.getText();
  +        if (VALUE_YES.equalsIgnoreCase(csValue))
  +        {
  +            setCreateSession(true);
  +        }
  +        else if (VALUE_NO.equalsIgnoreCase(csValue))
  +        {
  +            setCreateSession(false);
  +        }
  +        else
  +        {
  +            log("Unknown value for create-session.  Valid options are 'yes' or 
'no'.");
  +            return false;
  +        }
  +        log("create-session is set to "+createSession);
  +        return true;
  +    }
  +
  +
  +    /**
        * Overrides XMLToolboxManager to read a {@link ServletToolInfo}
        * instead of a {@link org.apache.velocity.tools.view.ViewToolInfo}.
        */
  @@ -257,31 +346,33 @@
   
           if (!sessionToolInfo.isEmpty())
           {
  -            HttpSession session = ctx.getRequest().getSession();
  +            HttpSession session = ctx.getRequest().getSession(createSession);
   
  -            //synchronize session tool initialization to avoid potential
  -            //conflicts from multiple simultaneous requests in the same session
  -            synchronized(session)
  -            {
  -                //get the initialized 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)
  +            if (session != null) {
  +                //synchronize session tool initialization to avoid potential
  +                //conflicts from multiple simultaneous requests in the same session
  +                synchronized(session)
                   {
  -                    stmap = new HashMap(sessionToolInfo.size());
  -                    Iterator i = sessionToolInfo.iterator();
  -                    while(i.hasNext())
  +                    //get the initialized 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)
                       {
  -                        ToolInfo info = (ToolInfo)i.next();
  -                        stmap.put(info.getKey(), info.getInstance(ctx));
  +                        stmap = new HashMap(sessionToolInfo.size());
  +                        Iterator i = sessionToolInfo.iterator();
  +                        while(i.hasNext())
  +                        {
  +                            ToolInfo info = (ToolInfo)i.next();
  +                            stmap.put(info.getKey(), info.getInstance(ctx));
  +                        }
  +                        session.setAttribute(SESSION_TOOLS_KEY, stmap);
                       }
  -                    session.setAttribute(SESSION_TOOLS_KEY, stmap);
  -                }
   
  -                //add the initialized session tools to the toolbox
  -                toolbox.putAll(stmap);
  +                    //add the initialized session tools to the toolbox
  +                    toolbox.putAll(stmap);
  +                }
               }
           }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to