KickJava   Java API By Example, From Geeks To Geeks.

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


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  * --------------------------------------------------------------------------
22  * $Id: ListRealmsAction.java,v 1.9 2004/10/22 13:53:33 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.security;
27
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionForward;
39 import org.apache.struts.action.ActionMapping;
40 import org.objectweb.jonas.jmx.CatalinaObjectName;
41 import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
42 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
43
44 /**
45  * @author Michel-Ange ANTON
46  */

47
48 public class ListRealmsAction extends BaseMemoryRealmAction {
49
50 // --------------------------------------------------------- Public Methods
51

52     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
53         , HttpServletRequest JavaDoc p_Request, HttpServletResponse JavaDoc p_Response)
54         throws IOException JavaDoc, ServletException JavaDoc {
55
56         // Realms to list
57
String JavaDoc sTypeRealm = p_Request.getParameter("realm");
58         if (sTypeRealm == null) {
59             sTypeRealm = (String JavaDoc) m_Session.getAttribute("typeRealm");
60         }
61         else {
62             m_Session.setAttribute("typeRealm", sTypeRealm);
63         }
64         // Force view all realm if null
65
if (sTypeRealm == null) {
66             sTypeRealm = "all";
67             m_Session.setAttribute("typeRealm", sTypeRealm);
68         }
69
70         // Populate list
71
try {
72             String JavaDoc sSecurityRealmUsed = null;
73             // Detect realm used by the security service
74
if (m_WhereAreYou.isCatalinaServer() == true) {
75                 try {
76                     // Object name used Realm
77
ObjectName JavaDoc oObjectName = CatalinaObjectName.catalinaRealm(m_WhereAreYou.
78                         getCurrentCatalinaServiceName());
79                     // Populate
80
sSecurityRealmUsed = getStringAttribute(oObjectName, "resourceName");
81                 }
82                 catch (Exception JavaDoc e) {
83                     // no action
84
// Exception because catalina realm don't exists
85
}
86             }
87
88             ArrayList JavaDoc alRealms = new ArrayList JavaDoc();
89             // Detect the realm type
90
if (sTypeRealm.equals("all") == true) {
91                 // all Realms
92
addRealmItem(alRealms, "memory", sSecurityRealmUsed);
93                 addRealmItem(alRealms, "datasource", sSecurityRealmUsed);
94                 addRealmItem(alRealms, "ldap", sSecurityRealmUsed);
95                 // Force the node selected in tree
96
m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
97                     + WhereAreYou.NODE_SEPARATOR + "security", true);
98             }
99             // Memory realm factories
100
else if (sTypeRealm.equals("memory") == true) {
101                 addRealmItem(alRealms, "memory", sSecurityRealmUsed);
102                 // Force the node selected in tree
103
m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
104                     + WhereAreYou.NODE_SEPARATOR + "security" + WhereAreYou.NODE_SEPARATOR
105                     + "factory.memory", true);
106             }
107             // Datasource realm factories
108
else if (sTypeRealm.equals("datasource") == true) {
109                 addRealmItem(alRealms, "datasource", sSecurityRealmUsed);
110                 // Force the node selected in tree
111
m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
112                     + WhereAreYou.NODE_SEPARATOR + "security" + WhereAreYou.NODE_SEPARATOR
113                     + "factory.datasource", true);
114             }
115             // Ldap realm factories
116
else if (sTypeRealm.equals("ldap") == true) {
117                 addRealmItem(alRealms, "ldap", sSecurityRealmUsed);
118                 // Force the node selected in tree
119
m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
120                     + WhereAreYou.NODE_SEPARATOR + "security" + WhereAreYou.NODE_SEPARATOR
121                     + "factory.ldap", true);
122             }
123             // Unknown
124
else {
125                 String JavaDoc sMess = m_Resources.getMessage("error.security.factory.realms.type.unknown"
126                     , sTypeRealm);
127                 throw new Exception JavaDoc(sMess);
128             }
129             // Sort
130
Collections.sort(alRealms, new RealmItemByNameComparator());
131             // Send list to display
132
m_Session.setAttribute("listRealms", alRealms);
133
134         }
135         catch (Throwable JavaDoc t) {
136             addGlobalError(t);
137             saveErrors(p_Request, m_Errors);
138             return (p_Mapping.findForward("Global Error"));
139         }
140         // Forward to the jsp.
141
return (p_Mapping.findForward("Realms"));
142     }
143
144 // --------------------------------------------------------- Protected Methods
145
/**
146      * Add <code>RealmItem</code> in the list.
147      *
148      * @param p_Realms List to add found items
149      * @param p_Type Type item to add (memory or datasource or ldap)
150      * @param p_SecurityRealmUsed Name of realm used by the security service
151      * or null if nothing used
152      * @throws Exception
153      */

154     protected void addRealmItem(ArrayList JavaDoc p_Realms, String JavaDoc p_Type, String JavaDoc p_SecurityRealmUsed)
155         throws Exception JavaDoc {
156
157         ArrayList JavaDoc al = null;
158
159         // Memory realm factories
160
if (p_Type.equals("memory") == true) {
161             al = JonasAdminJmx.getSecurityMemoryFactories();
162         }
163         // Datasource realm factories
164
else if (p_Type.equals("datasource") == true) {
165             al = JonasAdminJmx.getSecurityDatasourceFactories();
166         }
167         // Ldap realm factories
168
else if (p_Type.equals("ldap") == true) {
169             al = JonasAdminJmx.getSecurityLdapFactories();
170         }
171         // Add
172
if (al != null) {
173             for (int i = 0; i < al.size(); i++) {
174                 if (p_SecurityRealmUsed != null) {
175                     p_Realms.add(new RealmItem(al.get(i).toString(), p_Type
176                         , p_SecurityRealmUsed.equals(al.get(i).toString())));
177                 }
178                 else {
179                     p_Realms.add(new RealmItem(al.get(i).toString(), p_Type));
180                 }
181             }
182         }
183     }
184 }
185
Popular Tags