Skip to content

Commit 3836674

Browse files
authored
Minor cleanup for #159 (#172)
1 parent 0a97bb5 commit 3836674

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ app:
2323
2424
If you run into issues, you can disable the new implementation and revert to the previous behavior by setting the `multiThreadedConsumer` property to `false`.
2525

26+
#### Bug fixes
27+
- [ISSUE-159](https://github.com/SourceLabOrg/kafka-webview/issues/159) Fix for file uploads in Windows environment. Thanks for the contribution @[quentingodeau](https://github.com/quentingodeau)!
28+
29+
2630
#### Internal Dependency Updates
2731
- Upgrade from Spring Boot 2.0.8 to 2.0.9
2832

kafka-webview-ui/src/main/java/org/sourcelab/kafka/webview/ui/manager/plugin/PluginFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public Class<? extends T> getPluginClass(final String jarName, final String clas
8989
final Path absolutePath = getPathForJar(jarName);
9090
final URL jarUrl = absolutePath.toUri().toURL();
9191
final ClassLoader pluginClassLoader = new PluginClassLoader(jarUrl, getClass().getClassLoader());
92-
//final ClassLoader pluginClassLoader = new PluginClassLoader(jarUrl);
9392
return getPluginClass(pluginClassLoader, classpath);
9493
} catch (MalformedURLException exception) {
9594
throw new LoaderException("Unable to load jar " + jarName, exception);
@@ -146,9 +145,10 @@ public T getPlugin(final String jarName, final String classpath) throws LoaderEx
146145
* Check if instance of the class given at the classpath can be load from the given Jar.
147146
* @param jarName Jar to load the class from
148147
* @param classpath Classpath to class.
148+
* @return boolean true on success.
149149
* @throws LoaderException LoaderException When we run into issues.
150150
*/
151-
public void checkPlugin(final String jarName, final String classpath) throws LoaderException {
151+
public boolean checkPlugin(final String jarName, final String classpath) throws LoaderException {
152152
try {
153153
final Path absolutePath = getPathForJar(jarName);
154154
final URL jarUrl = absolutePath.toUri().toURL();
@@ -159,10 +159,10 @@ public void checkPlugin(final String jarName, final String classpath) throws Loa
159159
try (URLClassLoader pluginClassLoader = new PluginClassLoader(jarUrl, getClass().getClassLoader())) {
160160
Class<? extends T> pluginClass = getPluginClass(pluginClassLoader, classpath);
161161
pluginClass.getDeclaredConstructor().newInstance();
162+
163+
return true;
162164
}
163-
} catch (MalformedURLException exception) {
164-
throw new LoaderException("Unable to load jar " + jarName, exception);
165-
} catch (IOException exception) {
165+
} catch (final IOException exception) {
166166
throw new LoaderException("Unable to load jar " + jarName, exception);
167167
} catch (final NoClassDefFoundError e) {
168168
// Typically this happens if the uploaded JAR references some dependency that was

kafka-webview-ui/src/test/java/org/sourcelab/kafka/webview/ui/manager/plugin/PluginFactoryTest.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ public void testWithRecordFilter() throws LoaderException {
7777
recordFilter.includeRecord("topic", 1, 1L, "Key", "Value");
7878
}
7979

80+
/**
81+
* Test checking a RecordFilter.
82+
*/
83+
@Test
84+
public void testCheckPlugin_WithRecordFilter() throws LoaderException {
85+
final String jarFilename = "testPlugins.jar";
86+
final String classPath = "examples.filter.LowOffsetFilter";
87+
88+
// Find jar on filesystem.
89+
final URL jar = getClass().getClassLoader().getResource("testDeserializer/" + jarFilename);
90+
final String jarPath = new File(jar.getFile()).getParent();
91+
92+
// Create factory
93+
final PluginFactory<RecordFilter> factory = new PluginFactory<>(jarPath, RecordFilter.class);
94+
final Path pathForJar = factory.getPathForJar(jarFilename);
95+
96+
// Validate path is correct
97+
assertEquals("Has expected Path", jar.getPath(), pathForJar.toString());
98+
99+
// Get class instance
100+
final Class<? extends RecordFilter> pluginFilterClass = factory.getPluginClass(jarFilename, classPath);
101+
102+
// Validate
103+
assertNotNull(pluginFilterClass);
104+
assertEquals("Has expected name", classPath, pluginFilterClass.getName());
105+
assertTrue("Validate came from correct class loader", pluginFilterClass.getClassLoader() instanceof PluginClassLoader);
106+
107+
// Check filter instance
108+
final boolean result = factory.checkPlugin(jarFilename, classPath);
109+
assertTrue(result);
110+
}
111+
80112
/**
81113
* Test creating a Deserializer.
82114
*/
@@ -104,7 +136,7 @@ public void testWithDeserializer() throws LoaderException {
104136
assertEquals("Has expected name", classPath, pluginFilterClass.getName());
105137
assertTrue("Validate came from correct class loader", pluginFilterClass.getClassLoader() instanceof PluginClassLoader);
106138

107-
// Crete filter instance
139+
// Crete Deserializer instance
108140
final Deserializer deserializer = factory.getPlugin(jarFilename, classPath);
109141
assertNotNull(deserializer);
110142
assertEquals("Has correct name", classPath, deserializer.getClass().getName());
@@ -114,6 +146,38 @@ public void testWithDeserializer() throws LoaderException {
114146
final String result = (String) deserializer.deserialize("MyTopic", value.getBytes(StandardCharsets.UTF_8));
115147
}
116148

149+
/**
150+
* Test checking a Deserializer.
151+
*/
152+
@Test
153+
public void testCheckPlugin_WithDeserializer() throws LoaderException {
154+
final String jarFilename = "testPlugins.jar";
155+
final String classPath = "examples.deserializer.ExampleDeserializer";
156+
157+
// Find jar on filesystem.
158+
final URL jar = getClass().getClassLoader().getResource("testDeserializer/" + jarFilename);
159+
final String jarPath = new File(jar.getFile()).getParent();
160+
161+
// Create factory
162+
final PluginFactory<Deserializer> factory = new PluginFactory<>(jarPath, Deserializer.class);
163+
final Path pathForJar = factory.getPathForJar(jarFilename);
164+
165+
// Validate path is correct
166+
assertEquals("Has expected Path", jar.getPath(), pathForJar.toString());
167+
168+
// Get class instance
169+
final Class<? extends Deserializer> pluginFilterClass = factory.getPluginClass(jarFilename, classPath);
170+
171+
// Validate
172+
assertNotNull(pluginFilterClass);
173+
assertEquals("Has expected name", classPath, pluginFilterClass.getName());
174+
assertTrue("Validate came from correct class loader", pluginFilterClass.getClassLoader() instanceof PluginClassLoader);
175+
176+
// Check Deserializer instance
177+
final boolean result = factory.checkPlugin(jarFilename, classPath);
178+
assertTrue(result);
179+
}
180+
117181
/**
118182
* Tests loading a deserializer not from an external jar.
119183
*/

0 commit comments

Comments
 (0)