KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.security.auth.Subject JavaDoc;
21 import javax.security.auth.callback.Callback JavaDoc;
22 import javax.security.auth.callback.CallbackHandler JavaDoc;
23 import javax.security.auth.callback.NameCallback JavaDoc;
24 import javax.security.auth.callback.PasswordCallback JavaDoc;
25 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
26
27 import org.apache.geronimo.gbean.GBeanInfo;
28 import org.apache.geronimo.gbean.GBeanInfoBuilder;
29
30
31 /**
32  * ConfiguredIdentityRealmBridge supplies a constant mapping between realms:
33  * it always returns the configured user and password, no matter what the
34  * source realm or source subject.
35  *
36  * @version $Rev: 56022 $ $Date: 2004-10-29 22:16:18 -0700 (Fri, 29 Oct 2004) $
37  */

38 public class ConfiguredIdentityUserPasswordRealmBridge extends AbstractRealmBridge {
39     private String JavaDoc configuredUser;
40     private char[] configuredPassword;
41
42     public ConfiguredIdentityUserPasswordRealmBridge() {
43     }
44
45     public ConfiguredIdentityUserPasswordRealmBridge(String JavaDoc targetRealm, String JavaDoc configuredUser, String JavaDoc configuredPassword) {
46         super(targetRealm);
47         this.configuredUser = configuredUser;
48         setConfiguredPassword(configuredPassword);
49     }
50
51     public String JavaDoc getConfiguredUser() {
52         return configuredUser;
53     }
54
55     public void setConfiguredUser(String JavaDoc configuredUser) {
56         this.configuredUser = configuredUser;
57     }
58
59     public String JavaDoc getConfiguredPassword() {
60         return configuredPassword == null ? null : new String JavaDoc(configuredPassword);
61     }
62
63     public void setConfiguredPassword(String JavaDoc configuredPassword) {
64         this.configuredPassword = configuredPassword == null ? null : configuredPassword.toCharArray();
65     }
66
67     protected CallbackHandler JavaDoc getCallbackHandler(Subject JavaDoc sourceSubject) {
68         return new CallbackHandler JavaDoc() {
69             public void handle(Callback JavaDoc[] callbacks) throws UnsupportedCallbackException JavaDoc {
70                 for (int i = 0; i < callbacks.length; i++) {
71                     Callback JavaDoc callback = callbacks[i];
72                     if (callback instanceof NameCallback JavaDoc) {
73                         ((NameCallback JavaDoc) callback).setName(configuredUser);
74                     } else if (callback instanceof PasswordCallback JavaDoc) {
75                         ((PasswordCallback JavaDoc) callback).setPassword(configuredPassword);
76                     } else {
77                         throw new UnsupportedCallbackException JavaDoc(callback);
78                     }
79                 }
80             }
81
82         };
83     }
84
85     public static final GBeanInfo GBEAN_INFO;
86
87     static {
88         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(ConfiguredIdentityUserPasswordRealmBridge.class, AbstractRealmBridge.GBEAN_INFO);
89         infoFactory.addAttribute("configuredUser", String JavaDoc.class, true);
90         infoFactory.addAttribute("configuredPassword", String JavaDoc.class, true);
91         infoFactory.setConstructor(new String JavaDoc[]{"targetRealm", "configuredUser", "configuredPassword"});
92         GBEAN_INFO = infoFactory.getBeanInfo();
93     }
94
95     public static GBeanInfo getGBeanInfo() {
96         return GBEAN_INFO;
97     }
98
99 }
100
Popular Tags