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.digitalgeneralists.assurance.ui.workers.DeleteScanDefinitionWorker.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  ava 2  s . co  m*/
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

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

    return this;
}

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

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

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

    return list;
}

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

@CrossOrigin
@RequestMapping(path = "/feeds/articles/{pageNumber}", method = RequestMethod.GET)
@ResponseBody/* ww w  .j a v a2s .c  om*/
public ResponseEntity<String> getArticles(@RequestHeader(value = "User-token") String userToken,
        @PathVariable int pageNumber) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserAuthDAO userAuthDAO = context.getBean(UserAuthDAO.class);
    FeedDAO feedDAO = context.getBean(FeedDAO.class);
    ArticleDAO articleDAO = context.getBean(ArticleDAO.class);

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

    List<Feed> feeds = feedDAO.findAll(userAuth);
    List<Article> articles = articleDAO.findArticlesByPageId(feeds, pageNumber);

    List<SuccessGetArticleWithIdResponse> finalList = new ArrayList<>();
    for (Article article : articles) {
        finalList.add(new SuccessGetArticleWithIdResponse(article.getId(), article.getFeed().getId(),
                article.getStatus().getLabel(), article.getLink(), article.getTitle(),
                article.getPreviewContent(), article.getFullContent()));
    }

    context.close();
    return new ResponseEntity(new SuccessGetFeedArticlesResponse(finalList), HttpStatus.OK);
}

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

public void testDeprecatedSetCommandMap() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {/*from ww  w .j a  v a 2s  .  c  om*/
        RuntimeExec deprecatedExec = (RuntimeExec) ctx.getBean("commandCheckDeprecatedSetCommandMap");
        assertNotNull(deprecatedExec);
        // Execute it
        deprecatedExec.execute();
    } finally {
        ctx.close();
    }
    // The best we can do is look at the log manually
    logger.warn("There should be a warning re. the use of deprecated 'setCommandMap'.");
}

From source file:org.activiti.crystalball.simulator.SimulateBottleneckTest.java

@Test
public void testOK() throws Exception {
    System.setProperty("liveDB", LIVE_DB);
    System.setProperty("_SIM_DB_PATH",
            tempDir + "/simulationRunDB-SimulateBottleNeck-OK-" + Thread.currentThread().getId());

    FileUtils.copyFile(new File(LIVE_DB + ".h2.db"), new File(
            tempDir + "/simulationRunDB-SimulateBottleNeck-OK-" + Thread.currentThread().getId() + ".h2.db"));

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "/org/activiti/crystalball/simulator/SimEngine-simulateBottleneck-h2-context.xml");
    ProcessEngine processEngine = (ProcessEngine) appContext.getBean("simProcessEngine");

    runSimulation(appContext, tempDir + "/SimulateBottleneckTest-1.png");

    processEngine.close();/*w ww  .  ja  v a  2  s . co m*/
    appContext.close();

    File expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/simulateBottleneckTest-1.png");
    File generated = new File(tempDir + "/SimulateBottleneckTest-1.png");
    assertTrue(FileUtils.contentEquals(expected, generated));
    // delete database file
    File f = new File(System.getProperty("_SIM_DB_PATH") + ".h2.db");
    if (!f.delete())
        System.err.println("unable to delete file");
}

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

@Test
public void testRegisterMetrics() {
    ClassPathXmlApplicationContext ctx = null;
    try {//www. j a  v a 2  s  . co m
        ctx = new ClassPathXmlApplicationContext("classpath:register-element-test.xml");
        MetricRegistry registry = ctx.getBean(MetricRegistry.class);
        Assert.assertTrue(registry.getMetrics().size() > 0);
        for (String metricName : registry.getMetrics().keySet()) {
            Assert.assertTrue(metricName.startsWith("jvm."));
        }
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

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

@CrossOrigin
@RequestMapping(path = "/feed/{feedId}/articles/{pageNumber}", method = RequestMethod.GET)
@ResponseBody/*from w w  w.  j  av a2s.  c  om*/
public ResponseEntity<String> getFeedArticles(@RequestHeader(value = "User-token") String userToken,
        @PathVariable int feedId, @PathVariable int pageNumber) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserAuthDAO userAuthDAO = context.getBean(UserAuthDAO.class);
    FeedDAO feedDAO = context.getBean(FeedDAO.class);
    ArticleDAO articleDAO = context.getBean(ArticleDAO.class);

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

    Feed feed = feedDAO.findById(userAuth, feedId);
    if (feed == null) {
        context.close();
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }

    List<Article> articles = articleDAO.findArticlesByFeedAndPageId(feed, pageNumber);

    List<SuccessGetArticleWithIdResponse> finalList = new ArrayList<>();
    for (Article article : articles) {
        finalList.add(new SuccessGetArticleWithIdResponse(article.getId(), article.getFeed().getId(),
                article.getStatus().getLabel(), article.getLink(), article.getTitle(),
                article.getPreviewContent(), article.getFullContent()));
    }

    context.close();
    return new ResponseEntity(new SuccessGetFeedArticlesResponse(finalList), HttpStatus.OK);
}

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

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

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

    return config;
}

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

@CrossOrigin
@RequestMapping(path = "/user/{username}", method = RequestMethod.PUT)
@ResponseBody/* ww  w  .j a va  2  s  .  c  o  m*/
public ResponseEntity<String> updateUser(@RequestHeader(value = "User-token") String userToken,
        @PathVariable String username, @RequestBody User userRequest, BindingResult bindingResult) {

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

    userUpdateValidator.validate(userRequest, bindingResult);
    if (bindingResult.hasErrors()) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("bad_params"), HttpStatus.BAD_REQUEST);
    }

    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())) || (userRequest.getType() != null && !isAdmin)) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("admin_required"), HttpStatus.FORBIDDEN);
    }

    if (userDAO.doesExist(userRequest.getUsername())
            && !user.getUsername().equalsIgnoreCase(userRequest.getUsername())) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("already_exist"), HttpStatus.BAD_REQUEST);
    }

    userDAO.updateUser(user, userRequest, isAdmin);
    context.close();

    return new ResponseEntity(new SuccessMessageResponse("success"), HttpStatus.OK);
}

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

@Override
public ApplicationConfiguration 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  ww w  . j a  va2  s.  c  o  m
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        modelDelegate.saveApplicationConfiguration(this.configuration);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return this.configuration;
}