You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Leon Rosenberg <ro...@googlemail.com> on 2008/05/16 18:12:27 UTC

understanding some tomcat code

Hi,

 was just browsing through tomcat code, and the following is just a
mystery to me ->StandardSession.fireSessionsEvent(type,data):

   public void fireSessionEvent(String type, Object data) {
        if (listeners.size() < 1)
            return;
        SessionEvent event = new SessionEvent(this, type, data);
        SessionListener list[] = new SessionListener[0];
--------> WHY synchronized?
   synchronized (listeners) {
            list = (SessionListener[]) listeners.toArray(list);
        }

        for (int i = 0; i < list.length; i++){
            ((SessionListener) list[i]).sessionEvent(event);
        }

    }


can someone enlighten me? :-)


Another question, are there any plans to bring generics into tomcat
code?     /**
     * The session event listeners for this Session.
     */
    protected transient ArrayList<SessionListener> listeners = new
ArrayList<SessionListener>();
could make the code easier to understand, i would volunteer the work force.

regards
Leon

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: understanding some tomcat code

Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
Leon Rosenberg wrote:
> Hi,
>
>  was just browsing through tomcat code, and the following is just a
> mystery to me ->StandardSession.fireSessionsEvent(type,data):
>
>    public void fireSessionEvent(String type, Object data) {
>         if (listeners.size() < 1)
>             return;
>         SessionEvent event = new SessionEvent(this, type, data);
>         SessionListener list[] = new SessionListener[0];
> --------> WHY synchronized?
>   
probably someone reported a concurrent modification exception,

>    synchronized (listeners) {
>             list = (SessionListener[]) listeners.toArray(list);
>         }
>
>         for (int i = 0; i < list.length; i++){
>             ((SessionListener) list[i]).sessionEvent(event);
>         }
>
>     }
>
>
> can someone enlighten me? :-)
>
>
> Another question, are there any plans to bring generics into tomcat
> code?     /**
>      * The session event listeners for this Session.
>      */
>     protected transient ArrayList<SessionListener> listeners = new
> ArrayList<SessionListener>();
> could make the code easier to understand, i would volunteer the work force.
>   
its done here and there as we change code

FIlip
> regards
> Leon
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>   


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: understanding some tomcat code

Posted by Ho Fat Tsang <na...@gmail.com>.
On Sat, May 17, 2008 at 12:12 AM, Leon Rosenberg <
rosenberg.leon@googlemail.com> wrote:

> Hi,
>
>  was just browsing through tomcat code, and the following is just a
> mystery to me ->StandardSession.fireSessionsEvent(type,data):
>
>   public void fireSessionEvent(String type, Object data) {
>        if (listeners.size() < 1)
>            return;
>        SessionEvent event = new SessionEvent(this, type, data);
>        SessionListener list[] = new SessionListener[0];
> --------> WHY synchronized?
>   synchronized (listeners) {
>            list = (SessionListener[]) listeners.toArray(list);
>        }


It is kind of Snapshot Pattern i thought. Synchronize copy the
SessionListener list as toArray is not thread safety. Then further
synchronized is not required in the for loop below (as the list is a
snapshot of the original list). Thus any registration or removal of session
listener does not need to wait the complete of event firing in
StandardSession.


>
>
>        for (int i = 0; i < list.length; i++){
>            ((SessionListener) list[i]).sessionEvent(event);
>        }
>
>    }
>
>
> can someone enlighten me? :-)
>
>
> regards
> Leon
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
Twinsen