com.baidu.jprotobuf 使用远程类编码和解码

背景

因业务需要,我们某个业务需要从远程拿到一批二进制数据,然后将这批数据转为java对象进行处理
数据是通过jprotobuf 将java对象编码后得到的
本地没有这个编码、解码的规则,需要从远程拿取class文件,然后在根据动态加载这个class文件,再根据这个class文件将数据流转为可用的java对象

碰到的问题

在 执行 ProtobufProxy.create(schoolClass); 的时候报错

代码:

   public void protobufProxyTest2() throws Exception {
        File file1 =  new File("D:\\code\\untitled\\class");
        URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        add.setAccessible(true);
        add.invoke(classloader, new Object[] { file1.toURI().toURL() });

        Class<?> schoolClass = classloader.loadClass("School");
        Codec schoolCodec = ProtobufProxy.create(schoolClass);
    }

报错:

java.lang.IllegalStateException: Compilation failed. class: School$$JProtoBufClass, diagnostics: [School$$JProtoBufClass.java:18: 警告: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment
public class School$$JProtoBufClass implements com.baidu.bjf.remoting.protobuf.Codec<School>{
       ^
  	//此处删除了很多栈xinxi
  	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
  	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
, School$$JProtoBufClass.java:18: 错误: 找不到符号
public class School$$JProtoBufClass implements com.baidu.bjf.remoting.protobuf.Codec<School>{
                                                                                     ^
  符号:School, School$$JProtoBufClass.java:21: 错误: 找不到符号
    public byte[] encode(School t) throws IOException {
                         ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:29: 错误: 找不到符号
    public School decode(byte[] bb) throws IOException {
           ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:34: 错误: 找不到符号
    public int size(School t) throws IOException {
                    ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:61: 错误: 找不到符号
    public void doWriteTo(School t, CodedOutputStream output)
                          ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:90: 错误: 找不到符号
    public void writeTo(School t, CodedOutputStream output)
                        ^
  符号:School
  位置:School$$JProtoBufClass, School$$JProtoBufClass.java:95: 错误: 找不到符号
public School readFrom(CodedInputStream input) throws IOException {
           ^
  符号:School
  位置:School$$JProtoBufClass]

	at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.doCompile(JdkCompiler.java:202)
	at com.baidu.bjf.remoting.protobuf.utils.compiler.AbstractCompiler.compile(AbstractCompiler.java:43)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.doCreate(ProtobufProxy.java:395)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:297)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:262)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:234)
	at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:193)
	at Test2.protobufProxyTest2(Test2.java:71)


追了一波源码,发现调了JavaCompiler进行编译class文件(可以理解为执行javac命令)

位置: JdkCompiler-146行

在这里插入图片描述

解决方案 (不想看debug过程直接点这里)

研究了一番,猜测是因为项目启动时没指定此类所在目录,于是在项目启动时执行下面的方法

addClassPath为下载的class文件所在目录

  public static void putClasspath(String addClassPath){
        String property = System.getProperty("java.class.path");
        System.setProperty("java.class.path", property +";"+addClassPath+";");
        System.out.println(System.getProperty("java.class.path"));
    }

完美解决!!

测试代码:

@Test
    public void protobufProxyTest() throws Exception {
        // 下载类的目录
        putClasspath("D:\\code\\untitled\\class");
        File file1 =  new File("D:\\code\\untitled\\class");
        //初始化URLClassLoader 用来动态加载下载下来的类
        URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        add.setAccessible(true);
        add.invoke(classloader, new Object[] { file1.toURI().toURL() });


        School2 school = new School2();
        school.setSchoolLocation("schoolLocation");
        school.setExtend(11111111L);
        //为了方便测试,直接创建和下载的类结构一致的类School2
        Codec<School2> tCodec = ProtobufProxy.create(School2.class);
        byte[] encode = tCodec.encode(school);
        School2 encode1 = tCodec.decode(encode);
        System.out.println(encode1);

		// 动态加载远程下载的类
        Class<?> schoolClass = classloader.loadClass("School");
        Codec schoolCodec = ProtobufProxy.create(schoolClass);
        //解码为java对象
        Object decode = schoolCodec.decode(encode);
        System.out.println(decode);
    }


    public static void putClasspath(String addClassPath){
        String property = System.getProperty("java.class.path");
        System.setProperty("java.class.path", property +";"+addClassPath+";");
        System.out.println(System.getProperty("java.class.path"));
    }

输出日志
在这里插入图片描述
测试demo地址
https://gitee.com/ayezs/protobuf-proxy-test

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用nmap工具进行扫描www.baidu.com的端口信息可以帮助我们了解百度服务器上哪些端口是开放的,从而确定可能存在的服务或漏洞。 百度是中国最大的搜索引擎之一,其服务器是高度安全的,我们仅用于演示目的,请勿将此操作用于非法用途。 首先,在命令行中输入"nmap www.baidu.com",nmap将开始扫描www.baidu.com的端口。扫描完成后,会显示出被扫描主机映射的IP地址及端口状态。 百度服务器上常见的端口有: - 端口80(HTTP)是用于网页的默认端口,一般情况下都是开放的。表明百度服务器上可能存在网站服务。 - 端口443(HTTPS)是加密的HTTP协议端口,一般用于安全的网页服务。它也可能是开放的,表明百度网页可能使用了加密保护。 - 端口22(SSH)是用于安全远程登录的标准协议。在百度服务器上可能是关闭的,因为SSH通常只开放给特定的管理人员使用。 - 端口443和8443可能是开放的,表示可能存在安全的网页和应用程序服务。 - 端口135、137、139和445一般用于Windows网络共享服务。这些端口在百度服务器上可能是关闭的,因为他们提供的服务与百度的主要业务无关。 此外,还有许多其他端口,具体情况取决于百度的网络架构和所需的服务。 扫描结果有助于帮助百度管理员识别他们的服务器可能存在的风险,从而采取相应的安全措施。然而,端口扫描属于一种主动安全测试,在未经授权的情况下对他人进行端口扫描是非法的,可能导致法律责任。所以,在进行任何安全测试之前,请确保已经获得相关授权。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值