Search in sources :

Example 1 with PluginDescriptor

use of alien4cloud.plugin.model.PluginDescriptor in project alien4cloud by alien4cloud.

the class PluginManager method uploadPlugin.

/**
 * Upload a plugin from a given path.
 *
 * @param uploadedPluginPath The path of the plugin to upload.<br>
 *            The state of the new uploaded plugin will be determined as follow:
 *            <ul>
 *            <li>plugin doesn't exists: load and enable</li>
 *            <li>plugin exists: keep the state (reload if enabled)</li>
 *            </ul>
 * @return the uploaded plugin
 * @throws IOException In case there is an issue with the access to the plugin file.
 * @throws PluginLoadingException
 * @throws AlreadyExistException if a plugin with the same id already exists in the repository
 * @throws MissingPlugingDescriptorFileException
 */
public Plugin uploadPlugin(Path uploadedPluginPath) throws PluginLoadingException, IOException, MissingPlugingDescriptorFileException {
    // load the plugin descriptor
    FileSystem fs = FileSystems.newFileSystem(uploadedPluginPath, null);
    PluginDescriptor descriptor;
    try {
        try {
            descriptor = YamlParserUtil.parseFromUTF8File(fs.getPath(PLUGIN_DESCRIPTOR_FILE), PluginDescriptor.class);
        } catch (IOException e) {
            if (e instanceof NoSuchFileException) {
                throw new MissingPlugingDescriptorFileException();
            } else {
                throw e;
            }
        }
        String pluginPathId = getPluginPathId();
        Plugin plugin = new Plugin(descriptor, pluginPathId);
        // check plugin already exists and is loaded
        if (pluginContexts.get(plugin.getId()) != null) {
            log.warn("Uploading Plugin [ {} ] impossible (already exists and enabled)", plugin.getId());
            throw new AlreadyExistException("A plugin with the given id already exists and is enabled.");
        }
        Plugin oldPlugin = alienDAO.findById(Plugin.class, plugin.getId());
        if (oldPlugin != null) {
            // remove all files for the old plugin but keep configuration.
            removePlugin(plugin.getId(), false);
        }
        Path pluginPath = getPluginPath(pluginPathId);
        FileUtil.unzip(uploadedPluginPath, pluginPath);
        // copy ui directory in case it exists
        Path pluginUiSourcePath = pluginPath.resolve(UI_DIRECTORY);
        Path pluginUiPath = getPluginUiPath(pluginPathId);
        if (Files.exists(pluginUiSourcePath)) {
            FileUtil.copy(pluginUiSourcePath, pluginUiPath);
        }
        alienDAO.save(plugin);
        if (oldPlugin == null || oldPlugin.isEnabled()) {
            enablePlugin(plugin);
        }
        return plugin;
    } finally {
        fs.close();
    }
}
Also used : Path(java.nio.file.Path) PluginDescriptor(alien4cloud.plugin.model.PluginDescriptor) FileSystem(java.nio.file.FileSystem) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) MissingPlugingDescriptorFileException(alien4cloud.plugin.exception.MissingPlugingDescriptorFileException) ManagedPlugin(alien4cloud.plugin.model.ManagedPlugin)

Aggregations

AlreadyExistException (alien4cloud.exception.AlreadyExistException)1 MissingPlugingDescriptorFileException (alien4cloud.plugin.exception.MissingPlugingDescriptorFileException)1 ManagedPlugin (alien4cloud.plugin.model.ManagedPlugin)1 PluginDescriptor (alien4cloud.plugin.model.PluginDescriptor)1 IOException (java.io.IOException)1 FileSystem (java.nio.file.FileSystem)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1