KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import org.apache.geronimo.gbean.GBeanInfo;
28 import org.apache.geronimo.gbean.GBeanInfoBuilder;
29
30
31 /**
32  * works off a property file with lines of the format:
33  * sourceprincipalname=targetprincipal:targetuser:targetpassword
34  * <p/>
35  * all three can be mapped separately; the source for each key is
36  * from the appropriate principal class and possibly
37  * callback name.
38  *
39  * @version $Rev: 56022 $ $Date: 2004-10-29 22:16:18 -0700 (Fri, 29 Oct 2004) $
40  */

41 public class PropertiesFilePrincipalMappingUserPasswordRealmBridge extends AbstractPrincipalMappingUserPasswordRealmBridge {
42     private URL JavaDoc propertyFileURL;
43
44     public PropertiesFilePrincipalMappingUserPasswordRealmBridge() {
45     }
46
47     public PropertiesFilePrincipalMappingUserPasswordRealmBridge(String JavaDoc targetRealm,
48             Class JavaDoc principalSourceType,
49             String JavaDoc principalTargetCallbackName,
50             Class JavaDoc userNameSourceType,
51             String JavaDoc userNameTargetCallbackName,
52             Class JavaDoc passwordSourceType,
53             URL JavaDoc propertyFileURL) {
54
55         super(targetRealm,
56                 principalSourceType,
57                 principalTargetCallbackName,
58                 userNameSourceType,
59                 userNameTargetCallbackName,
60                 passwordSourceType);
61         this.propertyFileURL = propertyFileURL;
62     }
63
64     public URL JavaDoc getPropertyFileURL() {
65         return propertyFileURL;
66     }
67
68     public void setPropertyFileURL(URL JavaDoc propertyFileURL) throws IOException JavaDoc {
69         this.propertyFileURL = propertyFileURL;
70         principalMap.clear();
71         userNameMap.clear();
72         passwordMap.clear();
73         Properties JavaDoc properties = new Properties JavaDoc();
74         properties.load(propertyFileURL.openStream());
75         setMaps(properties, principalMap, userNameMap, passwordMap);
76     }
77
78     void setMaps(Properties JavaDoc properties, Map JavaDoc principalMap, Map JavaDoc userNameMap, Map JavaDoc passwordMap) {
79         for (Iterator JavaDoc iterator = properties.entrySet().iterator(); iterator.hasNext();) {
80             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
81             String JavaDoc key = (String JavaDoc) entry.getKey();
82             String JavaDoc values = (String JavaDoc) entry.getValue();
83             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(values, ":");
84             String JavaDoc targetPrincipal = tokenizer.nextToken();
85             String JavaDoc targetUserName = tokenizer.nextToken();
86             char[] targetPassword = tokenizer.nextToken().toCharArray();
87             principalMap.put(key, targetPrincipal);
88             userNameMap.put(key, targetUserName);
89             passwordMap.put(key, targetPassword);
90         }
91     }
92
93     public static final GBeanInfo GBEAN_INFO;
94
95     static {
96         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(PropertiesFilePrincipalMappingUserPasswordRealmBridge.class, AbstractPrincipalMappingUserPasswordRealmBridge.GBEAN_INFO);
97         infoFactory.addAttribute("propertyFileURL", URL JavaDoc.class, true);
98         infoFactory.setConstructor(new String JavaDoc[]{"targetRealm", "principalSourceType", "principalTargetCallbackName", "userNameSourceType", "userNameTargetCallbackName", "passwordSourceType", "propertyFileURL"});
99         GBEAN_INFO = infoFactory.getBeanInfo();
100     }
101
102     public static GBeanInfo getGBeanInfo() {
103         return GBEAN_INFO;
104     }
105
106 }
107
Popular Tags