KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > bridge > CallerIdentityUserPasswordRealmBridge


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
18 package org.apache.geronimo.security.bridge;
19
20 import java.util.Set JavaDoc;
21 import javax.security.auth.Subject JavaDoc;
22 import javax.security.auth.callback.Callback JavaDoc;
23 import javax.security.auth.callback.CallbackHandler JavaDoc;
24 import javax.security.auth.callback.NameCallback JavaDoc;
25 import javax.security.auth.callback.PasswordCallback JavaDoc;
26 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
27
28 import org.apache.geronimo.gbean.GBeanInfo;
29 import org.apache.geronimo.gbean.GBeanInfoBuilder;
30 import org.apache.geronimo.security.realm.providers.GeronimoPasswordCredential;
31
32
33 /**
34  * @version $Rev: 56022 $ $Date: 2004-10-29 22:16:18 -0700 (Fri, 29 Oct 2004) $
35  */

36 public class CallerIdentityUserPasswordRealmBridge extends AbstractRealmBridge {
37
38     private static final GBeanInfo GBEAN_INFO;
39
40     public CallerIdentityUserPasswordRealmBridge() {
41     }
42
43     public CallerIdentityUserPasswordRealmBridge(String JavaDoc targetRealm) {
44         super(targetRealm);
45     }
46
47
48     protected CallbackHandler JavaDoc getCallbackHandler(final Subject JavaDoc sourceSubject) {
49         return new CallbackHandler JavaDoc() {
50             public void handle(Callback JavaDoc[] callbacks) throws UnsupportedCallbackException JavaDoc {
51                 Set JavaDoc credentials = sourceSubject.getPrivateCredentials(GeronimoPasswordCredential.class);
52                 if (credentials == null || credentials.size() != 1) {
53                     throw new UnsupportedCallbackException JavaDoc(null, "No GeronimoPasswordCredential to read");
54                 }
55                 GeronimoPasswordCredential geronimoPasswordCredential = (GeronimoPasswordCredential) credentials.iterator().next();
56                 for (int i = 0; i < callbacks.length; i++) {
57                     Callback JavaDoc callback = callbacks[i];
58                     if (callback instanceof NameCallback JavaDoc) {
59                         ((NameCallback JavaDoc) callback).setName(geronimoPasswordCredential.getUserName());
60                     } else if (callback instanceof PasswordCallback JavaDoc) {
61                         ((PasswordCallback JavaDoc) callback).setPassword(geronimoPasswordCredential.getPassword());
62                     } else {
63                         throw new UnsupportedCallbackException JavaDoc(callback, "Only name and password callbacks supported");
64                     }
65
66                 }
67             }
68
69         };
70     }
71
72     static {
73         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(CallerIdentityUserPasswordRealmBridge.class, AbstractRealmBridge.GBEAN_INFO);
74         GBEAN_INFO = infoFactory.getBeanInfo();
75     }
76
77     public static GBeanInfo getGBeanInfo() {
78         return GBEAN_INFO;
79     }
80
81 }
82
Popular Tags