Skip to content

Commit

Permalink
Add Struts converters initialization to prevent GC issues
Browse files Browse the repository at this point in the history
Bind custom converters to the ServletContext to ensure they are not garbage collected when "convertNull" is enabled. This includes handling null-safe conversions for types like BigDecimal, Boolean, and Integer. Also, deregisters and re-registers default converters accordingly.
  • Loading branch information
KlausRicharz committed Dec 17, 2024
1 parent d5696a5 commit 617b428
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/java/org/tb/common/struts/DelegatingRequestProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
Expand Down Expand Up @@ -37,6 +53,31 @@ public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws
protected WebApplicationContext initWebApplicationContext(ActionServlet actionServlet) throws IllegalStateException {
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(actionServlet.getServletContext());
context.getServletContext().setAttribute("salatProperties", context.getBean(SalatProperties.class));

// ensure converters are bound to servlet context to not get garbage collected
String convertNull = actionServlet.getServletConfig().getInitParameter("convertNull");
if ("true".equalsIgnoreCase(convertNull) || "yes".equalsIgnoreCase(convertNull)
|| "on".equalsIgnoreCase(convertNull) || "y".equalsIgnoreCase(convertNull)
|| "1".equalsIgnoreCase(convertNull)) {

Map<Class<?>, Converter> converters = new HashMap<>();
converters.put(BigDecimal.class, new BigDecimalConverter(null));
converters.put(BigInteger.class, new BigIntegerConverter(null));
converters.put(Boolean.class, new BooleanConverter(null));
converters.put(Byte.class, new ByteConverter(null));
converters.put(Character.class, new CharacterConverter(null));
converters.put(Double.class, new DoubleConverter(null));
converters.put(Float.class, new FloatConverter(null));
converters.put(Integer.class, new IntegerConverter(null));
converters.put(Long.class, new LongConverter(null));
converters.put(Short.class, new ShortConverter(null));

context.getServletContext().setAttribute("struts.converters", converters);

ConvertUtils.deregister();
converters.forEach((clazz, converter) -> ConvertUtils.register(converter, clazz));
}

return context;
}

Expand Down

0 comments on commit 617b428

Please sign in to comment.