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

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

View File

@ -18,6 +18,9 @@ package org.apache.activemq.openwire;
public class OpenWireUtil {
private static final String jmsPackageToReplace = "javax.jms";
private static final String jmsPackageToUse = "jakarta.jms";
/**
* Verify that the provided class extends {@link Throwable} and throw an
* {@link IllegalArgumentException} if it does not.
@ -29,4 +32,19 @@ public class OpenWireUtil {
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;
import static org.apache.activemq.openwire.OpenWireUtil.convertJmsPackage;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -197,7 +199,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
for (int i = 0; i < ss.length; i++) {
try {
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),
dataIn.readInt()});
@ -227,6 +229,7 @@ public abstract class BaseDataStreamMarshaller implements DataStreamMarshaller {
private Throwable createThrowable(String className, String message) {
try {
className = convertJmsPackage(className);
Class clazz = Class.forName(className, false, BaseDataStreamMarshaller.class.getClassLoader());
OpenWireUtil.validateIsThrowable(clazz);
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++) {
try {
ss[i] = (StackTraceElement)STACK_TRACE_ELEMENT_CONSTRUCTOR
.newInstance(new Object[] {looseUnmarshalString(dataIn),
.newInstance(new Object[] {convertJmsPackage(looseUnmarshalString(dataIn)),
looseUnmarshalString(dataIn),
looseUnmarshalString(dataIn),
dataIn.readInt()});