Search in sources :

Example 1 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class ActionServlet method processFormBeanConfigClass.

/**
     * <p>Checks if the current beanConfig is using the correct class based on
     * the class of its ancestor form bean config.</p>
     *
     * @param beanConfig   The form bean to check.
     * @param moduleConfig The config for the current module.
     * @return The form bean config using the correct class as determined by
     *         the config's ancestor and its own overridden value.
     * @throws UnavailableException if an instance of the form bean config
     *                              class cannot be created.
     * @throws ServletException     on class creation error
     */
protected FormBeanConfig processFormBeanConfigClass(FormBeanConfig beanConfig, ModuleConfig moduleConfig) throws ServletException {
    String ancestor = beanConfig.getExtends();
    if (ancestor == null) {
        // Nothing to do, then
        return beanConfig;
    }
    // Make sure that this bean is of the right class
    FormBeanConfig baseConfig = moduleConfig.findFormBeanConfig(ancestor);
    if (baseConfig == null) {
        throw new UnavailableException("Unable to find " + "form bean '" + ancestor + "' to extend.");
    }
    // Was our bean's class overridden already?
    if (beanConfig.getClass().equals(FormBeanConfig.class)) {
        // Ensure that our bean is using the correct class
        if (!baseConfig.getClass().equals(beanConfig.getClass())) {
            // Replace the bean with an instance of the correct class
            FormBeanConfig newBeanConfig = null;
            String baseConfigClassName = baseConfig.getClass().getName();
            try {
                newBeanConfig = (FormBeanConfig) RequestUtils.applicationInstance(baseConfigClassName);
                // copy the values
                BeanUtils.copyProperties(newBeanConfig, beanConfig);
                FormPropertyConfig[] fpc = beanConfig.findFormPropertyConfigs();
                for (int i = 0; i < fpc.length; i++) {
                    newBeanConfig.addFormPropertyConfig(fpc[i]);
                }
            } catch (Exception e) {
                handleCreationException(baseConfigClassName, e);
            }
            // replace beanConfig with newBeanConfig
            moduleConfig.removeFormBeanConfig(beanConfig);
            moduleConfig.addFormBeanConfig(newBeanConfig);
            beanConfig = newBeanConfig;
        }
    }
    return beanConfig;
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) FormBeanConfig(org.apache.struts.config.FormBeanConfig) UnavailableException(javax.servlet.UnavailableException) ServletException(javax.servlet.ServletException) MissingResourceException(java.util.MissingResourceException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException)

Example 2 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class ActionServlet method initModuleFormBeans.

/**
     * <p>Initialize the form beans for the specified module.</p>
     *
     * @param config ModuleConfig information for this module
     * @throws ServletException if initialization cannot be performed
     * @since Struts 1.3
     */
protected void initModuleFormBeans(ModuleConfig config) throws ServletException {
    if (log.isDebugEnabled()) {
        log.debug("Initializing module path '" + config.getPrefix() + "' form beans");
    }
    // Process form bean extensions.
    FormBeanConfig[] formBeans = config.findFormBeanConfigs();
    for (int i = 0; i < formBeans.length; i++) {
        FormBeanConfig beanConfig = formBeans[i];
        processFormBeanExtension(beanConfig, config);
    }
    for (int i = 0; i < formBeans.length; i++) {
        FormBeanConfig formBean = formBeans[i];
        // Verify that required fields are all present for the form config
        if (formBean.getType() == null) {
            handleValueRequiredException("type", formBean.getName(), "form bean");
        }
        // ... and the property configs
        FormPropertyConfig[] fpcs = formBean.findFormPropertyConfigs();
        for (int j = 0; j < fpcs.length; j++) {
            FormPropertyConfig property = fpcs[j];
            if (property.getType() == null) {
                handleValueRequiredException("type", property.getName(), "form property");
            }
        }
        // for all dynamic form beans
        if (formBean.getDynamic()) {
            formBean.getDynaActionFormClass();
        }
    }
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) FormBeanConfig(org.apache.struts.config.FormBeanConfig)

Example 3 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class DynaActionForm method initialize.

// ----------------------------------------------------- ActionForm Methods
/**
     * <p>Initialize all bean properties to their initial values, as specified
     * in the {@link FormPropertyConfig} elements associated with the
     * definition of this <code>DynaActionForm</code>.</p>
     *
     * @param mapping The mapping used to select this instance
     */
public void initialize(ActionMapping mapping) {
    String name = mapping.getName();
    if (name == null) {
        return;
    }
    FormBeanConfig config = mapping.getModuleConfig().findFormBeanConfig(name);
    if (config == null) {
        return;
    }
    initialize(config);
}
Also used : FormBeanConfig(org.apache.struts.config.FormBeanConfig)

