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

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

Introduction

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

Prototype

@Override
public void close() 

Source Link

Document

Close this application context, destroying all beans in its bean factory.

Usage

From source file:com.ryantenney.metrics.spring.RegistryTest.java

@Test
public void testDefaultRegistries() {
    ClassPathXmlApplicationContext ctx = null;
    try {/*from  w ww.  ja v  a  2 s  .c o  m*/
        ctx = new ClassPathXmlApplicationContext("classpath:default-registries.xml");
        Assert.assertNotNull("Should have a MetricRegistry bean.", ctx.getBean(MetricRegistry.class));
        Assert.assertNotNull("Should have a HealthCheckRegistry bean.", ctx.getBean(HealthCheckRegistry.class));
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:de.uniwue.dmir.heatmap.SpringTest3.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testHeatmap() throws IOException {

    System.setProperty("minTimestamp", "2013-06-01 00:00:00");
    System.setProperty("maxTimestamp", "2013-07-30 00:00:00");
    System.setProperty("workDir", "out/basic/work-jar");
    System.setProperty("configDir", "src/main/resources/spring/example/basic/config");

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            new String[] { "spring/example/basic/config/config.xml" }, false);
    appContext.refresh();//from ww w . ja  va2 s .  c om

    IHeatmap heatmap = appContext.getBean(HEATMAP_BEAN, IHeatmap.class);

    ITileProcessor tileProcessor = appContext.getBean(WRITER_BEAN, ITileProcessor.class);

    heatmap.processTiles(tileProcessor);

    tileProcessor.close();
    appContext.close();
}

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

public void testSimpleSuccess() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {/*w  w  w.ja  v  a  2 s.co  m*/
        RuntimeExec dirRootExec = (RuntimeExec) ctx.getBean("commandListRootDir");
        assertNotNull(dirRootExec);
        // Execute it
        dirRootExec.execute();
    } finally {
        ctx.close();
    }
}

From source file:com.ryantenney.metrics.spring.DefaultRegistryTest.java

@Test
public void testSuppliedRegistries() {
    ClassPathXmlApplicationContext ctx = null;
    try {//from   w w  w. j  a v  a 2  s  . c om
        ctx = new ClassPathXmlApplicationContext("classpath:supplied-registries.xml");
        Assert.assertNotSame("Should have provided MetricRegistry.", ctx.getBean(MetricRegistry.class));
        Assert.assertNotSame("Should have provided HealthCheckRegistry.",
                ctx.getBean(HealthCheckRegistry.class));
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

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

@Override
protected Object doInBackground() throws Exception {
    // 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 {//  w  w w  .  j av a 2 s .  co  m
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        modelDelegate.deleteScan(scan);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return this;
}

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  a  v a2 s. c om
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.LoadScansWorker.java

@Override
protected List<Scan> doInBackground() throws Exception {
    List<Scan> list = 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  ww .  j a  v a  2  s  .  c  om
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        list = modelDelegate.getScans();
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return list;
}

From source file:com.chevres.rss.restapi.controller.UserController.java

@CrossOrigin
@RequestMapping(path = "/user/{username}", method = RequestMethod.GET)
@ResponseBody// w  w w  .j  ava 2 s. co  m
public ResponseEntity<String> getUser(@RequestHeader(value = "User-token") String userToken,
        @PathVariable String username) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserDAO userDAO = context.getBean(UserDAO.class);
    UserAuthDAO userAuthDAO = context.getBean(UserAuthDAO.class);

    UserAuth userAuth = userAuthDAO.findByToken(userToken);
    if (userAuth == null) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("invalid_token"), HttpStatus.BAD_REQUEST);
    }

    User user = userDAO.findByUsername(username);
    if (user == null) {
        context.close();
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }

    boolean isAdmin = userDAO.isAdmin(userAuth.getIdUser());
    if (!isAdmin && (userAuth.getIdUser() != user.getId())) {
        return new ResponseEntity(new ErrorMessageResponse("admin_required"), HttpStatus.FORBIDDEN);
    }

    context.close();

    return new ResponseEntity(new SuccessGetUserResponse(user.getUsername(), user.getType()), HttpStatus.OK);
}

From source file:com.chevres.rss.restapi.controller.UserController.java

@CrossOrigin
@RequestMapping(path = "/users", method = RequestMethod.GET)
@ResponseBody// ww  w  .  j a v a2  s.com
public ResponseEntity<String> getUsers(@RequestHeader(value = "User-token") String userToken) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserDAO userDAO = context.getBean(UserDAO.class);
    UserAuthDAO userAuthDAO = context.getBean(UserAuthDAO.class);

    UserAuth userAuth = userAuthDAO.findByToken(userToken);
    if (userAuth == null) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("invalid_token"), HttpStatus.BAD_REQUEST);
    }

    boolean isAdmin = userDAO.isAdmin(userAuth.getIdUser());
    if (!isAdmin) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("admin_required"), HttpStatus.FORBIDDEN);
    }

    List<User> users = userDAO.findEveryone();
    List<SuccessGetUserWithIdResponse> finalList = new ArrayList<>();
    for (User user : users) {
        finalList.add(new SuccessGetUserWithIdResponse(user.getId(), user.getUsername(), user.getType()));
    }

    context.close();

    return new ResponseEntity(new SuccessGetUsersResponse(finalList), HttpStatus.OK);
}

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

@Override
public SwingWorker<Object, Object> doInBackground() {
    // 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");

        modelDelegate.saveScanDefinition(scanDefinition);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return this;
}