I have a main application in a jar file, and it dynamically loads 'plug-ins', which are separate jar files.
I want one of these plug-ins to be a REST server, so I'm using jersey. The problem is when I load a plug-in with jersey, I can run the http server, but when I go to ../myapp/myresource, I get nothing. The browser hangs.
If I put a main method in the plug-in jar, and run the plug-in directly, it works. The page shows my static text in the browser.
Here is the code in the main.jar that loads plug-ins. I then call Start on each plug-in.
Vector<IPlugin> plugins = new Vector<IPlugin>();
// Get all files in directory "plugins"
File pluginDir = new File("plugins");
File [] pluginFiles = pluginDir.listFiles();
for(File file: pluginFiles)
{
// Load the file
URL url = null;
try
{
url = file.toURI().toURL();
} catch (MalformedURLException e)
{
System.out.println("Failed to get URL of .jar file");
continue;
}
ClassLoader loader = URLClassLoader.newInstance(
new URL[]{url},
Main.class.getClassLoader());
// Load the plug-in interface
try
{
Class cPluginClass = Class.forName("com.company.plugin_test.Plugin", true, loader);
IPlugin plugin = (IPlugin) cPluginClass.newInstance();
// Success, add the plugin to the plugin list
plugins.add(plugin);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
Here is the plug-in code.
public class Plugin
implements IPlugin
{
private IServer m_server;
public void Start ()
{
System.out.println("Loaded the http_plugin");
String BASE_URI = "http://localhost:8080/myapp/";
final ResourceConfig rc = new ResourceConfig().packages ("com.company.plugin_test");
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
this.start ();
}
public void SetServerInterface(IServer server)
{
this.m_server = server;
}
public void Stop ()
{
}
}
I'm guessing it's a classloader issue, but I'm new to java (native C++ background). Any ideas? I'll keep chipping away at it.
EDIT //---------------------------- So changing the above jersey code to the following works...
final ResourceConfig rc = new ResourceConfig().register (MyResource.class);
The question is, how do I set the resource config to the current jar context? Also, currently everything has the same package name (com.company.plugin_test).
This worked. I have yet to find a way to do this generically (ie scan all classes in a package or directory). Truth is I won't have that many web resources, so calling each out is fine.
final ResourceConfig rc = new ResourceConfig().register (MyResource.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.