Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #159 to allow custom formatter uploading #164

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public String create(

// Attempt to load jar?
try {
deserializerLoader.getPlugin(tempFilename, messageFormatForm.getClasspath());
deserializerLoader.checkPlugin(tempFilename, messageFormatForm.getClasspath());
} catch (final LoaderException exception) {
// If we had issues, remove the temp location
Files.delete(Paths.get(jarPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

package org.sourcelab.kafka.webview.ui.manager.plugin;

import java.io.IOException;
import org.sourcelab.kafka.webview.ui.manager.plugin.exception.LoaderException;
import org.sourcelab.kafka.webview.ui.manager.plugin.exception.UnableToFindClassException;
import org.sourcelab.kafka.webview.ui.manager.plugin.exception.WrongImplementationException;

import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand Down Expand Up @@ -84,16 +86,16 @@ public Class<? extends T> getPluginClass(final String classpath) throws LoaderEx
*/
public Class<? extends T> getPluginClass(final String jarName, final String classpath) throws LoaderException {
try {
final String absolutePath = getPathForJar(jarName).toString();
final URL jarUrl = new URL("file://" + absolutePath);
final Path absolutePath = getPathForJar(jarName);
final URL jarUrl = absolutePath.toUri().toURL();
final ClassLoader pluginClassLoader = new PluginClassLoader(jarUrl, getClass().getClassLoader());
//final ClassLoader pluginClassLoader = new PluginClassLoader(jarUrl);
return getPluginClass(pluginClassLoader, classpath);
} catch (MalformedURLException exception) {
throw new LoaderException("Unable to load jar " + jarName, exception);
}
}

/**
* Internal method to load the given classpath using the given ClassLoader.
* @return Class instance.
Expand Down Expand Up @@ -139,12 +141,51 @@ public T getPlugin(final String jarName, final String classpath) throws LoaderEx
throw new LoaderException(errorMsg, e);
}
}

/**
* Check if instance of the class given at the classpath can be load from the given Jar.
* @param jarName Jar to load the class from
* @param classpath Classpath to class.
* @throws LoaderException LoaderException When we run into issues.
*/
public void checkPlugin(final String jarName, final String classpath) throws LoaderException {
try {
final Path absolutePath = getPathForJar(jarName);
final URL jarUrl = absolutePath.toUri().toURL();
// Windows issue, URLClassLoader open file so if we need to delete them
// (that the case for new uploaded file) then the close must be explicitly call.
// More information available here:
// https://docs.oracle.com/javase/8/docs/technotes/guides/net/ClassLoader.html
try (URLClassLoader pluginClassLoader = new PluginClassLoader(jarUrl, getClass().getClassLoader())) {
Class<? extends T> pluginClass = getPluginClass(pluginClassLoader, classpath);
pluginClass.getDeclaredConstructor().newInstance();
}
} catch (MalformedURLException exception) {
throw new LoaderException("Unable to load jar " + jarName, exception);
} catch (IOException exception) {
throw new LoaderException("Unable to load jar " + jarName, exception);
} catch (final NoClassDefFoundError e) {
// Typically this happens if the uploaded JAR references some dependency that was
// not package in the JAR. Attempt to provide a useful error msg.
final String errorMsg = e.getMessage()
+ " - Does your JAR include all of its required dependencies? "
+ "See https://github.com/SourceLabOrg/kafka-webview-examples#packaging-a-jar";
throw new LoaderException(errorMsg, e);
} catch (final InstantiationException | IllegalAccessException e) {
throw new LoaderException(e.getMessage(), e);
} catch (final NoSuchMethodException | InvocationTargetException e) {
// Typically this happens if referenced class in the uploaded JAR has no default constructor.
final String errorMsg = e.getMessage()
+ " - Does your class contain a default no argument constructor?";
throw new LoaderException(errorMsg, e);
}
}

/**
* Get the full path on disk to the given Jar file.
* @param jarName Jar to lookup full path to.
*/
public Path getPathForJar(final String jarName) {
return Paths.get(jarDirectory + "/", jarName).toAbsolutePath();
return Paths.get(jarDirectory, jarName).toAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.sourcelab.kafka.webview.ui.manager.plugin;

import java.io.BufferedInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -156,8 +157,9 @@ private String handleFileUpload(final MultipartFile file, final String outFileNa
}

// Get the file and save it somewhere
final byte[] bytes = file.getBytes();
Files.write(fullOutputPath, bytes);
try (BufferedInputStream in = new BufferedInputStream(file.getInputStream())) {
Files.copy(in, fullOutputPath);
}

return fullOutputPath.toString();
}
Expand Down