Skip to topic | Skip to bottom
Minfo.SeTD5y1314r1.4 - 26 Nov 2013 - 15:54 - PhilippeCollettopic end

Start of topic | Skip to actions

Lab 5: dynamic loading and plugins


Questions on dynamic loading can be realized out of Eclipse (compiling on a console term) or by desactiving the automatic building from the project menu and manipulating .class from outside Eclipse.

Experimenting the class loading time

  • Download this archive.
  • Study classes,,, They have static blocks that print something as soon as the class is loaded. Compile all classe and execute Example1.
  • Observe all class loading and what triggers the loading.
  • Note (B is never loaded) that declaring a variable/attribute is not sufficient to ensure the type loading (class or interface). Classes are loaded lazily by the used JVM.
  • Note: for any class (without static blocks), one can use the -verbose option from the Java command to see what's loaded.

Using dynamic loading to make plugins

  1. Study Example2 that uses forName and newInstance to load classes and create instances. Execute Example2.
  2. Example3 introduces the notion of plugin. It uses interface Plugin and Plugin1 as a plugin (a class implementing the interface).
  3. Refactor Plugin1 and Plugin so that they are now in the fr.unice.plugin package. re-execute Example3. Take care of the path so that the .class files are put in the appropriate directory structure.

Hot reloading of a new version of a plugin

  1. In Example4, the code is similar to Example3 but there is loop that creates an instance of Plugin1 and slows execution with a Thread.sleep. Before launching the example, prepare another .class file from Plugin1 with a different output of the describeMe method (same class name). You should have two Plugin1.class and you'll replace one by another at runtime. Is the change visible? Why?
  2. To really load the new version of Plugin1 you need to create a new URLClassLoader each time you want to "reload" new stuff. ALERT! IMPORTANT : to make it work, you need to put Plugin1.class is a specific directory "plugins". This directory must not be in the classpath, as explained in the course.
The code for creating the load looks like this:
URLClassLoader cl = new URLClassLoader(new URL[] { new URL("file:plugins/") });
c = cl.loadClass(...);
  1. Redo the experiment and observe the (good) result.

Application to the simulator

this archive contains the source of the simulator with plugins. There are some TODOs to fill in some classes:

  • Loading and checking of plugin classes in the class PluginLoader (method loadOnePluginClass) ;
  • the code to get the correct constructor of Creature and to create and instance in CreaturePluginFactory ;
  • Creation of creatures and simulator launching through the lisnter on the button "(Re-)start simulation".

At runtime, the problem is similar to the previous questions. Plugins must not be in the application classpath.

Here is a class diagram and some explanations on classes:


  • PluginLoader :
    • Class that loads plugin of a given type by looking recursively inside a given directory.
    • Loaded plugins can be retried through a function that returns a list of Class.

  • IPlugin :
    • Plugins are classes that must implement this interface.

  • CreatePluginFactory :
    • A specific factory that uses the PluginLoader? with Creature.class

  • Launcher :
    • Initialises the CreatePluginFactory, creates des boutons to pilot loading/reloading and starting/restarting.
    • builds a menu with one entry per loadded class.

  • PluginMenuItemBuilder :
    • small util to build the plugin menu.

  • check also the log on the console when executing the system, they could help you understanding the functioning as a whole.

TIP Correction:

-- PhilippeCollet - 26 Nov 2013
to top

I Attachment sort Action Size Date Who Comment
tp5-diag.png manage 153.8 K 07 Nov 2013 - 22:45 PhilippeCollet manage 5.0 K 07 Nov 2013 - 22:48 PhilippeCollet manage 37.5 K 07 Nov 2013 - 22:51 PhilippeCollet manage 47.1 K 26 Nov 2013 - 15:51 PhilippeCollet  

You are here: Minfo > SeTD5y1314

to top

Copyright © 1999-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding WIKIDeptinfo? Send feedback