-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error updating plugin because file already in use #39
Comments
Thanks for reporting the issue! |
org.pf4j:pf4j:2.6.0 Path pluginCacheDir = Paths.get( "D:\\plugincache");
PluginManager pluginManager = new DefaultPluginManager( pluginCacheDir);
pluginManager.loadPlugins();
pluginManager.startPlugins();
UpdateManager updateManager = new UpdateManager( pluginManager,
Paths.get( "D:/plugin_repositories/repositories.json"));
// >> keep system up-to-date <<
boolean systemUpToDate = true;
// check for updates
if (updateManager.hasUpdates())
{
List<PluginInfo> updates = updateManager.getUpdates();
LOGGER.debug( "Found {} updates", updates.size());
for (PluginInfo plugin : updates)
{
LOGGER.debug( "Found update for plugin '{}'", plugin.id);
PluginInfo.PluginRelease lastRelease = plugin
.getLastRelease( pluginManager.getSystemVersion(), pluginManager.getVersionManager());
String lastVersion = lastRelease.version;
String installedVersion = pluginManager.getPlugin( plugin.id).getDescriptor().getVersion();
// pluginManager.stopPlugin( plugin.id);
pluginManager.unloadPlugin( plugin.id);
LOGGER.debug( "Update plugin '{}' from version {} to version {}", plugin.id, installedVersion,
lastVersion);
boolean updated = updateManager.updatePlugin( plugin.id, lastVersion);
if (updated)
{
LOGGER.debug( "Updated plugin '{}'", plugin.id);
}
else
{
LOGGER.error( "Cannot update plugin '{}'", plugin.id);
systemUpToDate = false;
}
}
}
else
{
LOGGER.debug( "No updates found");
} |
Also, please note the |
@hazemkmammu |
My earlier proposal (deleted comment) to fix the issue was incorrect. The actual issue is org.pf4j.DefaultPluginRepository.deletePluginPath(Path) throwing "java.nio.file.FileSystemException: XXX.jar: The process cannot access the file because it is being used by another process.". I am not sure why. I will update if I can figure out. Closing this issue. Sorry for the confusion. |
I believe I have isolated the cause of the issue. It seems to be somehow related to org.pf4j.LegacyExtensionFinder.readPluginsStorages(). Please see the following code snippets. Case# 1 try {
URL jarUrl = new File("D:\\plugincache\\SpanishGreetingPlugin.jar").getCanonicalFile().toURI().toURL();
URL[] urls = new URL[] { jarUrl };
URLClassLoader ucl = new URLClassLoader(urls);
Enumeration<URL> extensionResources = ucl.findResources("META-INF/extensions.idx");
while (extensionResources.hasMoreElements()) {
URL extensionResource = extensionResources.nextElement();
try (Reader reader = new InputStreamReader(extensionResource.openStream(), StandardCharsets.UTF_8)) {
}
}
Class<?> pluginClass = ucl.loadClass("com.fisc.pf4jdemo.SpanishGreetingPlugin$SpanishGreeting");
GreetingExtensionPoint plugin = (GreetingExtensionPoint) pluginClass.newInstance();
System.out.println("Greeting:" + plugin.greeting());
ucl.close();
FileUtils.delete(Paths.get("D:\\plugincache\\SpanishGreetingPlugin.jar"));
} catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} Case# 2 try {
URL jarUrl = new File("D:\\plugincache\\SpanishGreetingPlugin.jar").getCanonicalFile().toURI().toURL();
URL[] urls = new URL[] { jarUrl };
URLClassLoader ucl = new URLClassLoader(urls);
Enumeration<URL> extensionResources = ucl.findResources("META-INF/extensions.idx");
while (extensionResources.hasMoreElements()) {
URL extensionResource = extensionResources.nextElement();
// try (Reader reader = new InputStreamReader(extensionResource.openStream(), StandardCharsets.UTF_8)) {
// }
}
Class<?> pluginClass = ucl.loadClass("com.fisc.pf4jdemo.SpanishGreetingPlugin$SpanishGreeting");
GreetingExtensionPoint plugin = (GreetingExtensionPoint) pluginClass.newInstance();
System.out.println("Greeting:" + plugin.greeting());
ucl.close();
FileUtils.delete(Paths.get("D:\\plugincache\\SpanishGreetingPlugin.jar"));
} catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} |
@hazemkmammu
You help a lot with your analyse. I really like that you try to find where is the problem in code and how can it be solved. |
@decebals Thank you. You created PF4J in a simple and clean way such that it encourages contribution. I am glad I have been of help. Yes, #217 appears to be related. I observed that using Case# 3 try {
URL jarUrl = new File("D:\\plugincache\\SpanishGreetingPlugin.jar").getCanonicalFile().toURI().toURL();
URL[] urls = new URL[] { jarUrl };
URLClassLoader ucl = new URLClassLoader(urls);
Set<String> entries = new HashSet<>();
try (Reader reader = new InputStreamReader(ucl.getResourceAsStream("META-INF/extensions.idx"))) {
LegacyExtensionStorage.read(reader, entries);
}
Class<?> pluginClass = ucl.loadClass("com.fisc.pf4jdemo.SpanishGreetingPlugin$SpanishGreeting");
GreetingExtensionPoint plugin = (GreetingExtensionPoint) pluginClass.newInstance();
System.out.println("Greeting:" + plugin.greeting());
ucl.close();
FileUtils.delete(Paths.get("D:\\plugincache\\SpanishGreetingPlugin.jar"));
} catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} The I will create a PR so that you can understand this better. |
I tried running the example in the 'How to use' section of README.md. The update manager failed to update the plugin to the latest version because it could not delete the old version. The old version was already loaded and started before the update call.
So I unloaded the plugin being updated before calling update. This threw an exception "Plugin {} cannot be updated since it is not installed" because the plugin was unloaded. The method
org.pf4j.update.UpdateManager.updatePlugin(String, String)
is checkingpluginManager.getPlugin(id) == null
, shouldn't it instead check if the Jar/Zip exists in the pluginRoot directory. It is installed, it's just not loaded.Right now it seems impossible to update a plugin using the default update manager.
The text was updated successfully, but these errors were encountered: