Merge pull request #1140 from cshannon/jakarta-javax-exception-conversion

AMQ-9418 - Support converting jakarta jms exceptions to javax
This commit is contained in:
Christopher L. Shannon 2024-01-16 15:22:02 -05:00 committed by GitHub
commit ef9c0bd597
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 12 deletions

View File

@ -108,18 +108,50 @@
<goals> <goals>
<goal>replace</goal> <goal>replace</goal>
</goals> </goals>
<configuration>
<includes>
<include>${project.build.directory}/copied-sources/activemq-client/**/*.java</include>
</includes>
<excludes>
<exclude>${project.build.directory}/copied-sources/activemq-client/**/openwire/OpenWireUtil.java</exclude>
</excludes>
<token>javax.jms</token>
<value>jakarta.jms</value>
<regexFlags>
<regexFlag>MULTILINE</regexFlag>
</regexFlags>
</configuration>
</execution>
<!-- Special case to handle OpenWireUtil which will do conversions for JMS exception types -->
<execution>
<id>jms-package-to-replace</id>
<phase>initialize</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${project.build.directory}/copied-sources/activemq-client/**/openwire/OpenWireUtil.java</include>
</includes>
<token>jmsPackageToReplace = "jakarta.jms"</token>
<value>jmsPackageToReplace = "javax.jms"</value>
</configuration>
</execution>
<execution>
<id>jms-package-to-use</id>
<phase>initialize</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${project.build.directory}/copied-sources/activemq-client/**/openwire/OpenWireUtil.java</include>
</includes>
<token>jmsPackageToUse = "javax.jms"</token>
<value>jmsPackageToUse = "jakarta.jms"</value>
</configuration>
</execution> </execution>
</executions> </executions>
<configuration>
<includes>
<include>${project.build.directory}/copied-sources/activemq-client/**/*.java</include>
</includes>
<token>javax.jms</token>
<value>jakarta.jms</value>
<regexFlags>
<regexFlag>MULTILINE</regexFlag>
</regexFlags>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>

View File

@ -18,6 +18,9 @@ package org.apache.activemq.openwire;
public class OpenWireUtil { public class OpenWireUtil {
private static final String jmsPackageToReplace = "jakarta.jms";
private static final String jmsPackageToUse = "javax.jms";
/** /**
* Verify that the provided class extends {@link Throwable} and throw an * Verify that the provided class extends {@link Throwable} and throw an
* {@link IllegalArgumentException} if it does not. * {@link IllegalArgumentException} if it does not.
@ -29,4 +32,19 @@ public class OpenWireUtil {
throw new IllegalArgumentException("Class " + clazz + " is not assignable to Throwable"); throw new IllegalArgumentException("Class " + clazz + " is not assignable to Throwable");
} }
} }
/**
* This method can be used to convert from javax -> jakarta or
* vice versa depending on the version used by the client
*
* @param className
* @return
*/
public static String convertJmsPackage(String className) {
if (className != null && className.startsWith(jmsPackageToReplace)) {
return className.replace(jmsPackageToReplace, jmsPackageToUse);
}
return className;
}
} }

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.activemq.openwire.v12; package org.apache.activemq.openwire.v12;
import static org.apache.activemq.openwire.OpenWireUtil.convertJmsPackage;
import java.io.DataInput; import java.io.DataInput;
import java.io.DataOutput; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
@ -197,7 +199,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
for (int i = 0; i < ss.length; i++) { for (int i = 0; i < ss.length; i++) {
try { try {
ss[i] = (StackTraceElement)STACK_TRACE_ELEMENT_CONSTRUCTOR ss[i] = (StackTraceElement)STACK_TRACE_ELEMENT_CONSTRUCTOR
.newInstance(new Object[] {tightUnmarshalString(dataIn, bs), .newInstance(new Object[] {convertJmsPackage(tightUnmarshalString(dataIn, bs)),
tightUnmarshalString(dataIn, bs), tightUnmarshalString(dataIn, bs),
tightUnmarshalString(dataIn, bs), tightUnmarshalString(dataIn, bs),
Integer.valueOf(dataIn.readInt())}); Integer.valueOf(dataIn.readInt())});
@ -227,6 +229,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
private Throwable createThrowable(String className, String message) { private Throwable createThrowable(String className, String message) {
try { try {
className = convertJmsPackage(className);
Class clazz = Class.forName(className, false, BaseDataStreamMarshaller.class.getClassLoader()); Class clazz = Class.forName(className, false, BaseDataStreamMarshaller.class.getClassLoader());
OpenWireUtil.validateIsThrowable(clazz); OpenWireUtil.validateIsThrowable(clazz);
Constructor constructor = clazz.getConstructor(new Class[] {String.class}); Constructor constructor = clazz.getConstructor(new Class[] {String.class});
@ -521,7 +524,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
for (int i = 0; i < ss.length; i++) { for (int i = 0; i < ss.length; i++) {
try { try {
ss[i] = (StackTraceElement)STACK_TRACE_ELEMENT_CONSTRUCTOR ss[i] = (StackTraceElement)STACK_TRACE_ELEMENT_CONSTRUCTOR
.newInstance(new Object[] {looseUnmarshalString(dataIn), .newInstance(new Object[] {convertJmsPackage(looseUnmarshalString(dataIn)),
looseUnmarshalString(dataIn), looseUnmarshalString(dataIn),
looseUnmarshalString(dataIn), looseUnmarshalString(dataIn),
Integer.valueOf(dataIn.readInt())}); Integer.valueOf(dataIn.readInt())});