BaseMemoryRealmAction.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Michel-Ange ANTON
00022  * --------------------------------------------------------------------------
00023  * $Id: BaseMemoryRealmAction.java,v 1.5 2004/03/19 14:31:48 sauthieg Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 
00027 package org.objectweb.jonas.webapp.jonasadmin.security;
00028 
00029 import java.util.ArrayList;
00030 import java.util.Arrays;
00031 import java.util.Collections;
00032 
00033 import javax.management.ObjectName;
00034 import javax.servlet.http.HttpServletRequest;
00035 
00036 import org.apache.struts.action.ActionMapping;
00037 import org.objectweb.jonas.jmx.JonasManagementRepr;
00038 import org.objectweb.jonas.jmx.JonasObjectName;
00039 import org.objectweb.jonas.webapp.jonasadmin.Jlists;
00040 
00045 abstract public class BaseMemoryRealmAction extends BaseSecurityAction {
00046 
00047 // --------------------------------------------------------- Public Methods
00048 
00049 // --------------------------------------------------------- Protected Methods
00050     protected MemoryRealmForm getForm(ActionMapping p_Mapping, HttpServletRequest p_Request) {
00051         // Form used
00052         MemoryRealmForm oForm = null;
00053         // Memory realm to edit
00054         String sResource = p_Request.getParameter("resource");
00055 
00056         // Build a new form
00057         if (sResource != null) {
00058             oForm = new MemoryRealmForm();
00059             m_Session.setAttribute("memoryRealmForm", oForm);
00060             oForm.reset(p_Mapping, p_Request);
00061             oForm.setResource(sResource);
00062             // free old items of session
00063             m_Session.removeAttribute("userMemoryRealmForm");
00064             m_Session.removeAttribute("roleMemoryRealmForm");
00065             m_Session.removeAttribute("groupMemoryRealmForm");
00066             m_Session.removeAttribute("itemsMemoryRealmForm");
00067         }
00068         else {
00069             oForm = (MemoryRealmForm) m_Session.getAttribute("memoryRealmForm");
00070         }
00071         return oForm;
00072     }
00073 
00080     protected void removeItemsMemoryRealmForm(String p_Type) {
00081         ItemsMemoryRealmForm oForm = (ItemsMemoryRealmForm) m_Session.getAttribute(
00082             "itemsMemoryRealmForm");
00083         if (oForm != null) {
00084             if ((oForm.getType() != null) && (oForm.getType().equals(p_Type) == false)) {
00085                 m_Session.removeAttribute("itemsMemoryRealmForm");
00086             }
00087         }
00088     }
00089 
00099     protected void populateUserForm(MemoryRealmForm p_RealmForm, UserMemoryRealmForm p_UserForm
00100         , String p_UserName)
00101         throws Exception {
00102 
00103         if (p_UserName != null) {
00104             // Populate with Mbean 'user'
00105             ObjectName oObjectName = JonasObjectName.user(p_RealmForm.getResource(), p_UserName);
00106             p_UserForm.setUser(getStringAttribute(oObjectName, "Name"));
00107 
00108             p_UserForm.setListGroupsUser(new ArrayList(Arrays.asList((String[]) JonasManagementRepr.
00109                 getAttribute(oObjectName, "ArrayGroups"))));
00110             p_UserForm.setListGroupsUsed(new ArrayList(p_UserForm.getListGroupsUser()));
00111 
00112             p_UserForm.setListRolesUser(new ArrayList(Arrays.asList((String[]) JonasManagementRepr.
00113                 getAttribute(oObjectName, "ArrayRoles"))));
00114             p_UserForm.setListRolesUsed(new ArrayList(p_UserForm.getListRolesUser()));
00115         }
00116         // Populate with Mbean 'realm'
00117         ObjectName oObjectName = JonasObjectName.securityMemoryFactory(p_RealmForm.getResource());
00118         p_UserForm.setListGroupsRealm(new ArrayList(Arrays.asList((String[]) JonasManagementRepr.
00119             invoke(oObjectName, "listGroups", null, null))));
00120         p_UserForm.setListRolesRealm(new ArrayList(Arrays.asList((String[]) JonasManagementRepr.
00121             invoke(oObjectName, "listRoles", null, null))));
00122 
00123         // Calculate Unused
00124         ArrayList alUnused = new ArrayList(p_UserForm.getListGroupsRealm());
00125         alUnused.removeAll(p_UserForm.getListGroupsUser());
00126         Collections.sort(alUnused);
00127         p_UserForm.setListGroupsNotused(alUnused);
00128 
00129         alUnused = new ArrayList(p_UserForm.getListRolesRealm());
00130         alUnused.removeAll(p_UserForm.getListRolesUser());
00131         Collections.sort(alUnused);
00132         p_UserForm.setListRolesNotused(alUnused);
00133 
00134         // Format list to string
00135         p_UserForm.setGroupsUsed(Jlists.getString(p_UserForm.getListGroupsUsed(), Jlists.SEPARATOR));
00136         p_UserForm.setGroupsNotused(Jlists.getString(p_UserForm.getListGroupsNotused()
00137             , Jlists.SEPARATOR));
00138         p_UserForm.setRolesUsed(Jlists.getString(p_UserForm.getListRolesUsed(), Jlists.SEPARATOR));
00139         p_UserForm.setRolesNotused(Jlists.getString(p_UserForm.getListRolesNotused()
00140             , Jlists.SEPARATOR));
00141     }
00142 
00151     protected String encryptPassword(String p_Password, String p_EncrypMethod)
00152         throws Exception {
00153         ObjectName onSecurityService = JonasObjectName.securityService();
00154         String[] asParam = {
00155             p_Password, p_EncrypMethod};
00156         String[] asSignature = {
00157             "java.lang.String", "java.lang.String"};
00158         return (String) JonasManagementRepr.invoke(onSecurityService, "encryptPassword", asParam
00159             , asSignature);
00160     }
00161 
00171     protected void populateRoleForm(MemoryRealmForm p_RealmForm, RoleMemoryRealmForm p_RoleForm
00172         , String p_RoleName)
00173         throws Exception {
00174 
00175         if (p_RoleName != null) {
00176             // Populate with Mbean 'Role'
00177             ObjectName oObjectName = JonasObjectName.role(p_RealmForm.getResource(), p_RoleName);
00178             p_RoleForm.setRole(getStringAttribute(oObjectName, "Name"));
00179             p_RoleForm.setDescription(getStringAttribute(oObjectName, "Description"));
00180         }
00181     }
00182 
00192     protected void populateGroupForm(MemoryRealmForm p_RealmForm, GroupMemoryRealmForm p_GroupForm
00193         , String p_GroupName)
00194         throws Exception {
00195 
00196         if (p_GroupName != null) {
00197             // Populate with Mbean 'group'
00198             ObjectName oObjectName = JonasObjectName.group(p_RealmForm.getResource(), p_GroupName);
00199             p_GroupForm.setGroup(getStringAttribute(oObjectName, "Name"));
00200             p_GroupForm.setDescription(getStringAttribute(oObjectName, "Description"));
00201 
00202             p_GroupForm.setListRolesGroup(new ArrayList(Arrays.asList((String[])
00203                 JonasManagementRepr.getAttribute(oObjectName, "ArrayRoles"))));
00204             p_GroupForm.setListRolesUsed(new ArrayList(p_GroupForm.getListRolesGroup()));
00205         }
00206         // Populate with Mbean 'realm'
00207         ObjectName oObjectName = JonasObjectName.securityMemoryFactory(p_RealmForm.getResource());
00208         p_GroupForm.setListRolesRealm(new ArrayList(Arrays.asList((String[]) JonasManagementRepr.
00209             invoke(oObjectName, "listRoles", null, null))));
00210 
00211         // Calculate Unused
00212         ArrayList alUnused = new ArrayList(p_GroupForm.getListRolesRealm());
00213         alUnused.removeAll(p_GroupForm.getListRolesGroup());
00214         Collections.sort(alUnused);
00215         p_GroupForm.setListRolesNotused(alUnused);
00216 
00217         // Format list to string
00218         p_GroupForm.setRolesUsed(Jlists.getString(p_GroupForm.getListRolesUsed(), Jlists.SEPARATOR));
00219         p_GroupForm.setRolesNotused(Jlists.getString(p_GroupForm.getListRolesNotused()
00220             , Jlists.SEPARATOR));
00221     }
00222 }

Generated on Tue Feb 15 15:05:30 2005 for JOnAS by  doxygen 1.3.9.1