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

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

Introduction

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

Prototype

String[] getBeanNamesForType(ResolvableType type);

Source Link

Document

Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

Usage

From source file:org.dspace.webmvc.servlet.SpringFreemarkerDecoratorServlet.java

private Configuration getSpringConfiguration() {
    if (fmConfiguration != null) {
        return fmConfiguration;
    }//from   w  ww . j  a va2s  . c o m

    WebApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    String[] names = ctx.getBeanNamesForType(freemarker.template.Configuration.class);
    if (names != null && names.length > 0) {
        fmConfiguration = (Configuration) ctx.getBean(names[0]);
    }

    return fmConfiguration;
}

From source file:com.agiletec.apsadmin.tags.HookPointTag.java

private List<HookPointElementContainer> extractElements(HttpServletRequest request) {
    WebApplicationContext wac = ApsWebApplicationUtils.getWebApplicationContext(request);
    String[] beanNames = wac.getBeanNamesForType(HookPointElementContainer.class);
    List<HookPointElementContainer> containers = new ArrayList<HookPointElementContainer>();
    for (int i = 0; i < beanNames.length; i++) {
        HookPointElementContainer container = (HookPointElementContainer) wac.getBean(beanNames[i]);
        if (null != container && null != container.getHookPointKey()
                && container.getHookPointKey().equals(this.getKey())) {
            containers.add(container);//from   w ww .  j  ava 2  s  .  co m
        }
    }
    BeanComparator comparator = new BeanComparator("priority");
    Collections.sort(containers, comparator);
    return containers;
}

From source file:com.boundlessgeo.geoserver.api.controllers.IconControllerTest.java

@Before
public void setUpAppContext() {
    WebApplicationContext appContext = mock(WebApplicationContext.class);
    when(appContext.getBeanNamesForType(StyleHandler.class))
            .thenReturn(new String[] { "ysldHandler", "sldHandler" });
    when(appContext.getBean("ysldHandler")).thenReturn(new YsldHandler());
    when(appContext.getBean("sldHandler")).thenReturn(new SLDHandler());

    new GeoServerExtensions().setApplicationContext(appContext);
}

From source file:org.grails.plugin.batch.Launcher.java

private void executeMainClass(GrailsApplication application, DefaultGrailsMainClass main) {
    Map<String, Object> context = new LinkedHashMap<String, Object>();
    for (@SuppressWarnings("rawtypes")
    Enumeration e = servletContext.getAttributeNames(); e.hasMoreElements();) {
        String key = (String) e.nextElement();
        Object value = servletContext.getAttribute(key);
        context.put(key, value);/*www  . ja  v a2 s  . com*/
    }

    WebApplicationContext webContext = (WebApplicationContext) application.getMainContext();

    PersistenceContextInterceptor interceptor = null;
    String[] beanNames = webContext.getBeanNamesForType(PersistenceContextInterceptor.class);
    if (beanNames.length > 0) {
        interceptor = (PersistenceContextInterceptor) webContext.getBean(beanNames[0]);
    }

    if (interceptor != null) {
        interceptor.init();
    }

    try {
        main.callRun(context);

        if (interceptor != null) {
            interceptor.flush();
        }
    } finally {
        if (interceptor != null) {
            interceptor.destroy();
        }
    }
}

From source file:com.agiletec.aps.tags.ExecWidgetTag.java

protected List<IFrameDecoratorContainer> extractDecorators() throws ApsSystemException {
    HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
    WebApplicationContext wac = ApsWebApplicationUtils.getWebApplicationContext(request);
    List<IFrameDecoratorContainer> containters = new ArrayList<IFrameDecoratorContainer>();
    try {//from   ww  w .j a v a  2s.  c  o  m
        String[] beanNames = wac.getBeanNamesForType(IFrameDecoratorContainer.class);
        for (int i = 0; i < beanNames.length; i++) {
            IFrameDecoratorContainer container = (IFrameDecoratorContainer) wac.getBean(beanNames[i]);
            containters.add(container);
        }
        BeanComparator comparator = new BeanComparator("order");
        Collections.sort(containters, comparator);
    } catch (Throwable t) {
        ApsSystemUtils.logThrowable(t, this, "extractDecorators", "Error extracting widget decorators");
        throw new ApsSystemException("Error extracting widget decorators", t);
    }
    return containters;
}

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   ww  w  .  ja va2s  .  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() );
    }
    */
}

From source file:org.codehaus.groovy.grails.web.context.GrailsConfigUtils.java

