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:com.enterra.batch.admin.sample.BootstrapTests.java

@Test
public void testServletConfiguration() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml" },
            parent);/*from w  ww. j a v  a  2  s  .c o  m*/

    assertTrue(context.containsBean("jobRepository"));
    String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(),
            JobController.class);
    assertEquals(1, beanNames.length);

    Job job = context.getBean(JobRegistry.class).getJob("job1");
    final JobExecution jobExecution = parent.getBean(JobLauncher.class).run(job,
            new JobParametersBuilder().addString("fail", "false").toJobParameters());

    new DirectPoller<BatchStatus>(100).poll(new Callable<BatchStatus>() {
        public BatchStatus call() throws Exception {
            BatchStatus status = jobExecution.getStatus();
            if (status.isLessThan(BatchStatus.STOPPED) && status != BatchStatus.COMPLETED) {
                return null;
            }
            return status;
        }
    }).get(2000, TimeUnit.MILLISECONDS);

    context.close();
    parent.close();

    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

}

From source file:org.agiso.tempel.support.test.AbstractTemplateTest.java

protected ITempel createTempelInstance() {
    ClassPathXmlApplicationContext ctx = null;
    try {/* w  w w  .  j  ava2s . c  om*/
        ctx = new ClassPathXmlApplicationContext(getContextConfigLocations());

        File tempelInf = new File("src/main/resources/TEMPEL-INF");
        if (tempelInf.exists() && tempelInf.isDirectory()) {
            Archive<?> archive = createTemplateArchive();

            // Mamy do czynienia z testem projektu szablonu:
            IArchiveTemplateProviderElement atp = ctx.getBean(IArchiveTemplateProviderElement.class);
            atp.addArchive(groupId + ":" + templateId + ":" + version, archive);
        }

        return ctx.getBean(ITempel.class);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.hupo.psi.mi.psicquic.ws.SolrBasedPsicquicRestServiceTest.java

@Before
public void setupSolrPsicquicService() throws Exception {

    // Start a jetty server to host the solr index
    solrJettyRunner = new SolrJettyRunner();
    solrJettyRunner.start();//  ww w .  jav  a  2s  .c o  m

    // index data to be hosted by PSICQUIC
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "/META-INF/beans.spring.test.xml", "classpath*:/META-INF/psicquic-spring.xml",
                    "/META-INF/psicquic-indexing-spring-test.xml" });

    SolrMitabIndexer indexer = (SolrMitabIndexer) context.getBean("solrMitabIndexer");
    indexer.startJob("mitabIndexNegativeJob");

    HttpSolrServer solrServer = solrJettyRunner.getSolrServer();
    Assert.assertEquals(4L, solrServer.query(new SolrQuery("*:*")).getResults().getNumFound());

    PsicquicConfig config = (PsicquicConfig) context.getBean("testPsicquicConfig");
    config.setSolrUrl(solrJettyRunner.getSolrUrl());

    service = (SolrBasedPsicquicRestService) context.getBean("solrBasedPsicquicRestService");
}

From source file:com.nokia.dempsy.mpcluster.TestAllMpClusterImpls.java

private <T, N> void runAllCombinations(Checker<T, N> checker) throws Throwable {
    for (String clusterManager : clusterManagers) {
        ClassPathXmlApplicationContext actx = new ClassPathXmlApplicationContext(clusterManager);
        actx.registerShutdownHook();/*  w  w w.j ava 2  s . c  o m*/

        @SuppressWarnings("unchecked")
        MpClusterSessionFactory<T, N> factory = (MpClusterSessionFactory<T, N>) actx
                .getBean("clusterSessionFactory");

        if (checker != null)
            checker.check("pass for:" + clusterManager, factory);

        actx.stop();
        actx.destroy();
    }
}

From source file:org.hupo.psi.mi.psicquic.ws.SolrBasedPsicquicServiceTest.java

