KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > SubjectInterceptor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.outbound;
19
20 import javax.resource.ResourceException JavaDoc;
21 import javax.resource.spi.ApplicationServerInternalException JavaDoc;
22 import javax.security.auth.Subject JavaDoc;
23
24 import org.apache.geronimo.security.ContextManager;
25
26 /**
27  * SubjectInterceptor.java
28  *
29  *
30  * Created: Mon Oct 6 14:31:56 2003
31  *
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  */

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 JavaDoc {
43         Subject JavaDoc currentSubject = null;
44         if (!connectionInfo.isApplicationManagedSecurity()) {
45             try {
46                 currentSubject = ContextManager.getNextCaller();
47             } catch (SecurityException JavaDoc e) {
48                 throw new ResourceException JavaDoc("Can not obtain Subject for login", e);
49             }
50             assert currentSubject != null;
51         }
52         ManagedConnectionInfo originalManagedConnectionInfo = connectionInfo.getManagedConnectionInfo();
53         //No existing managed connection, get an appropriate one and return.
54
if (originalManagedConnectionInfo.getManagedConnection() == null) {
55             originalManagedConnectionInfo.setSubject(currentSubject);
56             next.getConnection(connectionInfo);
57         } else {
58             Subject JavaDoc oldSubject = originalManagedConnectionInfo.getSubject();
59             if (currentSubject == null ? oldSubject != null : !currentSubject.equals(oldSubject)) {
60                 if (connectionInfo.isUnshareable()) {
61                     throw new ApplicationServerInternalException JavaDoc("Unshareable resource is attempting to change security context: expected request under: " + oldSubject + ", received request under: " + currentSubject);
62                 } else {
63                     //existing managed connection, wrong subject: must re-associate.
64
//make a ConnectionInfo to process removing the handle from the old mc
65
ConnectionInfo returningConnectionInfo = new ConnectionInfo();
66                     returningConnectionInfo.setManagedConnectionInfo(originalManagedConnectionInfo);
67                     //This should decrement handle count, but not close the handle, when returnConnection is called
68
//I'm not sure how to test/assure this.
69
returningConnectionInfo.setConnectionHandle(connectionInfo.getConnectionHandle());
70
71                     //make a new ManagedConnectionInfo for the mc we will ask for
72
ManagedConnectionInfo newManagedConnectionInfo =
73                             new ManagedConnectionInfo(
74                                     originalManagedConnectionInfo.getManagedConnectionFactory(),
75                                     originalManagedConnectionInfo.getConnectionRequestInfo());
76                     newManagedConnectionInfo.setSubject(currentSubject);
77                     connectionInfo.setManagedConnectionInfo(newManagedConnectionInfo);
78                     next.getConnection(connectionInfo);
79                     //process the removal of the handle from the previous mc
80
returnConnection(returningConnectionInfo, ConnectionReturnAction.RETURN_HANDLE);
81                 }
82             }
83         }
84         //otherwise, the current ManagedConnection matches the security info, we keep it.
85
//set up the tx context
86
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