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

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

Introduction

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

Prototype

@Override
    public Object getBean(String name) throws BeansException 

Source Link

Usage

From source file:org.alfresco.util.exec.RuntimeExecBeansTest.java

public void testExecOfNeverEndingProcess() {
    File dir = new File(DIR);
    dir.mkdir();//from   w ww.  j  a  v  a 2  s.co  m
    assertTrue("Directory not created", dir.exists());

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {
        RuntimeExec failureExec = (RuntimeExec) ctx.getBean("commandNeverEnding");
        assertNotNull(failureExec);
        // Execute it
        failureExec.execute();
        // The command is never-ending, so this should be out immediately
    } finally {
        ctx.close();
    }
}

From source file:org.opencredo.cloud.storage.si.adapter.config.InboundChannelAdapterParserTest.java

@Test
public void testInboundAdapterLoadFull() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "InboundChannelAdapterParserTest-full-context.xml", this.getClass());

    Object bean = context.getBean("inbound-adapter");
    assertNotNull("Adapter not found", bean);
    System.out.println(bean.getClass());
    DirectFieldAccessor d = new DirectFieldAccessor(bean);
    Object value = d.getPropertyValue("source");
    assertNotNull("Source not found", value);
    System.out.println(value.getClass());

    ReadingMessageSource rms = (ReadingMessageSource) value;
    DirectFieldAccessor adapterDirect = new DirectFieldAccessor(rms);
    assertNotNull("'template' not found", adapterDirect.getPropertyValue("template"));
    assertNotNull("'filter' queue not found", adapterDirect.getPropertyValue("filter"));
    assertTrue(adapterDirect.getPropertyValue("filter") instanceof PatternMatchingBlobNameFilter);
    assertEquals(TestPropertiesAccessor.getDefaultContainerName(),
            adapterDirect.getPropertyValue("containerName"));
}

From source file:com.digitalgeneralists.assurance.ui.workers.MergeScanResultWorker.java

@Override
protected ComparisonResult doInBackground() throws Exception {
    this.notifier.fireEvent(new ResultMergeStartedEvent(this.result));

    ComparisonResult result = null;//from ww w. j  ava  2s  . com

    // Because we are operating in a Swing application, the session context
    // closes after the initial bootstrap run. There appears to be an
    // "application"-level session configuration that should enable the
    // behavior we want for a desktop application, but there is no documentation
    // for it that I can find. 
    // So, Assurance mimics a web app request/response
    // cycle for calls into the model delegate through the Swing
    // application. All calls to the model initiated directly from the UI
    // essentially operate as a new "request" and rebuild the Hibernate and
    // Spring session contexts for use within that operation.
    ClassPathXmlApplicationContext springContext = null;
    try {
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        result = modelDelegate.mergeScanResult(this.result, this.strategy, this);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return result;
}

From source file:com.thinkbiganalytics.server.upgrade.KyloUpgrader.java

/**
 * Return the database version for Kylo.
 *
 * @return the version of Kylo stored in the database
 */// ww  w. j  ava 2  s  .  co m
public KyloVersion getCurrentVersion() {
    String profiles = System.getProperty("spring.profiles.active", "");
    if (!profiles.contains("native")) {
        profiles = StringUtils.isEmpty(profiles) ? "native" : profiles + ",native";
        System.setProperty("spring.profiles.active", profiles);
    }
    //Spring is needed to load the Spring Cloud context so we can decrypt the passwords for the database connection
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            "kylo-upgrade-check-application-context.xml");
    ctx.refresh();
    KyloUpgradeDatabaseVersionChecker upgradeDatabaseVersionChecker = (KyloUpgradeDatabaseVersionChecker) ctx
            .getBean("kyloUpgradeDatabaseVersionChecker");
    KyloVersion kyloVersion = upgradeDatabaseVersionChecker.getDatabaseVersion();
    ctx.close();
    return kyloVersion;
}

From source file:com.digitalgeneralists.assurance.ui.workers.MergeScanWorker.java

