org.pf4j.PluginRuntimeException Java Examples

The following examples show how to use org.pf4j.PluginRuntimeException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpdateManager.java    From pf4j-update with Apache License 2.0 6 votes vote down vote up
/**
 * Installs a plugin by id and version.
 *
 * @param id the id of plugin to install
 * @param version the version of plugin to install, on SemVer format, or null for latest
 * @return true if installation successful and plugin started
 * @exception PluginRuntimeException if plugin does not exist in repos or problems during
 */
public synchronized boolean installPlugin(String id, String version) {
    // Download to temporary location
    Path downloaded = downloadPlugin(id, version);

    Path pluginsRoot = pluginManager.getPluginsRoot();
    Path file = pluginsRoot.resolve(downloaded.getFileName());
    try {
        Files.move(downloaded, file, REPLACE_EXISTING);
    } catch (IOException e) {
        throw new PluginRuntimeException(e, "Failed to write file '{}' to plugins folder", file);
    }

    String pluginId = pluginManager.loadPlugin(file);
    PluginState state = pluginManager.startPlugin(pluginId);

    return PluginState.STARTED.equals(state);
}
 
Example #2
Source File: UpdateManager.java    From pf4j-update with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves Release from id and version.
 *
 * @param id of plugin
 * @param version of plugin or null to locate latest version
 * @return PluginRelease for downloading
 * @throws PluginRuntimeException if id or version does not exist
 */
protected PluginRelease findReleaseForPlugin(String id, String version) {
    PluginInfo pluginInfo = getPluginsMap().get(id);
    if (pluginInfo == null) {
        log.info("Plugin with id {} does not exist in any repository", id);
        throw new PluginRuntimeException("Plugin with id {} not found in any repository", id);
    }

    if (version == null) {
        return getLastPluginRelease(id);
    }

    for (PluginRelease release : pluginInfo.releases) {
        if (versionManager.compareVersions(version, release.version) == 0 && release.url != null) {
            return release;
        }
    }

    throw new PluginRuntimeException("Plugin {} with version @{} does not exist in the repository", id, version);
}
 
Example #3
Source File: SimpleFileDownloader.java    From pf4j-update with Apache License 2.0 5 votes vote down vote up
/**
 * Downloads a file. If HTTP(S) or FTP, stream content, if local file:/ do a simple filesystem copy to tmp folder.
 * Other protocols not supported.
 *
 * @param fileUrl the URI representing the file to download
 * @return the path of downloaded/copied file
 * @throws IOException in case of network or IO problems
 * @throws PluginRuntimeException in case of other problems
 */
public Path downloadFile(URL fileUrl) throws IOException {
    switch (fileUrl.getProtocol()) {
        case "http":
        case "https":
        case "ftp":
            return downloadFileHttp(fileUrl);
        case "file":
            return copyLocalFile(fileUrl);
        default:
            throw new PluginRuntimeException("URL protocol {} not supported", fileUrl.getProtocol());
    }
}
 
Example #4
Source File: UpdateManager.java    From pf4j-update with Apache License 2.0 5 votes vote down vote up
/**
 * Downloads a plugin with given coordinates, runs all {@link FileVerifier}s
 * and returns a path to the downloaded file.
 *
 * @param id of plugin
 * @param version of plugin or null to download latest
 * @return Path to file which will reside in a temporary folder in the system default temp area
 * @throws PluginRuntimeException if download failed
 */
protected Path downloadPlugin(String id, String version) {
    try {
        PluginRelease release = findReleaseForPlugin(id, version);
        Path downloaded = getFileDownloader(id).downloadFile(new URL(release.url));
        getFileVerifier(id).verify(new FileVerifier.Context(id, release), downloaded);
        return downloaded;
    } catch (IOException e) {
        throw new PluginRuntimeException(e, "Error during download of plugin {}", id);
    }
}
 
Example #5
Source File: UpdateManager.java    From pf4j-update with Apache License 2.0 5 votes vote down vote up
/**
 * Updates a plugin id to given version or to latest version if {@code version == null}.
 *
 * @param id the id of plugin to update
 * @param version the version to update to, on SemVer format, or null for latest
 * @return true if update successful
 * @exception PluginRuntimeException in case the given version is not available, plugin id not already installed etc
*/
public boolean updatePlugin(String id, String version) {
    if (pluginManager.getPlugin(id) == null) {
        throw new PluginRuntimeException("Plugin {} cannot be updated since it is not installed", id);
    }

    PluginInfo pluginInfo = getPluginsMap().get(id);
    if (pluginInfo == null) {
        throw new PluginRuntimeException("Plugin {} does not exist in any repository", id);
    }

    if (!hasPluginUpdate(id)) {
        log.warn("Plugin {} does not have an update available which is compatible with system version {}", id, systemVersion);
        return false;
    }

    // Download to temp folder
    Path downloaded = downloadPlugin(id, version);

    if (!pluginManager.deletePlugin(id)) {
        return false;
    }

    Path pluginsRoot = pluginManager.getPluginsRoot();
    Path file = pluginsRoot.resolve(downloaded.getFileName());
    try {
        Files.move(downloaded, file, REPLACE_EXISTING);
    } catch (IOException e) {
        throw new PluginRuntimeException("Failed to write plugin file {} to plugin folder", file);
    }

    String newPluginId = pluginManager.loadPlugin(file);
    PluginState state = pluginManager.startPlugin(newPluginId);

    return PluginState.STARTED.equals(state);
}
 
Example #6
Source File: EvilPluginFailingToStart.java    From exonum-java-binding with Apache License 2.0 4 votes vote down vote up
@Override
public void start() {
  throw new PluginRuntimeException("I am an evil plugin so I won't start");
}
 
Example #7
Source File: InstallAndDownloadTest.java    From pf4j-update with Apache License 2.0 4 votes vote down vote up
@Test(expected = PluginRuntimeException.class)
public void updateVersionNotExist() {
    assertTrue(updateManager.installPlugin("myPlugin", "1.2.3"));
    updateManager.updatePlugin("myPlugin", "9.9.9");
}
 
Example #8
Source File: FileDownloadTest.java    From pf4j-update with Apache License 2.0 4 votes vote down vote up
@Test(expected = PluginRuntimeException.class)
public void unsupportedProtocol() throws Exception {
    downloader.downloadFile(new URL("jar:file:!/myfile.jar"));
}