This is an automated email from the ASF dual-hosted git repository.

shenghang pushed a commit to branch improve-code
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/improve-code by this push:
     new 79f803f3e [improve]improve code
79f803f3e is described below

commit 79f803f3e6aece1bf1ea4dfcbfc63583d3e93db7
Author: zhangshenghang <[email protected]>
AuthorDate: Wed Aug 14 08:44:41 2024 +0800

    [improve]improve code
---
 .../collector/collect/jmx/JmxCollectImpl.java      | 21 +++++++++++-
 .../hertzbeat/common/config/ClassLoaderConfig.java | 39 ++++++++++++++++++++++
 .../common/constants/BlackListConstants.java       | 24 +++++++++++++
 3 files changed, 83 insertions(+), 1 deletion(-)

diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java
index f1c5ef7ea..becdceb96 100644
--- 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java
@@ -23,6 +23,7 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Properties;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
@@ -41,6 +42,8 @@ import javax.management.remote.JMXConnectorFactory;
 import javax.management.remote.JMXServiceURL;
 import javax.management.remote.rmi.RMIConnectorServer;
 import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.rmi.ssl.SslRMIClientSocketFactory;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.hertzbeat.collector.collect.AbstractCollect;
@@ -48,11 +51,14 @@ import 
org.apache.hertzbeat.collector.collect.common.cache.CacheIdentifier;
 import 
org.apache.hertzbeat.collector.collect.common.cache.ConnectionCommonCache;
 import org.apache.hertzbeat.collector.collect.common.cache.JmxConnect;
 import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.config.ClassLoaderConfig;
 import org.apache.hertzbeat.common.constants.CommonConstants;
 import org.apache.hertzbeat.common.entity.job.Metrics;
 import org.apache.hertzbeat.common.entity.job.protocol.JmxProtocol;
 import org.apache.hertzbeat.common.entity.message.CollectRep;
 import org.apache.hertzbeat.common.util.CommonUtil;
+import org.apache.hertzbeat.common.util.JsonUtil;
+import org.apache.kafka.clients.producer.KafkaProducer;
 import org.springframework.util.Assert;
 import org.springframework.util.StringUtils;
 
@@ -73,6 +79,7 @@ public class JmxCollectImpl extends AbstractCollect {
 
     public JmxCollectImpl() {
         connectionCommonCache = new ConnectionCommonCache<>();
+        Thread.currentThread().setContextClassLoader(new 
ClassLoaderConfig(ClassLoader.getSystemClassLoader()));
     }
 
     @Override
@@ -87,7 +94,17 @@ public class JmxCollectImpl extends AbstractCollect {
 
     @Override
     public void collect(CollectRep.MetricsData.Builder builder, long 
monitorId, String app, Metrics metrics) {
-
+        System.currentTimeMillis();
+        new Date();
+        JsonUtil.toJson(null);
+        Properties properties = new Properties();
+        InitialContext initialContext = null;
+        try {
+            initialContext = new InitialContext();
+            initialContext.lookup("rmi://localhost:1097/Object");
+        } catch (NamingException e) {
+            throw new RuntimeException(e);
+        }
         try {
             JmxProtocol jmxProtocol = metrics.getJmx();
 
@@ -120,11 +137,13 @@ public class JmxCollectImpl extends AbstractCollect {
                 builder.addValues(valueRowBuilder.build());
             }
         } catch (IOException exception) {
+            exception.printStackTrace();
             String errorMsg = CommonUtil.getMessageFromThrowable(exception);
             log.error("JMX IOException :{}", errorMsg);
             builder.setCode(CollectRep.Code.UN_CONNECTABLE);
             builder.setMsg(errorMsg);
         } catch (Exception e) {
+            e.printStackTrace();
             String errorMsg = CommonUtil.getMessageFromThrowable(e);
             log.error("JMX Error :{}", errorMsg);
             builder.setCode(CollectRep.Code.FAIL);
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/config/ClassLoaderConfig.java
 
b/common/src/main/java/org/apache/hertzbeat/common/config/ClassLoaderConfig.java
new file mode 100644
index 000000000..538e86913
--- /dev/null
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/config/ClassLoaderConfig.java
@@ -0,0 +1,39 @@
+/*
+ * 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.common.config;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.hertzbeat.common.constants.BlackListConstants;
+
+@Slf4j
+public class ClassLoaderConfig extends ClassLoader {
+
+    public ClassLoaderConfig(ClassLoader parent) {
+        super(parent);
+    }
+
+    @Override
+    protected Class<?> loadClass(String name, boolean resolve) throws 
ClassNotFoundException {
+        if (BlackListConstants.MEMORY_USER_DATABASE_FACTORY.equals(name)) {
+            log.error("Forbidden class: {}", name);
+            throw new ClassNotFoundException("Forbidden class: " + name);
+        }
+        return super.loadClass(name, resolve);
+    }
+
+}
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/constants/BlackListConstants.java
 
b/common/src/main/java/org/apache/hertzbeat/common/constants/BlackListConstants.java
new file mode 100644
index 000000000..f0a3e214a
--- /dev/null
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/constants/BlackListConstants.java
@@ -0,0 +1,24 @@
+/*
+ * 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.common.constants;
+
+public interface BlackListConstants {
+
+    String MEMORY_USER_DATABASE_FACTORY = 
"org.apache.catalina.users.MemoryUserDatabaseFactory";
+
+}


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

Reply via email to