ListRealmsAction.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  * --------------------------------------------------------------------------
00022  * $Id: ListRealmsAction.java,v 1.9 2004/10/22 13:53:33 danesa Exp $
00023  * --------------------------------------------------------------------------
00024  */
00025 
00026 package org.objectweb.jonas.webapp.jonasadmin.security;
00027 
00028 import java.io.IOException;
00029 import java.util.ArrayList;
00030 import java.util.Collections;
00031 
00032 import javax.management.ObjectName;
00033 import javax.servlet.ServletException;
00034 import javax.servlet.http.HttpServletRequest;
00035 import javax.servlet.http.HttpServletResponse;
00036 
00037 import org.apache.struts.action.ActionForm;
00038 import org.apache.struts.action.ActionForward;
00039 import org.apache.struts.action.ActionMapping;
00040 import org.objectweb.jonas.jmx.CatalinaObjectName;
00041 import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
00042 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
00043 
00048 public class ListRealmsAction extends BaseMemoryRealmAction {
00049 
00050 // --------------------------------------------------------- Public Methods
00051 
00052     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
00053         , HttpServletRequest p_Request, HttpServletResponse p_Response)
00054         throws IOException, ServletException {
00055 
00056         // Realms to list
00057         String sTypeRealm = p_Request.getParameter("realm");
00058         if (sTypeRealm == null) {
00059             sTypeRealm = (String) m_Session.getAttribute("typeRealm");
00060         }
00061         else {
00062             m_Session.setAttribute("typeRealm", sTypeRealm);
00063         }
00064         // Force view all realm if null
00065         if (sTypeRealm == null) {
00066             sTypeRealm = "all";
00067             m_Session.setAttribute("typeRealm", sTypeRealm);
00068         }
00069 
00070         // Populate list
00071         try {
00072             String sSecurityRealmUsed = null;
00073             // Detect realm used by the security service
00074             if (m_WhereAreYou.isCatalinaServer() == true) {
00075                 try {
00076                     // Object name used Realm
00077                     ObjectName oObjectName = CatalinaObjectName.catalinaRealm(m_WhereAreYou.
00078                         getCurrentCatalinaServiceName());
00079                     // Populate
00080                     sSecurityRealmUsed = getStringAttribute(oObjectName, "resourceName");
00081                 }
00082                 catch (Exception e) {
00083                     // no action
00084                     // Exception because catalina realm don't exists
00085                 }
00086             }
00087 
00088             ArrayList alRealms = new ArrayList();
00089             // Detect the realm type
00090             if (sTypeRealm.equals("all") == true) {
00091                 // all Realms
00092                 addRealmItem(alRealms, "memory", sSecurityRealmUsed);
00093                 addRealmItem(alRealms, "datasource", sSecurityRealmUsed);
00094                 addRealmItem(alRealms, "ldap", sSecurityRealmUsed);
00095                 // Force the node selected in tree
00096                 m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
00097                     + WhereAreYou.NODE_SEPARATOR + "security", true);
00098             }
00099             // Memory realm factories
00100             else if (sTypeRealm.equals("memory") == true) {
00101                 addRealmItem(alRealms, "memory", sSecurityRealmUsed);
00102                 // Force the node selected in tree
00103                 m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
00104                     + WhereAreYou.NODE_SEPARATOR + "security" + WhereAreYou.NODE_SEPARATOR
00105                     + "factory.memory", true);
00106             }
00107             // Datasource realm factories
00108             else if (sTypeRealm.equals("datasource") == true) {
00109                 addRealmItem(alRealms, "datasource", sSecurityRealmUsed);
00110                 // Force the node selected in tree
00111                 m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
00112                     + WhereAreYou.NODE_SEPARATOR + "security" + WhereAreYou.NODE_SEPARATOR
00113                     + "factory.datasource", true);
00114             }
00115             // Ldap realm factories
00116             else if (sTypeRealm.equals("ldap") == true) {
00117                 addRealmItem(alRealms, "ldap", sSecurityRealmUsed);
00118                 // Force the node selected in tree
00119                 m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
00120                     + WhereAreYou.NODE_SEPARATOR + "security" + WhereAreYou.NODE_SEPARATOR
00121                     + "factory.ldap", true);
00122             }
00123             // Unknown
00124             else {
00125                 String sMess = m_Resources.getMessage("error.security.factory.realms.type.unknown"
00126                     , sTypeRealm);
00127                 throw new Exception(sMess);
00128             }
00129             // Sort
00130             Collections.sort(alRealms, new RealmItemByNameComparator());
00131             // Send list to display
00132             m_Session.setAttribute("listRealms", alRealms);
00133 
00134         }
00135         catch (Throwable t) {
00136             addGlobalError(t);
00137             saveErrors(p_Request, m_Errors);
00138             return (p_Mapping.findForward("Global Error"));
00139         }
00140         // Forward to the jsp.
00141         return (p_Mapping.findForward("Realms"));
00142     }
00143 
00144 // --------------------------------------------------------- Protected Methods
00154     protected void addRealmItem(ArrayList p_Realms, String p_Type, String p_SecurityRealmUsed)
00155         throws Exception {
00156 
00157         ArrayList al = null;
00158 
00159         // Memory realm factories
00160         if (p_Type.equals("memory") == true) {
00161             al = JonasAdminJmx.getSecurityMemoryFactories();
00162         }
00163         // Datasource realm factories
00164         else if (p_Type.equals("datasource") == true) {
00165             al = JonasAdminJmx.getSecurityDatasourceFactories();
00166         }
00167         // Ldap realm factories
00168         else if (p_Type.equals("ldap") == true) {
00169             al = JonasAdminJmx.getSecurityLdapFactories();
00170         }
00171         // Add
00172         if (al != null) {
00173             for (int i = 0; i < al.size(); i++) {
00174                 if (p_SecurityRealmUsed != null) {
00175                     p_Realms.add(new RealmItem(al.get(i).toString(), p_Type
00176                         , p_SecurityRealmUsed.equals(al.get(i).toString())));
00177                 }
00178                 else {
00179                     p_Realms.add(new RealmItem(al.get(i).toString(), p_Type));
00180                 }
00181             }
00182         }
00183     }
00184 }

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