Skip to content

Commit 7a02ae8

Browse files
authored
Merge pull request #1110 from the-thing/mesasge-factory-null-fix
Safer context class loader `MessageFactory` init
2 parents 73892e7 + 32a8a69 commit 7a02ae8

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

quickfixj-core/src/main/java/quickfix/DefaultMessageFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ public void addFactory(String beginString, String factoryClassName) throws Class
118118
// try using our own classloader
119119
factoryClass = (Class<? extends MessageFactory>) Class.forName(factoryClassName);
120120
} catch (ClassNotFoundException e) {
121-
// try using context classloader (i.e. allow caller to specify it)
122-
Thread.currentThread().getContextClassLoader().loadClass(factoryClassName);
121+
// try using context classloader (i.e. allow caller to specify it)
122+
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
123+
124+
if (contextClassLoader != null) {
125+
factoryClass = (Class<? extends MessageFactory>) contextClassLoader.loadClass(factoryClassName);
126+
}
123127
}
124128
// if factory is found, add it
125129
if (factoryClass != null) {

quickfixj-core/src/test/java/quickfix/DefaultMessageFactoryTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package quickfix;
22

33
import static org.junit.Assert.*;
4+
import static org.mockito.Mockito.doReturn;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
48
import static quickfix.FixVersions.*;
59
import static quickfix.field.ApplVerID.*;
610

@@ -9,6 +13,7 @@
913
import org.junit.runner.RunWith;
1014
import org.junit.runners.Parameterized;
1115
import quickfix.field.*;
16+
import quickfix.fix44.MessageFactory;
1217
import quickfix.test.util.ExpectedTestFailure;
1318

1419
/**
@@ -81,6 +86,24 @@ protected void execute() throws Throwable {
8186
factory.create(BEGINSTRING_FIX40, MsgType.MARKET_DATA_SNAPSHOT_FULL_REFRESH, NoMDEntries.FIELD));
8287
}
8388

89+
@Test
90+
public void testContextClassLoaderFactory() throws ClassNotFoundException {
91+
ClassLoader customLoader = mock(ClassLoader.class);
92+
doReturn(MessageFactory.class).when(customLoader).loadClass("foo.DefaultMessageFactory");
93+
94+
ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
95+
Thread.currentThread().setContextClassLoader(customLoader);
96+
97+
try {
98+
factory.addFactory(BEGINSTRING_FIX44, "foo.DefaultMessageFactory");
99+
} finally {
100+
Thread.currentThread().setContextClassLoader(previousClassLoader);
101+
}
102+
103+
verify(customLoader).loadClass("foo.DefaultMessageFactory");
104+
verifyNoMoreInteractions(customLoader);
105+
}
106+
84107
private static void assertMessage(Class<?> expectedMessageClass, String expectedMessageType, Message message) throws Exception {
85108
assertEquals(expectedMessageClass, message.getClass());
86109
assertEquals(expectedMessageType, message.getHeader().getString(MsgType.FIELD));

0 commit comments

Comments
 (0)