@Override
protected Scan doInBackground() throws Exception {
    this.notifier.fireEvent(new ScanMergeStartedEvent(this.scan));

    Scan scan = null;//from ww w  .  j  ava2s . c  o  m

    // Because we are operating in a Swing application, the session context
    // closes after the initial bootstrap run. There appears to be an
    // "application"-level session configuration that should enable the
    // behavior we want for a desktop application, but there is no documentation
    // for it that I can find. 
    // So, Assurance mimics a web app request/response
    // cycle for calls into the model delegate through the Swing
    // application. All calls to the model initiated directly from the UI
    // essentially operate as a new "request" and rebuild the Hibernate and
    // Spring session contexts for use within that operation.
    ClassPathXmlApplicationContext springContext = null;
    try {
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");
        IAssuranceThreadPool threadPool = (IAssuranceThreadPool) springContext.getBean("ThreadPool");
        threadPool.setNumberOfThreads(modelDelegate.getApplicationConfiguration().getNumberOfScanThreads());

        scan = modelDelegate.mergeScan(this.scan, threadPool, this);
        modelDelegate = null;
        threadPool = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return scan;
}

From source file:org.opencredo.cloud.storage.si.adapter.config.InboundChannelAdapterParserTest.java

@Test
public void testInboundAdapterLoadWithFilter() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "InboundChannelAdapterParserTest-withFilter-context.xml", this.getClass());

    Object bean = context.getBean("inbound-adapter");
    assertNotNull("Adapter not found", bean);
    System.out.println(bean.getClass());
    DirectFieldAccessor d = new DirectFieldAccessor(bean);
    Object value = d.getPropertyValue("source");
    assertNotNull("Source not found", value);
    System.out.println(value.getClass());

    ReadingMessageSource rms = (ReadingMessageSource) value;
    DirectFieldAccessor adapterDirect = new DirectFieldAccessor(rms);
    assertNotNull("'template' not found", adapterDirect.getPropertyValue("template"));
    assertNotNull("'filter' queue not found", adapterDirect.getPropertyValue("filter"));
    assertTrue(adapterDirect.getPropertyValue("filter") instanceof PatternMatchingBlobNameFilter);
    assertEquals(TestPropertiesAccessor.getDefaultContainerName(),
            adapterDirect.getPropertyValue("containerName"));
}

From source file:org.opencredo.cloud.storage.si.adapter.config.InboundChannelAdapterParserTest.java

@Test
public void testInboundAdapterLoadWithComparator() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "InboundChannelAdapterParserTest-withComparator-context.xml", this.getClass());

    Object bean = context.getBean("inbound-adapter");
    assertNotNull("Adapter not found", bean);
    System.out.println(bean.getClass());
    DirectFieldAccessor d = new DirectFieldAccessor(bean);
    Object value = d.getPropertyValue("source");
    assertNotNull("Source not found", value);
    System.out.println(value.getClass());

    ReadingMessageSource rms = (ReadingMessageSource) value;
    DirectFieldAccessor adapterDirect = new DirectFieldAccessor(rms);
    assertNotNull("'template' not found", adapterDirect.getPropertyValue("template"));
    assertNotNull("'filter' queue not found", adapterDirect.getPropertyValue("filter"));
    assertTrue(adapterDirect.getPropertyValue("filter") instanceof AcceptOnceBlobNameFilter);
    assertEquals(TestPropertiesAccessor.getDefaultContainerName(),
            adapterDirect.getPropertyValue("containerName"));
}

From source file:org.jasig.cas.client.validation.Cas20ProxyTicketValidatorTests.java

@Test
public void testConstructionFromSpringBean() throws TicketValidationException, UnsupportedEncodingException {
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:cas20ProxyTicketValidator.xml");
    final Cas20ProxyTicketValidator v = (Cas20ProxyTicketValidator) context.getBean("proxyTicketValidator");

    final String USERNAME = "username";
    final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>username</cas:user><cas:proxyGrantingTicket>PGTIOU-84678-8a9d...</cas:proxyGrantingTicket><cas:proxies><cas:proxy>proxy1</cas:proxy><cas:proxy>proxy2</cas:proxy><cas:proxy>proxy3</cas:proxy></cas:proxies></cas:authenticationSuccess></cas:serviceResponse>";
    server.content = RESPONSE.getBytes(server.encoding);

    final Assertion assertion = v.validate("test", "test");
    assertEquals(USERNAME, assertion.getPrincipal().getName());

}

From source file:com.digitalgeneralists.assurance.ui.workers.InitializeApplicationStateWorker.java

@Override
protected List<Object> doInBackground() throws Exception {
    List<Object> bootstrapObjects = new ArrayList<Object>();
    List<ScanDefinition> scanDefinitions = null;
    ApplicationConfiguration config = null;

    // Because we are operating in a Swing application, the session context
    // closes after the initial bootstrap run. There appears to be an
    // "application"-level session configuration that should enable the
    // behavior we want for a desktop application, but there is no documentation
    // for it that I can find. 
    // So, Assurance mimics a web app request/response
    // cycle for calls into the model delegate through the Swing
    // application. All calls to the model initiated directly from the UI
    // essentially operate as a new "request" and rebuild the Hibernate and
    // Spring session contexts for use within that operation.
    ClassPathXmlApplicationContext springContext = null;
    try {/*from   w w  w. j a va 2  s  . c o m*/
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        scanDefinitions = modelDelegate.getScanDefinitions();
        config = modelDelegate.getApplicationConfiguration();
        if (scanDefinitions != null) {
            bootstrapObjects.add(scanDefinitions);
        }
        if (config != null) {
            bootstrapObjects.add(config);
        }
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return bootstrapObjects;
}

From source file:ws.antonov.config.consumer.ConfigClientTest.java

public void testSpring() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    context.refresh();//from  w ww.j  a v  a 2  s. co m

    FlatConfigService service = context.getBean(FlatConfigService.class);

    FlatConfigObject config = service.getConfig("build/classes", "test/config.properties");

    assertEquals(config.getTimeout(), 10);
    assertEquals(config.getValidate(), false);
    assertEquals(config.getSystemCode(), "101");
}