Example usage for org.springframework.context ApplicationContext getParent

List of usage examples for org.springframework.context ApplicationContext getParent

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getParent.

Prototype

@Nullable
ApplicationContext getParent();

Source Link

Document

Return the parent context, or null if there is no parent and this is the root of the context hierarchy.

Usage

From source file:com.benfante.minimark.controllers.AdminController.java

@RequestMapping
public String spring(HttpServletRequest req, HttpServletResponse res, Model model) {
    ApplicationContext ctx = this.ctx;
    List<String> beans = new LinkedList<String>();
    while (ctx != null) {
        String[] beanNames = ctx.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            Object bean = null;//from w  w w  . j av a 2 s . com
            try {
                bean = ctx.getBean(beanName);
                beans.add(beanName + " (" + bean.getClass() + ") [" + ctx.getDisplayName() + "]");
            } catch (BeansException beansException) {
                beans.add(beanName + " (only definition) [" + ctx.getDisplayName() + "]");
            }
        }
        ctx = ctx.getParent();
    }
    Collections.sort(beans);
    model.addAttribute("beans", beans);
    return "admin/spring";
}

From source file:org.gwtwidgets.server.spring.GWTHandler.java

/**
 * Scans the application context and its parents for service beans that
 * implement the {@link GWTRequestMapping}
 * /*from w  ww  . jav  a2  s.c o m*/
 * @param appContext
 *            Application context
 */
private void scanForAnnotatedBeans(final ApplicationContext appContext) {
    if (appContext == null) {
        return;
    }
    for (String beanName : appContext.getBeanNamesForType(RemoteService.class)) {
        Object service = appContext.getBean(beanName);
        if (service == null)
            continue;
        final Class<?> beanClass = service.getClass();

        final RemoteServiceRelativePath requestMapping = ReflectionUtils.findAnnotation(beanClass,
                RemoteServiceRelativePath.class);
        if (requestMapping == null) {
            continue;
        }

        // Create serviceExporter to bind to
        String mapping = requestMapping.value();
        if (mapping.contains("/")) {
            mapping = mapping.substring(mapping.lastIndexOf("/"));
        }
        if (getMappings().containsKey(mapping))
            logger.warn("Bean '" + mapping + "' already in mapping, skipping.");
        else
            getMappings().put(mapping, service);
    }
    if (scanParentApplicationContext)
        scanForAnnotatedBeans(appContext.getParent());
}

From source file:com.dushyant.portlet.gwt.PathBasedGwtRpcServiceResolver.java

private void scanForGwtRpcBeans(ApplicationContext appContext, Map<String, Object> rpcServiceMap,
        String[] remoteServiceBeans) {
    //Iterate through the beans from the application context and scan for beans with RemoteServiceRelativePath
    for (String beanName : remoteServiceBeans) {
        Object service = appContext.getBean(beanName);
        if (service != null) {
            // Get the RemoteServiceRelativePath annotation on the class
            final RemoteServiceRelativePath servicePathAnnotation = ReflectionUtils
                    .findAnnotation(service.getClass(), RemoteServiceRelativePath.class);

            if (servicePathAnnotation != null) {
                // This class is annotated with RemoteServiceRelativePath so treat it as a GWT RPC service bean
                // and add corresponding entry to the map
                addRpcServiceBeanMapping(rpcServiceMap, service, servicePathAnnotation);
            }/*from  www. ja  v a  2s .co  m*/
        }
    }
    if (scanParentApplicationContext) {
        // Call this method recursively with parent application context if parent
        // application context needs to be scanned.
        scanForGwtRpcBeans(appContext.getParent(), rpcServiceMap, remoteServiceBeans);
    }
}

From source file:com.opensymphony.xwork2.spring.SpringObjectFactory.java

/**
 * If the given context is assignable to AutowireCapbleBeanFactory or contains a parent or a factory that is, then
 * set the autoWiringFactory appropriately.
 *
 * @param context the application context
 *
 * @return the bean factory/*from   w w  w  .  j  a v  a2  s .  c  o m*/
 */
protected AutowireCapableBeanFactory findAutoWiringBeanFactory(ApplicationContext context) {
    if (context instanceof AutowireCapableBeanFactory) {
        // Check the context
        return (AutowireCapableBeanFactory) context;
    } else if (context instanceof ConfigurableApplicationContext) {
        // Try and grab the beanFactory
        return ((ConfigurableApplicationContext) context).getBeanFactory();
    } else if (context.getParent() != null) {
        // And if all else fails, try again with the parent context
        return findAutoWiringBeanFactory(context.getParent());
    }
    return null;
}

From source file:org.broadleafcommerce.test.BroadleafGenericGroovyXmlWebContextLoader.java

