mardi 10 avril 2018

Loading classes and their data from a JAR file isn't fully working?

I didn't really know what to set my question title as because it's so specific and to fully describe it would use up way too much space.

Here's what I'm trying to do:

I've developed an application, and part of it allows other people to program their own plug-ins for it. In order to do this, they need to download my library, and extend their plug-in class to one of my abstract classes in the library: SimplePlugin and AdvancedPlugin. I also have an interface, called Plugin which both classes implement. In order to dynamically load plug-ins at runtime, I have a ClassLoader set up to search for the Plugin interface as it combs through the .class files. I haven't had any problems with this, and I've been able to create plug-ins just fine without any issue.

The problem I've run into is that I decided that I wanted to make my application able to tell what build of my library a plug-in is using, so that I can prevent a plug-in from loading if it's using an older build. So, in my Plugin interface, I have a variable set to determine this:

public interface Plugin {

    public static String PluginLibrary_BUILD = "0.1.1";

    //methods...
}

Here's the other two classes for visual reference:

public abstract class SimplePlugin implements Plugin { 

    //methods...
}

public abstract class AdvancedPlugin extends SimplePlugin implements Plugin {

    //methods...
}

But every time I load in a new plug-in and try to read the build variable, it always ends up having the value contained within the Plugin class inside my project and not (what I assume should be), the value inside the JAR file I generate from exporting my plug-in.

Here's an example:

//import dependencies from my library

public class TestPlugin extends SimplePlugin {

    public TestPlugin() {
        super("TestPlugin", "v1.0");
    }

    @Override
    public void doOnAction() {
        System.out.println("Hello World!");
    }

}

So from here, I'd export the TestPlugin class as a JAR file, and it would export the dependencies from my library with it. Then, I'd drag the JAR file into a folder that I have set up, and from there my application would read it as a plug-in and do its thing. Unfortunately, "its thing" involves reading my PluginLibrary_BUILD variable from my project files and not from the JAR file (maybe?) because if I change it within my project files, the plug-in tells me that its build changed as well.

I'm not exactly sure what's going on, that's just my best guess, so I'm looking for some insight as to why what's happening is happening, and how to solve the problem. If you want to look at the source files exactly as they're presented, I have them uploaded here. Plugin is StreamSAKPlugin and the two abstract classes are located under /types and are StreamSAKSimplePlugin and StreamSAKAdvancedPlugin (I just simplified the names for this question).

If you don't want to go file-digging, here is every file I think you might find important:

StreamSAKPlugin

StreamSAKSimplePlugin

StreamSAKAdvancedPlugin

StreamSAKFileHandler (responsible for loading in the plug-ins, scroll down towards the bottom)





Aucun commentaire:

Enregistrer un commentaire