Skip to content

Commit

Permalink
内存优化
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed Nov 13, 2024
1 parent 84a729f commit 33dd64f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ public void amazing() {
if (requestAttributeListeners.isEmpty()) {
requestAttributeListeners = Collections.emptyList();
}
servlets.values().forEach(servletInfo -> {
servletInfo.setServletClass(null);
servletInfo.setJspFile(null);
});
filters.values().forEach(filterInfo -> {
filterInfo.setFilterClass(null);
});
}

public void addServletContextListener(ServletContextListener contextListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,71 @@
* @version V1.0 , 2020/11/14
*/
public class FilterMappingInfo extends UrlPattern {

private static final byte MAPPING_URL = 0x00;
private static final byte MAPPING_SERVLET = 0x01;
private static final byte DISPATCHER_FORWARD = 0x02;
private static final byte DISPATCHER_INCLUDE = DISPATCHER_FORWARD << 1;
private static final byte DISPATCHER_REQUEST = DISPATCHER_FORWARD << 2;
private static final byte DISPATCHER_ERROR = DISPATCHER_FORWARD << 3;
private static final byte DISPATCHER_ASYNC = DISPATCHER_FORWARD << 4;
private final String filterName;
private final FilterMappingType mappingType;
private final Set<DispatcherType> dispatcher;
private final byte mask;
private final String servletNameMapping;

public FilterMappingInfo(final String filterName, final FilterMappingType mappingType, final String servletNameMapping, String urlPattern, final Set<DispatcherType> dispatcher) {
public FilterMappingInfo(final String filterName, final FilterMappingType mappingType, final String servletNameMapping, String urlPattern, final Set<DispatcherType> dispatchers) {
super(urlPattern);
if (mappingType == null) {
throw new IllegalArgumentException("mappingType can not be null");
}
this.filterName = filterName;
this.mappingType = mappingType;
this.servletNameMapping = servletNameMapping;
this.dispatcher = dispatcher;
byte mask;
if (mappingType == FilterMappingType.SERVLET) {
mask = MAPPING_SERVLET;
} else {
mask = MAPPING_URL;
}

for (DispatcherType dispatcherType : dispatchers) {
switch (dispatcherType) {
case FORWARD:
mask |= DISPATCHER_FORWARD;
break;
case INCLUDE:
mask |= DISPATCHER_INCLUDE;
break;
case REQUEST:
mask |= DISPATCHER_REQUEST;
break;
case ERROR:
mask |= DISPATCHER_ERROR;
break;
case ASYNC:
mask |= DISPATCHER_ASYNC;
break;
default:
throw new IllegalArgumentException("Unsupported dispatcher type: " + dispatcherType);
}
}
this.mask = mask;
}

public FilterMappingType getMappingType() {
return mappingType;
public boolean isServletMappingType() {
return (mask & MAPPING_SERVLET) == MAPPING_SERVLET;
}

public String getServletNameMapping() {
return servletNameMapping;
}

public Set<DispatcherType> getDispatcher() {
return dispatcher;
public boolean contains(DispatcherType dispatcherType) {
return switch (dispatcherType) {
case FORWARD -> (mask & DISPATCHER_FORWARD) != 0;
case INCLUDE -> (mask & DISPATCHER_INCLUDE) != 0;
case REQUEST -> (mask & DISPATCHER_REQUEST) != 0;
case ERROR -> (mask & DISPATCHER_ERROR) != 0;
case ASYNC -> (mask & DISPATCHER_ASYNC) != 0;
};
}

public String getFilterName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
public class ServletInfo {
private static final Logger LOGGER = LoggerFactory.getLogger(ServletInfo.class);
private static final MultipartConfigElement DEFAULT_MULTIPART_CONFIG = new MultipartConfigElement("", -1, -1, -1);
public static final String DEFAULT_SERVLET_NAME = "default";
private final Map<String, String> initParams = new HashMap<>();
private final Map<String, String> securityRoles = new HashMap<>();
Expand All @@ -47,11 +48,11 @@ public class ServletInfo {
private String jspFile;

private boolean dynamic;
private MultipartConfigElement multipartConfig;
private MultipartConfigElement multipartConfig = DEFAULT_MULTIPART_CONFIG;

private boolean asyncSupported;
private boolean init = false;
private List<SecurityConstraint> securityConstraints = new ArrayList<>();
private final List<SecurityConstraint> securityConstraints = new ArrayList<>();

public synchronized void init(ServletContextImpl servletContext) {
if (init) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.smartboot.http.common.utils.StringUtils;
import tech.smartboot.servlet.conf.FilterInfo;
import tech.smartboot.servlet.conf.FilterMappingInfo;
import tech.smartboot.servlet.enums.FilterMappingType;
import tech.smartboot.servlet.util.PathMatcherUtil;

import java.io.IOException;
Expand Down Expand Up @@ -78,21 +77,19 @@ private List<FilterInfo> matchFilters(HandlerContext handlerContext) {
HttpServletRequest request = (HttpServletRequest) handlerContext.getRequest();
List<FilterInfo> filters = new ArrayList<>();
List<FilterMappingInfo> mappings = handlerContext.getServletContext().getDeploymentInfo().getFilterMappings();
mappings.stream().filter(filterMappingInfo -> filterMappingInfo.getDispatcher().contains(request.getDispatcherType())).forEach(mappingInfo -> {
if (mappingInfo.getMappingType() == FilterMappingType.URL) {
mappings.stream().filter(filterMappingInfo -> filterMappingInfo.contains(request.getDispatcherType())).forEach(mappingInfo -> {
if (mappingInfo.isServletMappingType()) {
if (handlerContext.getServletInfo() != null && StringUtils.equals(mappingInfo.getServletNameMapping(), handlerContext.getServletInfo().getServlet().getServletConfig().getServletName())) {
filters.add(handlerContext.getServletContext().getDeploymentInfo().getFilters().get(mappingInfo.getFilterName()));
}
} else {
String requestURI = request.getRequestURI();
if (request.getDispatcherType() == DispatcherType.INCLUDE) {
requestURI = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
}
if (PathMatcherUtil.matches(requestURI, contextPath.length(), mappingInfo)) {
filters.add(handlerContext.getServletContext().getDeploymentInfo().getFilters().get(mappingInfo.getFilterName()));
}
} else if (mappingInfo.getMappingType() == FilterMappingType.SERVLET) {
if (handlerContext.getServletInfo() != null && StringUtils.equals(mappingInfo.getServletNameMapping(), handlerContext.getServletInfo().getServlet().getServletConfig().getServletName())) {
filters.add(handlerContext.getServletContext().getDeploymentInfo().getFilters().get(mappingInfo.getFilterName()));
}
} else {
throw new IllegalStateException();
}
});
return filters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, bo

@Override
public Collection<String> getServletNameMappings() {
return deploymentInfo.getFilterMappings().stream().filter(filterMappingInfo -> filterMappingInfo.getFilterName().equals(filterDef.getFilterName()) && filterMappingInfo.getMappingType() == FilterMappingType.SERVLET).map(FilterMappingInfo::getServletNameMapping).collect(Collectors.toList());
return deploymentInfo.getFilterMappings().stream().filter(filterMappingInfo -> filterMappingInfo.getFilterName().equals(filterDef.getFilterName()) && filterMappingInfo.isServletMappingType()).map(FilterMappingInfo::getServletNameMapping).collect(Collectors.toList());
}

@Override
public Collection<String> getUrlPatternMappings() {
return deploymentInfo.getFilterMappings().stream().filter(filterMappingInfo -> filterMappingInfo.getFilterName().equals(filterDef.getFilterName()) && filterMappingInfo.getMappingType() == FilterMappingType.URL).map(FilterMappingInfo::getUrlPattern).collect(Collectors.toList());
return deploymentInfo.getFilterMappings().stream().filter(filterMappingInfo -> filterMappingInfo.getFilterName().equals(filterDef.getFilterName()) && !filterMappingInfo.isServletMappingType()).map(FilterMappingInfo::getUrlPattern).collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,6 @@ private void parseParts() throws ServletException {
}
try {
MultipartConfigElement multipartConfigElement = servletInfo.getMultipartConfig();
if (multipartConfigElement == null) {
multipartConfigElement = new MultipartConfigElement("");
}
//获取文件存放目录
File location = getLocation(multipartConfigElement);
if (!location.isDirectory()) {
Expand Down

0 comments on commit 33dd64f

Please sign in to comment.