/**
 * Configures web resources for the supplied web application context (WAC).
 *
 * <h4>Implementation Details</h4>
 *
 * <p>If the supplied WAC has no parent or its parent is not a WAC, the
 * supplied WAC will be configured as the Root WAC (see "<em>Root WAC
 * Configuration</em>" below).//  w w  w . j  a va2s  .  c o  m
 *
 * <p>Otherwise the context hierarchy of the supplied WAC will be traversed
 * to find the top-most WAC (i.e., the root); and the {@link ServletContext}
 * of the Root WAC will be set as the {@code ServletContext} for the supplied
 * WAC.
 *
 * <h4>Root WAC Configuration</h4>
 *
 * <ul>
 * <li>The resource base path is retrieved from the supplied
 * {@code WebMergedContextConfiguration}.</li>
 * <li>A {@link ResourceLoader} is instantiated for the {@link MockServletContext}:
 * if the resource base path is prefixed with "{@code classpath:/}", a
 * {@link DefaultResourceLoader} will be used; otherwise, a
 * {@link FileSystemResourceLoader} will be used.</li>
 * <li>A {@code MockServletContext} will be created using the resource base
 * path and resource loader.</li>
 * <li>The supplied {@link MergeXmlWebApplicationContext} is then stored in
 * the {@code MockServletContext} under the
 * {@link MergeXmlWebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li>
 * <li>Finally, the {@code MockServletContext} is set in the
 * {@code MergeXmlWebApplicationContext}.</li>
 *
 * @param context the merge xml web application context for which to configure the web
 * resources
 * @param webMergedConfig the merged context configuration to use to load the
 * merge xml web application context
 */
protected void configureWebResources(MergeXmlWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    ApplicationContext parent = context.getParent();

    // if the WAC has no parent or the parent is not a WAC, set the WAC as
    // the Root WAC:
    if (parent == null || (!(parent instanceof WebApplicationContext))) {
        String resourceBasePath = webMergedConfig.getResourceBasePath();
        ResourceLoader resourceLoader = resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
                ? new DefaultResourceLoader()
                : new FileSystemResourceLoader();

        ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
        context.setServletContext(servletContext);
    } else {
        ServletContext servletContext = null;

        // find the Root WAC
        while (parent != null) {
            if (parent instanceof WebApplicationContext
                    && !(parent.getParent() instanceof WebApplicationContext)) {
                servletContext = ((WebApplicationContext) parent).getServletContext();
                break;
            }
            parent = parent.getParent();
        }
        Assert.state(servletContext != null,
                "Failed to find Root MergeXmlWebApplicationContext in the context hierarchy");
        context.setServletContext(servletContext);
    }
}

From source file:org.easyrec.store.dao.web.impl.LoaderDAOMysqlImpl.java

@Override
public void reloadBackend() {
    List<String> configLocs = Lists.newArrayList("classpath:spring/web/commonContext.xml");

    if ("on".equals(properties.getProperty("easyrec.rest"))) {
        configLocs.add(configLocations.get("easyrec.rest"));
    }/*from w  w  w  .  jav a 2s.  com*/

    // if no config found use default
    if (configLocs.size() == 1) {
        configLocs.add(configLocations.get("easyrec.rest"));
    }

    if ("on".equals(properties.getProperty("easyrec.dev"))) {
        configLocs.add(configLocations.get("easyrec.dev"));
    }

    ApplicationContext webctx = applicationContext;

    ApplicationContext parent = webctx.getParent();

    if (parent instanceof ConfigurableWebApplicationContext) {

        ((ConfigurableWebApplicationContext) parent)
                .setConfigLocations(configLocs.toArray(new String[configLocs.size()]));
        ((ConfigurableWebApplicationContext) parent).refresh();

    }

    setDataSource(parent.getBean("easyrecDataSource", com.zaxxer.hikari.HikariDataSource.class));
}

From source file:org.jahia.services.seo.urlrewrite.UrlRewriteService.java

protected List<HandlerMapping> getRenderMapping() {
    if (renderMapping == null) {
        LinkedList<HandlerMapping> mapping = new LinkedList<HandlerMapping>();
        ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute(
                "org.springframework.web.servlet.FrameworkServlet.CONTEXT.RendererDispatcherServlet");
        if (ctx != null) {
            mapping.addAll(ctx.getBeansOfType(HandlerMapping.class).values());
            mapping.addAll(ctx.getParent().getBeansOfType(HandlerMapping.class).values());
            renderMapping = mapping;/*from  ww w  . j  a  va2 s.  c o m*/
        }
    }
    if (renderMapping != null) {
        List<HandlerMapping> l = new LinkedList<HandlerMapping>(renderMapping);
        l.addAll(ServicesRegistry.getInstance().getJahiaTemplateManagerService().getTemplatePackageRegistry()
                .getSpringHandlerMappings());
        return l;
    }
    return null;
}

From source file:org.openmrs.api.context.ServiceContext.java

/**
 * Private method which returns all components registered in a Spring applicationContext of a
 * given type This method recurses through each parent ApplicationContext
 *
 * @param context - The applicationContext to check
 * @param type - The type of component to retrieve
 * @return all components registered in a Spring applicationContext of a given type
 *//*from  w ww  .  j av a 2s  .  co m*/
@SuppressWarnings("unchecked")
private <T> Map<String, T> getRegisteredComponents(ApplicationContext context, Class<T> type) {
    Map<String, T> components = new HashMap<String, T>();
    Map registeredComponents = context.getBeansOfType(type);
    if (log.isTraceEnabled()) {
        log.trace("getRegisteredComponents(" + context + ", " + type + ") = " + registeredComponents);
    }
    if (registeredComponents != null) {
        components.putAll(registeredComponents);
    }
    if (context.getParent() != null) {
        components.putAll(getRegisteredComponents(context.getParent(), type));
    }
    return components;
}