Example usage for org.apache.commons.chain.web WebContext getSessionScope

List of usage examples for org.apache.commons.chain.web WebContext getSessionScope

Introduction

In this page you can find the example usage for org.apache.commons.chain.web WebContext getSessionScope.

Prototype

public abstract Map getSessionScope();

Source Link

Document

Return a mutable Map that maps session scope attribute names to their values.

Usage

From source file:com.liulangf.pattern.gof.behavioral.chain.jakarta.simple.CountCommand.java

/**
 * <p>Execute the command.</p>
 *
 * @param context The {@link Context} we are operating on
 * @return <code>false</code> so that processng will continue
 * @throws Exception If an error occurs during execution.
 *//*from  ww w .  ja  v  a2 s . c om*/
@SuppressWarnings("unchecked")
public boolean execute(Context context) throws Exception {

    count++;
    log.info("Executing: " + attribute + "=" + count);

    WebContext webContext = (WebContext) context;
    webContext.getSessionScope().put(attribute, new Integer(count));

    return false;

}

From source file:org.apache.commons.chain.apps.example.CountCommand.java

/**
 * <p>Execute the command.</p>
 *
 * @param context The {@link Context} we are operating on
 * @return <code>false</code> so that processng will continue
 * @throws Exception If an error occurs during execution.
 *///  ww  w  .java  2 s  .  c  o m
public boolean execute(Context context) throws Exception {

    count++;
    log.info("Executing: " + attribute + "=" + count);

    WebContext webContext = (WebContext) context;
    webContext.getSessionScope().put(attribute, new Integer(count));

    return false;

}

From source file:org.apache.struts.chain.CreateActionForm.java

/**
 * <p>Create (if necessary) and cache a form bean for this request.</p>
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> so that processing continues
 *///from   w w  w . j  a  v  a 2  s .c  om
public boolean execute(Context context) throws Exception {

    // Is there a form bean associated with this ActionConfig?
    ActionConfig actionConfig = (ActionConfig) context.get(getActionConfigKey());
    String name = actionConfig.getName();
    if (name == null) {
        context.remove(getActionFormKey());
        return (false);
    }

    log.trace("Look up form-bean " + name);

    // Look up the corresponding FormBeanConfig (if any)
    FormBeanConfig formBeanConfig = actionConfig.getModuleConfig().findFormBeanConfig(name);
    if (formBeanConfig == null) {
        log.warn("No FormBeanConfig found in module " + actionConfig.getModuleConfig().getPrefix()
                + " under name " + name);
        context.remove(getActionFormKey());
        return (false);
    }

    // Look up the session scope ActionForm instance (if any)
    WebContext wcontext = (WebContext) context;
    ActionForm instance = null;
    if ("session".equals(actionConfig.getScope())) {
        instance = (ActionForm) wcontext.getSessionScope().get(actionConfig.getAttribute());
    }

    // Can we recycle the existing instance (if any)?
    if (instance != null) {
        log.trace("Found an instance in the session; test for reusability");
        if (formBeanConfig.getDynamic()) {
            String className = ((DynaBean) instance).getDynaClass().getName();
            if (className.equals(formBeanConfig.getName())) {
                wcontext.put(getActionFormKey(), instance);
                /* It should already be in session scope
                if ("session".equals(actionConfig.getScope())) {
                wcontext.getSessionScope().put
                    (actionConfig.getAttribute(), instance);
                }
                */
                log.debug("Using existing instance (dynamic)");
                return (false);
            }
        } else {
            try {
                Class configClass = ClassUtils.getApplicationClass(formBeanConfig.getType());
                if (configClass.isAssignableFrom(instance.getClass())) {
                    wcontext.put(getActionFormKey(), instance);
                    /* It should already be in session scope
                       if ("session".equals(actionConfig.getScope())) {
                       wcontext.getSessionScope().put
                       (actionConfig.getAttribute(), instance);
                       }
                    */
                    log.debug("Using existing instance (non-dynamic)");
                    return (false);
                }
            } catch (Exception e) {
                log.debug("Error testing existing instance for reusability; just create a new instance", e);
            }
        }
    }

    log.trace("Make a new instance of: " + formBeanConfig);
    // Create a new form bean instance
    if (formBeanConfig.getDynamic()) {
        ModuleConfig moduleConfig = (ModuleConfig) wcontext.get(getModuleConfigKey());
        DynaActionFormClass dynaClass = DynaActionFormClass.createDynaActionFormClass(formBeanConfig);
        instance = (ActionForm) dynaClass.newInstance();
        ((DynaActionForm) instance).initialize((ActionMapping) actionConfig);
    } else {
        instance = (ActionForm) ClassUtils.getApplicationInstance(formBeanConfig.getType());
    }

    // Configure and cache the new instance
    ActionServlet servlet = (ActionServlet) wcontext.get(getActionServletKey());
    instance.setServlet(servlet);
    wcontext.put(getActionFormKey(), instance);
    if ("session".equals(actionConfig.getScope())) {
        wcontext.getSessionScope().put(actionConfig.getAttribute(), instance);
    } else if ("request".equals(actionConfig.getScope())) {
        wcontext.getRequestScope().put(actionConfig.getAttribute(), instance);
    }

    return (false);

}