Search in sources :

Example 1 with BeanValidatorForm

use of org.apache.struts.validator.BeanValidatorForm in project sonarqube by SonarSource.

the class FormBeanConfig method createActionForm.

// --------------------------------------------------------- Public Methods
/**
     * <p>Create and return an <code>ActionForm</code> instance appropriate to
     * the information in this <code>FormBeanConfig</code>.</p>
     *
     * <p>Although this method is not formally deprecated yet, where possible,
     * the form which accepts an <code>ActionContext</code> as an argument is
     * preferred, to help sever direct dependencies on the Servlet API.  As
     * the ActionContext becomes more familiar in Struts, this method will
     * almost certainly be deprecated.</p>
     *
     * @param servlet The action servlet
     * @return ActionForm instance
     * @throws IllegalAccessException if the Class or the appropriate
     *                                constructor is not accessible
     * @throws InstantiationException if this Class represents an abstract
     *                                class, an array class, a primitive type,
     *                                or void; or if instantiation fails for
     *                                some other reason
     */
public ActionForm createActionForm(ActionServlet servlet) throws IllegalAccessException, InstantiationException {
    Object obj = null;
    // Create a new form bean instance
    if (getDynamic()) {
        obj = getDynaActionFormClass().newInstance();
    } else {
        obj = formBeanClass().newInstance();
    }
    ActionForm form = null;
    if (obj instanceof ActionForm) {
        form = (ActionForm) obj;
    } else {
        form = new BeanValidatorForm(obj);
    }
    form.setServlet(servlet);
    if (form instanceof DynaBean && ((DynaBean) form).getDynaClass() instanceof MutableDynaClass) {
        DynaBean dynaBean = (DynaBean) form;
        MutableDynaClass dynaClass = (MutableDynaClass) dynaBean.getDynaClass();
        // Add properties
        dynaClass.setRestricted(false);
        FormPropertyConfig[] props = findFormPropertyConfigs();
        for (int i = 0; i < props.length; i++) {
            dynaClass.add(props[i].getName(), props[i].getTypeClass());
            dynaBean.set(props[i].getName(), props[i].initial());
        }
        dynaClass.setRestricted(isRestricted());
    }
    if (form instanceof BeanValidatorForm) {
        ((BeanValidatorForm) form).initialize(this);
    }
    return form;
}
Also used : DynaBean(org.apache.commons.beanutils.DynaBean) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm) BeanValidatorForm(org.apache.struts.validator.BeanValidatorForm) MutableDynaClass(org.apache.commons.beanutils.MutableDynaClass)

Example 2 with BeanValidatorForm

use of org.apache.struts.validator.BeanValidatorForm in project sonarqube by SonarSource.

the class FormBeanConfig method canReuse.

/**
     * <p>Checks if the given <code>ActionForm</code> instance is suitable for
     * use as an alternative to calling this <code>FormBeanConfig</code>
     * instance's <code>createActionForm</code> method.</p>
     *
     * @param form an existing form instance that may be reused.
     * @return true if the given form can be reused as the form for this
     *         config.
     */
public boolean canReuse(ActionForm form) {
    if (form != null) {
        if (this.getDynamic()) {
            String className = ((DynaBean) form).getDynaClass().getName();
            if (className.equals(this.getName())) {
                log.debug("Can reuse existing instance (dynamic)");
                return (true);
            }
        } else {
            try {
                // check if the form's class is compatible with the class
                //      we're configured for
                Class formClass = form.getClass();
                if (form instanceof BeanValidatorForm) {
                    BeanValidatorForm beanValidatorForm = (BeanValidatorForm) form;
                    if (beanValidatorForm.getInstance() instanceof DynaBean) {
                        String formName = beanValidatorForm.getStrutsConfigFormName();
                        if (getName().equals(formName)) {
                            log.debug("Can reuse existing instance (BeanValidatorForm)");
                            return true;
                        } else {
                            return false;
                        }
                    }
                    formClass = beanValidatorForm.getInstance().getClass();
                }
                Class configClass = ClassUtils.getApplicationClass(this.getType());
                if (configClass.isAssignableFrom(formClass)) {
                    log.debug("Can reuse existing instance (non-dynamic)");
                    return (true);
                }
            } catch (Exception e) {
                log.debug("Error testing existing instance for reusability; just create a new instance", e);
            }
        }
    }
    return false;
}
Also used : DynaBean(org.apache.commons.beanutils.DynaBean) DynaActionFormClass(org.apache.struts.action.DynaActionFormClass) MutableDynaClass(org.apache.commons.beanutils.MutableDynaClass) BeanValidatorForm(org.apache.struts.validator.BeanValidatorForm) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

DynaBean (org.apache.commons.beanutils.DynaBean)2 MutableDynaClass (org.apache.commons.beanutils.MutableDynaClass)2 BeanValidatorForm (org.apache.struts.validator.BeanValidatorForm)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ActionForm (org.apache.struts.action.ActionForm)1 DynaActionForm (org.apache.struts.action.DynaActionForm)1 DynaActionFormClass (org.apache.struts.action.DynaActionFormClass)1