Skip to content

Commit

Permalink
Merge branch 'master' of github.com:serac/cas
Browse files Browse the repository at this point in the history
  • Loading branch information
serac committed Oct 10, 2011
2 parents e12da0e + 2234cfe commit e5d0c71
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 70 deletions.
Expand Up @@ -8,45 +8,47 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceContext;
import javax.validation.constraints.NotNull;

import org.jasig.cas.ticket.ServiceTicket;
import org.jasig.cas.ticket.ServiceTicketImpl;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.TicketGrantingTicketImpl;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
*
* JPA implementation of a CAS {@link TicketRegistry}. This implementation of
* ticket registry is suitable for HA environments.
*
* @author Scott Battaglia
* @author Marvin S. Addison
*
* @version $Revision: 1.1 $ $Date: 2005/08/19 18:27:17 $
* @since 3.2.1
*
*/
public final class JpaTicketRegistry extends AbstractDistributedTicketRegistry {

@NotNull
private JpaTemplate jpaTemplate;
@PersistenceContext
private EntityManager entityManager;

@NotNull
private String ticketGrantingTicketPrefix = "TGT";

public JpaTicketRegistry(final EntityManagerFactory factory) {
this.jpaTemplate = new JpaTemplate(factory);
}


protected void updateTicket(final Ticket ticket) {
this.jpaTemplate.merge(ticket);
entityManager.merge(ticket);
}

@Transactional(readOnly = false)
public void addTicket(final Ticket ticket) {
this.jpaTemplate.persist(ticket);
entityManager.persist(ticket);
}

@Transactional(readOnly = false)
Expand All @@ -63,14 +65,19 @@ public boolean deleteTicket(final String ticketId) {
}

deleteTicketAndChildren(ticket);
return true;
return true;
}

private void deleteTicketAndChildren(final Ticket ticket) {
final Map<String,Object> params = new HashMap<String,Object>();
params.put("id", ticket.getId());
final List<TicketGrantingTicketImpl> ticketGrantingTicketImpls = this.jpaTemplate.findByNamedParams("select t from TicketGrantingTicketImpl t where t.ticketGrantingTicket.id = :id", params);
final List<ServiceTicketImpl> serviceTicketImpls = this.jpaTemplate.findByNamedParams("select s from ServiceTicketImpl s where s.ticketGrantingTicket.id = :id", params);
final List<TicketGrantingTicketImpl> ticketGrantingTicketImpls = entityManager
.createQuery("select t from TicketGrantingTicketImpl t where t.ticketGrantingTicket.id = :id", TicketGrantingTicketImpl.class)
.setLockMode(LockModeType.PESSIMISTIC_WRITE)
.setParameter("id", ticket.getId())
.getResultList();
final List<ServiceTicketImpl> serviceTicketImpls = entityManager
.createQuery("select s from ServiceTicketImpl s where s.ticketGrantingTicket.id = :id", ServiceTicketImpl.class)
.setParameter("id", ticket.getId())
.getResultList();

for (final ServiceTicketImpl s : serviceTicketImpls) {
removeTicket(s);
Expand All @@ -89,23 +96,24 @@ private void removeTicket(final Ticket ticket) {
final Date creationDate = new Date(ticket.getCreationTime());
log.debug("Removing Ticket >" + ticket.getId() + "< created: " + creationDate.toString());
}
this.jpaTemplate.remove(ticket);
entityManager.remove(ticket);
} catch (final Exception e) {
log.error("Error removing " + ticket + " from registry.", e);
}
}

@Transactional(readOnly=true)
public Ticket getTicket(final String ticketId) {
return getProxiedTicketInstance(getRawTicket(ticketId));
}

