1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import javax.resource.ResourceException ; 21 import javax.resource.spi.ApplicationServerInternalException ; 22 import javax.security.auth.Subject ; 23 24 import org.apache.geronimo.security.ContextManager; 25 26 34 public class SubjectInterceptor implements ConnectionInterceptor { 35 36 private final ConnectionInterceptor next; 37 38 public SubjectInterceptor(final ConnectionInterceptor next) { 39 this.next = next; 40 } 41 42 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 43 Subject currentSubject = null; 44 if (!connectionInfo.isApplicationManagedSecurity()) { 45 try { 46 currentSubject = ContextManager.getNextCaller(); 47 } catch (SecurityException e) { 48 throw new ResourceException ("Can not obtain Subject for login", e); 49 } 50 assert currentSubject != null; 51 } 52 ManagedConnectionInfo originalManagedConnectionInfo = connectionInfo.getManagedConnectionInfo(); 53 if (originalManagedConnectionInfo.getManagedConnection() == null) { 55 originalManagedConnectionInfo.setSubject(currentSubject); 56 next.getConnection(connectionInfo); 57 } else { 58 Subject oldSubject = originalManagedConnectionInfo.getSubject(); 59 if (currentSubject == null ? oldSubject != null : !currentSubject.equals(oldSubject)) { 60 if (connectionInfo.isUnshareable()) { 61 throw new ApplicationServerInternalException ("Unshareable resource is attempting to change security context: expected request under: " + oldSubject + ", received request under: " + currentSubject); 62 } else { 63 ConnectionInfo returningConnectionInfo = new ConnectionInfo(); 66 returningConnectionInfo.setManagedConnectionInfo(originalManagedConnectionInfo); 67 returningConnectionInfo.setConnectionHandle(connectionInfo.getConnectionHandle()); 70 71 ManagedConnectionInfo newManagedConnectionInfo = 73 new ManagedConnectionInfo( 74 originalManagedConnectionInfo.getManagedConnectionFactory(), 75 originalManagedConnectionInfo.getConnectionRequestInfo()); 76 newManagedConnectionInfo.setSubject(currentSubject); 77 connectionInfo.setManagedConnectionInfo(newManagedConnectionInfo); 78 next.getConnection(connectionInfo); 79 returnConnection(returningConnectionInfo, ConnectionReturnAction.RETURN_HANDLE); 81 } 82 } 83 } 84 next.getConnection(connectionInfo); 87 } 88 89 public void returnConnection( 90 ConnectionInfo connectionInfo, 91 ConnectionReturnAction connectionReturnAction) { 92 next.returnConnection(connectionInfo, connectionReturnAction); 93 } 94 95 public void destroy() { 96 next.destroy(); 97 } 98 } 99 | Popular Tags |