KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > JaasSecurityContext


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 package org.apache.geronimo.security.jaas;
18
19 import java.security.Principal JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.security.auth.Subject JavaDoc;
26
27 import org.apache.geronimo.security.ContextManager;
28 import org.apache.geronimo.security.RealmPrincipal;
29
30 /**
31  * Tracks security information about a single user. This is used before,
32  * during, and after the login.
33  *
34  * @version $Rev: 46019 $ $Date: 2004-09-14 05:56:06 -0400 (Tue, 14 Sep 2004) $
35  */

36 public class JaasSecurityContext {
37     private String JavaDoc realmName;
38     private Subject JavaDoc subject;
39     private long created;
40     private boolean done;
41     private JaasLoginModuleConfiguration[] modules;
42     private DecouplingCallbackHandler handler;
43     private Set JavaDoc processedPrincipals = new HashSet JavaDoc();
44
45     public JaasSecurityContext(String JavaDoc realmName, JaasLoginModuleConfiguration[] modules) {
46         this.realmName = realmName;
47         this.created = System.currentTimeMillis();
48         this.done = false;
49         this.modules = modules;
50         subject = new Subject JavaDoc();
51     }
52
53     public Subject JavaDoc getSubject() {
54         return subject;
55     }
56
57     public long getCreated() {
58         return created;
59     }
60
61     public boolean isDone() {
62         return done;
63     }
64
65     public void setDone(boolean done) {
66         this.done = done;
67     }
68
69     public JaasLoginModuleConfiguration[] getModules() {
70         return modules;
71     }
72
73     public DecouplingCallbackHandler getHandler() {
74         if(handler == null) { //lazy create
75
handler = new DecouplingCallbackHandler();
76         }
77         return handler;
78     }
79
80     public void processPrincipals(String JavaDoc loginDomainName) {
81         List JavaDoc list = new LinkedList JavaDoc();
82         for (Iterator JavaDoc it = subject.getPrincipals().iterator(); it.hasNext();) {
83             Principal JavaDoc p = (Principal JavaDoc) it.next();
84             if(!(p instanceof RealmPrincipal) && !processedPrincipals.contains(p)) {
85                 list.add(ContextManager.registerPrincipal(new RealmPrincipal(loginDomainName, p)));
86                 processedPrincipals.add(p);
87             }
88         }
89         subject.getPrincipals().addAll(list);
90     }
91
92     public void processPrincipals(Principal JavaDoc[] principals, String JavaDoc loginDomainName) {
93         List JavaDoc list = new LinkedList JavaDoc();
94         for (int i = 0; i < principals.length; i++) {
95             Principal JavaDoc p = principals[i];
96             list.add(p);
97             list.add(ContextManager.registerPrincipal(new RealmPrincipal(loginDomainName, p)));
98             processedPrincipals.add(p);
99         }
100         subject.getPrincipals().addAll(list);
101     }
102
103     public Set JavaDoc getProcessedPrincipals() {
104         return processedPrincipals;
105     }
106
107     public String JavaDoc getRealmName() {
108         return realmName;
109     }
110 }
111
Popular Tags