private Ticket getRawTicket(final String ticketId) {
try {
if (ticketId.startsWith(this.ticketGrantingTicketPrefix)) {
return this.jpaTemplate.find(TicketGrantingTicketImpl.class, ticketId);
return entityManager.find(TicketGrantingTicketImpl.class, ticketId, LockModeType.PESSIMISTIC_WRITE);
}

return this.jpaTemplate.find(ServiceTicketImpl.class, ticketId);
return entityManager.find(ServiceTicketImpl.class, ticketId);
} catch (final Exception e) {
log.error("Error getting ticket " + ticketId + " from registry.", e);
}
Expand All @@ -114,8 +122,12 @@ private Ticket getRawTicket(final String ticketId) {

@Transactional(readOnly=true)
public Collection<Ticket> getTickets() {
final List<TicketGrantingTicketImpl> tgts = this.jpaTemplate.find("select t from TicketGrantingTicketImpl t");
final List<ServiceTicketImpl> sts = this.jpaTemplate.find("select s from ServiceTicketImpl s");
final List<TicketGrantingTicketImpl> tgts = entityManager
.createQuery("select t from TicketGrantingTicketImpl t", TicketGrantingTicketImpl.class)
.getResultList();
final List<ServiceTicketImpl> sts = entityManager
.createQuery("select s from ServiceTicketImpl s", ServiceTicketImpl.class)
.getResultList();

final List<Ticket> tickets = new ArrayList<Ticket>();
tickets.addAll(tgts);
Expand Down
75 changes: 75 additions & 0 deletions cas-server-core/src/test/java/org/jasig/cas/mock/MockService.java
@@ -0,0 +1,75 @@
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.cas.mock;

import java.util.Map;

import org.jasig.cas.authentication.principal.Principal;
import org.jasig.cas.authentication.principal.Response;
import org.jasig.cas.authentication.principal.Service;

/**
* Simple mock implementation of a service principal.
*
* @author Marvin S. Addison
* @version $Revision: $
*
*/
public class MockService implements Service {
/** MockService.java */
private static final long serialVersionUID = 117438127028057173L;
private boolean loggedOut = false;
private String id;

public MockService(final String id) {
this.id = id;
}

public String getArtifactId() {
return null;
}

public Response getResponse(String ticketId) {
return null;
}

public boolean logOutOfService(final String sessionIdentifier) {
this.loggedOut = true;
return false;
}

public boolean isLoggedOut() {
return this.loggedOut;
}

public void setPrincipal(final Principal principal) {}

public Map<String, Object> getAttributes() {
return null;
}

public String getId() {
return id;
}

public boolean matches(final Service service) {
return true;
}

}
Expand Up @@ -7,14 +7,10 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jasig.cas.TestUtils;
import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.authentication.principal.Principal;
import org.jasig.cas.authentication.principal.Response;
import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.authentication.principal.WebApplicationService;
import org.jasig.cas.mock.MockService;
import org.jasig.cas.ticket.support.NeverExpiresExpirationPolicy;
import org.jasig.cas.util.DefaultUniqueTicketIdGenerator;
import org.jasig.cas.util.UniqueTicketIdGenerator;
Expand Down Expand Up @@ -138,7 +134,7 @@ public void testServiceTicketAsFromNotInitialCredentials() {
}

public void testWebApplicationSignOut() {
final TestService testService = new TestService();
final MockService testService = new MockService("test");
TicketGrantingTicket t = new TicketGrantingTicketImpl("test", null,
TestUtils.getAuthentication(), new NeverExpiresExpirationPolicy());
t.grantServiceTicket(this.uniqueTicketIdGenerator
Expand All @@ -149,46 +145,4 @@ public void testWebApplicationSignOut() {

assertTrue(testService.isLoggedOut());
}

protected static class TestService implements WebApplicationService {

/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = -8318147503545267651L;
private boolean loggedOut = false;

public String getArtifactId() {
return null;
}

public Response getResponse(String ticketId) {
return null;
}

public boolean logOutOfService(final String sessionIdentifier) {
this.loggedOut = true;
return false;
}

public boolean isLoggedOut() {
return this.loggedOut;
}

public void setPrincipal(Principal principal) {
// nothing to do
}

public Map<String, Object> getAttributes() {
return null;
}

public String getId() {
return "test";
}

public boolean matches(final Service service) {
return true;
}
}
}

0 comments on commit e5d0c71

Please sign in to comment.