@Before
public void setupSolrPsicquicService() throws Exception {

    // Start a jetty server to host the solr index
    solrJettyRunner = new SolrJettyRunner();
    solrJettyRunner.start();//from www . j  a v  a2  s  . co  m

    // index data to be hosted by PSICQUIC
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "/META-INF/beans.spring.test.xml", "classpath*:/META-INF/psicquic-spring.xml",
                    "/META-INF/psicquic-indexing-spring-test.xml" });

    SolrMitabIndexer indexer = (SolrMitabIndexer) context.getBean("solrMitabIndexer");
    indexer.startJob("mitabIndexNegativeJob");

    HttpSolrServer solrServer = solrJettyRunner.getSolrServer();
    Assert.assertEquals(4L, solrServer.query(new SolrQuery("*:*")).getResults().getNumFound());

    PsicquicConfig config = (PsicquicConfig) context.getBean("testPsicquicConfig");
    config.setSolrUrl(solrJettyRunner.getSolrUrl());

    service = (PsicquicService) context.getBean("solrBasedPsicquicService");
}

From source file:se.vgregion.delegation.server.Server.java

public void startServer(ClassPathXmlApplicationContext ctx, String hostname, String port) {

    Server.setEndpoints(new ArrayList<Endpoint>());

    try {/*  www .ja  v  a2s. com*/
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        LOGGER.error("ClassNotFoundException for: org.postgresql.Driver " + e.getMessage());
    }

    DelegationService delegationService = (DelegationService) ctx.getBean("delegationService");
    propertiesBean = (PropertiesBean) ctx.getBean("propertiesBean");

    // Make CXF use log4j (instead of JDK-logging), currently can't use slf4j.
    //        System.setProperty("org.apache.cxf.Logger", "org.apache.cxf.common.logging.Log4jLogger");

    https = (propertiesBean.getCertPass() != null && !propertiesBean.getCertPass().equals(""));

    String http = "http";

    // Setups SSL and Certificates.
    if (https) {
        try {
            http = "https";
            setupServerEngineFactory(Integer.parseInt(port));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
    }

    String address2 = http + "://" + hostname + ":" + port + "/getactivedelegations";
    String address3 = http + "://" + hostname + ":" + port + "/getdelegation";
    String address4 = http + "://" + hostname + ":" + port + "/getinactivedelegations";
    String address5 = http + "://" + hostname + ":" + port + "/getdelegationsbyunitandrole";
    String address6 = http + "://" + hostname + ":" + port + "/getdelegations";
    String address7 = http + "://" + hostname + ":" + port + "/hasdelegation";
    String address8 = http + "://" + hostname + ":" + port + "/savedelegations";
    String address9 = http + "://" + hostname + ":" + port + "/removedelegation";
    String address10 = http + "://" + hostname + ":" + port + "/finddelegations";
    String address11 = http + "://" + hostname + ":" + port + "/pingforconfiguration";

    LOGGER.info(
            "RIV TA Basic Profile v2.1 - Delegation Service , Apache CXF Producer running on Java version {}",
            System.getProperty("java.version"));
    LOGGER.info("Starting server...");

    startService(new GetActiveDelegationsResponderInterfaceImpl(delegationService), address2);
    startService(new GetDelegationResponderInterfaceImpl(delegationService), address3);
    startService(new GetInactiveDelegationsResponderInterfaceImpl(delegationService), address4);
    startService(new GetDelegationsbyUnitAndRoleResponderInterfaceImpl(delegationService), address5);
    startService(new GetDelegationsResponderInterfaceImpl(delegationService), address6);
    startService(new HasDelegationResponderInterfaceImpl(delegationService), address7);
    startService(new SaveDelegationsResponderInterfaceImpl(delegationService), address8);
    startService(new RemoveDelegationResponderInterfaceImpl(delegationService), address9);
    startService(new FindDelegationsResponderInterfaceImpl(delegationService), address10);
    startService(new PingForConfigurationResponderInterfaceImpl(delegationService), address11);

    LOGGER.info("Server ready!");
}

From source file:org.drools.server.CxfRsClientServerTest.java

@org.junit.Test
public void test1() throws Exception {
    ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext(
            "classpath:beans-test.xml");

    String cmd = "";
    cmd += "<batch-execution lookup=\"ksession1\">\n";
    cmd += "  <insert out-identifier=\"message\">\n";
    cmd += "      <org.drools.server.Message>\n";
    cmd += "         <text>Hello World</text>\n";
    cmd += "      </org.drools.server.Message>\n";
    cmd += "   </insert>\n";
    cmd += "</batch-execution>\n";

    CamelServerApp test = new CamelServerApp();
    String response = test.execute(cmd, (CamelContext) springContext.getBean("camel-client-ctx"));

    assertTrue(response.contains("execution-results"));
    assertTrue(response.contains("echo"));
    springContext.stop();/*from   w w  w  .  j a  v a 2s.  c  o m*/
}

From source file:org.drools.container.spring.SpringDroolsTest.java

@Test
public void testAgents() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "org/drools/container/spring/kagents-beans.xml");

    ResourceChangeScannerImpl scanner = (ResourceChangeScannerImpl) ResourceFactory
            .getResourceChangeScannerService();
    assertEquals(5, scanner.getInterval());

    KnowledgeBaseImpl kbase1 = (KnowledgeBaseImpl) context.getBean("kbase1");
    KnowledgeBaseImpl kbase2 = (KnowledgeBaseImpl) context.getBean("kbase2");

    KnowledgeAgentImpl kagent1 = (KnowledgeAgentImpl) context.getBean("kagent1");
    assertSame(kagent1.getKnowledgeBase(), kbase1);
    assertEquals(0, kagent1.getResourceDirectories().size());
    assertFalse(kagent1.isNewInstance());
    assertFalse(kagent1.isUseKBaseClassLoaderForCompiling());

    KnowledgeAgentImpl kagent2 = (KnowledgeAgentImpl) context.getBean("kagent2");
    assertSame(kagent2.getKnowledgeBase(), kbase2);
    assertEquals(1, kagent2.getResourceDirectories().size());
    assertFalse(kagent2.isNewInstance());
    assertFalse(kagent2.isUseKBaseClassLoaderForCompiling());

    KnowledgeAgentImpl kagent3 = (KnowledgeAgentImpl) context.getBean("kagent3");
    assertTrue(kagent3.isNewInstance());
    assertTrue(kagent3.isUseKBaseClassLoaderForCompiling());

    StatelessKnowledgeSessionImpl ksession1 = (StatelessKnowledgeSessionImpl) context.getBean("ksession1");
    assertSame(kbase1.getRuleBase(), ksession1.getRuleBase());
    assertSame(kagent1, ksession1.getKnowledgeAgent());

    StatefulKnowledgeSessionImpl ksession2 = (StatefulKnowledgeSessionImpl) context.getBean("ksession2");
    assertSame(kbase1.getRuleBase(), ksession2.getRuleBase());

    StatelessKnowledgeSessionImpl ksession3 = (StatelessKnowledgeSessionImpl) context.getBean("ksession3");
    assertSame(kagent2, ksession3.getKnowledgeAgent());
    assertSame(kbase2.getRuleBase(), ksession3.getRuleBase());

}

