Example usage for org.springframework.web.context WebApplicationContext getAliases

List of usage examples for org.springframework.web.context WebApplicationContext getAliases

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getAliases.

Prototype

String[] getAliases(String name);

Source Link

Document

Return the aliases for the given bean name, if any.

Usage

From source file:net.mojodna.sprout.SproutAutoLoaderPlugIn.java

private void loadSprouts(final WebApplicationContext wac) throws BeansException {
    final String[] beanNames = wac.getBeanNamesForType(Sprout.class);

    // create a default actionform
    final FormBeanConfig fbc = new FormBeanConfig();
    fbc.setName(Sprout.SPROUT_DEFAULT_ACTION_FORM_NAME);
    fbc.setType(LazyValidatorForm.class.getName());
    getModuleConfig().addFormBeanConfig(fbc);

    for (int i = 0; i < beanNames.length; i++) {
        final Sprout bean = (Sprout) wac.getBean(beanNames[i]);
        final String[] aliases = wac.getAliases(beanNames[i]);
        for (int j = 0; j < aliases.length; j++) {
            final String name = aliases[j].substring(aliases[j].lastIndexOf('/') + 1);
            try {
                final Method method = findMethod(name, bean.getClass());
                log.debug(aliases[j] + " -> " + beanNames[i] + "." + name);

                final ActionMapping ac = new ActionMapping();
                ac.setParameter(method.getName());
                ac.setPath(aliases[j]);//from   w  w  w . ja v a 2 s . c  o  m

                // establish defaults
                String actionForm = bean.getClass().getSimpleName() + Sprout.DEFAULT_FORM_SUFFIX;
                String input = aliases[j] + Sprout.DEFAULT_VIEW_EXTENSION;
                String scope = Sprout.DEFAULT_SCOPE;
                boolean validate = false;
                ac.addForwardConfig(makeForward(Sprout.FWD_SUCCESS, aliases[j] + ".jsp"));

                // process annotations and override defaults where appropriate
                final Annotation[] annotations = method.getAnnotations();
                for (int k = 0; k < annotations.length; k++) {
                    final Annotation a = annotations[k];
                    final Class type = a.annotationType();
                    if (type.equals(Sprout.FormName.class))
                        actionForm = ((Sprout.FormName) a).value();
                    else if (type.equals(Sprout.Forward.class)) {
                        final Forward fwd = (Sprout.Forward) a;
                        for (int m = 0; m < fwd.path().length; m++) {
                            String fwdPath = fwd.path()[m];
                            String fwdName = Sprout.FWD_SUCCESS;
                            boolean fwdRedirect = false;
                            if (fwd.name().length - 1 >= m)
                                fwdName = fwd.name()[m];
                            if (fwd.redirect().length - 1 >= m)
                                fwdRedirect = fwd.redirect()[m];
                            ac.addForwardConfig(makeForward(fwdName, fwdPath, fwdRedirect, null));
                        }
                    } else if (type.equals(Sprout.Input.class))
                        input = ((Sprout.Input) a).value();
                    if (type.equals(Sprout.Scope.class))
                        scope = ((Sprout.Scope) a).value();
                    else if (type.equals(Sprout.Validate.class))
                        validate = ((Sprout.Validate) a).value();
                }

                // use values
                if (null != getModuleConfig().findFormBeanConfig(actionForm))
                    ac.setName(actionForm);
                else {
                    log.info("No ActionForm defined: " + actionForm + ". Using default.");
                    ac.setName(Sprout.SPROUT_DEFAULT_ACTION_FORM_NAME);
                }
                ac.setValidate(validate);
                ac.setInput(input);
                ac.setScope(scope);

                getModuleConfig().addActionConfig(ac);
            } catch (final NoSuchMethodException e) {
                log.warn("Could not register action; no such method: " + name, e);
            }
        }
    }

    /* Useful if you'd like a view into registered paths
     * TODO create a ServletFilter that displays these
    log.debug("Dumping action configs...");
    final ActionConfig[] configs = getModuleConfig().findActionConfigs();
    for ( int i = 0; i < configs.length; i++ ) {
    log.debug( configs[i].getPath() );
    }
    */
}