From 086d579a0461dc45f974360a722fe0b4c8226a97 Mon Sep 17 00:00:00 2001 From: remm Date: Fri, 15 Nov 2024 13:48:39 +0100 Subject: [PATCH] Compatibility with checked exceptions For example SecretKeyCredentialHandler. Also fix autoboxing in generated code. --- .../org/apache/catalina/startup/Catalina.java | 10 ++++++--- .../catalina/startup/ContextConfig.java | 22 ++++++++++++++----- .../catalina/startup/LocalStrings.properties | 1 + .../tomcat/util/IntrospectionUtils.java | 18 ++++++++++++--- webapps/docs/changelog.xml | 5 +++++ 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/java/org/apache/catalina/startup/Catalina.java b/java/org/apache/catalina/startup/Catalina.java index 01d7d3cbb981..3d5ddabbcf5c 100644 --- a/java/org/apache/catalina/startup/Catalina.java +++ b/java/org/apache/catalina/startup/Catalina.java @@ -587,7 +587,11 @@ protected void parseServerXml(boolean start) { } if (serverXml != null) { - serverXml.load(this); + try { + serverXml.load(this); + } catch (Exception e) { + log.warn(sm.getString("catalina.configFail", "GeneratedCode"), e); + } } else { try (ConfigurationSource.Resource resource = ConfigFileLoader.getSource().getServerXml()) { // Create and execute our Digester @@ -938,7 +942,7 @@ protected void generateClassHeader(Digester digester, boolean start) { code.append(" implements "); code.append(ServerXml.class.getName().replace('$', '.')).append(" {").append(System.lineSeparator()); code.append("public void load(").append(Catalina.class.getName()); - code.append(' ').append(digester.toVariableName(this)).append(") {").append(System.lineSeparator()); + code.append(' ').append(digester.toVariableName(this)).append(") throws Exception {").append(System.lineSeparator()); } @@ -950,7 +954,7 @@ protected void generateClassFooter(Digester digester) { public interface ServerXml { - void load(Catalina catalina); + void load(Catalina catalina) throws Exception; } diff --git a/java/org/apache/catalina/startup/ContextConfig.java b/java/org/apache/catalina/startup/ContextConfig.java index 8b2e3de8c6eb..d24b88d2763d 100644 --- a/java/org/apache/catalina/startup/ContextConfig.java +++ b/java/org/apache/catalina/startup/ContextConfig.java @@ -513,7 +513,7 @@ protected void generateClassHeader(Digester digester, String packageName, String code.append("public void load("); code.append(Context.class.getName()); String contextArgument = digester.toVariableName(context); - code.append(' ').append(contextArgument).append(") {").append(System.lineSeparator()); + code.append(' ').append(contextArgument).append(") throws Exception {").append(System.lineSeparator()); // Create a new variable with the concrete type digester.setKnown(context); code.append(context.getClass().getName()).append(' ').append(digester.toVariableName(context)); @@ -530,7 +530,7 @@ protected void generateClassFooter(Digester digester) { public interface ContextXml { - void load(Context context); + void load(Context context) throws Exception; } @@ -573,7 +573,11 @@ protected void contextConfig(Digester digester) { contextXml = (ContextXml) Digester.loadGeneratedClass(contextXmlClassName); } if (contextXml != null) { - contextXml.load(context); + try { + contextXml.load(context); + } catch (Exception e) { + log.warn(sm.getString("contextConfig.loadError"), e); + } contextXml = null; } else if (!useGeneratedCode) { try (ConfigurationSource.Resource contextXmlResource = @@ -614,7 +618,11 @@ protected void contextConfig(Digester digester) { contextXml = (ContextXml) Digester.loadGeneratedClass(contextXmlClassName); } if (contextXml != null) { - contextXml.load(context); + try { + contextXml.load(context); + } catch (Exception e) { + log.warn(sm.getString("contextConfig.loadError"), e); + } contextXml = null; } else if (!useGeneratedCode) { String hostContextFile = Container.getConfigPath(context, Constants.HostContextXml); @@ -654,7 +662,11 @@ protected void contextConfig(Digester digester) { contextXml = (ContextXml) Digester.loadGeneratedClass(contextXmlClassName); } if (contextXml != null) { - contextXml.load(context); + try { + contextXml.load(context); + } catch (Exception e) { + log.warn(sm.getString("contextConfig.loadError"), e); + } contextXml = null; } else if (!useGeneratedCode) { if (generateCode) { diff --git a/java/org/apache/catalina/startup/LocalStrings.properties b/java/org/apache/catalina/startup/LocalStrings.properties index b3c53f8c19fc..b9b000407c35 100644 --- a/java/org/apache/catalina/startup/LocalStrings.properties +++ b/java/org/apache/catalina/startup/LocalStrings.properties @@ -75,6 +75,7 @@ contextConfig.invalidSciHandlesTypes=Unable to load class [{0}] to check against contextConfig.jarFile=Unable to process Jar [{0}] for annotations contextConfig.jspFile.error=JSP file [{0}] must start with a ''/'' contextConfig.jspFile.warning=WARNING: JSP file [{0}] must start with a ''/'' in Servlet 2.4 +contextConfig.loadError=Error loading generated code contextConfig.missingRealm=No Realm has been configured to authenticate against contextConfig.noAntiLocking=The value [{0}] configured for java.io.tmpdir does not point to a valid directory. The antiResourceLocking setting for the web application [{1}] will be ignored. contextConfig.noJsp=Skipping JSP property group for URL [{0}], no JSP Servlet found for name [{1}] diff --git a/java/org/apache/tomcat/util/IntrospectionUtils.java b/java/org/apache/tomcat/util/IntrospectionUtils.java index 89a459f692f0..e93a77d2a92b 100644 --- a/java/org/apache/tomcat/util/IntrospectionUtils.java +++ b/java/org/apache/tomcat/util/IntrospectionUtils.java @@ -106,7 +106,11 @@ public static boolean setProperty(Object o, String name, String value, ok = false; } if (actualMethod != null) { - actualMethod.append(method.getName()).append("(Integer.valueOf(\"").append(value).append("\"))"); + if ("java.lang.Integer".equals(paramType.getName())) { + actualMethod.append(method.getName()).append("(Integer.valueOf(\"").append(value).append("\"))"); + } else { + actualMethod.append(method.getName()).append("(Integer.parseInt(\"").append(value).append("\"))"); + } } // Try a setFoo ( long ) } else if ("java.lang.Long".equals(paramType.getName()) @@ -117,14 +121,22 @@ public static boolean setProperty(Object o, String name, String value, ok = false; } if (actualMethod != null) { - actualMethod.append(method.getName()).append("(Long.valueOf(\"").append(value).append("\"))"); + if ("java.lang.Long".equals(paramType.getName())) { + actualMethod.append(method.getName()).append("(Long.valueOf(\"").append(value).append("\"))"); + } else { + actualMethod.append(method.getName()).append("(Long.parseLong(\"").append(value).append("\"))"); + } } // Try a setFoo ( boolean ) } else if ("java.lang.Boolean".equals(paramType.getName()) || "boolean".equals(paramType.getName())) { params[0] = Boolean.valueOf(value); if (actualMethod != null) { - actualMethod.append(method.getName()).append("(Boolean.valueOf(\"").append(value).append("\"))"); + if ("java.lang.Boolean".equals(paramType.getName())) { + actualMethod.append(method.getName()).append("(Boolean.valueOf(\"").append(value).append("\"))"); + } else { + actualMethod.append(method.getName()).append("(Boolean.parseBoolean(\"").append(value).append("\"))"); + } } // Try a setFoo ( InetAddress ) } else if ("java.net.InetAddress".equals(paramType diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 79f33e5371b3..9c162c5c6eca 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -123,6 +123,11 @@ Refactor duplicate code for extracting media type and subtype from content-type into a single method. (markt) + + Compatibility of generated embedded code with components where + constructors or property related methods throw a checked exception. + (remm) +