001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ListRealmsAction.java 9864 2006-11-24 10:14:22Z danesa $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.webapp.jonasadmin.security;
027:
028: import java.io.IOException;
029: import java.util.ArrayList;
030: import java.util.Collections;
031:
032: import javax.management.ObjectName;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: import org.apache.struts.action.ActionForm;
038: import org.apache.struts.action.ActionForward;
039: import org.apache.struts.action.ActionMapping;
040: import org.objectweb.jonas.jmx.CatalinaObjectName;
041: import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
042: import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
043:
044: /**
045: * @author Michel-Ange ANTON
046: */
047:
048: public class ListRealmsAction extends BaseMemoryRealmAction {
049:
050: // --------------------------------------------------------- Public Methods
051:
052: public ActionForward executeAction(ActionMapping p_Mapping,
053: ActionForm p_Form, HttpServletRequest p_Request,
054: HttpServletResponse p_Response) throws IOException,
055: ServletException {
056:
057: WhereAreYou oWhere = (WhereAreYou) p_Request.getSession()
058: .getAttribute(WhereAreYou.SESSION_NAME);
059: String serverName = oWhere.getCurrentJonasServerName();
060:
061: // Realms to list
062: String sTypeRealm = p_Request.getParameter("realm");
063: if (sTypeRealm == null) {
064: sTypeRealm = (String) m_Session.getAttribute("typeRealm");
065: } else {
066: m_Session.setAttribute("typeRealm", sTypeRealm);
067: }
068: // Force view all realm if null
069: if (sTypeRealm == null) {
070: sTypeRealm = "all";
071: m_Session.setAttribute("typeRealm", sTypeRealm);
072: }
073:
074: // Populate list
075: try {
076: String sSecurityRealmUsed = null;
077: // Detect realm used by the security service
078: if (m_WhereAreYou.isCatalinaServer()) {
079: try {
080: // Object name used Realm
081: ObjectName oObjectName = CatalinaObjectName
082: .catalinaRealm(m_WhereAreYou
083: .getCurrentCatalinaDomainName());
084: // Populate
085: sSecurityRealmUsed = getStringAttribute(
086: oObjectName, "resourceName");
087: } catch (Exception e) {
088: // no action
089: // Exception because catalina realm don't exists
090: }
091: }
092:
093: ArrayList alRealms = new ArrayList();
094: // Detect the realm type
095: if (sTypeRealm.equals("all")) {
096: // all Realms
097: addRealmItem(alRealms, "memory", sSecurityRealmUsed,
098: serverName);
099: addRealmItem(alRealms, "datasource",
100: sSecurityRealmUsed, serverName);
101: addRealmItem(alRealms, "ldap", sSecurityRealmUsed,
102: serverName);
103: // Force the node selected in tree
104: m_WhereAreYou.selectNameNode(
105: getTreeBranchName(DEPTH_SERVER)
106: + WhereAreYou.NODE_SEPARATOR
107: + "security", true);
108: }
109: // Memory realm factories
110: else if (sTypeRealm.equals("memory")) {
111: addRealmItem(alRealms, "memory", sSecurityRealmUsed,
112: serverName);
113: // Force the node selected in tree
114: m_WhereAreYou.selectNameNode(
115: getTreeBranchName(DEPTH_SERVER)
116: + WhereAreYou.NODE_SEPARATOR
117: + "security"
118: + WhereAreYou.NODE_SEPARATOR
119: + "factory.memory", true);
120: }
121: // Datasource realm factories
122: else if (sTypeRealm.equals("datasource")) {
123: addRealmItem(alRealms, "datasource",
124: sSecurityRealmUsed, serverName);
125: // Force the node selected in tree
126: m_WhereAreYou.selectNameNode(
127: getTreeBranchName(DEPTH_SERVER)
128: + WhereAreYou.NODE_SEPARATOR
129: + "security"
130: + WhereAreYou.NODE_SEPARATOR
131: + "factory.datasource", true);
132: }
133: // Ldap realm factories
134: else if (sTypeRealm.equals("ldap")) {
135: addRealmItem(alRealms, "ldap", sSecurityRealmUsed,
136: serverName);
137: // Force the node selected in tree
138: m_WhereAreYou.selectNameNode(
139: getTreeBranchName(DEPTH_SERVER)
140: + WhereAreYou.NODE_SEPARATOR
141: + "security"
142: + WhereAreYou.NODE_SEPARATOR
143: + "factory.ldap", true);
144: }
145: // Unknown
146: else {
147: String sMess = m_Resources.getMessage(
148: "error.security.factory.realms.type.unknown",
149: sTypeRealm);
150: throw new Exception(sMess);
151: }
152: // Sort
153: Collections.sort(alRealms, new RealmItemByNameComparator());
154: // Send list to display
155: m_Session.setAttribute("listRealms", alRealms);
156:
157: } catch (Throwable t) {
158: addGlobalError(t);
159: saveErrors(p_Request, m_Errors);
160: return (p_Mapping.findForward("Global Error"));
161: }
162: // Forward to the jsp.
163: return (p_Mapping.findForward("Realms"));
164: }
165:
166: // --------------------------------------------------------- Protected Methods
167: /**
168: * Add <code>RealmItem</code> in the list.
169: *
170: * @param p_Realms List to add found items
171: * @param p_Type Type item to add (memory or datasource or ldap)
172: * @param p_SecurityRealmUsed Name of realm used by the security service
173: * or null if nothing used
174: * @throws Exception
175: */
176: protected void addRealmItem(ArrayList p_Realms, String p_Type,
177: String p_SecurityRealmUsed, String serverName)
178: throws Exception {
179:
180: ArrayList al = null;
181:
182: // Memory realm factories
183: if (p_Type.equals("memory")) {
184: al = JonasAdminJmx.getSecurityMemoryFactories(serverName);
185: }
186: // Datasource realm factories
187: else if (p_Type.equals("datasource")) {
188: al = JonasAdminJmx
189: .getSecurityDatasourceFactories(serverName);
190: }
191: // Ldap realm factories
192: else if (p_Type.equals("ldap")) {
193: al = JonasAdminJmx.getSecurityLdapFactories(serverName);
194: }
195: // Add
196: if (al != null) {
197: for (int i = 0; i < al.size(); i++) {
198: if (p_SecurityRealmUsed != null) {
199: p_Realms.add(new RealmItem(al.get(i).toString(),
200: p_Type, p_SecurityRealmUsed.equals(al
201: .get(i).toString())));
202: } else {
203: p_Realms.add(new RealmItem(al.get(i).toString(),
204: p_Type));
205: }
206: }
207: }
208: }
209: }
|