mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-2537 cleans up code and logging
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1419038 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+84
-82
@@ -3,8 +3,8 @@ package org.apache.struts2.dispatcher.mapper;
|
||||
import com.opensymphony.xwork2.config.ConfigurationManager;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -14,7 +14,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: description -->
|
||||
*
|
||||
*
|
||||
* A prefix based action mapper that is capable of delegating to other {@link ActionMapper}s based on the request's prefix
|
||||
*
|
||||
* It is configured through struts.xml
|
||||
@@ -37,98 +37,100 @@ import java.util.Map;
|
||||
*
|
||||
* @see ActionMapper
|
||||
* @see ActionMapping
|
||||
*
|
||||
*/
|
||||
public class PrefixBasedActionMapper extends DefaultActionMapper implements ActionMapper {
|
||||
protected transient final Log log = LogFactory.getLog(getClass());
|
||||
protected Container container;
|
||||
protected Map<String,ActionMapper> actionMappers = new HashMap<String,ActionMapper>();
|
||||
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PrefixBasedActionMapper.class);
|
||||
|
||||
@Inject(StrutsConstants.PREFIX_BASED_MAPPER_CONFIGURATION)
|
||||
public void setPrefixBasedActionMappers(String list) {
|
||||
if (list != null) {
|
||||
String[] mappers = list.split(",");
|
||||
for (String mapper : mappers) {
|
||||
String[] thisMapper = mapper.split(":");
|
||||
if ((thisMapper != null) && (thisMapper.length == 2)) {
|
||||
String mapperPrefix = thisMapper[0].trim();
|
||||
String mapperName = thisMapper[1].trim();
|
||||
Object obj = container.getInstance(ActionMapper.class, mapperName);
|
||||
if (obj != null) {
|
||||
actionMappers.put(mapperPrefix, (ActionMapper) obj);
|
||||
} else if (log.isDebugEnabled()) {
|
||||
log.debug("invalid PrefixBasedActionMapper config entry: " + mapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected Container container;
|
||||
protected Map<String, ActionMapper> actionMappers = new HashMap<String, ActionMapper>();
|
||||
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
|
||||
String uri = getUri(request);
|
||||
for (int lastIndex = uri.lastIndexOf('/'); lastIndex > (-1); lastIndex = uri.lastIndexOf('/', lastIndex-1)) {
|
||||
ActionMapper actionMapper = actionMappers.get(uri.substring(0,lastIndex));
|
||||
if (actionMapper != null) {
|
||||
ActionMapping actionMapping = actionMapper.getMapping(request, configManager);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Using ActionMapper "+actionMapper);
|
||||
}
|
||||
if (actionMapping != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
if (actionMapping.getParams() != null) {
|
||||
log.debug("ActionMapper found mapping. Parameters: "+actionMapping.getParams());
|
||||
for (Map.Entry<String,Object> mappingParameterEntry : ((Map<String,Object>)(actionMapping.getParams())).entrySet()) {
|
||||
Object paramValue = mappingParameterEntry.getValue();
|
||||
if (paramValue == null) {
|
||||
log.debug(mappingParameterEntry.getKey()+" : null!");
|
||||
} else if (paramValue instanceof String[]) {
|
||||
log.debug(mappingParameterEntry.getKey()+" : (String[]) "+Arrays.toString((String[])paramValue));
|
||||
} else if (paramValue instanceof String) {
|
||||
log.debug(mappingParameterEntry.getKey()+" : (String) "+(String)paramValue);
|
||||
} else {
|
||||
log.debug(mappingParameterEntry.getKey()+" : (Object) "+(paramValue.toString()));
|
||||
@Inject(StrutsConstants.PREFIX_BASED_MAPPER_CONFIGURATION)
|
||||
public void setPrefixBasedActionMappers(String list) {
|
||||
if (list != null) {
|
||||
String[] mappers = list.split(",");
|
||||
for (String mapper : mappers) {
|
||||
String[] thisMapper = mapper.split(":");
|
||||
if ((thisMapper != null) && (thisMapper.length == 2)) {
|
||||
String mapperPrefix = thisMapper[0].trim();
|
||||
String mapperName = thisMapper[1].trim();
|
||||
Object obj = container.getInstance(ActionMapper.class, mapperName);
|
||||
if (obj != null) {
|
||||
actionMappers.put(mapperPrefix, (ActionMapper) obj);
|
||||
} else if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("invalid PrefixBasedActionMapper config entry: [#0]", mapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return actionMapping;
|
||||
} else if (log.isDebugEnabled()) {
|
||||
log.debug("ActionMapper "+actionMapper+" failed to return an ActionMapping");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("no ActionMapper found");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getUriFromActionMapping(ActionMapping mapping) {
|
||||
String namespace = mapping.getNamespace();
|
||||
for (int lastIndex = namespace.length(); lastIndex > (-1); lastIndex = namespace.lastIndexOf('/', lastIndex-1)) {
|
||||
ActionMapper actionMapper = actionMappers.get(namespace.substring(0,lastIndex));
|
||||
if (actionMapper != null) {
|
||||
String uri = actionMapper.getUriFromActionMapping(mapping);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Using ActionMapper "+actionMapper);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
|
||||
String uri = getUri(request);
|
||||
for (int lastIndex = uri.lastIndexOf('/'); lastIndex > (-1); lastIndex = uri.lastIndexOf('/', lastIndex - 1)) {
|
||||
ActionMapper actionMapper = actionMappers.get(uri.substring(0, lastIndex));
|
||||
if (actionMapper != null) {
|
||||
ActionMapping actionMapping = actionMapper.getMapping(request, configManager);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Using ActionMapper [#0]", actionMapper.toString());
|
||||
}
|
||||
if (actionMapping != null) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
if (actionMapping.getParams() != null) {
|
||||
LOG.debug("ActionMapper found mapping. Parameters: [#0]", actionMapping.getParams().toString());
|
||||
for (Map.Entry<String, Object> mappingParameterEntry : actionMapping.getParams().entrySet()) {
|
||||
Object paramValue = mappingParameterEntry.getValue();
|
||||
if (paramValue == null) {
|
||||
LOG.debug("[#0] : null!", mappingParameterEntry.getKey());
|
||||
} else if (paramValue instanceof String[]) {
|
||||
LOG.debug("[#0] : (String[]) #1", mappingParameterEntry.getKey(), Arrays.toString((String[]) paramValue));
|
||||
} else if (paramValue instanceof String) {
|
||||
LOG.debug("[#0] : (String) [#1]", mappingParameterEntry.getKey(), paramValue.toString());
|
||||
} else {
|
||||
LOG.debug("[#0] : (Object) [#1]", mappingParameterEntry.getKey(), paramValue.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return actionMapping;
|
||||
} else if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("ActionMapper [#0] failed to return an ActionMapping", actionMapper.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (uri != null) {
|
||||
return uri;
|
||||
} else if (log.isDebugEnabled()) {
|
||||
log.debug("ActionMapper "+actionMapper+" failed to return an ActionMapping (null)");
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("No ActionMapper found");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("ActionMapper failed to return a uri");
|
||||
|
||||
public String getUriFromActionMapping(ActionMapping mapping) {
|
||||
String namespace = mapping.getNamespace();
|
||||
for (int lastIndex = namespace.length(); lastIndex > (-1); lastIndex = namespace.lastIndexOf('/', lastIndex - 1)) {
|
||||
ActionMapper actionMapper = actionMappers.get(namespace.substring(0, lastIndex));
|
||||
if (actionMapper != null) {
|
||||
String uri = actionMapper.getUriFromActionMapping(mapping);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Using ActionMapper [#0]", actionMapper.toString());
|
||||
}
|
||||
if (uri != null) {
|
||||
return uri;
|
||||
} else if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("ActionMapper [#0] failed to return an ActionMapping (null)", actionMapper.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("ActionMapper failed to return a uri");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user