Skip to content

Commit

Permalink
Avoid classloader manipulation when installing a new plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed Dec 10, 2024
1 parent d3b3436 commit ed16d12
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public PluginUploader(PluginsPanel pluginsPanel) {
setHeaderControls(HeaderControls.HEADER_LABEL, HeaderControls.CLOSE_BUTTON);
setTitle(I18N.message("uploadplugin"));
setWidth(430);
setHeight(130);
setHeight(170);
setCanDragResize(true);
setIsModal(true);
setShowModalMask(true);
Expand All @@ -42,7 +42,7 @@ public PluginUploader(PluginsPanel pluginsPanel) {
submitButton.addClickHandler(event -> onSubmit());

VLayout layout = new VLayout();
layout.setMembersMargin(5);
layout.setMembersMargin(1);
layout.setMargin(2);

uploader = new Upload(submitButton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,10 @@ public static void replaceInFile(String sourcePath, String token, String newValu
* @throws IOException if the copy resulted in an error
*/
public static void copyFile(File source, File target) throws IOException {
try (FileChannel in = new FileInputStream(source).getChannel();
FileChannel out = new FileOutputStream(target).getChannel();) {
try (FileInputStream fis = new FileInputStream(source);
FileChannel in = fis.getChannel();
FileOutputStream fos = new FileOutputStream(target);
FileChannel out = fos.getChannel();) {

ByteBuffer buffer = ByteBuffer.allocateDirect(BUFF_SIZE);
while (in.read(buffer) != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
Expand Down Expand Up @@ -71,7 +67,6 @@
import com.logicaldoc.util.config.ContextProperties;
import com.logicaldoc.util.config.LogConfigurator;
import com.logicaldoc.util.config.PluginDescriptorConfigurator;
import com.logicaldoc.util.exec.Exec;
import com.logicaldoc.util.io.FileUtil;
import com.logicaldoc.util.io.ZipUtil;
import com.logicaldoc.util.plugin.LogicalDOCPlugin;
Expand Down Expand Up @@ -603,7 +598,7 @@ public List<GUIHistory> search(Long userId, Date from, Date till, int maxResult,
query.append(" union ");
query.append(
"select A.ld_username, A.ld_event, A.ld_date, A.ld_filename, A.ld_folderid, A.ld_path, A.ld_sessionid, A.ld_docid, A.ld_userid, A.ld_ip as ip, A.ld_userlogin, A.ld_comment, A.ld_reason, A.ld_device, A.ld_geolocation, A.ld_keylabel from TABLE A where A.ld_tenantid = "
.replace("TABLE", table).replace("A", tableAlias) + session.getTenantId());
.replace("TABLE", table).replace("A", tableAlias) + session.getTenantId());
appendUserCondition(tableAlias, userId, query);
appendSessionCondition(tableAlias, historySid, query);
appendDatesCondition(tableAlias, from, till, query);
Expand Down Expand Up @@ -820,18 +815,6 @@ public void uninstallPlugin(String pluginId) throws ServerException {
// Nothing to do
}

try {
if (pluginJarFile.exists()) {
if (new Exec().isWindows())
Runtime.getRuntime().exec(
new String[] { "cmd.exe", "/c", "del /F /Q \"" + pluginJarFile.getAbsolutePath() + "\"" });
else
Runtime.getRuntime().exec(new String[] { "rm", "-rf", pluginJarFile.getAbsolutePath() });
}
} catch (IOException e) {
throwServerException(session, log, e);
}

if (pluginJarFile.exists())
throw new ServerException("Cannot remove plugin file " + pluginJarFile.getAbsolutePath()
+ ". Please stop the application and delete that file manually.");
Expand Down Expand Up @@ -940,21 +923,6 @@ public void installPlugin() throws ServerException {
log.info("Deleted existing plugin home {}", pluginHome.getAbsolutePath());
}

File libFolder = new File(new File(rootFolder, "WEB-INF"), "lib");
File pluginJarFile = new File(libFolder, pluginJar);

/*
* Append the plugin jar in the classpath
*/
appendPluginJarInClasspath(pluginJarFile);

/*
* Initialize the plugin
*/
PluginRegistry pluginRegistry = PluginRegistry.getInstance();
pluginRegistry.init(libFolder.getAbsolutePath());
initializePlugin(pluginId);

/*
* Copy the plugin archive as .installed so it will be maintained
* over the updates
Expand All @@ -963,30 +931,14 @@ public void installPlugin() throws ServerException {
File targetFile = new File(pluginsDir, pluginId + "-" + pluginVersion + "-plugin.zip.installed");
log.info("Copying plugin package {} into {}", pluginPackage.getName(), targetFile.getAbsolutePath());
FileUtil.copyFile(pluginPackage, targetFile);

if (pluginRegistry.isRestartRequired())
ApplicationListener.restartRequired();
} catch (ServerException | IOException | NoSuchMethodException | IllegalArgumentException
| InvocationTargetException | PluginException e) {
ApplicationListener.restartRequired();
} catch (ServerException | IOException | IllegalArgumentException e) {
throwServerException(session, log, e);
} finally {
UploadServlet.cleanUploads(session.getSid());
}
}

private void appendPluginJarInClasspath(File pluginJarFile)
throws NoSuchMethodException, InvocationTargetException, MalformedURLException {
final ClassLoader sysloader = this.getClass().getClassLoader();
final Class<URLClassLoader> sysclass = URLClassLoader.class;
final Method method = sysclass.getDeclaredMethod("addURL", URL.class);

try {
method.invoke(sysloader, pluginJarFile.toURI().toURL());
} catch (IllegalAccessException iae) {
log.warn(iae.getMessage(), iae);
}
}

@Override
public List<GUIValue> getPlugins() throws ServerException {
validateSession();
Expand Down

0 comments on commit ed16d12

Please sign in to comment.