Search in sources :

Example 1 with AgentCookie

use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.

the class AgentDao method findAgentCookieByUuid.

private AgentCookie findAgentCookieByUuid(final AgentIdentifier agentIdentifier) {
    return (AgentCookie) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from AgentCookie where uuid = :uuid");
            query.setString("uuid", agentIdentifier.getUuid());
            return query.uniqueResult();
        }
    });
}
Also used : Query(org.hibernate.Query) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) AgentCookie(com.thoughtworks.go.server.domain.AgentCookie) Session(org.hibernate.Session)

Example 2 with AgentCookie

use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.

the class AgentDao method cookieFor.

public String cookieFor(final AgentIdentifier agentIdentifier) {
    String key = agentCacheKey(agentIdentifier);
    AgentCookie cookie = (AgentCookie) cache.get(key);
    if (cookie == null) {
        synchronized (key) {
            cookie = findAgentCookieByUuid(agentIdentifier);
            cache.put(key, cookie);
        }
    }
    if (cookie != null) {
        return cookie.getCookie();
    }
    return null;
}
Also used : AgentCookie(com.thoughtworks.go.server.domain.AgentCookie)

Example 3 with AgentCookie

use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.

the class AgentDaoTest method shouldCacheCookieForAgent.

@Test
public void shouldCacheCookieForAgent() throws Exception {
    AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
    agentDao.associateCookie(agentIdentifier, "cookie");
    assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
    hibernateTemplate.execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            AgentCookie agentCookie = (AgentCookie) session.createQuery("from AgentCookie where uuid = 'uuid'").uniqueResult();
            agentCookie.updateCookie("updated_cookie");
            session.update(agentCookie);
            return null;
        }
    });
    assertThat(cookieForUuid(), is("updated_cookie"));
    assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
    goCache.clear();
    assertThat(agentDao.cookieFor(agentIdentifier), is("updated_cookie"));
}
Also used : HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) AgentIdentifier(com.thoughtworks.go.remote.AgentIdentifier) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Session(org.hibernate.Session) AgentCookie(com.thoughtworks.go.server.domain.AgentCookie) Test(org.junit.Test)

Example 4 with AgentCookie

use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.

the class AgentDaoTest method shouldNotClearCacheIfTransactionFails.

@Test
public void shouldNotClearCacheIfTransactionFails() throws Exception {
    AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
    agentDao.associateCookie(agentIdentifier, "cookie");
    assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
    hibernateTemplate.execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            AgentCookie agentCookie = (AgentCookie) session.createQuery("from AgentCookie where uuid = 'uuid'").uniqueResult();
            agentCookie.updateCookie("updated_cookie");
            session.update(agentCookie);
            return null;
        }
    });
    assertThat(cookieForUuid(), is("updated_cookie"));
    agentDao.setHibernateTemplate(mockHibernateTemplate);
    doThrow(new RuntimeException("holy smoke")).when(mockHibernateTemplate).saveOrUpdate(any(AgentCookie.class));
    try {
        agentDao.associateCookie(agentIdentifier, "cookie");
        fail("should have propagated saveOrUpdate exception");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("holy smoke"));
    }
    assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
}
Also used : HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) AgentIdentifier(com.thoughtworks.go.remote.AgentIdentifier) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) AgentCookie(com.thoughtworks.go.server.domain.AgentCookie) Test(org.junit.Test)

Aggregations

AgentCookie (com.thoughtworks.go.server.domain.AgentCookie)4 Session (org.hibernate.Session)3 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)3 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)2 SQLException (java.sql.SQLException)2 HibernateException (org.hibernate.HibernateException)2 Test (org.junit.Test)2 Query (org.hibernate.Query)1