net.opentsdb.utils
public final class PluginLoader extends Object
Before attempting any of the plugin loader calls, users should call one or more of the jar loader methods to append files to the class path that may have not been loaded on startup. This is particularly useful for plugins that have dependencies not included by OpenTSDB.
For example, a typical process may be:
Plugin creation is pretty simple, just implement the abstract plugin class, create a Manifest file, add the "services" folder and plugin file and export a jar file.
Note: All plugins must have a parameterless constructor for the ServiceLoader to work. This means you can't have final class variables, but we'll make a promise to call an initialize() method with the proper parameters, such as configs or the TSDB object, immediately after loading a plugin and before trying to access any of its methods.
Note: All plugins must also implement a shutdown() method to clean up gracefully.
Constructor and Description |
---|
PluginLoader() |
Modifier and Type | Method and Description |
---|---|
static void |
loadJAR(String jar)
Attempts to load the given jar into the class path
|
static void |
loadJARs(String directory)
Recursively traverses a directory searching for files ending with .jar and
loads them into the class path
|
static <T> List<T> |
loadPlugins(Class<T> type)
Searches the class path for implementations of the given type, returning a
list of all plugins that were found
|
static <T> T |
loadSpecificPlugin(String name,
Class<T> type)
Searches the class path for the specific plugin of a given type
|
public static <T> T loadSpecificPlugin(String name, Class<T> type)
Note: If you want to load JARs dynamically, you need to call
loadJAR(java.lang.String)
or loadJARs(java.lang.String)
methods with the proper file
or directory first, otherwise this will only search whatever was loaded
on startup.
WARNING: If there are multiple versions of the request plugin in the class path, only one will be returned, so check the logs to see that the correct version was loaded.
name
- The specific name of a plugin to search for, e.g.
net.opentsdb.search.ElasticSearchtype
- The class type to search forServiceConfigurationError
- if the plugin cannot be instantiatedIllegalArgumentName
- if the plugin name is null or emptypublic static <T> List<T> loadPlugins(Class<T> type)
Note: If you want to load JARs dynamically, you need to call
loadJAR(java.lang.String)
or loadJARs(java.lang.String)
methods with the proper file
or directory first, otherwise this will only search whatever was loaded
on startup.
WARNING: If there are multiple versions of the request plugin in the class path, only one will be returned, so check the logs to see that the correct version was loaded.
type
- The class type to search forServiceConfigurationError
- if any of the plugins could not be
instantiatedpublic static void loadJAR(String jar) throws IOException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
jar
- Full path to a .jar fileIOException
- if the file does not exist or cannot be accessedSecurityException
- if there is a security manager present and the
operation is deniedIllegalArgumentException
- if the filename did not end with .jarNoSuchMethodException
- if there is an error with the class loaderIllegalAccessException
- if a security manager is present and the
operation was deniedInvocationTargetException
- if there is an issue loading the jarpublic static void loadJARs(String directory) throws SecurityException, IllegalArgumentException, IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
WARNING: This can be pretty slow if you have a directory with many sub-directories. Keep the directory structure shallow.
directory
- The directoryIOException
- if the directory does not exist or cannot be accessedSecurityException
- if there is a security manager present and the
operation is deniedIllegalArgumentException
- if the path was not a directoryNoSuchMethodException
- if there is an error with the class loaderIllegalAccessException
- if a security manager is present and the
operation was deniedInvocationTargetException
- if there is an issue loading the jar