Example usage for org.springframework.context.support GenericApplicationContext getBean

List of usage examples for org.springframework.context.support GenericApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.context.support GenericApplicationContext getBean.

Prototype

@Override
    public Object getBean(String name) throws BeansException 

Source Link

Usage

From source file:org.lilyproject.runtime.module.build.ModuleBuilder.java

private Module buildInt(ModuleConfig cfg, ClassLoader classLoader, LilyRuntime runtime)
        throws ArtifactNotFoundException, MalformedURLException {
    infolog.info("Starting module " + cfg.getId() + " - " + cfg.getLocation());
    ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {//from  w ww  .j a va  2  s  .  c  o m
        Thread.currentThread().setContextClassLoader(classLoader);

        GenericApplicationContext applicationContext = new GenericApplicationContext();
        applicationContext.setDisplayName(cfg.getId());
        applicationContext.setClassLoader(classLoader);

        // Note: before loading any beans in the spring container:
        //   * the spring build context needs access to the module, for possible injection & module-protocol resolving during bean initialization
        //   * the module also needs to have the reference to the applicationcontext, as there might be beans trying to get while initializing
        ModuleImpl module = new ModuleImpl(classLoader, applicationContext, cfg.getDefinition(),
                cfg.getModuleSource());

        SpringBuildContext springBuildContext = new SpringBuildContext(runtime, module, classLoader);
        SPRING_BUILD_CONTEXT.set(springBuildContext);

        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
        xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
        xmlReader.setBeanClassLoader(classLoader);

        for (ModuleSource.SpringConfigEntry entry : cfg.getModuleSource().getSpringConfigs(runtime.getMode())) {
            InputStream is = entry.getStream();
            try {
                xmlReader.loadBeanDefinitions(new InputStreamResource(is,
                        entry.getLocation() + " in " + cfg.getDefinition().getFile().getAbsolutePath()));
            } finally {
                IOUtils.closeQuietly(is, entry.getLocation());
            }
        }
        applicationContext.refresh();

        // Handle the service exports
        for (SpringBuildContext.JavaServiceExport entry : springBuildContext.getExportedJavaServices()) {
            Class serviceType = entry.serviceType;
            if (!serviceType.isInterface()) {
                throw new LilyRTException("Exported service is not an interface: " + serviceType.getName());
            }

            String beanName = entry.beanName;
            Object component;
            try {
                component = applicationContext.getBean(beanName);
            } catch (NoSuchBeanDefinitionException e) {
                throw new LilyRTException("Bean not found for service to export, service type "
                        + serviceType.getName() + ", bean name " + beanName, e);
            }

            if (!serviceType.isAssignableFrom(component.getClass())) {
                throw new LilyRTException(
                        "Exported service does not implemented specified type interface. Bean = " + beanName
                                + ", interface = " + serviceType.getName());
            }

            infolog.debug(" exporting bean " + beanName + " for service " + serviceType.getName());
            Object service = shieldJavaService(serviceType, component, module, classLoader);
            runtime.getJavaServiceManager().addService(serviceType, cfg.getId(), entry.name, service);
        }

        module.start();
        return module;
    } catch (Throwable e) {
        // TODO module source and classloader handle might need disposing!
        // especially important if the lily runtime is launched as part of a longer-living VM
        throw new LilyRTException(
                "Error constructing module defined at " + cfg.getDefinition().getFile().getAbsolutePath(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(previousContextClassLoader);
        SPRING_BUILD_CONTEXT.set(null);
    }
}

From source file:org.marketcetera.util.auth.SpringSetter.java

/**
 * Returns the receiver's property value.
 *
 * @param context The context hosting the receiver's proxy bean.
 *
 * @return The property value.//from   w w  w  .  j  a  v a 2 s .  c  o m
 */

public Object getPropertyValue(GenericApplicationContext context) {
    return context.getBean(mBeanName);
}

From source file:org.marketcetera.util.spring.SpringUtilsTest.java

@Test
public void stringBean() {
    GenericApplicationContext context = new GenericApplicationContext();
    SpringUtils.addStringBean(context, TEST_NAME_BEAN, TEST_VALUE);
    assertEquals(TEST_VALUE, context.getBean(TEST_NAME_BEAN));
}

From source file:org.marketcetera.util.spring.SpringUtilsTest.java

@Test
public void propertiesConfigurerStringSet() {
    GenericApplicationContext context = new GenericApplicationContext(
            new FileSystemXmlApplicationContext(TEST_SPRING_FILE));
    SpringUtils.addPropertiesConfigurer(context, TEST_CONFIGURER_BEAN, TEST_PROPERTIES_FILE_BEAN);
    SpringUtils.addStringBean(context, TEST_NAME_BEAN, "${" + TEST_NAME_PROP + "}");
    context.refresh();/*from w w  w .  j  a  va  2s .c om*/
    assertEquals(TEST_VALUE_PROP, context.getBean(TEST_NAME_BEAN));
}

From source file:org.marketcetera.util.spring.SpringUtilsTest.java

@Test
public void propertiesConfigurerStringUnset() {
    GenericApplicationContext context = new GenericApplicationContext(
            new FileSystemXmlApplicationContext(TEST_SPRING_FILE));
    SpringUtils.addPropertiesConfigurer(context, TEST_CONFIGURER_BEAN, TEST_PROPERTIES_FILE_BEAN);
    SpringUtils.addStringBean(context, TEST_NAME_BEAN, "${" + TEST_NAME_PROP_BLANK + "}");
    context.refresh();/*  w  ww .  ja  v a  2s  . c  om*/
    assertEquals(StringUtils.EMPTY, context.getBean(TEST_NAME_BEAN));
}

From source file:org.marketcetera.util.spring.SpringUtilsTest.java

@Test
public void propertiesConfigurerList() {
    GenericApplicationContext context = new GenericApplicationContext(
            new FileSystemXmlApplicationContext(TEST_SPRING_FILE));
    SpringUtils.addPropertiesConfigurer(context, TEST_CONFIGURER_BEAN, TEST_PROPERTIES_FILES_BEAN);
    SpringUtils.addStringBean(context, TEST_NAME_BEAN, "${" + TEST_NAME_PROP + "}");
    context.refresh();//from w  w  w. jav a2  s. c  o m
    assertEquals(TEST_VALUE_PROP_OVERRIDE, context.getBean(TEST_NAME_BEAN));
}

From source file:org.openspaces.admin.application.ApplicationFileDeployment.java

private static <T> T getSpringBeanFromResource(Resource resource, Class<T> type) throws BeansException {
    final GenericApplicationContext context = new GenericApplicationContext();
    try {/* w w w.  j  ava  2 s.  c  o  m*/
        final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
        xmlReader.loadBeanDefinitions(resource);
        context.refresh();
        return context.getBean(type);
    } finally {
        if (context.isActive()) {
            context.close();
        }
    }
}

From source file:org.opentripplanner.integration.benchmark.RunBenchmarkPlanMain.java

public void run() throws Exception {
    GenericApplicationContext context = getApplicationContext();
    PathService service = (PathService) context.getBean("pathServiceImpl");

    List<Plan> plans = readPlans();

    long tTotal = 0;
    for (int i = 0; i < _repetitions; i++) {
        for (Plan plan : plans) {

            String from = plan.latFrom + " " + plan.lonFrom;
            String to = plan.latTo + " " + plan.lonTo;

            try {
                RoutingRequest opt = new RoutingRequest();
                opt.setDateTime(plan.time);
                opt.setFrom(from);/*from w  w  w  . j a  v  a  2  s .com*/
                opt.setTo(to);
                long t0 = System.currentTimeMillis();
                service.getPaths(opt);
                long t1 = System.currentTimeMillis();

                System.out.println("t=" + (t1 - t0));
                tTotal += t1 - t0;
            } catch (VertexNotFoundException ex) {
                System.out.println("no vertex: from=" + from + " to=" + to);
            }
        }
    }
    System.out.println("totalTime=" + tTotal);
}

From source file:org.pentaho.platform.plugin.services.pluginmgr.DefaultPluginManager.java

public Object getBean(String beanId, Class<?> requiredType) {
    if (beanId == null) {
        throw new IllegalArgumentException("beanId cannot be null"); //$NON-NLS-1$
    }//w  ww  .  ja v  a2s.  c om

    Object bean = null;
    for (GenericApplicationContext beanFactory : beanFactoryMap.values()) {
        if (beanFactory.containsBean(beanId)) {
            if (requiredType == null) {
                bean = beanFactory.getBean(beanId);
            } else {
                bean = beanFactory.getBean(beanId, requiredType);
            }
        }
    }
    if (bean == null) {
        throw new NoSuchBeanDefinitionException("Could not find bean with id " + beanId);
    }
    return bean;
}

From source file:org.pentaho.platform.plugin.services.pluginmgr.DefaultPluginManager.java

@Override
public Object getBean(String beanId) throws PluginBeanException {
    if (beanId == null) {
        throw new IllegalArgumentException("beanId cannot be null"); //$NON-NLS-1$
    }//from  w  w w.  ja  v  a 2  s  .  c  o m

    Object bean = null;
    for (GenericApplicationContext beanFactory : beanFactoryMap.values()) {
        if (beanFactory.containsBean(beanId)) {
            try {
                bean = beanFactory.getBean(beanId);
            } catch (Throwable ex) { // Catching throwable on purpose
                throw new PluginBeanException(ex);
            }
        }
    }
    if (bean == null) {
        throw new PluginBeanException(
                Messages.getInstance().getString("PluginManager.WARN_CLASS_NOT_REGISTERED", beanId)); //$NON-NLS-1$
    }
    return bean;
}