Code at your request:
(by the way, thi comes in handy lon larger scale projects, it auto 
cleans up sesion beans for your, so you can put more users per box).

V.

package com.rd;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;

/**
  *  [EMAIL PROTECTED]
  *
  *
  */
public abstract class ScopedAction extends Action {


        public static String TOKEN_KEY = ScopedAction.class.getName() + ".TOKEN";
        public static String ATTRIBUTES_KEY = ScopedAction.class.getName() + 
".ATTRIBUTES";

        /**
         *
         *
         */
        private void _init(ActionContext context) {
                HashMap map = new HashMap();
                context.setSessionAttribute(ATTRIBUTES_KEY, map);
                init(context);
        }


        /**
         *
         *
         */
        public abstract void init(ActionContext context);


        /**
         * Do any cleanup necessary within the session and application context.
         * The destroy event happens when the action is loosing
         * focus within the application.  Is is possible for the action to gain
         * control again, therefore, the destroy event may be called more than
         * once during an application.
         *
         */
        public abstract void destroy( ActionMapping map, HttpServletRequest 
req, HttpServletResponse res);

        /**
         *
         */
        protected void _destroy( ActionMapping map, HttpServletRequest req, 
HttpServletResponse res ) {
                req.getSession().removeAttribute(ATTRIBUTES_KEY);
                destroy( map, req, res );
        }

        /**
         *
         *
         *
         */
        public ActionForward execute( ActionMapping map, ActionForm form, 
HttpServletRequest req, HttpServletResponse res) {


                // create the context
                ActionContext context = new ActionContext();
                context.map  = map;
                context.form = form;
                context.req  = req;
                context.res  = res;

                /**
                 * call init if this is the first time since the last destroy
                 *
                 */
                if (beginScope(context)) {
                        
                        // tell the old action to destroy itself
                        ActionMapping oldScopeOwner =
                                (ActionMapping) context.getSessionAttribute(TOKEN_KEY);

                        if (oldScopeOwner != null) {
                                
                                String actionClassName = oldScopeOwner.getType();
                                
                                // we must instantiate the previous owner to invoke 
its destroy method
                                try {
                                        Class actionClass = 
Class.forName(actionClassName);
                                        Object subject = actionClass.newInstance();
                                        if (subject instanceof ScopedAction) {
                                                ((ScopedAction) 
subject)._destroy(oldScopeOwner, req, res);
                                        }
                                } catch (ClassNotFoundException e) {
                                        e.printStackTrace();
                                } catch (IllegalAccessException e) {
                                        e.printStackTrace();
                                } catch (InstantiationException e) {
                                        e.printStackTrace();
                                }
                        }

                        // take ownership the action scoping token
                        context.setSessionAttribute(TOKEN_KEY, map);
                        _init(context);
                }
                execute(context);
                return context.forward;
        }


        /**
         *
         */
        public abstract void execute(ActionContext context);


        /**
         * true if the current session is being handed off to this
         * action.  After the hand off, this action will own the "action
         * scope", i.e., be the action considered as current.
         *
         */
        private boolean beginScope(ActionContext context) {

                ActionMapping scopeOwner =
                        (ActionMapping) context.getSessionAttribute(TOKEN_KEY);

                // if there is no previous owner, we are initializing
                if ( scopeOwner == null )
                        return true;
                
                String lastAction = scopeOwner.getPath();
                String currentAction = context.map.getPath();
                System.out.println(lastAction + " " + currentAction);
                if (!currentAction.equals(lastAction))
                        return true;
                else
                        return false;
        }
}

ackage com.rd;

import java.util.*;

/**
  *
  *
  */
public class ActionContext {

        public org.apache.struts.action.ActionMapping map;
        public javax.servlet.http.HttpServletRequest req;
        public javax.servlet.http.HttpServletResponse res;
        public org.apache.struts.action.ActionForm form;
        public org.apache.struts.action.ActionForward forward;
        
        public Object getSessionAttribute(String key) {
                return req.getSession(true).getAttribute(key);
        }
        
        public void setSessionAttribute(String key, Object value) {
                req.getSession(true).setAttribute(key, value);
        }       

        /**
         *
         */
        public void setAttribute(String key, Object value) {
                HashMap hmap = (HashMap)req.getSession().getAttribute( 
ScopedAction.ATTRIBUTES_KEY );
                hmap.put(key,value);
        }

        /**
         *
         */
        public Object getAttribute(String key) {
                HashMap hmap = (HashMap)req.getSession().getAttribute( 
ScopedAction.ATTRIBUTES_KEY );
                return hmap.get(key);
        }

}


Hoang, Hai wrote:
> Vic,
> 
> This is exactly what I am talking about but is there any sample codes for
> this "token" sharing algorithm where I can use?
> 
> Thanks,
> 
> Hai
> 
> -----Original Message-----
> From: V. Cekvenich [mailto:[EMAIL PROTECTED]] 
> Sent: Friday, September 20, 2002 2:57 PM
> To: [EMAIL PROTECTED]
> Subject: Re: How to remove ActionForm with session scope from the session
> obje ct
> 
> http://www.mail-archive.com/mvc-programmers@basebeans.com/msg00124.html
> 
> Read part about 5th scope.
> 
> V.
> 
> Hoang, Hai wrote:
> 
>>I've a number of forms with session scope and I want to remove them from
> 
> the
> 
>>session object when I am no longer used them.  The trick is how to find
> 
> out
> 
>>when they are no longer being used?  For example, let say I've 10
>>actionForms associated with 10 jsp page.  If I am on page number 1
>>therefore, page 9-10 is not being used and I can just loop through it a
>>remove the corresponding actionForms from the session object.  If I am
> 
> page
> 
>>number 2 then I loop through page 1 and 8-10 and so on.  Is there a better
>>way for me to do this?  Thanks
>>
>>Hai Hoang
>>
>>
>>
>>_________________________________________________________________________
>>Introducing the all new and improved continental.com.  With a totally new 
>>personalized design, it's the best place to go. Before you go.
>>
>>Continental Airlines. Work Hard. Fly Right.
>>
>>http://www.continental.com
>>
>>
> 
> 
> 
> 
> 
> --
> To unsubscribe, e-mail:
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>




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

Reply via email to