zqr10159 commented on code in PR #1973:
URL: https://github.com/apache/hertzbeat/pull/1973#discussion_r1597845963


##########
manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarm.java:
##########
@@ -113,6 +118,13 @@ public void run() {
                         alertStoreHandler.store(alert);
                         // Notice distribution
                         sendNotify(alert);
+                        // Execute the plugin
+                        List<Object> beans = pluginConfig.getBean();
+                        for (Object bean : beans) {
+                            if (bean instanceof Plugin) {

Review Comment:
   Please help me modify it.



##########
manager/src/main/java/org/apache/hertzbeat/manager/config/PluginConfig.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.
+ */
+
+package org.apache.hertzbeat.manager.config;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.jar.JarFile;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
+import org.jetbrains.annotations.NotNull;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.beans.factory.support.BeanNameGenerator;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
+import org.springframework.core.type.AnnotationMetadata;
+import org.springframework.stereotype.Component;
+
+
+/**
+ * Plugin configuration
+ */
+@Slf4j
+@Component
+public class PluginConfig implements ImportBeanDefinitionRegistrar, 
ApplicationContextAware {
+    @Autowired
+    private ApplicationContext applicationContext;
+    @Autowired
+    private DefaultListableBeanFactory defaultListableBeanFactory;
+
+    private static final String CLASS_SUFFIX = ".class";
+
+    private static final String BASE_PLUGIN_PATH = 
"org/apache/hertzbeat/plugin/Plugin";
+
+    private static final String BASE_PATH = System.getProperty("user.dir") + 
File.separator + "ext-lib";
+
+    private static final String PACKAGE = "org/apache/hertzbeat/plugin";
+
+    private URLClassLoader getClassLoader() {
+        List<String> jars = getAllJars();
+        if (jars == null) {
+            return null;
+        }
+        List<URL> urlList = new ArrayList<>();
+        for (String jar : jars) {
+            try {
+                URL url = new File(jar).toURI().toURL();
+                urlList.add(url);
+            } catch (MalformedURLException e) {
+                log.error("Error converting jar to URL: {}", jar, e);
+            }
+        }
+        if (urlList.isEmpty()) {
+            return null;
+        }
+        URL[] urls = urlList.toArray(new URL[0]);
+        ClassLoader contextClassLoader = 
Thread.currentThread().getContextClassLoader();
+        return new URLClassLoader(urls, contextClassLoader);
+    }
+
+
+    private List<String> getAllJars() {
+        log.debug("BASE_PATH:{}", PluginConfig.BASE_PATH);
+        File file = new File(PluginConfig.BASE_PATH);
+        File[] files = file.listFiles();
+        if (files == null) {
+            return null;
+        }
+        return Arrays.stream(files)
+                .filter(f -> f.getName().endsWith(".jar"))
+                .map(File::getAbsolutePath)
+                .collect(Collectors.toList());
+    }
+
+    private List<String> getAllClassNamesFromJar() throws IOException {

Review Comment:
   Please help me modify it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to