Skip to content

Commit

Permalink
AMQ-9418 - Support converting javax jms exceptions to jakarta
Browse files Browse the repository at this point in the history
This fixes the OpenWire v12 marshaller so that if an exception is
received by a javax based broker the client will convert back to a
jakarta jms exception type.
  • Loading branch information
cshannon committed Jan 16, 2024
1 parent 343e3de commit be64fdb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

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.
Expand All @@ -29,4 +32,19 @@ public static void validateIsThrowable(Class<?> clazz) {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -197,7 +199,7 @@ protected Throwable tightUnmarsalThrowable(OpenWireFormat wireFormat, DataInput
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()});
Expand Down Expand Up @@ -227,6 +229,7 @@ protected Throwable tightUnmarsalThrowable(OpenWireFormat wireFormat, DataInput

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});
Expand Down Expand Up @@ -521,7 +524,7 @@ protected Throwable looseUnmarsalThrowable(OpenWireFormat wireFormat, DataInput
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()});
Expand Down

0 comments on commit be64fdb

Please sign in to comment.