KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > security > BaseMemoryRealmAction


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Michel-Ange ANTON
22  * --------------------------------------------------------------------------
23  * $Id: BaseMemoryRealmAction.java,v 1.5 2004/03/19 14:31:48 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.webapp.jonasadmin.security;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35
36 import org.apache.struts.action.ActionMapping;
37 import org.objectweb.jonas.jmx.JonasManagementRepr;
38 import org.objectweb.jonas.jmx.JonasObjectName;
39 import org.objectweb.jonas.webapp.jonasadmin.Jlists;
40
41 /**
42  *
43  */

44
45 abstract public class BaseMemoryRealmAction extends BaseSecurityAction {
46
47 // --------------------------------------------------------- Public Methods
48

49 // --------------------------------------------------------- Protected Methods
50
protected MemoryRealmForm getForm(ActionMapping p_Mapping, HttpServletRequest JavaDoc p_Request) {
51         // Form used
52
MemoryRealmForm oForm = null;
53         // Memory realm to edit
54
String JavaDoc sResource = p_Request.getParameter("resource");
55
56         // Build a new form
57
if (sResource != null) {
58             oForm = new MemoryRealmForm();
59             m_Session.setAttribute("memoryRealmForm", oForm);
60             oForm.reset(p_Mapping, p_Request);
61             oForm.setResource(sResource);
62             // free old items of session
63
m_Session.removeAttribute("userMemoryRealmForm");
64             m_Session.removeAttribute("roleMemoryRealmForm");
65             m_Session.removeAttribute("groupMemoryRealmForm");
66             m_Session.removeAttribute("itemsMemoryRealmForm");
67         }
68         else {
69             oForm = (MemoryRealmForm) m_Session.getAttribute("memoryRealmForm");
70         }
71         return oForm;
72     }
73
74     /**
75      * Remove of session the <code>ItemsMemoryRealmForm</code> instance
76      * if the given type is different of the current type.
77      *
78      * @param p_Type Current type (user, role, group)
79      */

80     protected void removeItemsMemoryRealmForm(String JavaDoc p_Type) {
81         ItemsMemoryRealmForm oForm = (ItemsMemoryRealmForm) m_Session.getAttribute(
82             "itemsMemoryRealmForm");
83         if (oForm != null) {
84             if ((oForm.getType() != null) && (oForm.getType().equals(p_Type) == false)) {
85                 m_Session.removeAttribute("itemsMemoryRealmForm");
86             }
87         }
88     }
89
90     /**
91      * Populate the <code>UserMemoryRealmForm</code> with MBeans.
92      * If the user name is null, the User MBean is not called to populate the form.
93      *
94      * @param p_RealmForm Used for the resource name
95      * @param p_UserForm Form to populate
96      * @param p_UserName The user (Can be null)
97      * @throws Exception
98      */

99     protected void populateUserForm(MemoryRealmForm p_RealmForm, UserMemoryRealmForm p_UserForm
100         , String JavaDoc p_UserName)
101         throws Exception JavaDoc {
102
103         if (p_UserName != null) {
104             // Populate with Mbean 'user'
105
ObjectName JavaDoc oObjectName = JonasObjectName.user(p_RealmForm.getResource(), p_UserName);
106             p_UserForm.setUser(getStringAttribute(oObjectName, "Name"));
107
108             p_UserForm.setListGroupsUser(new ArrayList JavaDoc(Arrays.asList((String JavaDoc[]) JonasManagementRepr.
109                 getAttribute(oObjectName, "ArrayGroups"))));
110             p_UserForm.setListGroupsUsed(new ArrayList JavaDoc(p_UserForm.getListGroupsUser()));
111
112             p_UserForm.setListRolesUser(new ArrayList JavaDoc(Arrays.asList((String JavaDoc[]) JonasManagementRepr.
113                 getAttribute(oObjectName, "ArrayRoles"))));
114             p_UserForm.setListRolesUsed(new ArrayList JavaDoc(p_UserForm.getListRolesUser()));
115         }
116         // Populate with Mbean 'realm'
117
ObjectName JavaDoc oObjectName = JonasObjectName.securityMemoryFactory(p_RealmForm.getResource());
118         p_UserForm.setListGroupsRealm(new ArrayList JavaDoc(Arrays.asList((String JavaDoc[]) JonasManagementRepr.
119             invoke(oObjectName, "listGroups", null, null))));
120         p_UserForm.setListRolesRealm(new ArrayList JavaDoc(Arrays.asList((String JavaDoc[]) JonasManagementRepr.
121             invoke(oObjectName, "listRoles", null, null))));
122
123         // Calculate Unused
124
ArrayList JavaDoc alUnused = new ArrayList JavaDoc(p_UserForm.getListGroupsRealm());
125         alUnused.removeAll(p_UserForm.getListGroupsUser());
126         Collections.sort(alUnused);
127         p_UserForm.setListGroupsNotused(alUnused);
128
129         alUnused = new ArrayList JavaDoc(p_UserForm.getListRolesRealm());
130         alUnused.removeAll(p_UserForm.getListRolesUser());
131         Collections.sort(alUnused);
132         p_UserForm.setListRolesNotused(alUnused);
133
134         // Format list to string
135
p_UserForm.setGroupsUsed(Jlists.getString(p_UserForm.getListGroupsUsed(), Jlists.SEPARATOR));
136         p_UserForm.setGroupsNotused(Jlists.getString(p_UserForm.getListGroupsNotused()
137             , Jlists.SEPARATOR));
138         p_UserForm.setRolesUsed(Jlists.getString(p_UserForm.getListRolesUsed(), Jlists.SEPARATOR));
139         p_UserForm.setRolesNotused(Jlists.getString(p_UserForm.getListRolesNotused()
140             , Jlists.SEPARATOR));
141     }
142
143     /**
144      * Encrypt a password with MBean security service method.
145      *
146      * @param p_Password Password to encrypt
147      * @param p_EncrypMethod MD5 or SHA string
148      * @return The encrypted password
149      * @throws Exception
150      */

151     protected String JavaDoc encryptPassword(String JavaDoc p_Password, String JavaDoc p_EncrypMethod)
152         throws Exception JavaDoc {
153         ObjectName JavaDoc onSecurityService = JonasObjectName.securityService();
154         String JavaDoc[] asParam = {
155             p_Password, p_EncrypMethod};
156         String JavaDoc[] asSignature = {
157             "java.lang.String", "java.lang.String"};
158         return (String JavaDoc) JonasManagementRepr.invoke(onSecurityService, "encryptPassword", asParam
159             , asSignature);
160     }
161
162     /**
163      * Populate the <code>RoleMemoryRealmForm</code> with MBeans.
164      * If the Role name is null, the Role MBean is not called to populate the form.
165      *
166      * @param p_RealmForm Used for the resource name
167      * @param p_RoleForm Form to populate
168      * @param p_RoleName The user (Can be null)
169      * @throws Exception
170      */

171     protected void populateRoleForm(MemoryRealmForm p_RealmForm, RoleMemoryRealmForm p_RoleForm
172         , String JavaDoc p_RoleName)
173         throws Exception JavaDoc {
174
175         if (p_RoleName != null) {
176             // Populate with Mbean 'Role'
177
ObjectName JavaDoc oObjectName = JonasObjectName.role(p_RealmForm.getResource(), p_RoleName);
178             p_RoleForm.setRole(getStringAttribute(oObjectName, "Name"));
179             p_RoleForm.setDescription(getStringAttribute(oObjectName, "Description"));
180         }
181     }
182
183     /**
184      * Populate the <code>GroupMemoryRealmForm</code> with MBeans.
185      * If the user name is null, the Group MBean is not called to populate the form.
186      *
187      * @param p_RealmForm Used for the resource name
188      * @param p_GroupForm Form to populate
189      * @param p_GroupName The user (Can be null)
190      * @throws Exception
191      */

192     protected void populateGroupForm(MemoryRealmForm p_RealmForm, GroupMemoryRealmForm p_GroupForm
193         , String JavaDoc p_GroupName)
194         throws Exception JavaDoc {
195
196         if (p_GroupName != null) {
197             // Populate with Mbean 'group'
198
ObjectName JavaDoc oObjectName = JonasObjectName.group(p_RealmForm.getResource(), p_GroupName);
199             p_GroupForm.setGroup(getStringAttribute(oObjectName, "Name"));
200             p_GroupForm.setDescription(getStringAttribute(oObjectName, "Description"));
201
202             p_GroupForm.setListRolesGroup(new ArrayList JavaDoc(Arrays.asList((String JavaDoc[])
203                 JonasManagementRepr.getAttribute(oObjectName, "ArrayRoles"))));
204             p_GroupForm.setListRolesUsed(new ArrayList JavaDoc(p_GroupForm.getListRolesGroup()));
205         }
206         // Populate with Mbean 'realm'
207
ObjectName JavaDoc oObjectName = JonasObjectName.securityMemoryFactory(p_RealmForm.getResource());
208         p_GroupForm.setListRolesRealm(new ArrayList JavaDoc(Arrays.asList((String JavaDoc[]) JonasManagementRepr.
209             invoke(oObjectName, "listRoles", null, null))));
210
211         // Calculate Unused
212
ArrayList JavaDoc alUnused = new ArrayList JavaDoc(p_GroupForm.getListRolesRealm());
213         alUnused.removeAll(p_GroupForm.getListRolesGroup());
214         Collections.sort(alUnused);
215         p_GroupForm.setListRolesNotused(alUnused);
216
217         // Format list to string
218
p_GroupForm.setRolesUsed(Jlists.getString(p_GroupForm.getListRolesUsed(), Jlists.SEPARATOR));
219         p_GroupForm.setRolesNotused(Jlists.getString(p_GroupForm.getListRolesNotused()
220             , Jlists.SEPARATOR));
221     }
222 }
223
Popular Tags