-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#806 Support custom ClassLookup in WireTypeConverter
- Loading branch information
1 parent
2ca5caf
commit 4756379
Showing
5 changed files
with
237 additions
and
50 deletions.
There are no files selected for viewing
18 changes: 14 additions & 4 deletions
18
src/main/java/net/openhft/chronicle/wire/WireTypeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,37 @@ | ||
package net.openhft.chronicle.wire; | ||
|
||
import net.openhft.chronicle.core.pool.ClassLookup; | ||
import net.openhft.chronicle.wire.internal.WireTypeConverterInternal; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class WireTypeConverter { | ||
private final WireTypeConverterInternal delegate; | ||
|
||
public WireTypeConverter(Validate validate) { | ||
public WireTypeConverter(@NotNull Validate validate, @NotNull ClassLookup classLookup) { | ||
delegate = new WireTypeConverterInternal(validate, classLookup); | ||
} | ||
|
||
public WireTypeConverter(@NotNull ClassLookup classLookup) { | ||
delegate = new WireTypeConverterInternal(classLookup); | ||
} | ||
|
||
public WireTypeConverter(@NotNull Validate validate) { | ||
delegate = new WireTypeConverterInternal(validate); | ||
} | ||
|
||
public WireTypeConverter() { | ||
delegate = new WireTypeConverterInternal(); | ||
} | ||
|
||
public CharSequence jsonToYaml(CharSequence json) throws Exception { | ||
public CharSequence jsonToYaml(CharSequence json) { | ||
return delegate.jsonToYaml(json); | ||
} | ||
|
||
public CharSequence yamlToJson(CharSequence yaml) throws Exception { | ||
public CharSequence yamlToJson(CharSequence yaml) { | ||
return delegate.yamlToJson(yaml); | ||
} | ||
|
||
public void addAlias(Class newClass, String oldTypeName) { | ||
public void addAlias(Class<?> newClass, String oldTypeName) { | ||
delegate.addAlias(newClass, oldTypeName); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/net/openhft/chronicle/wire/internal/UnknownClassBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.openhft.chronicle.wire.internal; | ||
|
||
import net.openhft.chronicle.core.io.IORuntimeException; | ||
import net.openhft.chronicle.core.io.InvalidMarshallableException; | ||
import net.openhft.chronicle.wire.SelfDescribingMarshallable; | ||
import net.openhft.chronicle.wire.WireIn; | ||
import net.openhft.chronicle.wire.WireOut; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public abstract class UnknownClassBase extends SelfDescribingMarshallable { | ||
private Map<Object, Object> map = new LinkedHashMap<>(); | ||
|
||
@Override | ||
public void readMarshallable(@NotNull WireIn wire) throws IORuntimeException, InvalidMarshallableException { | ||
map = wire.readAllAsMap(Object.class, Object.class, new LinkedHashMap<>()); | ||
} | ||
|
||
@Override | ||
public void writeMarshallable(@NotNull WireOut wire) throws InvalidMarshallableException { | ||
wire.writeAllAsMap(Object.class, Object.class, map); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/net/openhft/chronicle/wire/internal/UnknownClassLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.openhft.chronicle.wire.internal; | ||
|
||
import net.openhft.chronicle.core.Jvm; | ||
import net.openhft.chronicle.core.OS; | ||
import net.openhft.chronicle.core.pool.ClassLookup; | ||
import net.openhft.chronicle.core.util.ClassNotFoundRuntimeException; | ||
import net.openhft.compiler.CachedCompiler; | ||
|
||
import java.io.File; | ||
|
||
public class UnknownClassLookup implements ClassLookup { | ||
public static final CachedCompiler CACHED_COMPILER = | ||
new CachedCompiler(Jvm.isDebug() ? new File(OS.getTarget(), "generated-test-sources") : null, null); | ||
|
||
private final ClassLookup delegate; | ||
|
||
public UnknownClassLookup(ClassLookup delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public Class<?> forName(CharSequence name) throws ClassNotFoundRuntimeException { | ||
try { | ||
return delegate.forName(name); | ||
} catch (Exception e) { | ||
String className = name.toString(); | ||
Class<?> unknownClass; | ||
try { | ||
unknownClass = CACHED_COMPILER.loadFromJava(className, | ||
"public class " + className + " extends " + UnknownClassBase.class.getName() + "{}"); | ||
} catch (ClassNotFoundException ex) { | ||
throw new ClassNotFoundRuntimeException(ex); | ||
} | ||
|
||
addAlias(unknownClass, className); | ||
return delegate.forName(name); | ||
} | ||
} | ||
|
||
@Override | ||
public String nameFor(Class<?> clazz) throws IllegalArgumentException { | ||
return delegate.nameFor(clazz); | ||
} | ||
|
||
@Override | ||
public void addAlias(Class<?>... classes) { | ||
delegate.addAlias(classes); | ||
} | ||
|
||
@Override | ||
public void addAlias(Class<?> clazz, String names) { | ||
delegate.addAlias(clazz, names); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/net/openhft/chronicle/wire/WireTypeConverterWithUnknownClassesLookupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package net.openhft.chronicle.wire; | ||
|
||
import net.openhft.chronicle.core.pool.ClassAliasPool; | ||
import net.openhft.chronicle.wire.internal.UnknownClassLookup; | ||
import org.junit.Test; | ||
|
||
public class WireTypeConverterWithUnknownClassesLookupTest extends WireTestCommon { | ||
|
||
@Test | ||
public void fromYamlToJsonAndBackToYaml() { | ||
WireTypeConverter wireTypeConverter = new WireTypeConverter(new UnknownClassLookup(ClassAliasPool.CLASS_ALIASES)); | ||
String yaml = "!ChronicleServicesCfg {\n" + | ||
" queues: {\n" + | ||
" in: { path: tmp/benchmark/in },\n" + | ||
" sender-one-out: { path: tmp/benchmark/sender-one-out, builder: !SingleChronicleQueueBuilder { useSparseFiles: true, rollCycle: HUGE_DAILY } },\n" + | ||
" sender-two-out: { path: tmp/benchmark/sender-two-out, builder: !SingleChronicleQueueBuilder { useSparseFiles: true, rollCycle: HUGE_DAILY } },\n" + | ||
" sender-three-out: { path: tmp/benchmark/sender-three-out, builder: !SingleChronicleQueueBuilder { useSparseFiles: true, rollCycle: HUGE_DAILY } },\n" + | ||
" receiver-out: { path: tmp/benchmark/receiver-out },\n" + | ||
" },\n" + | ||
" services: {\n" + | ||
" sender-one: {\n" + | ||
" inputs: [ in ],\n" + | ||
" output: sender-one-out,\n" + | ||
" startFromStrategy: START,\n" + | ||
" affinityCpu: any,\n" + | ||
" pretouchMS: 100,\n" + | ||
" serviceConfig: {\n" + | ||
" param: !CustomClass1 {\n" + | ||
" param2: value\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"; | ||
CharSequence json = wireTypeConverter.yamlToJson(yaml); | ||
System.out.println(json); | ||
|
||
CharSequence backToYaml = wireTypeConverter.jsonToYaml(json); | ||
System.out.println(backToYaml); | ||
} | ||
|
||
@Test | ||
public void typeReference() { | ||
WireTypeConverter wireTypeConverter = new WireTypeConverter(new UnknownClassLookup(ClassAliasPool.CLASS_ALIASES)); | ||
String yaml = "!ChronicleServicesCfg {\n" + | ||
" queues: {\n" + | ||
" in: { path: tmp/benchmark/in },\n" + | ||
" sender-one-out: { path: tmp/benchmark/sender-one-out, builder: !SingleChronicleQueueBuilder { useSparseFiles: true, rollCycle: HUGE_DAILY } },\n" + | ||
" sender-two-out: { path: tmp/benchmark/sender-two-out, builder: !SingleChronicleQueueBuilder { useSparseFiles: true, rollCycle: HUGE_DAILY } },\n" + | ||
" sender-three-out: { path: tmp/benchmark/sender-three-out, builder: !SingleChronicleQueueBuilder { useSparseFiles: true, rollCycle: HUGE_DAILY } },\n" + | ||
" receiver-out: { path: tmp/benchmark/receiver-out },\n" + | ||
" },\n" + | ||
" services: {\n" + | ||
" sender-one: {\n" + | ||
" inputs: [ in ],\n" + | ||
" output: sender-one-out,\n" + | ||
" startFromStrategy: START,\n" + | ||
" affinityCpu: any,\n" + | ||
" pretouchMS: 100,\n" + | ||
" implClass: !type non.existing.package.SenderOneService,\n" + | ||
" serviceConfig: {\n" + | ||
" param: !CustomClass1 {\n" + | ||
" param2: value\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"; | ||
|
||
CharSequence json = wireTypeConverter.yamlToJson(yaml); | ||
System.out.println(json); | ||
|
||
CharSequence backToYaml = wireTypeConverter.jsonToYaml(json); | ||
System.out.println(backToYaml); | ||
} | ||
} |