Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30683 > unrolled thread

Optimal way to call methods of dynamcic loaded calls (from Jars) and vice versa.

Started bydiamantis.kiriakakis@gmail.com
First post2015-01-12 03:30 -0800
Last post2015-01-19 16:18 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Optimal way to call methods of dynamcic loaded calls (from Jars) and vice versa. diamantis.kiriakakis@gmail.com - 2015-01-12 03:30 -0800
    Re: Optimal way to call methods of dynamcic loaded calls (from Jars) and vice versa. Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-12 10:27 -0500
    Re: Optimal way to call methods of dynamcic loaded calls (from Jars) and vice versa. Roedy Green <see_website@mindprod.com.invalid> - 2015-01-13 14:10 -0800
    Re: Optimal way to call methods of dynamcic loaded calls (from Jars) and vice versa. Arne Vajhøj <arne@vajhoej.dk> - 2015-01-19 16:18 -0500

#30683 — Optimal way to call methods of dynamcic loaded calls (from Jars) and vice versa.

Fromdiamantis.kiriakakis@gmail.com
Date2015-01-12 03:30 -0800
SubjectOptimal way to call methods of dynamcic loaded calls (from Jars) and vice versa.
Message-ID<13fa0735-411b-4a42-8e3a-b5b016269abd@googlegroups.com>
Dear Javaworld,

I need to create a java application that will use a plugin system to enable extra features. The plugins will be actually .JAR files. The process is well understandable, but in case i need to call a method of main application from the dynamically loaded .JAR, what are the best practices to do it? I managed to do it using Callbacks but im not sure if this is the best way to achieve it.

Thank you,

Diamantis

[toc] | [next] | [standalone]


#30689

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-01-12 10:27 -0500
Message-ID<m90p4o$kp0$1@dont-email.me>
In reply to#30683
On 1/12/2015 6:30 AM, diamantis.kiriakakis@gmail.com wrote:
> Dear Javaworld,
>
> I need to create a java application that will use a plugin system to enable extra features. The plugins will be actually .JAR files. The process is well understandable, but in case i need to call a method of main application from the dynamically loaded .JAR, what are the best practices to do it? I managed to do it using Callbacks but im not sure if this is the best way to achieve it.

     If the main application methods called by the plugin are static
methods, call them the same way you'd call Math.sqrt():

	class Plugin {
	    ...
	    Thing thing = MainApplication.thingInstance(42);
	    ...
	}

     If the plugin calls instance methods of a main application object,
you need to decide how to communicate the main application instance to
the plugin.  One way (if the main application exists before the plugin
is constructed) is to pass the instance to the plugin constructor:

	class MainApplication {
	    ...
	    Plugin plugin = new Plugin(this);
	    ...
	    plugin.doYourThing();
	    ...
	    Thing getThing(int count) { ... }
	}

	class Plugin {
	    private final MainApplication mainApp;
	    public Plugin(MainApplication mainApp) {
		this.mainApp = mainApp;
	        ...
	    }
	    ...
	    Thing thing = mainApp.getThing(42);
	    ...
	}

     Another possibility would be to construct the plugin independently
of the main application, but to pass the instance on invocation:

	class MainApplication {
	    ...
	    Plugin plugin = new Plugin();
	    ...
	    plugin.doYourThing(this);
	    ...
	    Thing getThing(int count) { ... }
	}

	class Plugin {
	    public Plugin() { ... }
	    ...
	    public void doYourThing(MainApplication mainApp) {
	       Thing thing = mainApp.getThing(42);
	       ...
	    }
	}

     Still another possibility: Construct the plugin independently, call
an initialization method to pass it the main application instance, and
then invoke its working methods:

	class MainApplication {
	    ...
	    Plugin plugin = new Plugin();
	    ...
	    plugin.initialize(this);
	    ...
	    plugin.doYourThing();
	    ...
	    Thing getThing(int count) { ... }
	}

	class Plugin {
	    private MainApplication mainApp;
	    public Plugin() { ... }
	    ...
	    public void initialize(MainApplication mainApp) {
	        if (this.mainApp != null)
	            throw new IllegalStateException("already init'ed");
	        this.mainApp = mainApp;
	    }
	    ...
	    public void doYourThing() {
	        if (mainApp == null)
	            throw new IllegalStateException("not init'ed");
	        Thing thing = mainApp.getThing(42);
	        ...
	    }
	}

     Or you could use interfaces rather than class names:

	interface MainInterface {
	    Thing getThing(int);
	}

	class MainApplication implements MainInterface {
	    ...
	    Plugin plugin = new Plugin(this);
	    ...
	    plugin.doYourThing();
	    ...
	    @Override
	    Thing getThing(int count) { ... }
	}

	class Plugin {
	    private final MainInterface mainApp;
	    public Plugin(MainInterface mainApp) {
		this.mainApp = mainApp;
	        ...
	    }
	    ...
	    Thing thing = mainApp.getThing(42);
	    ...
	}

     ... and on and on and on it goes.  Choose an approach (one of these
or something else entirely) that best fits the problem at hand.  There
is no such thing as one way that is "optimal" for every situation.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#30696

FromRoedy Green <see_website@mindprod.com.invalid>
Date2015-01-13 14:10 -0800
Message-ID<dd5bbap9a2dbdo1u028bkbjugjlbbtq1m3@4ax.com>
In reply to#30683
On Mon, 12 Jan 2015 03:30:46 -0800 (PST),
diamantis.kiriakakis@gmail.com wrote, quoted or indirectly quoted
someone who said :

>
>I need to create a java application that will use a plugin system to enable=
> extra features. The plugins will be actually .JAR files. The process is we=
>ll understandable, but in case i need to call a method of main application =
>from the dynamically loaded .JAR, what are the best practices to do it? I m=
>anaged to do it using Callbacks but im not sure if this is the best way to =
>achieve it.

Your plugins must implement an interface or an abstract class.

Let's say that abstract class were called "Macro".

final Class<? extends Macro> macroClass = Class.forName(
binaryClassName ).asSubclass( Macro.class );

return macroClass.newInstance();

You might want to cache your class objects if you are doing frequent
repeat lookups.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
To solve an interesting problem, start by finding a problem
that is interesting to you.
~ Eric S. Raymond (born: 1957-12-04 age: 57)

[toc] | [prev] | [next] | [standalone]


#30751

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-01-19 16:18 -0500
Message-ID<54bd7492$0$285$14726298@news.sunsite.dk>
In reply to#30683
On 1/12/2015 6:30 AM, diamantis.kiriakakis@gmail.com wrote:
> I need to create a java application that will use a plugin system to
> enable extra features. The plugins will be actually .JAR files. The
> process is well understandable, but in case i need to call a method
> of main application from the dynamically loaded .JAR, what are the
> best practices to do it? I managed to do it using Callbacks but im
> not sure if this is the best way to achieve it.

I would be tempted to do it this style:

public interface Plugin {
	public static interface FooCallBack {
		public void setStatus(boolean status);
	}
	public void foo(int val, FooCallBack fcb);
	public static interface BarCallBack {
		public boolean isOK(String id);
	}
	public void bar(String id, BarCallBack bcb);
}

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web