Skip to content

Commit

Permalink
Compatibility with checked exceptions
Browse files Browse the repository at this point in the history
For example SecretKeyCredentialHandler.
Also fix autoboxing in generated code.
  • Loading branch information
rmaucher committed Nov 15, 2024
1 parent 1275c60 commit 086d579
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
10 changes: 7 additions & 3 deletions java/org/apache/catalina/startup/Catalina.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}


Expand All @@ -950,7 +954,7 @@ protected void generateClassFooter(Digester digester) {


public interface ServerXml {
void load(Catalina catalina);
void load(Catalina catalina) throws Exception;
}


Expand Down
22 changes: 17 additions & 5 deletions java/org/apache/catalina/startup/ContextConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -530,7 +530,7 @@ protected void generateClassFooter(Digester digester) {


public interface ContextXml {
void load(Context context);
void load(Context context) throws Exception;
}


Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions java/org/apache/catalina/startup/LocalStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down
18 changes: 15 additions & 3 deletions java/org/apache/tomcat/util/IntrospectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
Refactor duplicate code for extracting media type and subtype from
<code>content-type</code> into a single method. (markt)
</scode>
<fix>
Compatibility of generated embedded code with components where
constructors or property related methods throw a checked exception.
(remm)
</fix>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down

0 comments on commit 086d579

Please sign in to comment.