use of io.apiman.common.plugin.PluginSpec in project apiman by apiman.
the class AbstractPluginRegistry method loadPlugin.
/**
* @see io.apiman.manager.api.core.IPluginRegistry#loadPlugin(io.apiman.common.plugin.PluginCoordinates)
*/
@Override
public Plugin loadPlugin(PluginCoordinates coordinates) throws InvalidPluginException {
boolean isSnapshot = PluginUtils.isSnapshot(coordinates);
synchronized (mutex) {
if (pluginCache.containsKey(coordinates)) {
Plugin cachedPlugin = pluginCache.get(coordinates);
if (isSnapshot) {
pluginCache.remove(coordinates);
try {
cachedPlugin.getLoader().close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
return cachedPlugin;
}
}
String pluginRelativePath = PluginUtils.getPluginRelativePath(coordinates);
File pluginDir = new File(pluginsDir, pluginRelativePath);
if (!pluginDir.exists()) {
pluginDir.mkdirs();
}
// $NON-NLS-1$
File pluginFile = new File(pluginDir, "plugin." + coordinates.getType());
// Clean up stale files in the case of a snapshot.
if (pluginFile.exists() && isSnapshot) {
try {
FileUtils.deleteDirectory(pluginFile.getParentFile());
pluginFile.getParentFile().mkdirs();
} catch (IOException e) {
e.printStackTrace();
}
}
// Doesn't exist (or it's a snapshot)? Better download it.
if (!pluginFile.exists()) {
downloadPlugin(pluginFile, coordinates);
}
// Still doesn't exist? That's a failure.
if (!pluginFile.exists()) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.PluginNotFound"));
}
PluginClassLoader pluginClassLoader;
try {
pluginClassLoader = createPluginClassLoader(pluginFile);
} catch (IOException e) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e);
}
URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH);
if (specFile == null) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH));
}
try {
PluginSpec spec = PluginUtils.readPluginSpecFile(specFile);
Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader);
pluginCache.put(coordinates, plugin);
return plugin;
} catch (Exception e) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.FailedToReadSpecFile", PluginUtils.PLUGIN_SPEC_PATH), e);
}
}
}
use of io.apiman.common.plugin.PluginSpec in project apiman by apiman.
the class DefaultPluginRegistry method readPluginFile.
/**
* Reads the plugin into an object. This method will fail if the plugin is not valid.
* This could happen if the file is not a java archive, or if the plugin spec file is
* missing from the archive, etc.
*/
protected Plugin readPluginFile(PluginCoordinates coordinates, File pluginFile) throws Exception {
try {
PluginClassLoader pluginClassLoader = createPluginClassLoader(pluginFile);
URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH);
if (specFile == null) {
// $NON-NLS-1$
throw new Exception(Messages.i18n.format("DefaultPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH));
} else {
PluginSpec spec = PluginUtils.readPluginSpecFile(specFile);
Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader);
// $NON-NLS-1$
LOGGER.info("Read apiman plugin: {0}", spec);
return plugin;
}
} catch (Exception e) {
// $NON-NLS-1$
throw new Exception(Messages.i18n.format("DefaultPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e);
}
}
Aggregations