Skip to content
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

[improve] support plugins using other jar #2553

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

import jakarta.persistence.criteria.Predicate;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -96,6 +99,11 @@ public void deletePlugins(Set<Long> ids) {
if (jarFile.exists()) {
FileUtils.delete(jarFile);
}
// removing jar files that are dependencies for the plugin
File otherLibDir = new File(getOtherLibDir(plugin.getJarFilePath()));
if (otherLibDir.exists()) {
FileUtils.deleteDirectory(otherLibDir);
}
// delete metadata
metadataDao.deleteById(plugin.getId());
} catch (IOException e) {
Expand All @@ -108,6 +116,16 @@ public void deletePlugins(Set<Long> ids) {
loadJarToClassLoader();
}

/**
* get the directory where the JAR files dependent on the plugin are saved
*
* @param pluginJarPath jar file path
* @return lib dir
*/
private String getOtherLibDir(String pluginJarPath) {
return pluginJarPath.substring(0, pluginJarPath.lastIndexOf("."));
}

@Override
public void updateStatus(PluginMetadata plugin) {
Optional<PluginMetadata> pluginMetadata = metadataDao.findById(plugin.getId());
Expand Down Expand Up @@ -274,8 +292,9 @@ private void loadJarToClassLoader() {
System.gc();
List<PluginMetadata> plugins = metadataDao.findPluginMetadataByEnableStatusTrue();
for (PluginMetadata metadata : plugins) {
URL url = new File(metadata.getJarFilePath()).toURI().toURL();
pluginClassLoaders.add(new URLClassLoader(new URL[]{url}, Plugin.class.getClassLoader()));
List<URL> urls = loadLibInPlugin(metadata.getJarFilePath());
urls.add(new File(metadata.getJarFilePath()).toURI().toURL());
pluginClassLoaders.add(new URLClassLoader(urls.toArray(new URL[0]), Plugin.class.getClassLoader()));
}
} catch (MalformedURLException e) {
log.error("Failed to load plugin:{}", e.getMessage());
Expand All @@ -285,6 +304,42 @@ private void loadJarToClassLoader() {
}
}

/**
* loading other JAR files that are dependencies for the plugin
*
* @param pluginJarPath jar file path
* @return urls
*/
@SneakyThrows
private List<URL> loadLibInPlugin(String pluginJarPath) {
File libDir = new File(getOtherLibDir(pluginJarPath));
FileUtils.forceMkdir(libDir);
List<URL> libUrls = new ArrayList<>();
try (JarFile jarFile = new JarFile(pluginJarPath)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
File file = new File(libDir, entry.getName());
if (!entry.isDirectory() && entry.getName().endsWith(".jar")) {
if (!file.getParentFile().exists()) {
FileUtils.createParentDirectories(file);
}
try (InputStream in = jarFile.getInputStream(entry);
OutputStream out = new FileOutputStream(file)) {
byte[] buffer = new byte[4096];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
libUrls.add(file.toURI().toURL());
out.flush();
}
}
}
}
return libUrls;
}

@Override
public <T> void pluginExecute(Class<T> clazz, Consumer<T> execute) {
for (URLClassLoader pluginClassLoader : pluginClassLoaders) {
Expand Down
11 changes: 7 additions & 4 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ spring:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
freemarker:
enabled: false

servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB

management:
health:
Expand Down Expand Up @@ -81,17 +84,17 @@ spring:
eclipselink:
logging:
level: SEVERE

flyway:
enabled: true
clean-disabled: true
baseline-on-migrate: true
baseline-version: 1
locations:
- classpath:db/migration/{vendor}

mail:
# Mail server address, eg: qq-mailbox is smtp.qq.com, qq-exmail is smtp.exmail.qq.com
# Mail server address, eg: qq-mailbox is smtp.qq.com, qq-exmail is smtp.exmail.qq.com
host: smtp.qq.com
username: [email protected]
# Attention this is not email account password, this requires an email authorization code
Expand Down
24 changes: 24 additions & 0 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,32 @@
<dependency>
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-common</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<descriptor>src/main/resources/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
41 changes: 41 additions & 0 deletions plugin/src/main/resources/assembly/assembly.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 https://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>jar-with-lib</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<unpack>false</unpack>
<scope>runtime</scope>
<excludes>
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Loading