KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > server > WrappingLoginModuleProxy


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 package org.apache.geronimo.security.jaas.server;
18
19 import java.security.Principal JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import javax.security.auth.Subject JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.login.LoginException JavaDoc;
27 import javax.security.auth.spi.LoginModule JavaDoc;
28
29 import org.apache.geronimo.security.DomainPrincipal;
30 import org.apache.geronimo.security.RealmPrincipal;
31
32
33 /**
34  * @version $Revision: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class WrappingLoginModuleProxy implements LoginModule JavaDoc {
37     private final LoginModule JavaDoc source;
38     private final String JavaDoc loginDomainName;
39     private final String JavaDoc realmName;
40     private final Subject JavaDoc localSubject = new Subject JavaDoc();
41     private Subject JavaDoc subject;
42
43     public WrappingLoginModuleProxy(LoginModule JavaDoc source, String JavaDoc loginDomainName, String JavaDoc realmName) {
44         this.source = source;
45         this.loginDomainName = loginDomainName;
46         this.realmName = realmName;
47     }
48
49     public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options) {
50         this.subject = subject;
51         source.initialize(localSubject, callbackHandler, sharedState, options);
52     }
53
54     public boolean login() throws LoginException JavaDoc {
55         return source.login();
56     }
57
58     public boolean abort() throws LoginException JavaDoc {
59         return source.abort();
60     }
61
62     public boolean commit() throws LoginException JavaDoc {
63         boolean result = source.commit();
64
65         Set JavaDoc wrapped = new HashSet JavaDoc();
66         for (Iterator JavaDoc iter = localSubject.getPrincipals().iterator(); iter.hasNext();) {
67             Principal JavaDoc principal = (Principal JavaDoc) iter.next();
68
69             wrapped.add(new DomainPrincipal(loginDomainName, principal));
70             wrapped.add(new RealmPrincipal(realmName, loginDomainName, principal));
71         }
72         localSubject.getPrincipals().addAll(wrapped);
73         subject.getPrincipals().addAll(localSubject.getPrincipals());
74
75         return result;
76     }
77
78     public boolean logout() throws LoginException JavaDoc {
79         boolean result = source.logout();
80
81         subject.getPrincipals().removeAll(localSubject.getPrincipals());
82         localSubject.getPrincipals().clear();
83
84         return result;
85     }
86 }
Popular Tags