Example 4 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class CreateActionForm method execute.

// ---------------------------------------------------------- Public Methods
/**
     * <p>Create (if necessary) and cache a form bean for this request.</p>
     *
     * @param actionCtx The <code>Context</code> for the current request
     * @return <code>false</code> so that processing continues
     * @throws Exception on any error
     */
public boolean execute(ActionContext actionCtx) throws Exception {
    // Is there a form bean associated with this ActionConfig?
    ActionConfig actionConfig = actionCtx.getActionConfig();
    String name = actionConfig.getName();
    if (name == null) {
        actionCtx.setActionForm(null);
        return (false);
    }
    if (LOG.isTraceEnabled()) {
        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);
        actionCtx.setActionForm(null);
        return (false);
    }
    Map scope = actionCtx.getScope(actionConfig.getScope());
    ActionForm instance;
    instance = (ActionForm) scope.get(actionConfig.getAttribute());
    // Can we recycle the existing instance (if any)?
    if (!formBeanConfig.canReuse(instance)) {
        instance = formBeanConfig.createActionForm(actionCtx);
    }
    //  directly depends on ActionServlet
    if (actionCtx instanceof ServletActionContext) {
        // The servlet property of ActionForm is transient, so
        // ActionForms which are restored from a serialized state
        // need to have their servlet restored.
        ServletActionContext sac = (ServletActionContext) actionCtx;
        instance.setServlet(sac.getActionServlet());
    }
    actionCtx.setActionForm(instance);
    scope.put(actionConfig.getAttribute(), instance);
    return (false);
}
Also used : ActionConfig(org.apache.struts.config.ActionConfig) FormBeanConfig(org.apache.struts.config.FormBeanConfig) ActionForm(org.apache.struts.action.ActionForm) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) Map(java.util.Map)

Example 5 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class ActionContextBase method findOrCreateActionForm.

/**
     * <p> In the context of the given <code>ModuleConfig</code> and this
     * <code>ActionContext</code>, look for an existing
     * <code>ActionForm</code> in the specified scope. If one is found, return
     * it; otherwise, create a new instance, add it to that scope, and then
     * return it. </p>
     *
     * @param formName  The name attribute of our ActionForm
     * @param scopeName The scope identier (request, session)
     * @return The ActionForm for this request
     * @throws IllegalAccessException   If object cannot be created
     * @throws InstantiationException   If object cannot be created
     * @throws IllegalArgumentException If form config is missing from module
     *                                  or scopeName is invalid
     */
public ActionForm findOrCreateActionForm(String formName, String scopeName, ModuleConfig moduleConfig) throws IllegalAccessException, InstantiationException {
    Map scope = this.getScope(scopeName);
    ActionForm instance;
    FormBeanConfig formBeanConfig = moduleConfig.findFormBeanConfig(formName);
    if (formBeanConfig == null) {
        throw new IllegalArgumentException("No form config found under " + formName + " in module " + moduleConfig.getPrefix());
    }
    instance = (ActionForm) scope.get(formName);
    // ISSUE: Can we recycle the existing instance (if any)?
    if (instance != null) {
        getLogger().trace("Found an instance in scope " + scopeName + "; test for reusability");
        if (formBeanConfig.canReuse(instance)) {
            return instance;
        }
    }
    ActionForm form = formBeanConfig.createActionForm(this);
    // ISSUE: Should we check this call to put?
    scope.put(formName, form);
    return form;
}
Also used : FormBeanConfig(org.apache.struts.config.FormBeanConfig) ActionForm(org.apache.struts.action.ActionForm) Map(java.util.Map)

Aggregations

FormBeanConfig (org.apache.struts.config.FormBeanConfig)18 UnavailableException (javax.servlet.UnavailableException)5 FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)5 ServletException (javax.servlet.ServletException)4 ActionConfig (org.apache.struts.config.ActionConfig)4 ActionForm (org.apache.struts.action.ActionForm)3 Map (java.util.Map)2 ExceptionConfig (org.apache.struts.config.ExceptionConfig)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 MissingResourceException (java.util.MissingResourceException)1 StringTokenizer (java.util.StringTokenizer)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 JspException (javax.servlet.jsp.JspException)1 MockActionContext (org.apache.struts.chain.contexts.MockActionContext)1 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)1 ActionConfigMatcher (org.apache.struts.config.ActionConfigMatcher)1 ForwardConfig (org.apache.struts.config.ForwardConfig)1 MessageResourcesConfig (org.apache.struts.config.MessageResourcesConfig)1 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)1