From source file:de.randi2.testUtility.utility.Bootstrap.java

public Bootstrap() throws ServiceException {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            "classpath:/META-INF/spring-bootstrap.xml");
    rolesAndRights = (RolesAndRights) ctx.getBean("rolesAndRights");
    entityManager = (((EntityManagerFactory) ctx.getBean("entityManagerFactory")).createEntityManager());
    passwordEncoder = (PasswordEncoder) ctx.getBean("passwordEncoder");
    saltSourceUser = (ReflectionSaltSource) ctx.getBean("saltSourceUser");
    saltSourceTrialSite = (SystemWideSaltSource) ctx.getBean("saltSourceTrialSite");
    trialService = (TrialService) ctx.getBean("trialService");
    trialSiteService = (TrialSiteService) ctx.getBean("trialSiteService");
    userService = (UserService) ctx.getBean("userService");
    dataSource = (DataSource) ctx.getBean("dataSource");
    init();//from   ww  w.  j  a  va 2  s .  c  o m
    try {

        IDatabaseConnection conn = new DatabaseConnection(dataSource.getConnection());
        ITableFilter filter = new DatabaseSequenceFilter(conn);
        IDataSet dataset = new FilteredDataSet(filter, conn.createDataSet());

        FlatXmlDataSet.write(dataset, new FileWriter(new File("testDBUNIT.xml")));

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}