/**
 * Executes Grails bootstrap classes//w ww.  ja va2 s  .  c o m
 *
 * @param application The Grails ApplicationContext instance
 * @param webContext The WebApplicationContext instance
 * @param servletContext The ServletContext instance
 */
public static void executeGrailsBootstraps(GrailsApplication application, WebApplicationContext webContext,
        ServletContext servletContext) {

    PersistenceContextInterceptor interceptor = null;
    String[] beanNames = webContext.getBeanNamesForType(PersistenceContextInterceptor.class);
    if (beanNames.length > 0) {
        interceptor = (PersistenceContextInterceptor) webContext.getBean(beanNames[0]);
    }

    if (interceptor != null) {
        interceptor.init();
    }
    // init the Grails application
    try {
        GrailsClass[] bootstraps = application.getArtefacts(BootstrapArtefactHandler.TYPE);
        for (GrailsClass bootstrap : bootstraps) {
            final GrailsBootstrapClass bootstrapClass = (GrailsBootstrapClass) bootstrap;
            final Object instance = bootstrapClass.getReferenceInstance();
            webContext.getAutowireCapableBeanFactory().autowireBeanProperties(instance,
                    AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
            bootstrapClass.callInit(servletContext);
        }
        if (interceptor != null) {
            interceptor.flush();
        }
    } finally {
        if (interceptor != null) {
            interceptor.destroy();
        }
    }
}

From source file:org.entando.entando.aps.system.services.controller.executor.AbstractWidgetExecutorService.java

protected List<IFrameDecoratorContainer> extractDecorators(RequestContext reqCtx) throws ApsSystemException {
    HttpServletRequest request = reqCtx.getRequest();
    WebApplicationContext wac = ApsWebApplicationUtils.getWebApplicationContext(request);
    List<IFrameDecoratorContainer> containters = new ArrayList<IFrameDecoratorContainer>();
    try {// ww  w .  ja  v a2  s  . c  o m
        String[] beanNames = wac.getBeanNamesForType(IFrameDecoratorContainer.class);
        for (int i = 0; i < beanNames.length; i++) {
            IFrameDecoratorContainer container = (IFrameDecoratorContainer) wac.getBean(beanNames[i]);
            containters.add(container);
        }
        BeanComparator comparator = new BeanComparator("order");
        Collections.sort(containters, comparator);
    } catch (Throwable t) {
        _logger.error("Error extracting widget decorators", t);
        throw new ApsSystemException("Error extracting widget decorators", t);
    }
    return containters;
}

From source file:org.geoserver.jdbcconfig.internal.JdbcConfigTestSupport.java

public void setUp() throws Exception {
    ConfigDatabase.LOGGER.setLevel(Level.FINER);
    // just to avoid hundreds of warnings in the logs about extension lookups with no app
    // context set
    WebApplicationContext applicationContext = Mockito.mock(WebApplicationContext.class);
    new GeoServerExtensions().setApplicationContext(applicationContext);
    when(applicationContext.getBeansOfType((Class) anyObject())).thenReturn(Collections.EMPTY_MAP);
    when(applicationContext.getBeanNamesForType((Class) anyObject())).thenReturn(new String[] {});
    ///*from w  w w.j  a  v a 2 s.  com*/

    final File testDbDir = new File("target", "jdbcconfig");
    FileUtils.deleteDirectory(testDbDir);
    testDbDir.mkdirs();

    dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(connectionUrl);
    dataSource.setUsername("postgres");
    dataSource.setPassword("geo123");

    dataSource.setMinIdle(3);
    dataSource.setMaxActive(10);
    try {
        Connection connection = dataSource.getConnection();
        connection.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    try {
        dropDb(dataSource);
    } catch (Exception ignored) {
    }
    initDb(dataSource);

    XStreamInfoSerialBinding binding = new XStreamInfoSerialBinding(new XStreamPersisterFactory());

    catalog = new CatalogImpl();
    configDb = new ConfigDatabase(dataSource, binding);
    configDb.setCatalog(catalog);
    configDb.initDb(null);
}

From source file:org.geoserver.jdbcconfig.JDBCConfigTestSupport.java

protected void configureAppContext(WebApplicationContext appContext) {
    expect(appContext.getBeansOfType((Class) anyObject())).andReturn(Collections.EMPTY_MAP).anyTimes();
    expect(appContext.getBeanNamesForType((Class) anyObject())).andReturn(new String[] {}).anyTimes();

    ServletContext servletContext = createNiceMock(ServletContext.class);
    replay(servletContext);/*  ww w  .j  ava2 s .  com*/

    expect(appContext.getServletContext()).andReturn(servletContext);
}