KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > security > auth > weblogic61 > WeblogicCarbonRealm


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.security.auth.weblogic61;
19
20 // Import the necessary classes
21
import java.security.Principal JavaDoc;
22 import java.security.acl.Group JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.sape.carbon.core.component.Lookup;
28 import org.sape.carbon.services.security.management.UserManager;
29
30 import weblogic.management.configuration.CustomRealmMBean;
31 import weblogic.security.acl.AbstractListableRealm;
32 import weblogic.security.acl.User;
33 import weblogic.server.Server;
34
35 /**
36  * Adapter class for the Weblogic 6.1 Security SPI to Carbon's UserManager
37  * Service.
38  * <p>
39  * The class provides basic adapter behavior from the Weblogic Security SPI
40  * calls to Carbons user manager. This realm is not manageable directory,
41  * instead prefering to use the JmxAdapter implementation to manage the
42  * realm.
43  * </p>
44  * <p>
45  * The major functionality of this class is only to get back the various
46  * user and group objects from the Carbon service and convert them into
47  * the Weblogic specific instances of the classes.
48  * </p>
49  * @author $Author: dvoet $ $Date: 2003/05/05 21:21:34 $
50  * @version $Revision: 1.5 $
51  *
52  * @since carbon 1.2 */

53 public class WeblogicCarbonRealm extends AbstractListableRealm {
54     /** Holds the key for the configuration value for the User Manager. */
55     protected static final String JavaDoc CONFIG_LOCATION_NAME = "userManagerPath";
56
57     /** Holds a reference to the user manager. */
58     protected UserManager userManager;
59
60     /**
61      * Default constructor gets a reference to the user manager object.
62      */

63     public WeblogicCarbonRealm() {
64         super("WeblogicCarbonRealm");
65         configure();
66     }
67
68     /**
69      * Configures the realm by pulling values from config and getting a hold
70      * the user manager service.
71      */

72     protected void configure() {
73         CustomRealmMBean customRealmMBean = (CustomRealmMBean)
74             Server.getSecurityConfig().getRealm().
75                 getCachingRealm().getBasicRealm();
76
77         Properties JavaDoc configData = customRealmMBean.getConfigurationData();
78
79         String JavaDoc configLocation = configData.getProperty(CONFIG_LOCATION_NAME);
80
81         userManager =
82             (UserManager) Lookup.getInstance().fetchComponent(
83                 configLocation);
84     }
85
86
87     /**
88      * Authorizes a user by passing through to the UserManager service.
89      *
90      * @param name The username identifying the user inside the user
91      * store.
92      * @param passwd The credential object a user needs to prove they
93      * can be referenced by the principal.
94      * @return the user object representing the authenticated user or
95      * <code>null</code> if the user could not be authenticated.
96      *
97      * @see
98      * org.sape.carbon.services.security.management.UserManager#authenticate
99      */

100     protected User authUserPassword(String JavaDoc name, String JavaDoc passwd) {
101         WeblogicCarbonUser weblogicCarbonUser = null;
102
103         if (userManager.authenticate(name, passwd)) {
104             weblogicCarbonUser =
105                 new WeblogicCarbonUser(userManager.retreiveUser(name), this);
106         }
107
108         return weblogicCarbonUser;
109     }
110
111     /**
112      * Gets back a hashtable of all members.
113      * <p>
114      * This converts all users and groups from Principals and Groups
115      * into the weblogic specific classes.
116      * </p>
117      *
118      * @param name the name of the group to get back all members of
119      * @return a hashtable of principal names to principals
120      */

121     protected Hashtable JavaDoc getGroupMembersInternal(String JavaDoc name) {
122         Hashtable JavaDoc groupMembers = null;
123
124         Group JavaDoc group = userManager.retreiveGroup(name);
125         if (group != null) {
126             groupMembers = new Hashtable JavaDoc();
127             Enumeration JavaDoc membersEnumeration = group.members();
128             while (membersEnumeration.hasMoreElements()) {
129                 Principal JavaDoc currentMember =
130                     (Principal JavaDoc) membersEnumeration.nextElement();
131
132                 if (currentMember instanceof Group JavaDoc) {
133                     groupMembers.put(currentMember.getName(),
134                         new WeblogicCarbonGroup((Group JavaDoc) currentMember, this));
135                 } else {
136                     groupMembers.put(currentMember.getName(),
137                         new WeblogicCarbonUser(currentMember, this));
138                 }
139             }
140         }
141
142         return groupMembers;
143     }
144
145     /**
146      * Gets a specific user with a given name by passing through to the
147      * UserManager service.
148      *
149      * @param name the name of the user to get.
150      * @return the user associated with the name, or null if there is no user
151      * @see
152      * org.sape.carbon.services.security.management.UserManager#retreiveUser
153      */

154     public User getUser(String JavaDoc name) {
155         WeblogicCarbonUser weblogicCarbonUser = null;
156         Principal JavaDoc carbonUser = userManager.retreiveUser(name);
157         if (carbonUser != null) {
158             weblogicCarbonUser = new WeblogicCarbonUser(carbonUser, this);
159         }
160
161         return weblogicCarbonUser;
162     }
163
164     /**
165      * Gets a specific group with a given name by passing through to the
166      * UserManager service.
167      *
168      * @param name the name of the group to get.
169      * @return the group associated with the name, or null if there is
170      * no group
171      * @see
172      * org.sape.carbon.services.security.management.UserManager#retreiveGroup
173      */

174     public Group JavaDoc getGroup(String JavaDoc name) {
175         WeblogicCarbonGroup weblogicCarbonGroup = null;
176         Group JavaDoc carbonGroup = userManager.retreiveGroup(name);
177         if (carbonGroup != null) {
178             weblogicCarbonGroup = new WeblogicCarbonGroup(carbonGroup, this);
179         }
180
181         return weblogicCarbonGroup;
182     }
183 }
Popular Tags