📄 statusaction.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.setup.actions;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.util.RequestUtils;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.Panel;
import com.sslexplorer.core.PanelManager;
import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
import com.sslexplorer.policyframework.Permission;
import com.sslexplorer.policyframework.PolicyConstants;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.setup.forms.StatusForm;
/**
* Implementation of {@link AuthenticatedDispatchAction} that is used to display
* various system information.
*
* @author Brett Smith <a href="mailto: brett@3sp.com"><brett@3sp.com></a>
* @version $Revision: 1.11 $
*/
public class StatusAction extends AuthenticatedDispatchAction {
static Log log = LogFactory.getLog(StatusAction.class);
static Map actions = new HashMap();
/**
* Constructor.
*/
public StatusAction() {
super(PolicyConstants.STATUS_TYPE_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_VIEW });
}
protected Method getMethod(Action action, String name) throws NoSuchMethodException {
synchronized (methods) {
Method method = (Method) methods.get(action.getClass().getName() + "#" + name);
if (method == null) {
method = action.getClass().getMethod(name, types);
methods.put(action.getClass().getName() + "#" + name, method);
}
return (method);
}
}
protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response, String name) throws Exception {
List subActions = new ArrayList();
/*
* First get all of the availble action mappings and create all of the
* forms
*/
int idx = 0;
for (Iterator i = PanelManager.getInstance().getPanels(Panel.STATUS_TAB, request, response).iterator(); i.hasNext();) {
Panel p = (Panel) i.next();
/*
* First try to find the panels action config. Look for the same
* name as the panel id prefixed by a /
*/
ActionConfig config = mapping.getModuleConfig().findActionConfig("/" + p.getId());
if (config != null && config instanceof ActionMapping) {
ActionMapping subMapping = (ActionMapping) config;
/*
* We have an action config, so get the form
*
*/
ActionForm subForm = null;
String formName = subMapping.getName();
if ("request".equals(subMapping.getScope())) {
subForm = (ActionForm) request.getAttribute(formName);
} else {
subForm = (ActionForm) request.getSession().getAttribute(formName);
}
String className = null;
FormBeanConfig formBean = mapping.getModuleConfig().findFormBeanConfig(formName);
if (formBean != null)
className = formBean.getType();
else
return (null);
if ((subForm != null) && className.equals(subForm.getClass().getName())) {
// recycle
} else {
Class clazz = Class.forName(className);
subForm = (ActionForm) clazz.newInstance();
}
if ("request".equals(mapping.getScope()))
request.setAttribute(formName, subForm);
else
request.getSession().setAttribute(formName, subForm);
subActions.add(new SubActionWrapper(subForm, subMapping));
subForm.reset(mapping, request);
/*
* We dont want to try and populate all forms on a post, only
* the one that has requested it. For this the form must
* have a hidden parameter with the name of 'subForm' and the
* value being the form name to populate
*/
if(formName.equals(((StatusForm)form).getSubForm())) {
((StatusForm)form).setSelectedTab(((StatusForm)form).getTabName(idx));
BeanUtils.populate(subForm, request.getParameterMap());
}
}
idx++;
}
/*
* Initiailise the forms
*/
((StatusForm) form).init(subActions, mapping, request);
/*
* Now try to create actions and run the provided action target on each.
* Any forwards returned are ignored
*/
for (Iterator i = subActions.iterator(); i.hasNext();) {
SubActionWrapper wrapper = (SubActionWrapper) i.next();
/*
* We have an action config, so get the action instance
*/
Action a = processActionCreate(request, response, wrapper.getMapping());
if (name == null || name.equals("")) {
name = "unspecified";
}
// Identify the method object to be dispatched to
try {
Method method = getMethod(a, name);
Object args[] = { wrapper.getMapping(), wrapper.getForm(), request, response };
/*
* Invoke the method. We don't care about the forward returned,
* it will not be used.
*/
method.invoke(a, args);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Now do the normal dispatch handling. This is what determines the
* forward
*/
if (name == null) {
return this.unspecified(mapping, form, request, response);
}
Method method = null;
try {
method = getMethod(name);
} catch (NoSuchMethodException e) {
return this.unspecified(mapping, form, request, response);
}
ActionForward forward = null;
try {
Object args[] = { mapping, form, request, response };
forward = (ActionForward) method.invoke(this, args);
} catch (ClassCastException e) {
String message = messages.getMessage("dispatch.return", mapping.getPath(), name);
log.error(message, e);
throw e;
} catch (IllegalAccessException e) {
String message = messages.getMessage("dispatch.error", mapping.getPath(), name);
log.error(message, e);
throw e;
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t instanceof Exception) {
throw ((Exception) t);
} else {
String message = messages.getMessage("dispatch.error", mapping.getPath(), name);
log.error(message, e);
throw new ServletException(t);
}
}
return (forward);
}
protected Action processActionCreate(HttpServletRequest request, HttpServletResponse response, ActionConfig mapping)
throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
String className = mapping.getType();
Action instance = null;
synchronized (actions) {
instance = (Action) actions.get(className);
if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}
return (instance);
}
if (log.isTraceEnabled()) {
log.trace(" Creating new Action instance");
}
instance = (Action) RequestUtils.applicationInstance(className);
instance.setServlet(this.servlet);
actions.put(className, instance);
}
return (instance);
}
/*
* (non-Javadoc)
*
* @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
* org.apache.struts.action.ActionForm,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward("display");
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
* org.apache.struts.action.ActionForm,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
}
public class SubActionWrapper {
ActionForm form;
ActionMapping mapping;
SubActionWrapper(ActionForm form, ActionMapping mapping) {
this.form = form;
this.mapping = mapping;
}
public ActionForm getForm() {
return form;
}
public ActionMapping getMapping() {
return mapping;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -