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.chevres.rss.restapi.controller.ArticleController.java

@CrossOrigin
@RequestMapping(path = "/article/as_read/{articleId}", method = RequestMethod.POST)
@ResponseBody//from   w  w  w  .j a va2  s. c  o  m
public ResponseEntity<String> markArticleAsRead(@RequestHeader(value = "User-token") String userToken,
        @PathVariable int articleId) {

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

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

    Article article = articleDAO.findById(userAuth, articleId);
    if (article == null) {
        context.close();
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }

    ArticleState newState = articleStateDAO.findByLabel(ArticleState.READ_LABEL);
    articleDAO.markAsRead(article, newState);

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

From source file:com.chevres.rss.worker.controller.WorkerController.java

@CrossOrigin
@RequestMapping(path = "/worker/refresh/feed/{feedId}", method = RequestMethod.GET)
@ResponseBody/*  w  ww. j  a v a2  s  . c o m*/
public ResponseEntity<String> refreshFeed(@RequestHeader(value = "User-token") String userToken,
        @PathVariable int feedId) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    FeedDAO feedDAO = context.getBean(FeedDAO.class);
    ArticleDAO articleDAO = context.getBean(ArticleDAO.class);
    ArticleStateDAO articleStateDAO = context.getBean(ArticleStateDAO.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);
    }
    Feed feed = feedDAO.findById(userAuth, feedId);
    if (feed == null) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("bad_params"), HttpStatus.BAD_REQUEST);
    }
    FeedUpdater feedUpdater = new FeedUpdater(feedDAO, articleDAO, articleStateDAO);
    if (feedUpdater.updateFeed(feed)) {
        context.close();
        return new ResponseEntity(new SuccessMessageResponse("success"), HttpStatus.OK);
    } else {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("error"), HttpStatus.BAD_REQUEST);
    }
}

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

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

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

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

    ComparisonResult result = null;/*from w w  w. ja  va  2s  . 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");

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

    return result;
}

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

@CrossOrigin
@RequestMapping(path = "/feed/{feedId}", method = RequestMethod.DELETE)
@ResponseBody//from w  w w .ja  v  a2s  .c  om
public ResponseEntity<String> deleteFeed(@RequestHeader(value = "User-token") String userToken,
        @PathVariable int feedId) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    FeedDAO feedDAO = context.getBean(FeedDAO.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);
    }

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

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

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;/*ww w  .j  av  a  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.chevres.rss.restapi.controller.UserController.java

@CrossOrigin
@RequestMapping(path = "/user/{username}", method = RequestMethod.DELETE)
@ResponseBody/*  w  w w . j a  v  a2 s .c  o  m*/
public ResponseEntity<String> deleteUser(@RequestHeader(value = "User-token") String userToken,
        @PathVariable String username) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserDAO userDAO = context.getBean(UserDAO.class);
    FeedDAO feedDAO = context.getBean(FeedDAO.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())) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("admin_required"), HttpStatus.FORBIDDEN);
    }

    userAuthDAO.removeAllForUser(user);
    feedDAO.removeAllForUser(user);
    userDAO.delete(user);
    context.close();

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

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

public void testExecOfNeverEndingProcess() {
    File dir = new File(DIR);
    dir.mkdir();/*  w  w  w .  j av  a 2s .c o  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:de.uniwue.dmir.heatmap.SpringTest.java

public void testHeatmap() throws IOException {

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "spring/applicationContext.xml");

    @SuppressWarnings("unchecked")
    IGeoDatasource<SimpleGeoPoint<String>> geoDataSource = appContext.getBean(IGeoDatasource.class);

    List<SimpleGeoPoint<String>> points = geoDataSource
            .getData(new GeoBoundingBox(new GeoCoordinates(10, 10), new GeoCoordinates(100, 100)));

    for (int i = 0; i < 10; i++) {
        System.out.println(points.get(i));
    }/*from ww w  .  j  a v a  2 s  .co m*/

    appContext.close();

}

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

@Test
public void positiveContextLoadingTest() {
    ClassPathXmlApplicationContext ctx = null;
    try {/*w ww.  j  a  v  a 2  s  .c  o m*/
        ctx = new ClassPathXmlApplicationContext("classpath:proxy-target-class-enabled.xml");
        Assert.assertNotNull("Expected to be able to get ProxyTargetClass by class.",
                ctx.getBean(ProxyTargetClass.class));
        Assert.assertNotNull("Expected to be able to get ProxyTargetClass from AutowiredCollaborator.",
                ctx.getBean(AutowiredCollaborator.class).getDependency());
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}