Example usage for org.springframework.web.context.request WebRequest getAttribute

List of usage examples for org.springframework.web.context.request WebRequest getAttribute

Introduction

In this page you can find the example usage for org.springframework.web.context.request WebRequest getAttribute.

Prototype

@Nullable
Object getAttribute(String name, int scope);

Source Link

Document

Return the value for the scoped attribute of the given name, if any.

Usage

From source file:org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor.java

/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
 *//*from  ww  w. j  a v a 2s.co  m*/
@Override
public void preHandle(WebRequest request) throws DataAccessException {
    String participateAttributeName = getParticipateAttributeName();

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    if (asyncManager.hasConcurrentResult()) {
        if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
            return;
        }
    }

    if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
        // Do not modify the Session: just mark the request accordingly.
        Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        int newCount = (count != null ? count + 1 : 1);
        request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
    } else {
        logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
        Session session = openSession();
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

        AsyncRequestInterceptor asyncRequestInterceptor = new AsyncRequestInterceptor(getSessionFactory(),
                sessionHolder);
        asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
        asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
    }
}

From source file:org.springframework.orm.hibernate43.support.OpenSessionInViewInterceptor.java

/**
 * Open a new Hibernate {@code Session} according to the settings of this
 * {@code HibernateAccessor} and bind it to the thread via the
 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
 *///from   ww  w.ja va 2  s  . c o m
public void preHandle(WebRequest request) throws DataAccessException {

    String participateAttributeName = getParticipateAttributeName();

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    if (asyncManager.hasConcurrentResult()) {
        if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
            return;
        }
    }

    if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
        // Do not modify the Session: just mark the request accordingly.
        Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        int newCount = (count != null ? count + 1 : 1);
        request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
    } else {
        logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
        Session session = openSession();
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

        AsyncRequestInterceptor asyncRequestInterceptor = new AsyncRequestInterceptor(getSessionFactory(),
                sessionHolder);
        asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
        asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
    }
}

From source file:org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor.java

/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 *///from  w w w.jav a  2  s. c o  m
@Override
public void preHandle(WebRequest request) throws DataAccessException {
    String participateAttributeName = getParticipateAttributeName();

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    if (asyncManager.hasConcurrentResult()) {
        if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
            return;
        }
    }

    if (TransactionSynchronizationManager.hasResource(obtainSessionFactory())) {
        // Do not modify the Session: just mark the request accordingly.
        Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        int newCount = (count != null ? count + 1 : 1);
        request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
    } else {
        logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
        Session session = openSession();
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.bindResource(obtainSessionFactory(), sessionHolder);

        AsyncRequestInterceptor asyncRequestInterceptor = new AsyncRequestInterceptor(obtainSessionFactory(),
                sessionHolder);
        asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
        asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
    }
}

From source file:org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor.java

@Override
public void preHandle(WebRequest request) throws DataAccessException {
    if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
        // Do not modify the PersistenceManager: just mark the request accordingly.
        String participateAttributeName = getParticipateAttributeName();
        Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        int newCount = (count != null ? count + 1 : 1);
        request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
    } else {//w  w  w.  j a  va 2s  .c  om
        logger.debug("Opening JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
        PersistenceManager pm = PersistenceManagerFactoryUtils
                .getPersistenceManager(getPersistenceManagerFactory(), true);
        TransactionSynchronizationManager.bindResource(getPersistenceManagerFactory(),
                new PersistenceManagerHolder(pm));
    }
}

From source file:org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor.java

@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
    String participateAttributeName = getParticipateAttributeName();
    Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
    if (count != null) {
        // Do not modify the PersistenceManager: just clear the marker.
        if (count > 1) {
            request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
        } else {/* w  w  w .jav  a2 s  .  c  o m*/
            request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        }
    } else {
        PersistenceManagerHolder pmHolder = (PersistenceManagerHolder) TransactionSynchronizationManager
                .unbindResource(getPersistenceManagerFactory());
        logger.debug("Closing JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
        PersistenceManagerFactoryUtils.releasePersistenceManager(pmHolder.getPersistenceManager(),
                getPersistenceManagerFactory());
    }
}