├── settings.gradle ├── photos ├── swagger.png ├── import_json.png ├── swag_test1.png ├── swag_test2.png ├── swag_test3.png ├── grafana_demo.png └── add_datasource.png ├── src ├── main │ ├── install_scripts.tar.gz │ ├── install_scripts │ │ └── config │ │ │ └── resources │ │ │ ├── web3j.def │ │ │ └── application.properties │ ├── resources │ │ ├── templates │ │ │ ├── bo │ │ │ │ ├── event_bo_template.txt │ │ │ │ └── method_bo_template.txt │ │ │ ├── db │ │ │ │ ├── sql_event_template.sql │ │ │ │ ├── sql_method_template.sql │ │ │ │ ├── db_method_repository_template.txt │ │ │ │ ├── db_event_repository_template.txt │ │ │ │ ├── db_event_entity_template.txt │ │ │ │ └── db_method_entity_template.txt │ │ │ ├── config │ │ │ │ ├── application-sharding-tables.txt │ │ │ │ └── application_properties.txt │ │ │ ├── grafana │ │ │ │ └── panel_table.txt │ │ │ └── crawler │ │ │ │ ├── crawler_method_impl_template.txt │ │ │ │ └── crawler_event_impl_template.txt │ │ └── application-test.properties │ └── java │ │ └── com │ │ └── webank │ │ └── webasemonkey │ │ ├── vo │ │ ├── MethodMetaInfo.java │ │ ├── Web3jTypeVO.java │ │ ├── ContractNameVO.java │ │ ├── EventMetaInfo.java │ │ ├── FieldVO.java │ │ ├── ContractInfo.java │ │ └── ContractStructureMetaInfo.java │ │ ├── code │ │ ├── template │ │ │ ├── face │ │ │ │ ├── EventGenerateParas.java │ │ │ │ ├── ConfigGenerateParas.java │ │ │ │ ├── MethodGenerateParas.java │ │ │ │ ├── GrafanaParas.java │ │ │ │ ├── GenerateParas.java │ │ │ │ └── AtomicParas.java │ │ │ ├── GenerateParasFactory.java │ │ │ ├── paras │ │ │ │ ├── GrafanaDashboardParas.java │ │ │ │ ├── GrafanaPanelTableParas.java │ │ │ │ ├── EventRepositoryRenderParas.java │ │ │ │ ├── MethodRepositoryRenderParas.java │ │ │ │ ├── EventSqlFileRenderParas.java │ │ │ │ ├── EventBoRenderParas.java │ │ │ │ ├── MethodSqlRenderParas.java │ │ │ │ ├── MethodBoRenderParas.java │ │ │ │ ├── EventEntityRenderParas.java │ │ │ │ ├── EventCrawlImplParas.java │ │ │ │ ├── MethodCrawlerImplParas.java │ │ │ │ ├── MethodEntityRenderParas.java │ │ │ │ ├── ApplicationPropParas.java │ │ │ │ └── DBEnvironmentParas.java │ │ │ ├── CodeTemplateGenerateService.java │ │ │ └── GrafanaGenerateService.java │ │ └── service │ │ │ ├── CodeGenerateService.java │ │ │ └── ContractInfoService.java │ │ ├── bo │ │ ├── JavaArrayTypeBO.java │ │ ├── JavaBasicTypeBO.java │ │ └── Type.java │ │ ├── enums │ │ ├── NameStyleEnum.java │ │ ├── SubProjectEnum.java │ │ ├── SysTableEnum.java │ │ └── JavaTypeEnum.java │ │ ├── tools │ │ ├── NameValueVO.java │ │ ├── JacksonException.java │ │ ├── AddressListUtils.java │ │ ├── PackagePath.java │ │ ├── SqlNameUtils.java │ │ ├── TypeReferenceUtils.java │ │ ├── StringStyleUtils.java │ │ ├── PropertiesUtils.java │ │ ├── SolJavaTypeMappingUtils.java │ │ ├── SolSqlTypeMappingUtils.java │ │ ├── SolTypeMethodMappingUtils.java │ │ └── JacksonUtils.java │ │ ├── constants │ │ ├── GasConstants.java │ │ ├── ConfigConstants.java │ │ ├── AbiTypeConstants.java │ │ ├── ParserConstants.java │ │ ├── ConfigFileConstants.java │ │ ├── PackageConstants.java │ │ └── TemplateConstants.java │ │ ├── config │ │ ├── ServerConfig.java │ │ ├── ButtonEnvironmentConfig.java │ │ ├── ZookeeperConfig.java │ │ ├── ContractsConfig.java │ │ ├── SystemEnvironmentConfig.java │ │ └── BeanConfig.java │ │ ├── parser │ │ └── ContractJavaParserInterface.java │ │ ├── WebasemonkeyApplication.java │ │ └── task │ │ └── GenerateCodeApplicationRunner.java └── test │ └── java │ └── com │ └── webank │ └── webasemonkey │ ├── WebasemonkeyApplicationTests.java │ ├── code │ └── auto │ │ ├── GeneratorTest.java │ │ ├── GrafanaGenerateServiceTest.java │ │ └── MethodCodeGeneratorTest.java │ ├── tools │ └── AbiTypeRefUtilsTest.java │ └── parser │ └── MethodParserTest.java ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .travis.yml ├── ROADMAP.md ├── gradlew.bat ├── ChangeLog.md └── README.md /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'WeBASE-Codegen-Monkey' 2 | -------------------------------------------------------------------------------- /photos/swagger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/photos/swagger.png -------------------------------------------------------------------------------- /photos/import_json.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/photos/import_json.png -------------------------------------------------------------------------------- /photos/swag_test1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/photos/swag_test1.png -------------------------------------------------------------------------------- /photos/swag_test2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/photos/swag_test2.png -------------------------------------------------------------------------------- /photos/swag_test3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/photos/swag_test3.png -------------------------------------------------------------------------------- /photos/grafana_demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/photos/grafana_demo.png -------------------------------------------------------------------------------- /photos/add_datasource.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/photos/add_datasource.png -------------------------------------------------------------------------------- /src/main/install_scripts.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/src/main/install_scripts.tar.gz -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WeBankBlockchain/WeBASE-Codegen-Monkey/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/install_scripts/config/resources/web3j.def: -------------------------------------------------------------------------------- 1 | "StaticArray","blob","byte[]","String.valueOf" 2 | "DynamicArray","blob","byte[]","String.valueOf" -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | /bin/ 4 | bin/* 5 | config/* 6 | /.gradle/ 7 | 8 | 9 | 10 | ### STS ### 11 | .apt_generated 12 | .classpath 13 | .factorypath 14 | .project 15 | .settings 16 | .springBeans 17 | .sts4-cache 18 | 19 | ### IntelliJ IDEA ### 20 | .idea 21 | *.iws 22 | *.iml 23 | *.ipr 24 | 25 | ### NetBeans ### 26 | /nbproject/private/ 27 | /build/ 28 | /nbbuild/ 29 | /dist/ 30 | /nbdist/ 31 | /.nb-gradle/ 32 | 33 | src/main/resources/*.properties 34 | 35 | ## contracts ## 36 | src/main/java/com/webank/blockchain/ 37 | src/main/java/org/ 38 | -------------------------------------------------------------------------------- /src/main/resources/templates/bo/event_bo_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.bo.event; 5 | 6 | import com.webank.webasebee.common.bo.data.EventBO; 7 | import lombok.Data; 8 | import lombok.EqualsAndHashCode; 9 | import lombok.experimental.Accessors; 10 | 11 | @Data 12 | @Accessors(chain = true) 13 | @EqualsAndHashCode(callSuper=true) 14 | public class ${class_name} extends EventBO { 15 | 16 | <% 17 | for(entry in list){ 18 | var javaName = entry.javaName; 19 | var javaType = entry.javaType; 20 | var sqlName = entry.sqlName; 21 | var entityType = entry.entityType; 22 | var length = entry.length; 23 | %> 24 | private ${entityType} ${javaName}; 25 | <% } %> 26 | } 27 | -------------------------------------------------------------------------------- /src/main/resources/templates/bo/method_bo_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.bo.method; 5 | 6 | import com.webank.webasebee.common.bo.data.MethodBO; 7 | import lombok.Data; 8 | import lombok.EqualsAndHashCode; 9 | import lombok.experimental.Accessors; 10 | 11 | @Data 12 | @Accessors(chain = true) 13 | @EqualsAndHashCode(callSuper=true) 14 | public class ${class_name} extends MethodBO { 15 | <% 16 | for(entry in list){ 17 | var javaName = entry.javaName; 18 | var javaType = entry.javaType; 19 | %> 20 | private ${javaType} ${javaName}; 21 | <% } %> 22 | <% 23 | for(entry in outputList){ 24 | var javaName = entry.javaName; 25 | var javaType = entry.javaType; 26 | %> 27 | private ${javaType} ${javaName}; 28 | <% } %> 29 | } 30 | -------------------------------------------------------------------------------- /src/main/resources/templates/db/sql_event_template.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE ${table_name} ( 2 | pk_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', 3 | block_height bigint(20) NOT NULL COMMENT '交易所在块高', 4 | <% 5 | for(entry in list){ 6 | var key = entry.sqlName; 7 | var value = entry.sqlType; 8 | %> 9 | ${key} ${value} NOT NULL, 10 | <% } %> 11 | <% 12 | for(entry in list){ 13 | var key = entry.sqlName; 14 | var value = entry.sqlType; 15 | if(strutil.contain(key,"id") || strutil.contain(key,"owner")){ 16 | if(!strutil.contain(value,"ext")){ 17 | println(" KEY `" + key + "` (`" + key + "`),"); 18 | } 19 | } 20 | } 21 | %> 22 | `updatetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', 23 | PRIMARY KEY (pk_id) 24 | ) COMMENT='${table_name}' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | branches: 2 | only: 3 | - master 4 | - code_refactor_2020.01 5 | 6 | matrix: 7 | fast_finish: true 8 | include: 9 | - language: java 10 | jdk: openjdk8 11 | os: linux 12 | sudo: required 13 | after_success: 14 | - bash <(curl -s https://codecov.io/bash) 15 | 16 | - language: java 17 | jdk: openjdk9 18 | os: linux 19 | sudo: required 20 | 21 | - language: java 22 | jdk: openjdk10 23 | os: linux 24 | sudo: required 25 | 26 | - language: java 27 | jdk: openjdk11 28 | os: linux 29 | sudo: required 30 | 31 | cache: 32 | directories: 33 | - $HOME/.gradle/caches/ 34 | - $HOME/.gradle/wrapper/ 35 | 36 | before_script: 37 | - mv config/contract_example config/contract 38 | - chmod +x gradlew 39 | 40 | script: 41 | - ./gradlew test bootJar 42 | 43 | after_success: 44 | - bash <(curl -s https://codecov.io/bash) 45 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/vo/MethodMetaInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.vo; 17 | 18 | /** 19 | * MethodMetaInfo 20 | * 21 | * @Description: MethodMetaInfo 22 | * @author graysonzhang 23 | * @data 2018年-12-4 14:39:24 24 | * 25 | */ 26 | public class MethodMetaInfo extends ContractStructureMetaInfo { 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/com/webank/webasemonkey/WebasemonkeyApplicationTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey; 17 | 18 | import org.springframework.boot.test.context.SpringBootTest; 19 | import org.springframework.test.context.ActiveProfiles; 20 | 21 | @SpringBootTest 22 | @ActiveProfiles("test") 23 | public abstract class WebasemonkeyApplicationTests { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/resources/templates/db/sql_method_template.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE ${table_name} ( 2 | pk_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', 3 | block_height bigint(20) NOT NULL COMMENT '交易所在块高', 4 | tx_hash varchar(100) NOT NULL COMMENT '交易哈希', 5 | <% 6 | for(entry in list){ 7 | var key = entry.sqlName; 8 | var value = entry.sqlType; 9 | %> 10 | ${key} ${value} NOT NULL, 11 | <% } %> 12 | <% 13 | for(entry in list){ 14 | var key = entry.sqlName; 15 | var value = entry.sqlType; 16 | if(strutil.contain(key,"id") || strutil.contain(key,"owner")){ 17 | if(!strutil.contain(value,"ext")){ 18 | println(" KEY `" + key + "` (`" + key + "`),"); 19 | } 20 | } 21 | } 22 | %> 23 | <% 24 | for(entry in outputList){ 25 | var key = entry.sqlName; 26 | var value = entry.sqlType; 27 | %> 28 | ${key} ${value} NOT NULL, 29 | <% } %> 30 | `updatetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', 31 | PRIMARY KEY (pk_id) 32 | ) COMMENT='${table_name}' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/face/EventGenerateParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.face; 17 | 18 | import com.webank.webasemonkey.vo.EventMetaInfo; 19 | 20 | /** 21 | * EventGenerateParas 22 | * 23 | * @Description: EventGenerateParas 24 | * @author maojiayu 25 | * @date 2018-11-14 15:35:41 26 | * 27 | */ 28 | public interface EventGenerateParas extends GenerateParas { 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/face/ConfigGenerateParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.face; 17 | 18 | import com.webank.webasemonkey.vo.ContractInfo; 19 | 20 | /** 21 | * EventGenerateParas 22 | * 23 | * @Description: ConfigGenerateParas 24 | * @author maojiayu 25 | * @date 2018-11-14 15:35:41 26 | * 27 | */ 28 | public interface ConfigGenerateParas extends GenerateParas { 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/face/MethodGenerateParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.face; 17 | 18 | import com.webank.webasemonkey.vo.MethodMetaInfo; 19 | 20 | /** 21 | * MethodGenerateParas 22 | * 23 | * @Description: MethodGenerateParas 24 | * @author graysonzhang 25 | * @data 2018-12-04 16:19:39 26 | * 27 | */ 28 | public interface MethodGenerateParas extends GenerateParas { 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/bo/JavaArrayTypeBO.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.bo; 17 | 18 | import lombok.Data; 19 | import lombok.EqualsAndHashCode; 20 | 21 | /** 22 | * JavaTypeBO 23 | * 24 | * @Description: JavaTypeBO 25 | * @author maojiayu 26 | * @data Apr 27, 2020 2:51:20 PM 27 | * 28 | */ 29 | @Data 30 | @EqualsAndHashCode(callSuper=false) 31 | public class JavaArrayTypeBO extends JavaBasicTypeBO{ 32 | private Type type; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/face/GrafanaParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.face; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * GrafanaParas 22 | * 23 | * @Description: GrafanaParas 24 | * @author maojiayu 25 | * @param 26 | * @data Mar 28, 2019 11:05:12 AM 27 | * 28 | */ 29 | public interface GrafanaParas extends AtomicParas> { 30 | 31 | public String getGeneratedFilePath(); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ROADMAP.md: -------------------------------------------------------------------------------- 1 | # Roadmap 2 | 3 | ## WEBASE-MONKEY 4 | 5 | ### 新功能规划 6 | - [x] 适配FISCO-BCOS 2.0 7 | - [ ] 支持自动化生成日志采集模块 8 | - [ ] 支持数据导出到ElasticSearch,并提供ES搜索引擎的查询接口 9 | - [x] 支持灵活的复杂查询 10 | - [ ] 支持数据导出到Hadoop,并提供基本的大数据报表查询支持 11 | - [ ] 支持数据导出到Kafka,并集成spark 12 | - [ ] 支持Flink 13 | 14 | ### 修复与增强 15 | - [ ] 重构自动生成的脚本,添加更多的操作选项 16 | - [ ] 开发grafana console,使之全部自动化 17 | - [ ] 为grafana展示提供更多内置的指标 18 | - [ ] 集成travis,完善ci 19 | - [ ] 完善现有的单元测试 20 | - [ ] 集成checkstyle等代码检测工具 21 | 22 | ## WEBASE-BEE 23 | 24 | ### 新功能规划 25 | - [ ] 接入FISCO-BCOS 2.0的日志,并解析 26 | - [ ] 提供基本的业务监控功能 27 | - [ ] 更细粒度的监控指标,包含机器和应用 28 | - [ ] 接入WEBASE-FRONT,与之打通 29 | - [ ] 支持PreCompile合约 30 | - [x] 支持WEBASE-BEE集群,新增immuteble的调度策略 31 | - [ ] 提供独立的数据审计和数据完整性检查的模块 32 | - [ ] 支持Topic机制,实现基于Topic的数据导出与治理 33 | - [ ] 支持导出数据复原成链上格式(rlp或对应编码),可hash验证,可构建merkleroot 34 | - [x] 进一步拆解组件,抽象多个独立公共库组件 35 | 36 | ### 修复与增强 37 | - [x] 修复FISCO-BCOS长连接中断无法重连的问题 38 | - [x] 修复FISCO-BCOS频繁闪断后无法重连的问题 39 | - [x] 修复FISCO-BCOS节点断掉后部分数据异常的问题 40 | - [x] 解决FISCO-BCOS 2.0 函数报文解析失败的问题 41 | - [ ] 提供更为强大的数据查询引擎 42 | - [x] 日志格式的规范与修整 43 | - [x] 解决数据库部分字段长度与solidity长度不适配的问题 44 | - [ ] 增加quartz任务调度的支持 45 | - [ ] 增加SQL注入攻击的防御 46 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/enums/NameStyleEnum.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.enums; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Getter; 20 | 21 | /** 22 | * NameStyleEnum 23 | * 24 | * @Description: NameStyleEnum 25 | * @author maojiayu 26 | * @data May 7, 2019 10:05:00 AM 27 | * 28 | */ 29 | @AllArgsConstructor 30 | @Getter 31 | public enum NameStyleEnum { 32 | RAW_CASE("rawCase"), UNDER_SCORE_CASE("underScoreCase"); 33 | 34 | private String style; 35 | } 36 | -------------------------------------------------------------------------------- /src/main/resources/templates/config/application-sharding-tables.txt: -------------------------------------------------------------------------------- 1 | spring.shardingsphere.datasource.names=ds 2 | 3 | spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource 4 | spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driver 5 | spring.shardingsphere.datasource.ds.jdbc-url=${dbUrl} 6 | spring.shardingsphere.datasource.ds.username=${dbUser} 7 | spring.shardingsphere.datasource.ds.password=${dbPassword} 8 | spring.shardingsphere.datasource.ds.max-active=160 9 | 10 | <% 11 | for(var c in list){ 12 | name = c.name; 13 | shardingNO = c.shardingNO; 14 | tmpNO = shardingNO - 1; 15 | println("spring.shardingsphere.sharding.tables." + name + ".actual-data-nodes=ds." + name + "_$->{0.." + tmpNO + "}"); 16 | println("spring.shardingsphere.sharding.tables." + name + ".table-strategy.inline.sharding-column=block_height"); 17 | println("spring.shardingsphere.sharding.tables." + name + ".table-strategy.inline.algorithm-expression=" + name + "_$->{block_height % " + shardingNO + "}"); 18 | println("spring.shardingsphere.sharding.tables." + name + ".key-generator-column-name=pk_id"); 19 | println(); 20 | } 21 | %> -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/face/GenerateParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.face; 17 | 18 | /** 19 | * RenderParas 20 | * 21 | * @Description: RenderParas 22 | * @author maojiayu 23 | * @date 2018-11-12 16:09:42 24 | * 25 | */ 26 | public interface GenerateParas extends AtomicParas { 27 | 28 | /** 29 | * @Description: return the path of generated files. 30 | * @param: @param t 31 | * @return: String 32 | */ 33 | public String getGeneratedFilePath(T t); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/vo/Web3jTypeVO.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.vo; 17 | 18 | import lombok.Data; 19 | import lombok.experimental.Accessors; 20 | 21 | /** 22 | * Web3jTypeVO 23 | * 24 | * @Description: Web3jTypeVO 25 | * @author maojiayu 26 | * @data Oct 22, 2019 2:25:20 PM 27 | * 28 | */ 29 | @Data 30 | @Accessors(chain = true) 31 | public class Web3jTypeVO { 32 | 33 | private String solidityType; 34 | private String sqlType; 35 | private String javaType; 36 | private String typeMethod; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/NameValueVO.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Data; 20 | import lombok.NoArgsConstructor; 21 | import lombok.experimental.Accessors; 22 | 23 | /** 24 | * NameValueVO: common vo. 25 | * 26 | * @author maojiayu 27 | * @data Dec 28, 2018 3:54:40 PM 28 | * 29 | */ 30 | @Data 31 | @NoArgsConstructor 32 | @AllArgsConstructor 33 | @Accessors(chain = true) 34 | public class NameValueVO { 35 | private String name; 36 | private T value; 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/constants/GasConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.constants; 17 | 18 | import java.math.BigInteger; 19 | 20 | /** 21 | * GasConstants, whose concept was extended from ETH. 22 | * 23 | * @author maojiayu 24 | * @date 2018-7-4 14:21:02 25 | * 26 | */ 27 | public class GasConstants { 28 | public static final BigInteger GAS_PRICE = new BigInteger("30000000"); 29 | public static final BigInteger GAS_LIMIT = new BigInteger("30000000"); 30 | public static final BigInteger INITIAL_WEI_VALUE = new BigInteger("0"); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/vo/ContractNameVO.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.vo; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Data; 20 | import lombok.NoArgsConstructor; 21 | import lombok.experimental.Accessors; 22 | 23 | /** 24 | * ContractNameVO 25 | * 26 | * @Description: ContractNameVO 27 | * @author maojiayu 28 | * @data Dec 18, 2018 10:59:27 PM 29 | * 30 | */ 31 | @Data 32 | @AllArgsConstructor 33 | @NoArgsConstructor 34 | @Accessors(chain = true) 35 | public class ContractNameVO { 36 | private String contractName; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/config/ServerConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.config; 17 | 18 | import org.springframework.boot.context.properties.ConfigurationProperties; 19 | import org.springframework.context.annotation.Configuration; 20 | 21 | import lombok.Data; 22 | 23 | /** 24 | * ServerConfig 25 | * 26 | * @Description: ServerConfig 27 | * @author graysonzhang 28 | * @data 2019-03-04 17:28:37 29 | * 30 | */ 31 | @Configuration 32 | @ConfigurationProperties("server") 33 | @Data 34 | public class ServerConfig { 35 | 36 | private int port = 8080; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/config/ButtonEnvironmentConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.config; 17 | 18 | import org.springframework.boot.context.properties.ConfigurationProperties; 19 | import org.springframework.context.annotation.Configuration; 20 | 21 | import lombok.Data; 22 | 23 | /** 24 | * ButtonEnvironmentConfig 25 | * 26 | * @Description: ButtonEnvironmentConfig 27 | * @author maojiayu 28 | * @data May 13, 2020 4:22:03 PM 29 | * 30 | */ 31 | @Configuration 32 | @ConfigurationProperties("button") 33 | @Data 34 | public class ButtonEnvironmentConfig { 35 | 36 | private String swagger = "off"; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/config/ZookeeperConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.config; 17 | 18 | import org.springframework.boot.context.properties.ConfigurationProperties; 19 | import org.springframework.context.annotation.Configuration; 20 | 21 | import lombok.Data; 22 | 23 | /** 24 | * ZookeeperConfig 25 | * 26 | * @Description: ZookeeperConfig 27 | * @author graysonzhang 28 | * @data 2019-03-04 16:36:15 29 | * 30 | */ 31 | @Configuration 32 | @ConfigurationProperties("regcenter") 33 | @Data 34 | public class ZookeeperConfig { 35 | 36 | private String serverList; 37 | private String namespace; 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/enums/SubProjectEnum.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.enums; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Getter; 20 | 21 | /** 22 | * SubProjectEnum 23 | * 24 | * @Description: SubProjectEnum 25 | * @author maojiayu 26 | * @data Jul 2, 2019 11:10:06 AM 27 | * 28 | */ 29 | @AllArgsConstructor 30 | @Getter 31 | public enum SubProjectEnum { 32 | CORE("core", "WeBASE-Collect-Bee-core"), 33 | DB("db", "WeBASE-Collect-Bee-db"), 34 | PARSER("parser", "WeBASE-Collect-Bee-parser"); 35 | 36 | private String pakageName; 37 | private String pathName; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/constants/ConfigConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.constants; 17 | 18 | /** 19 | * ConfigConstants for default config 20 | * 21 | * @Description: ConfigConstants 22 | * @author graysonzhang 23 | * @data 2018-11-11 17:15:52 24 | * 25 | */ 26 | public class ConfigConstants { 27 | 28 | /** @Fields TABLE_COUNT : table sharding detault 1 */ 29 | public static final int TABLE_COUNT = 1; 30 | /** @Fields TASK_FREQUENCY : thread sleep time */ 31 | public static final String TASK_FREQUENCY = "5"; 32 | 33 | public static final String CONTRACT_PATH = "config/contract/"; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/parser/ContractJavaParserInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.parser; 17 | 18 | import java.util.List; 19 | 20 | import com.webank.webasemonkey.vo.ContractNameVO; 21 | 22 | /** 23 | * ContractJavaParserInterface 24 | * 25 | * @author maojiayu 26 | * @param 27 | * @data Dec 19, 2018 2:33:00 PM 28 | * 29 | */ 30 | public interface ContractJavaParserInterface { 31 | /** 32 | * convert a java contract class to a list of vos. 33 | * 34 | * @param clazz 35 | * @return List 36 | */ 37 | public List parseToInfoList(Class clazz); 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/constants/AbiTypeConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.constants; 17 | 18 | /** 19 | * AbiTypeConstants 20 | * 21 | * @Description: AbiTypeConstants 22 | * @author graysonzhang 23 | * @data 2018-12-4 20:02:13 24 | * 25 | */ 26 | public class AbiTypeConstants { 27 | 28 | /** @Fields ABI_FUNCTION_TYPE : abi function type */ 29 | public static final String ABI_FUNCTION_TYPE = "function"; 30 | /** @Fields ABI_CONSTRUCTOR_TYPE : abi constructor type */ 31 | public static final String ABI_CONSTRUCTOR_TYPE = "constructor"; 32 | /** @Fields ABI_EVENT_TYPE : abi event type */ 33 | public static final String ABI_EVENT_TYPE = "event"; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/enums/SysTableEnum.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.enums; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Getter; 20 | 21 | /** 22 | * SysTableEnum 23 | * 24 | * @Description: SysTableEnum 25 | * @author maojiayu 26 | * @data Jan 7, 2019 8:51:22 PM 27 | * 28 | */ 29 | @AllArgsConstructor 30 | @Getter 31 | public enum SysTableEnum { 32 | ACCOUNT_INFO("AccountInfo", "account_info"), 33 | BLOCK_DETAIL_INFO("BlockDetailInfo", "block_detail_info"), 34 | BLOCK_INFO("BlockInfo", "block_info"), 35 | BLOCK_TX_DETAIL_INFO("BlockTxDetailInfo", "block_tx_detail_info") 36 | ; 37 | 38 | String name; 39 | String tableName; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/vo/EventMetaInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.vo; 17 | 18 | import java.util.List; 19 | 20 | import lombok.AllArgsConstructor; 21 | import lombok.Data; 22 | import lombok.EqualsAndHashCode; 23 | import lombok.NoArgsConstructor; 24 | import lombok.experimental.Accessors; 25 | 26 | /** 27 | * EventMetaInfo 28 | * 29 | * @Description: EventMetaInfo 30 | * @author maojiayu 31 | * @data Dec 18, 2018 10:21:36 PM 32 | * 33 | */ 34 | @Data 35 | @NoArgsConstructor 36 | @AllArgsConstructor 37 | @Accessors(chain = true) 38 | @EqualsAndHashCode(callSuper=false) 39 | public class EventMetaInfo extends ContractStructureMetaInfo { 40 | private List ignoreParams; 41 | } 42 | -------------------------------------------------------------------------------- /src/main/resources/templates/db/db_method_repository_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.repository.method; 5 | 6 | import javax.transaction.Transactional; 7 | 8 | import org.springframework.data.jpa.repository.Modifying; 9 | import org.springframework.data.jpa.repository.Query; 10 | import org.springframework.data.jpa.repository.JpaRepository; 11 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 12 | import org.springframework.stereotype.Repository; 13 | 14 | import ${group}.${projectName}.generated.entity.method.${class_name}; 15 | import ${group}.${projectName}.repository.RollbackInterface; 16 | 17 | 18 | @Repository 19 | public interface ${class_name}MethodRepository extends JpaRepository<${class_name}, Long>, JpaSpecificationExecutor<${class_name}>, RollbackInterface { 20 | ${class_name} findByTxHash(String txHash); 21 | 22 | @Transactional 23 | @Modifying 24 | @Query(value = "delete from #{#entityName} where block_height >= ?1", nativeQuery = true) 25 | public void rollback(long blockHeight); 26 | 27 | @Transactional 28 | @Modifying 29 | @Query(value = "delete from #{#entityName} where block_height >= ?1 and block_height< ?2", nativeQuery = true) 30 | public void rollback(long startBlockHeight, long endBlockHeight); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/main/resources/templates/db/db_event_repository_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.repository.event; 5 | 6 | import javax.transaction.Transactional; 7 | 8 | import org.springframework.data.jpa.repository.Modifying; 9 | import org.springframework.data.jpa.repository.Query; 10 | import org.springframework.data.jpa.repository.JpaRepository; 11 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 12 | import org.springframework.stereotype.Repository; 13 | 14 | import ${group}.${projectName}.generated.entity.event.${class_name}; 15 | import ${group}.${projectName}.repository.RollbackInterface; 16 | 17 | 18 | @Repository 19 | public interface ${class_name}EventRepository extends JpaRepository<${class_name}, Long>, JpaSpecificationExecutor<${class_name}>, RollbackInterface { 20 | public ${class_name} findTopByOrderByBlockHeightDesc(); 21 | 22 | @Transactional 23 | @Modifying 24 | @Query(value = "delete from #{#entityName} where block_height >= ?1", nativeQuery = true) 25 | public void rollback(long blockHeight); 26 | 27 | @Transactional 28 | @Modifying 29 | @Query(value = "delete from #{#entityName} where block_height >= ?1 and block_height< ?2", nativeQuery = true) 30 | public void rollback(long startBlockHeight, long endBlockHeight); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/constants/ParserConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.constants; 17 | 18 | /** 19 | * ParserConstants 20 | * 21 | * @Description: ParserConstants 22 | * @author maojiayu 23 | * @data Jan 7, 2019 2:34:10 PM 24 | * 25 | */ 26 | public class ParserConstants { 27 | 28 | public static final String MONITOR = "monitor"; 29 | public static final String SHARDINGNO = "shardingNO"; 30 | public static final String LENGTH = "length"; 31 | public static final String ABI = "ABI"; 32 | public static final String EVENT_RESPONSE="EventResponse"; 33 | public static final String IGNORE_PARAM="ignoreParam"; 34 | public static final String SYSTEM = "system"; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/vo/FieldVO.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.vo; 17 | 18 | import lombok.Data; 19 | import lombok.experimental.Accessors; 20 | 21 | /** 22 | * FieldVO 23 | * 24 | * @Description: FieldVO 25 | * @author maojiayu 26 | * @data Dec 28, 2018 4:13:40 PM 27 | * 28 | */ 29 | @Data 30 | @Accessors(chain = true) 31 | public class FieldVO { 32 | private String sqlName; 33 | private String solidityName; 34 | private String javaName; 35 | private String sqlType; 36 | private String solidityType; 37 | private String javaType; 38 | private String entityType; 39 | private String typeMethod; // 转化方法 40 | private String javaCapName; 41 | private int length; 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/vo/ContractInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.vo; 17 | 18 | import java.util.List; 19 | 20 | import lombok.Data; 21 | import lombok.EqualsAndHashCode; 22 | import lombok.experimental.Accessors; 23 | 24 | /** 25 | * ContractInfo 26 | * 27 | * @Description: ContractInfo 28 | * @author graysonzhang 29 | * @data 2018-11-08 22:02:44 30 | * 31 | */ 32 | @Data 33 | @Accessors(chain = true) 34 | @EqualsAndHashCode(callSuper=false) 35 | public class ContractInfo extends ContractNameVO { 36 | /** @Fields eventList : event meta info list */ 37 | private List eventList; 38 | /** @Fields methodList : method meta info list */ 39 | private List methodList; 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/bo/JavaBasicTypeBO.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.bo; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | import lombok.Data; 21 | 22 | /** 23 | * JavaBasicType 24 | * 25 | * @Description: JavaBasicType 26 | * @author maojiayu 27 | * @data Apr 27, 2020 2:58:16 PM 28 | * 29 | */ 30 | @Data 31 | public class JavaBasicTypeBO { 32 | private boolean indexed; 33 | private String classType; 34 | private boolean isArray; 35 | 36 | public boolean isStaticArray() { 37 | return StringUtils.contains(classType, "StaticArray"); 38 | } 39 | 40 | public boolean isDyanmicArray() { 41 | return (StringUtils.contains(classType, "DynamicArray")); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/face/AtomicParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.face; 17 | 18 | import java.util.Map; 19 | 20 | /** 21 | * AtomicParas 22 | * 23 | * @Description: AtomicParas 24 | * @author maojiayu 25 | * @data Mar 27, 2019 5:07:07 PM 26 | * 27 | */ 28 | public interface AtomicParas { 29 | /** 30 | * @Description: wrap the render data to a map. And the render data is basically read from a pojo. 31 | * @param: @param t 32 | * @return: Map 33 | */ 34 | public Map getMap(T t); 35 | 36 | /** 37 | * @Description: return the path of template. 38 | * @return: String 39 | */ 40 | public String getTemplatePath(); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/config/ContractsConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.config; 17 | 18 | 19 | import java.util.List; 20 | 21 | import com.webank.webasemonkey.vo.ContractInfo; 22 | 23 | import lombok.Data; 24 | 25 | /** 26 | * 27 | * ContractsConfig 28 | * 29 | * @Description: ContractsConfig 30 | * @author graysonzhang 31 | * @data 2018-11-7 19:52:20 32 | * 33 | */ 34 | @Data 35 | public class ContractsConfig { 36 | 37 | /** @Fields contractsPackage : contracts package that contains all contract java code files */ 38 | private String contractsPackage; 39 | 40 | /** @Fields allTableCount : sharding for all tables */ 41 | private int allTableCount; 42 | 43 | /** @Fields contractList : contract list */ 44 | private List contractList; 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/JacksonException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | /** 19 | * JacksonException 20 | * 21 | * @Description: JacksonException 22 | * @author maojiayu 23 | * @data Dec 28, 2018 3:45:52 PM 24 | * 25 | */ 26 | public class JacksonException extends RuntimeException { 27 | 28 | private static final long serialVersionUID = -3313868940376241665L; 29 | 30 | public JacksonException() { 31 | super(); 32 | } 33 | 34 | public JacksonException(String message, Throwable cause) { 35 | super(message, cause); 36 | } 37 | 38 | public JacksonException(String message) { 39 | super(message); 40 | } 41 | 42 | public JacksonException(Throwable cause) { 43 | super(cause); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/WebasemonkeyApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey; 17 | 18 | import org.springframework.boot.SpringApplication; 19 | import org.springframework.boot.autoconfigure.SpringBootApplication; 20 | 21 | /** 22 | * webasemonkeyApplication 23 | * 24 | * @Description: webase-monkey is a powerful code generation project serving for fisco-bcos. In this version, it can 25 | * generate webase-bee, which could crawl data from fisco-bcos network, including block data,event data, 26 | * transaction data and account data. 27 | * @author maojiayu 28 | * @data Dec 28, 2018 10:58:11 AM 29 | * 30 | */ 31 | @SpringBootApplication 32 | public class WebasemonkeyApplication { 33 | public static void main(String[] args) { 34 | SpringApplication.run(WebasemonkeyApplication.class, args); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/constants/ConfigFileConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.constants; 17 | 18 | /** 19 | * ConfigFileConstants 20 | * 21 | * @Description: ConfigFileConstants 22 | * @author graysonzhang 23 | * @data 2018-12-17 21:40:55 24 | * 25 | */ 26 | public class ConfigFileConstants { 27 | 28 | /** @Fields GENERATED_APPLICATION_PROPERTIES_FILE_PATH : generated appilcation.properties file path */ 29 | public static final String GENERATED_APPLICATION_PROPERTIES_FILE_PATH = "src/main/resources/application.properties"; 30 | /** @Fields GENERATED_DB_ENV_FILE_PATH : generated db-env.xml file path */ 31 | public static final String GENERATED_DB_ENV_FILE_PATH = "src/main/resources/application-sharding-tables.properties"; 32 | 33 | public static final String GENERATED_GRAFANA_DEFAULT_DASHBOARD_PATH = "src/main/scripts/grafana/default_dashboard.json"; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/vo/ContractStructureMetaInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.vo; 17 | 18 | import java.util.List; 19 | 20 | import lombok.AllArgsConstructor; 21 | import lombok.Data; 22 | import lombok.EqualsAndHashCode; 23 | import lombok.NoArgsConstructor; 24 | import lombok.experimental.Accessors; 25 | 26 | /** 27 | * ContractStructureMetaInfo 28 | * 29 | * @Description: ContractStructureMetaInfo 30 | * @author maojiayu 31 | * @data Dec 18, 2018 10:21:35 PM 32 | * 33 | */ 34 | @Data 35 | @AllArgsConstructor 36 | @NoArgsConstructor 37 | @Accessors(chain = true) 38 | @EqualsAndHashCode(callSuper=false) 39 | public class ContractStructureMetaInfo extends ContractNameVO{ 40 | private String name; 41 | private String type; 42 | /** @Fields shardingNO : event sharding NO of db*/ 43 | private int shardingNO; 44 | private List list; 45 | private List outputList; 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/com/webank/webasemonkey/code/auto/GeneratorTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.auto; 17 | 18 | import java.util.Map; 19 | 20 | import org.junit.jupiter.api.Assertions; 21 | import org.junit.jupiter.api.Test; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | 24 | import com.webank.webasemonkey.WebasemonkeyApplicationTests; 25 | import com.webank.webasemonkey.code.template.face.GenerateParas; 26 | 27 | /** 28 | * GeneratorTest 29 | * 30 | * @Description: GeneratorTest 31 | * @author maojiayu 32 | * @date 2018年11月12日 下午7:51:27 33 | * 34 | */ 35 | public class GeneratorTest extends WebasemonkeyApplicationTests { 36 | 37 | @SuppressWarnings("rawtypes") 38 | @Autowired 39 | private Map parasMap; 40 | 41 | @Test 42 | public void go() { 43 | parasMap.forEach((k, v) -> System.out.println(k + " " + v)); 44 | Assertions.assertNotNull(parasMap); 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/webank/webasemonkey/code/auto/GrafanaGenerateServiceTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.auto; 17 | 18 | import java.io.IOException; 19 | 20 | import org.junit.jupiter.api.Assertions; 21 | import org.junit.jupiter.api.Test; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | 24 | import com.webank.webasemonkey.WebasemonkeyApplicationTests; 25 | import com.webank.webasemonkey.code.template.GrafanaGenerateService; 26 | 27 | /** 28 | * GrafanaGenerateService 29 | * 30 | * @Description: GrafanaGenerateService 31 | * @author maojiayu 32 | * @data Mar 27, 2019 5:42:59 PM 33 | * 34 | */ 35 | public class GrafanaGenerateServiceTest extends WebasemonkeyApplicationTests { 36 | @Autowired 37 | private GrafanaGenerateService service; 38 | 39 | @Test 40 | public void testGenerate() throws ClassNotFoundException, IOException { 41 | service.genereate(); 42 | Assertions.assertNotNull(service); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/GenerateParasFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template; 17 | 18 | import java.util.Map; 19 | 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.stereotype.Component; 22 | 23 | import com.webank.webasemonkey.code.template.face.AtomicParas; 24 | 25 | /** 26 | * CodeFactory 27 | * 28 | * @Description: GenerateParasFactory 29 | * @author maojiayu 30 | * @date 2018-11-12 18:20:55 31 | * 32 | */ 33 | @Component 34 | public class GenerateParasFactory { 35 | /** @Fields parasMap : typeName -> GenerateParas Interface */ 36 | @SuppressWarnings("rawtypes") 37 | @Autowired 38 | private Map parasMap; 39 | 40 | /** 41 | * get paras by type name. 42 | * 43 | * @param typeName 44 | * @return GenerateParas contains template path, generation path and render properties. 45 | */ 46 | @SuppressWarnings("rawtypes") 47 | public AtomicParas getParasByType(String typeName) { 48 | return parasMap.get(typeName); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/bo/Type.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.bo; 17 | 18 | import java.util.List; 19 | 20 | import org.apache.commons.lang3.StringUtils; 21 | 22 | import lombok.Data; 23 | 24 | /** 25 | * Type 26 | * 27 | * @Description: Type 28 | * @author maojiayu 29 | * @data Apr 27, 2020 2:49:09 PM 30 | * 31 | */ 32 | @Data 33 | public class Type { 34 | private List actualTypeArguments; 35 | private String rawType; 36 | private String ownerType; 37 | private String typeName; 38 | 39 | public int getArgType() { 40 | String arg = actualTypeArguments.get(0); 41 | if (StringUtils.containsIgnoreCase(arg, "int")) { 42 | // numeric 43 | return 1; 44 | } else if (StringUtils.containsIgnoreCase(arg, "byte")) { 45 | // bytes 46 | return 2; 47 | } else if (StringUtils.containsIgnoreCase(arg, "address")) { 48 | // address 49 | return 3; 50 | } else if (StringUtils.containsIgnoreCase(arg, "bool")) { 51 | // bool 52 | return 4; 53 | } else { 54 | return 0; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/AddressListUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | import java.util.stream.Collectors; 21 | 22 | import org.fisco.bcos.web3j.abi.datatypes.Address; 23 | 24 | 25 | /** 26 | * AddressListUtils 27 | * 28 | * @Description: AddressListUtils 29 | * @author maojiayu 30 | * @data Dec 28, 2018 3:43:23 PM 31 | * 32 | */ 33 | public class AddressListUtils { 34 | 35 | public static List
toAddressList(List strList) { 36 | if (strList.isEmpty()) { 37 | return new ArrayList
(); 38 | } else { 39 | return strList.stream().map(str -> { 40 | return new Address(str); 41 | }).collect(Collectors.toList()); 42 | } 43 | } 44 | 45 | public static List addressToStrList(List
adressList) { 46 | if (adressList.isEmpty()) { 47 | return new ArrayList(); 48 | } else { 49 | return adressList.stream().map(addr -> { 50 | return addr.toString().trim(); 51 | }).collect(Collectors.toList()); 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/PackagePath.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import com.webank.webasemonkey.constants.PackageConstants; 19 | import com.webank.webasemonkey.enums.SubProjectEnum; 20 | 21 | /** 22 | * PackageProcessor 23 | * 24 | * @Description: PackageProcessor 25 | * @author graysonzhang 26 | * @data 2018-11-10 1:54:19 27 | * 28 | */ 29 | public class PackagePath { 30 | 31 | /** @Fields ROOT_PATH : root path */ 32 | public static final String ROOT_PATH = "src/main/java/"; 33 | 34 | public static String getPackagePath(String postfix, String group, String subProjectPkg) { 35 | String packagePath = SubProjectEnum.valueOf(subProjectPkg.toUpperCase()).getPathName() + "/" + ROOT_PATH + group 36 | + "/" + PackageConstants.PROJECT_PKG_NAME + "/" + subProjectPkg + "/" + PackageConstants.GENERATED + "/" 37 | + postfix; 38 | packagePath = packagePath.replaceAll("\\.", "/"); 39 | return packagePath; 40 | } 41 | 42 | public static String getPackagePath(String packageName) { 43 | String packagePath = ROOT_PATH + packageName; 44 | packagePath = packagePath.replaceAll("\\.", "/"); 45 | return packagePath; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/com/webank/webasemonkey/code/auto/MethodCodeGeneratorTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.auto; 17 | 18 | import java.io.IOException; 19 | import java.util.Map; 20 | 21 | import org.junit.jupiter.api.Assertions; 22 | import org.junit.jupiter.api.Test; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | 25 | import com.webank.webasemonkey.WebasemonkeyApplicationTests; 26 | import com.webank.webasemonkey.code.service.CodeGenerateService; 27 | import com.webank.webasemonkey.code.template.face.GenerateParas; 28 | 29 | /** 30 | * MethodCodeGeneratorTest 31 | * 32 | * @Description: MethodCodeGeneratorTest 33 | * @author graysonzhang 34 | * @data 2018年12月4日 下午4:57:44 35 | * 36 | */ 37 | public class MethodCodeGeneratorTest extends WebasemonkeyApplicationTests { 38 | 39 | @Autowired 40 | private CodeGenerateService codeGenerator; 41 | @SuppressWarnings("rawtypes") 42 | @Autowired 43 | private Map parasMap; 44 | 45 | @Test 46 | public void go() throws ClassNotFoundException, IOException { 47 | parasMap.forEach((k, v) -> System.out.println(k + " " + v)); 48 | codeGenerator.generateBee(); 49 | Assertions.assertNotNull(codeGenerator); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/task/GenerateCodeApplicationRunner.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.task; 17 | 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.boot.ApplicationArguments; 20 | import org.springframework.boot.ApplicationRunner; 21 | import org.springframework.context.annotation.Profile; 22 | import org.springframework.core.annotation.Order; 23 | import org.springframework.stereotype.Component; 24 | 25 | import com.webank.webasemonkey.code.service.CodeGenerateService; 26 | 27 | import lombok.extern.slf4j.Slf4j; 28 | 29 | /** 30 | * GenerateCodeApplicationRunner: task entrance. 31 | * 32 | * @author maojiayu 33 | * @date 2018-11-29 16:37:38 34 | * 35 | */ 36 | @Component 37 | @Profile("!test") 38 | @Order(value = 1) 39 | @Slf4j 40 | public class GenerateCodeApplicationRunner implements ApplicationRunner { 41 | @Autowired 42 | private CodeGenerateService codeGenerateService; 43 | 44 | @Override 45 | public void run(ApplicationArguments var1) throws Exception { 46 | 47 | log.info("Begin to generate code."); 48 | codeGenerateService.generateBee(); 49 | log.info("Code generation Finished!"); 50 | Runtime.getRuntime().exit(0); 51 | } 52 | } -------------------------------------------------------------------------------- /src/main/install_scripts/config/resources/application.properties: -------------------------------------------------------------------------------- 1 | ### server configuration, you can config by your requires. 2 | ### fisco-bcos node configuration, you must config. 3 | system.nodeStr= 4 | system.groupId= 5 | ### 0-ECC, 1-gm 6 | system.encryptType=0 7 | 8 | ### data base configuration, you must config system.dbUrl,system.dbUser and system.dbPassword. 9 | ### And you can config data sharding slice number by your requires, default 1 without config. 10 | system.dbUrl=jdbc:mysql://[ip]:[port]/[db]?autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8 11 | system.dbUser= 12 | system.dbPassword= 13 | 14 | # system.[contractName].[methodName or eventName].shardingNO=5 15 | # system.sys.[sysTableName].shardingNO=5 16 | 17 | 18 | ### webase-bee project configuration, you must config system.group, system.contractPackName, system.baseProjectPath. 19 | ### And you can config frequency by your requires, default 5000 withoout config. 20 | system.group=com.webank 21 | system.baseProjectPath=../../../WeBASE-Collect-Bee 22 | system.contractPackName= 23 | 24 | # system.frequency=5 25 | 26 | ### thread pool configuration, you can config these by your requires. 27 | ### If not config, system will init by default. 28 | 29 | system.crawlBatchUnit=500 30 | 31 | ### config if multi deplyment 32 | system.multiLiving=false 33 | 34 | ### zookeeper config, you must config if multi deployment. Use , to split the cluster server list. 35 | #regcenter.serverList=IP:2181 36 | #regcenter.namespace=wecredit_job 37 | 38 | ### Other advanced configuration, you can config by your requires. 39 | 40 | # monitor.[contractName].[methodName or eventName].generated=on/off 41 | # monitor.[contractName].[methodName or eventName].ignoreParam=XXX,XXX 42 | # length.[contractName].[methodName or eventName].paraName=1024 43 | 44 | ### Springboot server config 45 | server.port=5200 46 | 47 | #system.tablePrefix= 48 | #system.tablePostfix= 49 | system.namePrefix=_ 50 | #system.namePostfix=_ 51 | 52 | # only in dev & test mode!! 53 | button.swagger=on 54 | -------------------------------------------------------------------------------- /src/main/resources/application-test.properties: -------------------------------------------------------------------------------- 1 | ### server configuration, you can config by your requires. 2 | ### fisco-bcos node configuration, you must config. 3 | system.nodeStr=node1@127.0.0.1:20200 4 | system.groupId=1 5 | system.keystorePassword=123456 6 | system.clientCertPassWord=123456 7 | 8 | ### data base configuration, you must config system.dbUrl,system.dbUser and system.dbPassword. 9 | ### And you can config data sharding slice number by your requires, default 1 without config. 10 | system.dbUrl=jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useSSL=false&serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=UTF-8 11 | system.dbUser=root 12 | system.dbPassword=test@123 13 | 14 | # system.[contractName].[methodName or eventName].shardingNO=5 15 | #system.sys.blockDetailInfo.shardingNO=5 16 | 17 | 18 | ### webase-bee project configuration, you must config system.group, system.contractPackName, system.baseProjectPath. 19 | ### And you can config frequency by your requires, default 5000 withoout config. 20 | system.group=com.webank 21 | system.baseProjectPath=../Webase-bee/ 22 | system.contractPackName=com.webank.webasemonkey.contracts 23 | #system.contractPackName=org.fisco.bcos.temp 24 | 25 | # system.frequency=5 26 | 27 | ### thread pool configuration, you can config these by your requires. 28 | ### If not config, system will init by default. 29 | 30 | system.maxBlockHeightThreshold=50 31 | system.crawlBatchUnit=100 32 | 33 | ### config if multi deplyment 34 | system.multiLiving=false 35 | 36 | ### sql name style: rawCase or underScoreCase 37 | #system.nameStyle=rawCase 38 | ### sql name settings: used in table name 39 | system.namePrefix=_ 40 | system.namePostfix=_ 41 | 42 | 43 | ### Other advanced configuration, you can config by your requires. 44 | 45 | # monitor.[contractName].[methodName or eventName].generated=on/off 46 | # monitor.[contractName].[methodName or eventName].ignoreParam=XXX,XXX 47 | # length.[contractName].[methodName or eventName].paraName=1024 48 | 49 | ### Springboot server config 50 | server.port=5200 51 | 52 | system.dbIdentifierSplit=true 53 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/constants/PackageConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.constants; 17 | 18 | /** 19 | * PackageConstants 20 | * 21 | * @Description: PackageConstants 22 | * @author graysonzhang 23 | * @data 2018-11-9 14:48:45 24 | * 25 | */ 26 | public class PackageConstants { 27 | // project package name define 28 | public static final String PROJECT_PKG_NAME = "webasebee"; 29 | public static final String SUB_PROJECT_PKG_CORE = "core"; 30 | public static final String SUB_PROJECT_PKG_PARSER = "parser"; 31 | public static final String SUB_PROJECT_PKG_DB = "db"; 32 | 33 | 34 | public static final String GENERATED = "generated"; 35 | 36 | // event package postfix 37 | public static final String EVENT_BO_PACKAGE_POSTFIX = "bo.event"; 38 | public static final String DB_EVENT_ENTRY_PACKAGE_POSTFIX = "entity.event"; 39 | public static final String DB_EVENT_REPOSITORY_PACKAGE_POSTFIX = "repository.event"; 40 | public static final String CRAWLER_EVENT_IMPL_PACKAGE_POSTFIX = "crawler.event"; 41 | 42 | // method package postfix 43 | public static final String METHOD_BO_PACKAGE_POSTFIX = "bo.method"; 44 | public static final String DB_METHOD_ENTRY_PACKAGE_POSTFIX = "entity.method"; 45 | public static final String DB__METHOD_REPOSITORY_PACKAGE_POSTFIX = "repository.method"; 46 | public static final String CRAWLER_METHOD_IMPL_PACKAGE_POSTFIX = "crawler.method"; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/resources/templates/grafana/panel_table.txt: -------------------------------------------------------------------------------- 1 | { 2 | "columns": [], 3 | "datasource": "MySQL", 4 | "fontSize": "110%", 5 | "gridPos": { 6 | "h": 8, 7 | "w": 24, 8 | "x": ${gridXPos}, 9 | "y": ${gridYPos} 10 | }, 11 | "id": ${id}, 12 | "links": [], 13 | "pageSize": 10, 14 | "scroll": true, 15 | "showHeader": true, 16 | "sort": { 17 | "col": 0, 18 | "desc": true 19 | }, 20 | "styles": [ 21 | { 22 | "alias": "depot_updatetime", 23 | "dateFormat": "YYYY-MM-DD HH:mm:ss", 24 | "pattern": "depot_updatetime", 25 | "type": "number" 26 | }, 27 | { 28 | "alias": "", 29 | "colorMode": "value", 30 | "colors": [ 31 | "#73BF69", 32 | "rgba(237, 129, 40, 0.89)", 33 | "rgba(245, 54, 54, 0.9)" 34 | ], 35 | "decimals": 0, 36 | "pattern": "block_height", 37 | "thresholds": [], 38 | "type": "string", 39 | "unit": "short" 40 | }, 41 | { 42 | "alias": "", 43 | "colorMode": "value", 44 | "colors": [ 45 | "#73BF69", 46 | "rgba(237, 129, 40, 0.89)", 47 | "rgba(245, 54, 54, 0.9)" 48 | ], 49 | "decimals": 0, 50 | "pattern": "pk_id", 51 | "thresholds": [], 52 | "type": "string", 53 | "unit": "short" 54 | } 55 | ], 56 | "targets": [ 57 | { 58 | "format": "table", 59 | "group": [], 60 | "metricColumn": "none", 61 | "rawQuery": true, 62 | "rawSql": "select * from ${table_name} order by depot_updatetime;", 63 | "refId": "A", 64 | "select": [ 65 | [ 66 | { 67 | "params": [ 68 | "value" 69 | ], 70 | "type": "column" 71 | } 72 | ] 73 | ], 74 | "timeColumn": "time", 75 | "where": [ 76 | { 77 | "name": "$__timeFilter", 78 | "params": [], 79 | "type": "macro" 80 | } 81 | ] 82 | } 83 | ], 84 | "timeFrom": null, 85 | "timeShift": null, 86 | "title": "${title}", 87 | "transform": "table", 88 | "type": "table" 89 | } -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/GrafanaDashboardParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.io.File; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | import org.springframework.stereotype.Component; 23 | 24 | import com.google.common.collect.Maps; 25 | import com.webank.webasemonkey.code.template.face.GrafanaParas; 26 | import com.webank.webasemonkey.constants.ConfigFileConstants; 27 | import com.webank.webasemonkey.constants.TemplateConstants; 28 | import com.webank.webasemonkey.enums.SubProjectEnum; 29 | 30 | /** 31 | * GrafanaDashboardParas 32 | * 33 | * @Description: GrafanaDashboardParas 34 | * @author maojiayu 35 | * @data Mar 28, 2019 11:12:50 AM 36 | * 37 | */ 38 | @Component 39 | public class GrafanaDashboardParas implements GrafanaParas> { 40 | 41 | @Override 42 | public Map getMap(List infoList) { 43 | Map map = Maps.newLinkedHashMap(); 44 | map.put("panels", infoList); 45 | return map; 46 | } 47 | 48 | @Override 49 | public String getTemplatePath() { 50 | return TemplateConstants.GRAFANA_DASHBOARD_PATH; 51 | } 52 | 53 | @Override 54 | public String getGeneratedFilePath() { 55 | return SubProjectEnum.CORE.getPathName() + File.separator 56 | + ConfigFileConstants.GENERATED_GRAFANA_DEFAULT_DASHBOARD_PATH; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/resources/templates/db/db_event_entity_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.entity.event; 5 | 6 | import java.util.Date; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.Index; 10 | import javax.persistence.Table; 11 | import javax.persistence.Temporal; 12 | import javax.persistence.TemporalType; 13 | import org.hibernate.annotations.UpdateTimestamp; 14 | import lombok.Data; 15 | import lombok.EqualsAndHashCode; 16 | import lombok.ToString; 17 | import lombok.experimental.Accessors; 18 | import com.webank.webasebee.db.entity.IdEntity; 19 | 20 | @SuppressWarnings("serial") 21 | @Data 22 | @Accessors(chain = true) 23 | @Entity(name = "${table_name}") 24 | @Table(name = "${table_name}", indexes = { @Index(name = "block_height", columnList = "block_height"), 25 | @Index(name = "block_timestamp", columnList = "block_timestamp"), 26 | @Index(name = "tx_hash", columnList = "tx_hash")}) 27 | @ToString(callSuper = true) 28 | @EqualsAndHashCode(callSuper = true) 29 | public class ${class_name} extends IdEntity { 30 | @Column(name = "block_height") 31 | private long blockHeight; 32 | @Column(name = "tx_hash") 33 | private String txHash; 34 | @Column(name = "event_contract_address") 35 | private String eventContractAddress; 36 | <% 37 | for(entry in list){ 38 | var javaName = entry.javaName; 39 | var javaType = entry.javaType; 40 | var sqlName = entry.sqlName; 41 | var entityType = entry.entityType; 42 | var length = entry.length; 43 | if(length>0){ 44 | println(" @Column(name = \"" + sqlName + "\", length = "+ length+")"); 45 | } 46 | else{ 47 | println(" @Column(name = \"" + sqlName+ "\")"); 48 | } 49 | %> 50 | private ${entityType} ${javaName}; 51 | <% } %> 52 | @Column(name = "block_timestamp") 53 | private Date blockTimeStamp; 54 | @UpdateTimestamp 55 | @Column(name = "depot_updatetime") 56 | @Temporal(TemporalType.TIMESTAMP) 57 | protected Date depotUpdatetime; 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/SqlNameUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | import org.springframework.stereotype.Service; 21 | 22 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 23 | import com.webank.webasemonkey.enums.NameStyleEnum; 24 | 25 | import lombok.extern.slf4j.Slf4j; 26 | 27 | /** 28 | * SqlNameUtils 29 | * 30 | * @Description: SqlNameUtils 31 | * @author maojiayu 32 | * @data Oct 12, 2019 11:16:54 AM 33 | * 34 | */ 35 | @Slf4j 36 | @Service 37 | public class SqlNameUtils { 38 | @Autowired 39 | private SystemEnvironmentConfig systemEnvironmentConfig; 40 | 41 | public String getSqlName(String contractName, String subName) { 42 | 43 | String sqlName = 44 | StringStyleUtils.upper2underline(contractName) + "_" + StringStyleUtils.upper2underline(subName); 45 | if (systemEnvironmentConfig.getNameStyle().equalsIgnoreCase(NameStyleEnum.RAW_CASE.getStyle())) { 46 | sqlName = contractName + subName; 47 | } 48 | // mysql最大支持64位,超过64位进行截取 49 | if (systemEnvironmentConfig.getDbIdentifierSplit().equalsIgnoreCase("true") && sqlName.length() > 55) { 50 | log.info("Cut sqlName {}", sqlName); 51 | sqlName = StringUtils.substring(sqlName, 0, 54); 52 | } 53 | return sqlName; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/config/SystemEnvironmentConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.config; 17 | 18 | import org.springframework.boot.context.properties.ConfigurationProperties; 19 | import org.springframework.context.annotation.Configuration; 20 | 21 | import com.webank.webasemonkey.enums.NameStyleEnum; 22 | 23 | import lombok.Data; 24 | 25 | /** 26 | * system environments. 27 | * 28 | * @author maojiayu 29 | * @data Dec 28, 2018 3:14:55 PM 30 | * 31 | */ 32 | @Configuration 33 | @ConfigurationProperties("system") 34 | @Data 35 | public class SystemEnvironmentConfig { 36 | 37 | /** @Fields nodeStr : [name]@[IP]:[PORT] */ 38 | private String nodeStr; 39 | private int groupId; 40 | 41 | private String dbUrl; 42 | private String dbUser; 43 | private String dbPassword; 44 | 45 | private String group; 46 | private String contractPackName; 47 | private String contractName; 48 | private String baseProjectPath; 49 | private int frequency = 5; 50 | 51 | /** @Fields crawlBatchUnit : to cut gaint mission to small missions, whose size is this */ 52 | private int crawlBatchUnit = 100; 53 | 54 | private String nameStyle = NameStyleEnum.UNDER_SCORE_CASE.getStyle(); 55 | private String tablePrefix = ""; 56 | private String tablePostfix = ""; 57 | private String namePrefix = ""; 58 | private String namePostfix = ""; 59 | 60 | private String multiLiving; 61 | 62 | private long startBlockHeight; 63 | private String startDate; 64 | 65 | private String dbIdentifierSplit = "false"; 66 | 67 | private int encryptType = 0; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/GrafanaPanelTableParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Component; 23 | import org.springframework.util.StringUtils; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.AtomicParas; 27 | import com.webank.webasemonkey.constants.TemplateConstants; 28 | import com.webank.webasemonkey.tools.SqlNameUtils; 29 | import com.webank.webasemonkey.vo.ContractStructureMetaInfo; 30 | import com.webank.webasemonkey.vo.FieldVO; 31 | 32 | /** 33 | * GrafanaPanelParas 34 | * 35 | * @Description: GrafanaPanelParas 36 | * @author maojiayu 37 | * @data Mar 27, 2019 4:48:14 PM 38 | * 39 | */ 40 | @Component 41 | public class GrafanaPanelTableParas implements AtomicParas { 42 | @Autowired 43 | private SqlNameUtils sqlNameUtils; 44 | 45 | @Override 46 | public Map getMap(ContractStructureMetaInfo info) { 47 | List list = info.getList(); 48 | Map map = Maps.newLinkedHashMap(); 49 | map.put("list", list); 50 | String className = info.getContractName() + StringUtils.capitalize(info.getName()); 51 | String tableName = sqlNameUtils.getSqlName(info.getContractName(), info.getName()+ "_" + info.getType()); 52 | map.put("table_name", tableName); 53 | map.put("title", className); 54 | return map; 55 | } 56 | 57 | @Override 58 | public String getTemplatePath() { 59 | return TemplateConstants.GRAFANA_PANEL_TABLE_PATH; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/enums/JavaTypeEnum.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.enums; 17 | 18 | import org.apache.commons.lang.StringUtils; 19 | 20 | import lombok.AllArgsConstructor; 21 | import lombok.Getter; 22 | import lombok.extern.slf4j.Slf4j; 23 | 24 | /** 25 | * JavaTypeEnum 26 | * 27 | * @Description: JavaTypeEnum 28 | * @author maojiayu 29 | * @data Apr 18, 2019 11:29:46 AM 30 | * 31 | */ 32 | @AllArgsConstructor 33 | @Getter 34 | @Slf4j 35 | public enum JavaTypeEnum { 36 | 37 | BIGINTEGER("BigInteger", "Long", "bigint", "BigIntegerUtils.toLong"), 38 | BOOL("Boolean", "String", "varchar(8)", "String.valueOf"), 39 | STRING("String", "String", "varchar(255)", "String.valueOf"), 40 | ByteArray("byte[]", "String", "varchar(10240)", "String.valueOf" ), 41 | LISTByteArray("List", "String", "varchar(10240)", "String.valueOf" ), 42 | LISTString("List", "String", "varchar(10240)", "String.valueOf" ), 43 | LISTBigInteger("List", "String", "varchar(4096)", "String.valueOf" ), 44 | LIST("List", "String", "varchar(10240)", "JacksonUtils.toJson" ) 45 | ; 46 | 47 | private String javaType; 48 | private String entityType; 49 | private String sqlType; 50 | private String typeMethod; 51 | 52 | public static JavaTypeEnum parse(String javaType) { 53 | for (JavaTypeEnum type : JavaTypeEnum.values()) { 54 | if ((type.getJavaType().equalsIgnoreCase(StringUtils.substringBefore(javaType, "<")) 55 | && !javaType.contains(">")) || type.getJavaType().equalsIgnoreCase(javaType)) { 56 | return type; 57 | } 58 | } 59 | log.error("javaType {} can't be converted.", javaType); 60 | return null; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/resources/templates/db/db_method_entity_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.entity.method; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.Index; 9 | import javax.persistence.Table; 10 | import lombok.Data; 11 | import lombok.EqualsAndHashCode; 12 | import lombok.ToString; 13 | import lombok.experimental.Accessors; 14 | import javax.persistence.Temporal; 15 | import javax.persistence.TemporalType; 16 | import org.hibernate.annotations.UpdateTimestamp; 17 | import java.util.Date; 18 | import com.webank.webasebee.db.entity.IdEntity; 19 | 20 | @SuppressWarnings("serial") 21 | @Data 22 | @Accessors(chain = true) 23 | @Entity(name = "${table_name}") 24 | @Table(name = "${table_name}", indexes = { @Index(name = "block_height", columnList = "block_height"), 25 | @Index(name = "block_timestamp", columnList = "block_timestamp"), 26 | @Index(name = "tx_hash", columnList = "tx_hash")}) 27 | @ToString(callSuper = true) 28 | @EqualsAndHashCode(callSuper = true) 29 | public class ${class_name} extends IdEntity { 30 | @Column(name = "block_height") 31 | private long blockHeight; 32 | @Column(name = "tx_hash") 33 | private String txHash; 34 | @Column(name = "contract_address") 35 | private String contractAddress; 36 | @Column(name = "method_status") 37 | private String methodStatus; 38 | <% 39 | for(entry in list){ 40 | var javaName = entry.javaName; 41 | var javaType = entry.javaType; 42 | var sqlName = entry.sqlName; 43 | var length = entry.length; 44 | if(length>0){ 45 | println(" @Column(name = \"" + sqlName + "\", length = "+ length+")"); 46 | } 47 | else{ 48 | println(" @Column(name = \"" + sqlName+ "\")"); 49 | } 50 | %> 51 | private ${javaType} ${javaName}; 52 | <% } %> 53 | 54 | <% 55 | for(entry in outputList){ 56 | var javaName = entry.javaName; 57 | var javaType = entry.javaType; 58 | var sqlName = entry.sqlName; 59 | var length = entry.length; 60 | if(length>0){ 61 | println(" @Column(name = \"" + sqlName + "\", length = "+ length+")"); 62 | } 63 | else{ 64 | println(" @Column(name = \"" + sqlName+ "\")"); 65 | } 66 | %> 67 | private ${javaType} ${javaName}; 68 | <% } %> 69 | 70 | @Column(name = "block_timestamp") 71 | private Date blockTimeStamp; 72 | @UpdateTimestamp 73 | @Column(name = "depot_updatetime") 74 | @Temporal(TemporalType.TIMESTAMP) 75 | protected Date depotUpdatetime; 76 | } 77 | -------------------------------------------------------------------------------- /src/test/java/com/webank/webasemonkey/tools/AbiTypeRefUtilsTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import org.fisco.bcos.web3j.abi.TypeReference; 19 | import org.fisco.bcos.web3j.tx.txdecode.BaseException; 20 | import org.junit.jupiter.api.Assertions; 21 | import org.junit.jupiter.api.Test; 22 | 23 | import com.webank.webasemonkey.WebasemonkeyApplicationTests; 24 | import com.webank.webasemonkey.bo.JavaArrayTypeBO; 25 | 26 | /** 27 | * AbiTypeRefUtilsTest 28 | * 29 | * @Description: AbiTypeRefUtilsTest 30 | * @author maojiayu 31 | * @data Apr 24, 2020 10:48:20 AM 32 | * 33 | */ 34 | public class AbiTypeRefUtilsTest extends WebasemonkeyApplicationTests { 35 | 36 | @SuppressWarnings("rawtypes") 37 | @Test 38 | public void testTypeReference() throws BaseException { 39 | System.out.println(JacksonUtils.toJson(TypeReferenceUtils.getTypeRef("address"))); 40 | System.out.println(JacksonUtils.toJson(TypeReferenceUtils.getTypeRef("bytes[]"))); 41 | System.out.println(JacksonUtils.toJson(TypeReferenceUtils.getTypeRef("address[2]"))); 42 | System.out.println(JacksonUtils.toJson(TypeReferenceUtils.getTypeRef("uint8[2]"))); 43 | TypeReference t = TypeReferenceUtils.getTypeRef("address[2]"); 44 | JavaArrayTypeBO bo = JacksonUtils.fromJson(JacksonUtils.toJson(t), JavaArrayTypeBO.class); 45 | System.out.println(); 46 | Assertions.assertEquals("org.fisco.bcos.web3j.abi.datatypes.generated.StaticArray2", bo.getType().getRawType()); 47 | Assertions.assertEquals("org.fisco.bcos.web3j.abi.datatypes.Address", 48 | bo.getType().getActualTypeArguments().get(0)); 49 | } 50 | 51 | @Test 52 | public void testJavaType() { 53 | System.out.println(JacksonUtils.toJson(TypeReferenceUtils.convertType("uint[]"))); 54 | Assertions.assertTrue(TypeReferenceUtils.convertType("uint[]").isArray()); 55 | System.out.println(JacksonUtils.toJson(TypeReferenceUtils.convertType("int8"))); 56 | Assertions.assertTrue(!TypeReferenceUtils.convertType("int8").isArray()); 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/resources/templates/config/application_properties.txt: -------------------------------------------------------------------------------- 1 | ### Springboot server config 2 | server.port=${port} 3 | spring.jackson.date-format=yyyy-MM-dd HH:mm:ss 4 | spring.jackson.time-zone=Asia/Shanghai 5 | logging.file=webasebee-core.log 6 | logging.level.org.hibernate=ERROR 7 | logging.level.org.fisco.bcos=ERROR 8 | 9 | ## Spring data jpa properties 10 | spring.datasource.test-on-borrow=false 11 | spring.datasource.test-while-idle=true 12 | spring.datasource.time-between-eviction-runs-millis= 3600000 13 | ## WARN!: please don't TURN ON this option in pruduce environment. Create the tables munally. 14 | ## Please turn off this option in produce environment. eg. 15 | ## spring.jpa.properties.hibernate.hbm2ddl.auto=none 16 | spring.jpa.properties.hibernate.hbm2ddl.auto=update 17 | spring.jpa.properties.hibernate.dialect=${group}.${projectName}.config.MysqlConfig 18 | spring.jpa.hibernate.naming.physical-strategy=${group}.${projectName}.config.DatabaseNamingStrategy 19 | spring.jpa.properties.hibernate.show_sql=true 20 | spring.jpa.properties.hibernate.globally_quoted_identifiers=true 21 | spring.profiles.active=sharding-tables 22 | 23 | ## System config 24 | system.nodeStr=${nodeStr} 25 | system.groupId=${groupId} 26 | system.configPath=config/ 27 | system.multiLiving=${multiLiving} 28 | 29 | ####### 30 | 31 | <% 32 | var flag =strutil.toLowerCase (multiLiving); 33 | if(flag=="true"){ 34 | %> 35 | ## zookeeper config 36 | regcenter.serverList=${serverListStr} 37 | regcenter.namespace=${nameSpace} 38 | 39 | ## job config 40 | prepareTaskJob.cron=0/${frequency} * * * * ? 41 | prepareTaskJob.shardingTotalCount=1 42 | prepareTaskJob.shardingItemParameters=0=A 43 | 44 | dataflowJob.cron=0/${frequency} * * * * ? 45 | dataflowJob.shardingTotalCount=8 46 | dataflowJob.shardingItemParameters=0=A,1=B,2=C,3=D,4=E,5=F,6=G,7=H 47 | 48 | <% 49 | }else{ 50 | %> 51 | system.crawlBatchUnit=${crawlBatchUnit} 52 | system.frequency=${frequency} 53 | <% 54 | } 55 | %> 56 | 57 | ## Monitor buttons 58 | #### event buttons 59 | #monitor.[ContractName].[EventName]CrawlerService=off 60 | <% 61 | for(var contract in eventList){ 62 | name = contract.name; 63 | contractName = contract.contractName; 64 | println("monitor." + contractName + "." + name + "CrawlerService=on"); 65 | } 66 | println("#### method buttons"); 67 | for(var m in methodList){ 68 | name = m.name; 69 | contractName = m.contractName; 70 | println("monitor." + contractName + "." + name + "MethodCrawlerService=on"); 71 | } 72 | %> 73 | ####### 74 | 75 | system.group=${group} 76 | system.projectName=${projectName} 77 | system.contractPackName=${contractPackName} 78 | system.contractPath=${contractPath} 79 | 80 | system.startBlockHeight=${startBlockHeight} 81 | system.startDate=${startDate} 82 | 83 | system.encryptType=${encryptType} 84 | 85 | button.swagger=${swagger} 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/EventRepositoryRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.Map; 19 | 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.stereotype.Component; 22 | 23 | import com.google.common.collect.Maps; 24 | import com.webank.webasemonkey.code.template.face.EventGenerateParas; 25 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 26 | import com.webank.webasemonkey.constants.PackageConstants; 27 | import com.webank.webasemonkey.constants.TemplateConstants; 28 | import com.webank.webasemonkey.tools.PackagePath; 29 | import com.webank.webasemonkey.vo.EventMetaInfo; 30 | 31 | /** 32 | * EventRepositoryRenderParas to generate repository of events. 33 | * 34 | * @author maojiayu 35 | * @data Dec 28, 2018 3:05:42 PM 36 | * 37 | */ 38 | @Component 39 | public class EventRepositoryRenderParas implements EventGenerateParas { 40 | @Autowired 41 | protected SystemEnvironmentConfig systemEnvironmentConfig; 42 | 43 | /* 44 | * input: 0-String 类名; 45 | */ 46 | @Override 47 | public Map getMap(EventMetaInfo event) { 48 | Map map = Maps.newLinkedHashMap(); 49 | String className = event.getContractName() + event.getName(); 50 | map.put("class_name", className); 51 | map.put("group", systemEnvironmentConfig.getGroup()); 52 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_DB); 53 | return map; 54 | } 55 | 56 | @Override 57 | public String getTemplatePath() { 58 | return TemplateConstants.DB_EVENT_REPOSITORY_TEMPLATE_PATH; 59 | } 60 | 61 | @Override 62 | public String getGeneratedFilePath(EventMetaInfo event) { 63 | String packagePath = PackagePath.getPackagePath(PackageConstants.DB_EVENT_REPOSITORY_PACKAGE_POSTFIX, 64 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_DB); 65 | String className = event.getContractName() + event.getName(); 66 | String javaFilePath = packagePath + "/" + className + "EventRepository.java"; 67 | return javaFilePath; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/MethodRepositoryRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.Map; 19 | 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.stereotype.Component; 22 | import org.springframework.util.StringUtils; 23 | 24 | import com.google.common.collect.Maps; 25 | import com.webank.webasemonkey.code.template.face.MethodGenerateParas; 26 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 27 | import com.webank.webasemonkey.constants.PackageConstants; 28 | import com.webank.webasemonkey.constants.TemplateConstants; 29 | import com.webank.webasemonkey.tools.PackagePath; 30 | import com.webank.webasemonkey.vo.MethodMetaInfo; 31 | 32 | /** 33 | * MethodRepositoryRenderParas uses to installing params for generating method repositories. 34 | * 35 | * @Description: MethodRepositoryRenderParas 36 | * @author graysonzhang 37 | * @data 2018-12-4 21:43:56 38 | * 39 | */ 40 | @Component 41 | public class MethodRepositoryRenderParas implements MethodGenerateParas { 42 | @Autowired 43 | protected SystemEnvironmentConfig systemEnvironmentConfig; 44 | 45 | @Override 46 | public Map getMap(MethodMetaInfo method) { 47 | Map map = Maps.newLinkedHashMap(); 48 | String className = method.getContractName() + StringUtils.capitalize(method.getName()); 49 | map.put("class_name", className); 50 | map.put("group", systemEnvironmentConfig.getGroup()); 51 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_DB); 52 | return map; 53 | } 54 | 55 | @Override 56 | public String getTemplatePath() { 57 | return TemplateConstants.DB_MEHTOD_REPOSITORY_TEMPLATE_PATH; 58 | } 59 | 60 | @Override 61 | public String getGeneratedFilePath(MethodMetaInfo method) { 62 | String packagePath = PackagePath.getPackagePath(PackageConstants.DB__METHOD_REPOSITORY_PACKAGE_POSTFIX, 63 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_DB); 64 | String className = method.getContractName() + StringUtils.capitalize(method.getName()); 65 | String javaFilePath = packagePath + "/" + className + "MethodRepository.java"; 66 | return javaFilePath; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/EventSqlFileRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.io.File; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.stereotype.Component; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.EventGenerateParas; 27 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 28 | import com.webank.webasemonkey.constants.TemplateConstants; 29 | import com.webank.webasemonkey.enums.SubProjectEnum; 30 | import com.webank.webasemonkey.tools.SqlNameUtils; 31 | import com.webank.webasemonkey.tools.StringStyleUtils; 32 | import com.webank.webasemonkey.vo.EventMetaInfo; 33 | import com.webank.webasemonkey.vo.FieldVO; 34 | 35 | /** 36 | * EventSqlFileRenderParas to generate event sql file. 37 | * 38 | * @Description: the generated sql file isn't used directly, Actually we use hibernate to operate db instead. This 39 | * record of sql files are still important. 40 | * @author maojiayu 41 | * @data Dec 28, 2018 3:06:14 PM 42 | * 43 | */ 44 | @Component 45 | public class EventSqlFileRenderParas implements EventGenerateParas { 46 | @Autowired 47 | protected SystemEnvironmentConfig systemEnvironmentConfig; 48 | @Autowired 49 | private SqlNameUtils sqlNameUtils; 50 | 51 | /* 52 | * input: 0-List 字段属性; 1-String SQL表名; 53 | */ 54 | @Override 55 | public Map getMap(EventMetaInfo event) { 56 | List list = event.getList(); 57 | Map map = Maps.newLinkedHashMap(); 58 | map.put("list", list); 59 | String tableName = sqlNameUtils.getSqlName(event.getContractName(), event.getName()) + "_event"; 60 | map.put("table_name", tableName); 61 | return map; 62 | } 63 | 64 | @Override 65 | public String getTemplatePath() { 66 | return TemplateConstants.SQL_EVENT_TEMPLATE_PATH; 67 | } 68 | 69 | @Override 70 | public String getGeneratedFilePath(EventMetaInfo event) { 71 | String javaFilePath = SubProjectEnum.CORE.getPathName() + File.separator + "src/main/scripts/event/" 72 | + StringStyleUtils.upper2underline(event.getName()) + ".sql"; 73 | return javaFilePath; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/EventBoRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Component; 23 | 24 | import com.google.common.collect.Maps; 25 | import com.webank.webasemonkey.code.template.face.EventGenerateParas; 26 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 27 | import com.webank.webasemonkey.constants.PackageConstants; 28 | import com.webank.webasemonkey.constants.TemplateConstants; 29 | import com.webank.webasemonkey.tools.PackagePath; 30 | import com.webank.webasemonkey.vo.EventMetaInfo; 31 | import com.webank.webasemonkey.vo.FieldVO; 32 | 33 | /** 34 | * EventBoRenderParas 35 | * 36 | * @Description: EventBoRenderParas 37 | * @author maojiayu 38 | * @data Jul 2, 2019 10:41:01 AM 39 | * 40 | */ 41 | @Component 42 | public class EventBoRenderParas implements EventGenerateParas { 43 | 44 | @Autowired 45 | protected SystemEnvironmentConfig systemEnvironmentConfig; 46 | 47 | @Override 48 | public Map getMap(EventMetaInfo event) { 49 | List list = event.getList(); 50 | Map map = Maps.newLinkedHashMap(); 51 | map.put("list", list); 52 | String className = getClassName(event); 53 | map.put("class_name", className); 54 | map.put("group", systemEnvironmentConfig.getGroup()); 55 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_PARSER); 56 | return map; 57 | } 58 | 59 | @Override 60 | public String getTemplatePath() { 61 | return TemplateConstants.EVENT_BO_TEMPLATE_PATH; 62 | } 63 | 64 | @Override 65 | public String getGeneratedFilePath(EventMetaInfo event) { 66 | String packagePath = PackagePath.getPackagePath(PackageConstants.EVENT_BO_PACKAGE_POSTFIX, 67 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_PARSER); 68 | String className = getClassName(event); 69 | String javaFilePath = packagePath + "/" + className + ".java"; 70 | return javaFilePath; 71 | } 72 | 73 | private String getClassName(EventMetaInfo event) { 74 | return event.getContractName() + event.getName() + "BO"; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/MethodSqlRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.io.File; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.stereotype.Component; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.MethodGenerateParas; 27 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 28 | import com.webank.webasemonkey.constants.TemplateConstants; 29 | import com.webank.webasemonkey.enums.SubProjectEnum; 30 | import com.webank.webasemonkey.tools.SqlNameUtils; 31 | import com.webank.webasemonkey.tools.StringStyleUtils; 32 | import com.webank.webasemonkey.vo.FieldVO; 33 | import com.webank.webasemonkey.vo.MethodMetaInfo; 34 | 35 | /** 36 | * MethodSqlRenderParas uses to installing params for generating sql scripts of method entities. 37 | * 38 | * @Description: MethodSqlRenderParas 39 | * @author graysonzhang 40 | * @data 2018-12-4 21:11:08 41 | * 42 | */ 43 | @Component 44 | public class MethodSqlRenderParas implements MethodGenerateParas { 45 | @Autowired 46 | protected SystemEnvironmentConfig systemEnvironmentConfig; 47 | @Autowired 48 | private SqlNameUtils sqlNameUtils; 49 | 50 | @Override 51 | public Map getMap(MethodMetaInfo method) { 52 | List list = method.getList(); 53 | List outputList = method.getOutputList(); 54 | Map map = Maps.newLinkedHashMap(); 55 | map.put("list", list); 56 | map.put("outputList", outputList); 57 | String tableName = sqlNameUtils.getSqlName(method.getContractName(), method.getName()) + "_method"; 58 | map.put("table_name", tableName); 59 | return map; 60 | } 61 | 62 | @Override 63 | public String getTemplatePath() { 64 | return TemplateConstants.SQL_METHOD_TEMPLATE_PATH; 65 | } 66 | 67 | @Override 68 | public String getGeneratedFilePath(MethodMetaInfo method) { 69 | String javaFilePath = SubProjectEnum.CORE.getPathName() + File.separator + "src/main/scripts/method/" 70 | + StringStyleUtils.upper2underline(method.getContractName()) + "_" 71 | + StringStyleUtils.upper2underline(method.getName()) + ".sql"; 72 | return javaFilePath; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/MethodBoRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Component; 23 | import org.springframework.util.StringUtils; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.MethodGenerateParas; 27 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 28 | import com.webank.webasemonkey.constants.PackageConstants; 29 | import com.webank.webasemonkey.constants.TemplateConstants; 30 | import com.webank.webasemonkey.tools.PackagePath; 31 | import com.webank.webasemonkey.vo.FieldVO; 32 | import com.webank.webasemonkey.vo.MethodMetaInfo; 33 | 34 | /** 35 | * MethodBoRenderParas 36 | * 37 | * @Description: MethodBoRenderParas 38 | * @author maojiayu 39 | * @data Jul 2, 2019 10:51:37 AM 40 | * 41 | */ 42 | @Component 43 | public class MethodBoRenderParas implements MethodGenerateParas { 44 | 45 | @Autowired 46 | protected SystemEnvironmentConfig systemEnvironmentConfig; 47 | 48 | @Override 49 | public Map getMap(MethodMetaInfo method) { 50 | List list = method.getList(); 51 | List outputList = method.getOutputList(); 52 | Map map = Maps.newLinkedHashMap(); 53 | map.put("list", list); 54 | map.put("outputList", outputList); 55 | String className = getClassName(method); 56 | map.put("class_name", className); 57 | map.put("group", systemEnvironmentConfig.getGroup()); 58 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_PARSER); 59 | return map; 60 | } 61 | 62 | @Override 63 | public String getTemplatePath() { 64 | return TemplateConstants.METHOD_BO_TEMPLATE_PATH; 65 | } 66 | 67 | @Override 68 | public String getGeneratedFilePath(MethodMetaInfo method) { 69 | String packagePath = PackagePath.getPackagePath(PackageConstants.METHOD_BO_PACKAGE_POSTFIX, 70 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_PARSER); 71 | String className = getClassName(method); 72 | String javaFilePath = packagePath + "/" + className + ".java"; 73 | return javaFilePath; 74 | } 75 | 76 | private String getClassName(MethodMetaInfo method) { 77 | return method.getContractName() + StringUtils.capitalize(method.getName()) + "BO"; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/EventEntityRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Component; 23 | 24 | import com.google.common.collect.Maps; 25 | import com.webank.webasemonkey.code.template.face.EventGenerateParas; 26 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 27 | import com.webank.webasemonkey.constants.PackageConstants; 28 | import com.webank.webasemonkey.constants.TemplateConstants; 29 | import com.webank.webasemonkey.tools.PackagePath; 30 | import com.webank.webasemonkey.tools.SqlNameUtils; 31 | import com.webank.webasemonkey.vo.EventMetaInfo; 32 | import com.webank.webasemonkey.vo.FieldVO; 33 | 34 | /** 35 | * EventEntityRenderParas to generate event entities. 36 | * 37 | * @author maojiayu 38 | * @data Dec 28, 2018 3:03:36 PM 39 | * 40 | */ 41 | @Component 42 | public class EventEntityRenderParas implements EventGenerateParas { 43 | @Autowired 44 | protected SystemEnvironmentConfig systemEnvironmentConfig; 45 | @Autowired 46 | private SqlNameUtils sqlNameUtils; 47 | 48 | @Override 49 | public Map getMap(EventMetaInfo event) { 50 | List list = event.getList(); 51 | Map map = Maps.newLinkedHashMap(); 52 | map.put("list", list); 53 | String className = event.getContractName() + event.getName(); 54 | String tableName = sqlNameUtils.getSqlName(event.getContractName(), event.getName()) + "_event"; 55 | map.put("table_name", systemEnvironmentConfig.getTablePrefix() + tableName + systemEnvironmentConfig.getTablePostfix()); 56 | map.put("class_name", className); 57 | map.put("group", systemEnvironmentConfig.getGroup()); 58 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_DB); 59 | return map; 60 | } 61 | 62 | @Override 63 | public String getTemplatePath() { 64 | return TemplateConstants.DB_EVENT_ENTITY_TEMPLATE_PATH; 65 | } 66 | 67 | @Override 68 | public String getGeneratedFilePath(EventMetaInfo event) { 69 | String packagePath = PackagePath.getPackagePath(PackageConstants.DB_EVENT_ENTRY_PACKAGE_POSTFIX, 70 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_DB); 71 | String className = event.getContractName() + event.getName(); 72 | String javaFilePath = packagePath + "/" + className + ".java"; 73 | return javaFilePath; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/constants/TemplateConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.constants; 17 | 18 | /** 19 | * TemplateConstants 20 | * 21 | * @Description: TemplateConstants 22 | * @author graysonzhang 23 | * @data 2018-11-12 16:20:46 24 | * 25 | */ 26 | public class TemplateConstants { 27 | 28 | /** @Fields CRAWLER_EVENT_IMPL_TEMPLATE_PATH : crawler event impl template path */ 29 | public static final String CRAWLER_EVENT_IMPL_TEMPLATE_PATH = "templates/crawler/crawler_event_impl_template.txt"; 30 | /** @Fields CRAWLER_METHOD_IMPL_TEMPLATE_PATH : crawler method impl template path */ 31 | public static final String CRAWLER_METHOD_IMPL_TEMPLATE_PATH = "templates/crawler/crawler_method_impl_template.txt"; 32 | 33 | public static final String EVENT_BO_TEMPLATE_PATH = "templates/bo/event_bo_template.txt"; 34 | public static final String METHOD_BO_TEMPLATE_PATH = "templates/bo/method_bo_template.txt"; 35 | 36 | /** @Fields DB_EVENT_ENTITY_TEMPLATE_PATH : event entity template path */ 37 | public static final String DB_EVENT_ENTITY_TEMPLATE_PATH = "templates/db/db_event_entity_template.txt"; 38 | /** @Fields DB_EVENT_REPOSITORY_TEMPLATE_PATH : event repository template path */ 39 | public static final String DB_EVENT_REPOSITORY_TEMPLATE_PATH = "templates/db/db_event_repository_template.txt"; 40 | /** @Fields SQL_EVENT_TEMPLATE_PATH : event sql template path */ 41 | public static final String SQL_EVENT_TEMPLATE_PATH = "templates/db/sql_event_template.sql"; 42 | 43 | /** @Fields DB_METHOD_ENTITY_TEMPLATE_PATH : method entity template path */ 44 | public static final String DB_METHOD_ENTITY_TEMPLATE_PATH = "templates/db/db_method_entity_template.txt"; 45 | /** @Fields DB_MEHTOD_REPOSITORY_TEMPLATE_PATH : method repository template path */ 46 | public static final String DB_MEHTOD_REPOSITORY_TEMPLATE_PATH = "templates/db/db_method_repository_template.txt"; 47 | /** @Fields SQL_METHOD_TEMPLATE_PATH : method sql template path */ 48 | public static final String SQL_METHOD_TEMPLATE_PATH = "templates/db/sql_method_template.sql"; 49 | 50 | /** @Fields APPLICATION_PROPERTIES_PATH : applicaiton properties path */ 51 | public static final String APPLICATION_PROPERTIES_PATH = "templates/config/application_properties.txt"; 52 | /** @Fields DB_PROPERTIES_PATH : db properties path */ 53 | public static final String DB_PROPERTIES_PATH = "templates/config/application-sharding-tables.txt"; 54 | 55 | public static final String GRAFANA_PANEL_TABLE_PATH = "templates/grafana/panel_table.txt"; 56 | public static final String GRAFANA_DASHBOARD_PATH = "templates/grafana/dashboard_default.txt"; 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/TypeReferenceUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | import org.fisco.bcos.web3j.abi.TypeReference; 20 | import org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition; 21 | import org.fisco.bcos.web3j.tx.txdecode.BaseException; 22 | import org.fisco.bcos.web3j.tx.txdecode.ContractTypeUtil; 23 | import org.fisco.bcos.web3j.tx.txdecode.DynamicArrayReference; 24 | import org.fisco.bcos.web3j.tx.txdecode.StaticArrayReference; 25 | 26 | import com.webank.webasemonkey.bo.JavaArrayTypeBO; 27 | import com.webank.webasemonkey.bo.JavaBasicTypeBO; 28 | 29 | import lombok.extern.slf4j.Slf4j; 30 | 31 | /** 32 | * TypeReferenceUtils 33 | * 34 | * @Description: TypeReferenceUtils 35 | * @author maojiayu 36 | * @data Apr 24, 2020 11:02:39 AM 37 | * 38 | */ 39 | @Slf4j 40 | public class TypeReferenceUtils { 41 | 42 | public static TypeReference getTypeRef(String solType) throws BaseException { 43 | AbiDefinition.NamedType.Type type = new AbiDefinition.NamedType.Type(solType); 44 | // nested array , not support now. 45 | if (type.getDepth() > 1) { 46 | throw new BaseException(201202, String.format("type:%s unsupported array decoding", type.getName())); 47 | } 48 | 49 | TypeReference typeReference = null; 50 | if (type.dynamicArray()) { 51 | typeReference = DynamicArrayReference.create(type.getBaseName(), false); 52 | } else if (type.staticArray()) { 53 | typeReference = StaticArrayReference.create(type.getBaseName(), type.getDimensions(), false); 54 | } else { 55 | typeReference = TypeReference.create(ContractTypeUtil.getType(solType), false); 56 | } 57 | return typeReference; 58 | } 59 | 60 | @SuppressWarnings("rawtypes") 61 | public static JavaBasicTypeBO convertType(String solType) { 62 | try { 63 | TypeReference t = getTypeRef(solType); 64 | String json = JacksonUtils.toJson(t); 65 | JavaBasicTypeBO bo = JacksonUtils.fromJson(json, JavaBasicTypeBO.class); 66 | if (StringUtils.contains(bo.getClassType(), "Array")) { 67 | JavaArrayTypeBO a = JacksonUtils.fromJson(json, JavaArrayTypeBO.class); 68 | a.setArray(true); 69 | return a; 70 | } else { 71 | bo.setArray(false); 72 | return bo; 73 | } 74 | 75 | } catch (BaseException e) { 76 | log.error("SolType {} can't be converted", solType); 77 | return null; 78 | } 79 | 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/EventCrawlImplParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Component; 23 | import org.springframework.util.StringUtils; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.EventGenerateParas; 27 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 28 | import com.webank.webasemonkey.constants.PackageConstants; 29 | import com.webank.webasemonkey.constants.TemplateConstants; 30 | import com.webank.webasemonkey.tools.PackagePath; 31 | import com.webank.webasemonkey.vo.EventMetaInfo; 32 | import com.webank.webasemonkey.vo.FieldVO; 33 | 34 | /** 35 | * EventCrawlImplParas to generate EventCrawler. 36 | * 37 | * @author maojiayu 38 | * @data Dec 28, 2018 2:59:25 PM 39 | * 40 | */ 41 | @Component 42 | public class EventCrawlImplParas implements EventGenerateParas { 43 | 44 | @Autowired 45 | protected SystemEnvironmentConfig systemEnvironmentConfig; 46 | 47 | @Override 48 | public Map getMap(EventMetaInfo event) { 49 | List list = event.getList(); 50 | Map map = Maps.newLinkedHashMap(); 51 | map.put("list", list); 52 | map.put("contractName", event.getContractName()); 53 | map.put("lowContractName", StringUtils.uncapitalize(event.getContractName())); 54 | map.put("group", systemEnvironmentConfig.getGroup()); 55 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_PARSER); 56 | map.put("contractPackName", systemEnvironmentConfig.getContractPackName()); 57 | map.put("eventName", event.getName()); 58 | String className = event.getContractName() + event.getName() + "BO"; 59 | map.put("class_name", className); 60 | return map; 61 | } 62 | 63 | @Override 64 | public String getTemplatePath() { 65 | return TemplateConstants.CRAWLER_EVENT_IMPL_TEMPLATE_PATH; 66 | } 67 | 68 | @Override 69 | public String getGeneratedFilePath(EventMetaInfo event) { 70 | String packagePath = PackagePath.getPackagePath(PackageConstants.CRAWLER_EVENT_IMPL_PACKAGE_POSTFIX, 71 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_PARSER); 72 | String javaFilePath = packagePath + "/" + event.getContractName() + StringUtils.capitalize(event.getName()) 73 | + "CrawlerImpl.java"; 74 | return javaFilePath; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem http://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/MethodCrawlerImplParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Component; 23 | import org.springframework.util.StringUtils; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.MethodGenerateParas; 27 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 28 | import com.webank.webasemonkey.constants.PackageConstants; 29 | import com.webank.webasemonkey.constants.TemplateConstants; 30 | import com.webank.webasemonkey.tools.PackagePath; 31 | import com.webank.webasemonkey.vo.FieldVO; 32 | import com.webank.webasemonkey.vo.MethodMetaInfo; 33 | 34 | /** 35 | * MethodCrawlerImplParas uses to installing params for generating method crawler class that implements 36 | * BcosMethodCrawlerInterface. 37 | * 38 | * @Description: MethodCrawlerImplParas 39 | * @author graysonzhang 40 | * @data 2018-12-11 12:16:24 41 | * 42 | */ 43 | @Component 44 | public class MethodCrawlerImplParas implements MethodGenerateParas { 45 | 46 | @Autowired 47 | protected SystemEnvironmentConfig systemEnvironmentConfig; 48 | 49 | @Override 50 | public Map getMap(MethodMetaInfo method) { 51 | List list = method.getList(); 52 | List outputList = method.getOutputList(); 53 | Map map = Maps.newLinkedHashMap(); 54 | map.put("list", list); 55 | map.put("outputList", outputList); 56 | String name = method.getContractName() + StringUtils.capitalize(method.getName()); 57 | map.put("contractName", method.getContractName()); 58 | map.put("methodName", name); 59 | map.put("oriMethodName", method.getName()); 60 | map.put("group", systemEnvironmentConfig.getGroup()); 61 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_PARSER); 62 | return map; 63 | } 64 | 65 | @Override 66 | public String getTemplatePath() { 67 | return TemplateConstants.CRAWLER_METHOD_IMPL_TEMPLATE_PATH; 68 | } 69 | 70 | @Override 71 | public String getGeneratedFilePath(MethodMetaInfo method) { 72 | String packagePath = PackagePath.getPackagePath(PackageConstants.CRAWLER_METHOD_IMPL_PACKAGE_POSTFIX, 73 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_PARSER); 74 | String className = method.getContractName() + StringUtils.capitalize(method.getName()); 75 | String javaFilePath = packagePath + "/" + className + "MethodCrawlerImpl.java"; 76 | return javaFilePath; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/service/CodeGenerateService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.service; 17 | 18 | import java.io.IOException; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Service; 23 | 24 | import com.webank.webasemonkey.code.template.CodeTemplateGenerateService; 25 | import com.webank.webasemonkey.code.template.GrafanaGenerateService; 26 | import com.webank.webasemonkey.code.template.face.ConfigGenerateParas; 27 | import com.webank.webasemonkey.code.template.face.EventGenerateParas; 28 | import com.webank.webasemonkey.code.template.face.MethodGenerateParas; 29 | import com.webank.webasemonkey.vo.ContractInfo; 30 | 31 | import lombok.extern.slf4j.Slf4j; 32 | 33 | /** 34 | * CodeGenerateService 35 | * 36 | * @Description: CodeGenerateService used to generate java code files for crawling block chain network data. 37 | * @author maojiayu 38 | * @data 2018-12-04 15:56:40 39 | * 40 | */ 41 | @Service 42 | @Slf4j 43 | public class CodeGenerateService { 44 | 45 | /** @Fields templateGenerateService : template generate service */ 46 | @SuppressWarnings("rawtypes") 47 | @Autowired 48 | private CodeTemplateGenerateService templateGenerateService; 49 | @Autowired 50 | private ContractInfoService contractInfoService; 51 | @Autowired 52 | private GrafanaGenerateService grafanaGenerateService; 53 | /** @Fields configParasMap : config params map for generating config files */ 54 | @Autowired 55 | private Map configParasMap; 56 | /** @Fields methodParasMap : method params map for generating code files of crawling method data */ 57 | @Autowired 58 | private Map methodParasMap; 59 | 60 | /** @Fields eventParasMap : event params map for generating code files of crawling event data */ 61 | @Autowired 62 | private Map eventParasMap; 63 | 64 | @SuppressWarnings("unchecked") 65 | public void generateBee() throws ClassNotFoundException, IOException { 66 | ContractInfo info = contractInfoService.parseFromContract(); 67 | // generate java code files for crawling event data from block chain network 68 | templateGenerateService.generate(info.getEventList(), eventParasMap); 69 | // generate java code files for crawling method data from blcok chain network 70 | templateGenerateService.generate(info.getMethodList(), methodParasMap); 71 | // generate config files for crawling data. 72 | templateGenerateService.generate(info, configParasMap); 73 | log.info("Begin to generate grafana dashboard json."); 74 | grafanaGenerateService.genereate(); 75 | log.info("Grafana json generation Finished!"); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/MethodEntityRenderParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.stereotype.Component; 23 | import org.springframework.util.StringUtils; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.MethodGenerateParas; 27 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 28 | import com.webank.webasemonkey.constants.PackageConstants; 29 | import com.webank.webasemonkey.constants.TemplateConstants; 30 | import com.webank.webasemonkey.tools.PackagePath; 31 | import com.webank.webasemonkey.tools.SqlNameUtils; 32 | import com.webank.webasemonkey.vo.FieldVO; 33 | import com.webank.webasemonkey.vo.MethodMetaInfo; 34 | 35 | /** 36 | * MethodEntityRenderParas uses to installing params for generating method entities. 37 | * 38 | * @Description: MethodEntityRenderParas 39 | * @author graysonzhang 40 | * @data 2018-12-04 16:42:29 41 | * 42 | */ 43 | @Component 44 | public class MethodEntityRenderParas implements MethodGenerateParas { 45 | 46 | @Autowired 47 | protected SystemEnvironmentConfig systemEnvironmentConfig; 48 | @Autowired 49 | private SqlNameUtils sqlNameUtils; 50 | 51 | @Override 52 | public Map getMap(MethodMetaInfo method) { 53 | List list = method.getList(); 54 | List outputList = method.getOutputList(); 55 | Map map = Maps.newLinkedHashMap(); 56 | map.put("list", list); 57 | map.put("outputList", outputList); 58 | String className = method.getContractName() + StringUtils.capitalize(method.getName()); 59 | String tableName = sqlNameUtils.getSqlName(method.getContractName(), method.getName()) + "_method"; 60 | map.put("table_name", systemEnvironmentConfig.getTablePrefix() + tableName + systemEnvironmentConfig.getTablePostfix()); 61 | map.put("class_name", className); 62 | map.put("group", systemEnvironmentConfig.getGroup()); 63 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_DB); 64 | return map; 65 | } 66 | 67 | @Override 68 | public String getTemplatePath() { 69 | return TemplateConstants.DB_METHOD_ENTITY_TEMPLATE_PATH; 70 | } 71 | 72 | @Override 73 | public String getGeneratedFilePath(MethodMetaInfo method) { 74 | String packagePath = PackagePath.getPackagePath(PackageConstants.DB_METHOD_ENTRY_PACKAGE_POSTFIX, 75 | systemEnvironmentConfig.getGroup(), PackageConstants.SUB_PROJECT_PKG_DB); 76 | String className = method.getContractName() + StringUtils.capitalize(method.getName()); 77 | String javaFilePath = packagePath + "/" + className + ".java"; 78 | return javaFilePath; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | ## 更新历史 2 | 3 | ### V1.3.2 (2020-07-01) 4 | - 增加配置生成表名的前缀和后缀的配置。 5 | - 升级web3sdk到2.4.2。 6 | - 一键运行脚本支持指定版本来编译及运行。 7 | - 修改Jackson和数据库的默认时区。 8 | - 添加ddl自动转义的配置。 9 | - 修复在Mac系统下脚本显示报错的问题。 10 | - 优化JPA快速查询的功能。 11 | 12 | ### V1.3.1 (2020-06-15) 13 | - 升级web3sdk到2.4.1 14 | - 升级Springboot到2.2.8 15 | - 删除fastjson 16 | 17 | ### V1.3.0 (2020-05-26) 18 | - 添加github勋章,包括coderefactor、travisci、codecov、synk、codacy等。 19 | - 升级bee的gradle版本 20 | - 升级web3sdk版本 21 | - 升级spring和junit版本 22 | - 升级shardingjdbc版本 23 | - 增加swagger开关,可通过配置关闭。 24 | - 增加国密/ECC的密码开关。 25 | - 增加对用户配置项的检查,增加执行过程中的出错检查,提升用户体验。 26 | - 修改项目的下载项,优化用户体验。 27 | - 支持通过config文件的方式配置solidity类型与Java类型的映射和个性化处理。 28 | - 修复grafana部分事件和日志无法展示的错误。 29 | - 优化部分系统表结构,使得占用空间更小。 30 | - 优化solidity转TypeReference的功能。 31 | - 去除bee项目编译中的部分报警信息。 32 | - 增加个性化测试配置,支持独立运行测试案例。 33 | - 修复部分event数据结构解析的错误。 34 | - 修复method导出无返回结果,修复method导出部分数组类型格式错误的问题。 35 | - 完善和修改文档中存在的问题。 36 | - 增加对Java文件配置和Java package命名配置的检查。 37 | - 修复国密配置下无法导出method数据的问题。 38 | 39 | ### V1.2.0 (2019-10-24) 40 | - 在导出的事件表,添加事件合约地址。 41 | - 在导出的函数表,添加函数执行响应码。 42 | - 增加int256定长数组支持。 43 | - 增加bytes32长定长数组支持。 44 | - 支持uint256数组支持 45 | - 增加对合约调用的事件导出的支持。 46 | - 增加对表结构字段长度的裁剪配置,支持对超过56位的表名进行裁剪。 47 | - 对is开头的变量进行裁剪转换。 48 | - 增加对fisco-bcos 2.1.0的支持。 49 | 50 | ### V1.1.0 (2019-08-20) 51 | - 大幅重构了系统的代码,使得系统安装多模块的方式可以灵活组装。 52 | - 重构了系统服务层的代码。 53 | - 适配了FISCO-BCOS2.0.0 和 web3sdk2.0.5 54 | - 修改了Java文件包名和类名映射的检查,修改了报错提示的方式 55 | - 新增两个Event API查询,支持通过指定任意and和or条件或任意两个参数进行查询。 56 | - 增加对method解析中selector方法忽略的判断 57 | - 适配DynamicBytes和Boolean的支持 58 | - 对所有method表,增加了contractAddress字段 59 | - 修复了sharding-jdbc-spring-boot-starter分页查询的问题 60 | - 修改了event表的命名方式,数据库表明以_event为后缀 61 | - 修改了account、constuctor method解析的方式,改为了读取runtime code来判断 62 | - 增加了对异构合约相同methodId场景的支持,将method解析改为按合约为维度 63 | - 新增了account specification API查询 64 | - 优化了部分性能,如优化了调用流程,添加了部分查询的EhCache机制 65 | - 修改了文档的组织样式 66 | - 支持了部分定长数组的映射方式 67 | - 支持了Java文件中,byte数组和List 68 | - 修改了服务启停的方式,增加了全路径判断,防止进程误杀 69 | - 修改了默认启动端口为5200 70 | - 修改了数据库默认字段添加前缀『_』 71 | - 修改了证书加载的默认方式,改为默认从config路径下加载 72 | - 修复了部分API参数为空的检测 73 | - 修复了byte数组转换错误的bug 74 | - 修复了默认分库inline表达式下不支持between的错误 75 | 76 | --- 77 | 78 | #### V1.0.0 (2019-06-27) 79 | - 优化了单机模式下的任务处理流程和性能。 80 | 81 | --- 82 | 83 | #### V0.7.0 (2019-06-20) 84 | - 适配FISCO-BCOS 2.0.0-rc3 85 | - 修复了不同环境下编译的合约无法导出account和contructor的缺陷 86 | - 重构了部分不规范日志打印方式 87 | - 重命名项目名,并适配 88 | - 去除了elastic-job的事件监听日志 89 | - 修复了不同编译器下构造函数解析的bug 90 | - 优化了单机模式的主流程和线程池,提升了性能 91 | - 修复了ubuntu的兼容问题 92 | 93 | --- 94 | 95 | #### V0.6.0 (2019-05-30) 96 | - 适配FISCO-BCOS 2.0 RC1。 97 | - 重构了主要的日志打印,便于显示和监控。 98 | - 新增了任务准备环节的事务控制。 99 | - 增加了对任务控制环节的扫描检查阶段,可自动检查缺块或少块等。 100 | - 修复了合约无event会报错的问题。 101 | - 将Mysql字符集从utf8改为utf8mb4。 102 | - 新增支持『原始数据命名』的数据库字段命名方式。 103 | - 新增支持数据库表的字段添加指定的前缀和后缀。 104 | - 提供了数据导出工具导出的监控脚本; 105 | 106 | --- 107 | 108 | #### V0.5.0 (2019-04-02) 109 | 110 | - 增加了数据导出的业务监控可视化页面,并支持自动化配置; 111 | - 重构数据回滚和异常处理的流程,增加了数据下载异常、超时的判断和处理; 112 | - 增加了链分叉判断的处理逻辑; 113 | - 重构了单机模式数据同步的流程和数据结构,合并为统一的block_task_pool表; 114 | - 支持设置从某个时间或某个区块高度导出区块数据; 115 | - 支持手动回滚并重新导出某个区块数据; 116 | 117 | --- 118 | 119 | #### V0.4.0 (2019-03-18) 120 | 121 | - 集成sharding-jdbc,支持分库分表导出 122 | - 集成elastic-job,支持集群部署 123 | - 支持静态数组StaticArray的导出 124 | 125 | --- 126 | 127 | #### V0.3.0 (2018-12-25) 128 | 129 | - 增加OpenAPI,支持以Restful的接口直接获取后台数据; 130 | - 支持Method数据导出的开关; 131 | - 修复各类已知bug; 132 | - 完善设计等文档; 133 | 134 | --- 135 | 136 | #### V0.2.0 (2018-12-21) 137 | 138 | - 增加Method数据depot功能; 139 | - 增加合约地址depot功能; 140 | - 修改任务分配方式,改为以Block为单位; 141 | - 支持智能下载的策略; 142 | - 支持Bytes等格式数据的解析; 143 | - 修改depot表的结构; 144 | 145 | --- 146 | 147 | #### V0.1.0 (2018-12-18) 148 | - 支持合约Event数据导出; 149 | - 支持根据脚本自动生成和执行; 150 | - 可基于多线程导出Events数据; 151 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/CodeTemplateGenerateService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template; 17 | 18 | import java.io.File; 19 | import java.io.IOException; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | import org.apache.commons.io.FileUtils; 24 | import org.beetl.core.GroupTemplate; 25 | import org.beetl.core.Template; 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.stereotype.Service; 28 | 29 | import com.webank.webasemonkey.code.template.face.GenerateParas; 30 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 31 | import com.webank.webasemonkey.vo.ContractNameVO; 32 | 33 | import lombok.extern.slf4j.Slf4j; 34 | 35 | /** 36 | * TemplateGenerateService 37 | * 38 | * @Description: TemplateGenerateService 39 | * @author maojiayu 40 | * @data 2018-12-19 14:51:00 41 | * 42 | */ 43 | @Slf4j 44 | @Service 45 | public class CodeTemplateGenerateService { 46 | 47 | /** @Fields gt : group template */ 48 | @Autowired 49 | protected GroupTemplate gt; 50 | @Autowired 51 | protected SystemEnvironmentConfig systemEnvironmentConfig; 52 | 53 | /** 54 | * write generated files to corresponding packages. 55 | * 56 | * @param generateParas: paras for writing files 57 | * @param vo 58 | * @return void 59 | */ 60 | @SuppressWarnings({ "unchecked", "rawtypes" }) 61 | public void write(GenerateParas generateParas, ContractNameVO vo) { 62 | Template template = gt.getTemplate(generateParas.getTemplatePath()); 63 | template.binding(generateParas.getMap(vo)); 64 | String renderResult = template.render(); 65 | try { 66 | String filePath = systemEnvironmentConfig.getBaseProjectPath() + File.separator 67 | + generateParas.getGeneratedFilePath(vo); 68 | FileUtils.write(new File(filePath), renderResult, "utf-8", false); 69 | log.info("Write succeed: {}", filePath); 70 | } catch (IOException e) { 71 | log.error("GeneratedFiles write fail. cause: {}", e.getMessage()); 72 | } 73 | } 74 | 75 | /** 76 | * create java code files for crawling data from block chain network and write to corresponding packages. 77 | * 78 | * @param clazz: contract class 79 | * @param parser: methodParser or eventParser 80 | * @param map 81 | * @return List 82 | */ 83 | public void generate(List metaInfos, Map map) { 84 | for (T mataInfo : metaInfos) { 85 | map.forEach((k, v) -> { 86 | write(v, mataInfo); 87 | }); 88 | } 89 | } 90 | 91 | /** 92 | * create config files for crawling data from block chain network and write to corresponding packages. 93 | * 94 | * @param config 95 | * @param map 96 | * @return void 97 | */ 98 | public void generate(T config, Map map) { 99 | map.forEach((k, v) -> { 100 | write(v, config); 101 | }); 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WeBASE-Codegen-Monkey 2 | 3 | [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 4 | [![Gitter](https://badges.gitter.im/WeBASE-Codegen-Monkey/WeBASE-Codegen-Monkey.svg)](https://gitter.im/webase-monkey/community) 5 | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/bd49c17906cd42f69fb1f6b1fa8c6760)](https://www.codacy.com/manual/dalaocu/WeBASE-Codegen-Monkey?utm_source=github.com&utm_medium=referral&utm_content=WeBankFinTech/WeBASE-Codegen-Monkey&utm_campaign=Badge_Grade) 6 | [![CodeFactor](https://www.codefactor.io/repository/github/webankfintech/webase-codegen-monkey/badge)](https://www.codefactor.io/repository/github/webankfintech/webase-codegen-monkey) 7 | [![codecov](https://codecov.io/gh/WeBankFinTech/WeBASE-Codegen-Monkey/branch/code_refactor_2020.01/graph/badge.svg)](https://codecov.io/gh/WeBankFinTech/WeBASE-Codegen-Monkey) 8 | [![buildStatus](https://travis-ci.org/WeBankFinTech/WeBASE-Codegen-Monkey.svg?branch=master)](https://travis-ci.org/WeBankFinTech/WeBASE-Codegen-Monkey) 9 | [![Documentation Status](https://readthedocs.org/projects/webasedoc/badge/?version=latest)](https://webasedoc.readthedocs.io/zh_CN/latest/docs/WeBASE-Codegen-Monkey/index.html) 10 | [![snyk](https://snyk.io/test/github/WeBankFinTech/WeBASE-Codegen-Monkey/badge.svg?targetFile=build.gradle)](https://snyk.io/test/github/WeBankFinTech/WeBASE-Codegen-Monkey?targetFile=build.gradle&tab=issues) 11 | [![Total Lines](https://tokei.rs/b1/github/WeBankFinTech/WeBASE-Codegen-Monkey?category=lines)](https://github.com/WeBankFinTech/WeBASE-Codegen-Monkey) 12 | [![Latest release](https://img.shields.io/github/release/WeBankFinTech/WeBASE-Codegen-Monkey.svg)](https://github.com/WeBankFinTech/WeBASE-Codegen-Monkey/releases/latest) 13 | 14 | > 道生一,一生二,二生三,三生万物。 15 | > 万物负阴而抱阳,冲气以为和。 16 | > 人之所恶,唯孤、寡、不谷,而王公以为称。 17 | > 故物或损之而益,或益之而损。 18 | > 人之所教,亦我而教人。 19 | > 强梁者不得其死——吾将以为教父。 20 | > -- 老子 21 | 22 | **目前本项目处于维护模式,新特性和新功能请移步:** https://github.com/WeBankBlockchain/Data-Export 23 | 24 | 代码自动生成组件:WeBASE-Codegen-Monkey是WeBASE数据导出工具的代码生成组件,可帮助用户自动生成基于[FISCO BCOS](https://github.com/FISCO-BCOS/FISCO-BCOS/tree/master)的数据导出组件[WeBASE-Collect-Bee](https://github.com/WeBankFinTech/WeBASE-Collect-Bee/tree/master)。 25 | 26 | 只需要在一个配置文件中进行少量简单的配置,同时按照要求提供相关的智能合约信息;当前版本可支持自动生成[WeBASE-Collect-Bee](https://github.com/WeBankFinTech/WeBASE-Collect-Bee/tree/master)。 27 | 28 | **此版本只支持**[FISCO BCOS 2.0](https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/)。 29 | 30 | ## 关键特性 31 | 32 | - 自动生成数据导出组件 33 | 34 | - 支持自定义导出数据内容 35 | 36 | - 内置Restful API,提供常用的查询功能 37 | 38 | - 支持多数据源,支持读写分离和分库分表 39 | 40 | - 支持多活部署,多节点自动导出 41 | 42 | - 支持区块重置导出 43 | 44 | - 支持可视化的监控页面 45 | 46 | - 提供可视化的互动API控制台 47 | 48 | ## 环境要求 49 | 50 | 在使用本组件前,请确认系统环境已安装相关依赖软件,清单如下: 51 | 52 | | 依赖软件 | 说明 |备注| 53 | | --- | --- | --- | 54 | | FISCO-BCOS | >= 2.0, 1.x版本请参考V0.5版本 dev分支| 55 | | Bash | 需支持Bash(理论上来说支持所有ksh、zsh等其他unix shell,但未测试)| 56 | | Java | >= JDK[1.8] || 57 | | Git | 下载的安装包使用Git | | 58 | | MySQL | >= mysql-community-server[5.7] | 理论上来说支持主流数据库,但未测试| 59 | | zookeeper | >= zookeeper[3.4] | 只有在进行集群部署的时候需要安装| 60 | | docker | >= docker[18.0.0] | 只有需要可视化监控页面的时候才需要安装| 61 | 62 | ## 文档 63 | - [**中文**](https://webasedoc.readthedocs.io/zh_CN/latest/docs/WeBASE-Codegen-Monkey/index.html) 64 | - [**快速安装**](https://webasedoc.readthedocs.io/zh_CN/latest/docs/WeBASE-Codegen-Monkey/install.html#) 65 | 66 | ## 贡献代码 67 | 欢迎参与本项目的社区建设: 68 | - 如项目对您有帮助,欢迎点亮我们的小星星(点击项目左上方Star按钮)。 69 | - 欢迎提交代码(Pull requests)。 70 | - [提问和提交BUG](https://github.com/WeBankFinTech/WeBASE-Codegen-Monkey/issues)。 71 | - 如果发现代码存在安全漏洞,请在[这里](https://security.webank.com)上报。 72 | 73 | ## 加入我们的社区 74 | 75 | FISCO BCOS开源社区是国内活跃的开源社区,社区长期为机构和个人开发者提供各类支持与帮助。已有来自各行业的数千名技术爱好者在研究和使用FISCO BCOS。如您对FISCO BCOS开源技术及应用感兴趣,欢迎加入社区获得更多支持与帮助。 76 | 77 | ![](https://media.githubusercontent.com/media/FISCO-BCOS/LargeFiles/master/images/QR_image.png) 78 | 79 | ## License 80 | ![license](http://img.shields.io/badge/license-Apache%20v2-blue.svg) 81 | 82 | 开源协议为[Apache License 2.0](http://www.apache.org/licenses/). 详情参考[LICENSE](../LICENSE)。 83 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/StringStyleUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import java.util.PrimitiveIterator.OfInt; 19 | import java.util.stream.IntStream; 20 | 21 | import javax.annotation.PostConstruct; 22 | 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.stereotype.Component; 25 | 26 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 27 | import com.webank.webasemonkey.enums.NameStyleEnum; 28 | 29 | /** 30 | * StringStyleUtils 31 | * 32 | * @Description: StringStyleUtils 33 | * @author maojiayu 34 | * @data Dec 28, 2018 4:11:29 PM 35 | * 36 | */ 37 | @Component 38 | public class StringStyleUtils { 39 | 40 | @Autowired 41 | private SystemEnvironmentConfig systemEnvironmentConfig; 42 | 43 | private static String nameStyle; 44 | 45 | @SuppressWarnings("static-access") 46 | @PostConstruct 47 | public void init() { 48 | this.nameStyle = systemEnvironmentConfig.getNameStyle(); 49 | } 50 | 51 | /** 52 | * Camel -> underline e.g. aBc -> a_bc 53 | * 54 | * @param str 55 | * @return 56 | */ 57 | public static String upper2underline(String str) { 58 | if (nameStyle.equalsIgnoreCase(NameStyleEnum.RAW_CASE.getStyle())) { 59 | return str; 60 | } 61 | if (str.length() == 1) { 62 | return str.toLowerCase(); 63 | } 64 | StringBuilder sb = new StringBuilder(); 65 | str.chars().forEach(c -> { 66 | char cc = (char) c; 67 | if (cc >= 'A' && cc <= 'Z') { 68 | sb.append("_").append((char) (cc + 32)); // 32 = 'a' - 'A' 69 | } else { 70 | sb.append(cc); 71 | } 72 | }); 73 | if (sb.indexOf("_") == 0) { 74 | sb.deleteCharAt(0); 75 | } 76 | String r = sb.toString(); 77 | return r.replaceAll("__", "_"); 78 | } 79 | 80 | /** 81 | * underline -> camel e.g. __a_bc_d__e_ -> _ABcD_E_ 82 | * 83 | * @param str 84 | * @return 85 | */ 86 | public static String underline2upper(String str) { 87 | StringBuilder sb = new StringBuilder(); 88 | boolean mode = false; 89 | IntStream intStream = str.chars(); 90 | OfInt iterator = intStream.iterator(); 91 | while (iterator.hasNext()) { 92 | int c = iterator.nextInt(); 93 | char cc = (char) c; 94 | if (mode) { 95 | if (cc >= 'a' && cc <= 'z') { 96 | sb.append(((char) (cc - 32))); 97 | mode = false; 98 | continue; 99 | } 100 | if (cc == '_') { 101 | sb.append('_'); 102 | continue; 103 | } 104 | sb.append((char) cc); 105 | mode = false; 106 | } else { 107 | if (cc == '_') { 108 | mode = true; 109 | } else { 110 | sb.append((char) cc); 111 | } 112 | } 113 | } 114 | if (mode) { 115 | sb.append('_'); 116 | } 117 | return sb.toString(); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/config/BeanConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.config; 17 | 18 | import java.io.File; 19 | import java.io.IOException; 20 | import java.util.HashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | import org.apache.commons.collections4.CollectionUtils; 25 | import org.apache.commons.io.FileUtils; 26 | import org.apache.commons.lang.StringUtils; 27 | import org.beetl.core.GroupTemplate; 28 | import org.beetl.core.resource.ClasspathResourceLoader; 29 | import org.springframework.context.annotation.Bean; 30 | import org.springframework.context.annotation.Configuration; 31 | import org.springframework.context.annotation.Profile; 32 | import org.springframework.core.io.Resource; 33 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 34 | 35 | import com.webank.webasemonkey.tools.JacksonUtils; 36 | import com.webank.webasemonkey.vo.Web3jTypeVO; 37 | 38 | import cn.hutool.core.io.resource.ClassPathResource; 39 | import lombok.extern.slf4j.Slf4j; 40 | 41 | /** 42 | * BeanConfig 43 | * 44 | * @Description: BeanConfig 45 | * @author maojiayu 46 | * @data Dec 28, 2018 3:08:50 PM 47 | * 48 | */ 49 | @Configuration 50 | @Slf4j 51 | public class BeanConfig { 52 | 53 | /** 54 | * Beetl render template. 55 | * 56 | * @return GroupTemplate 57 | * @throws IOException 58 | */ 59 | @Bean 60 | public GroupTemplate getGroupTemplateInstance() throws IOException { 61 | ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(""); 62 | org.beetl.core.Configuration cfg = org.beetl.core.Configuration.defaultConfiguration(); 63 | GroupTemplate gt = new GroupTemplate(resourceLoader, cfg); 64 | return gt; 65 | } 66 | 67 | /** 68 | * Load class path resource. 69 | * 70 | * @return ClassPathResource 71 | */ 72 | @Bean 73 | @Profile("!test") 74 | public ClassPathResource getClassPathResource() { 75 | return new ClassPathResource("application.properties"); 76 | } 77 | 78 | @Bean 79 | @Profile("test") 80 | public ClassPathResource getTestClassPathResource() { 81 | return new ClassPathResource("application-test.properties"); 82 | } 83 | 84 | @Bean 85 | public Map getCustomDefineWeb3jMap() throws IOException { 86 | PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 87 | Resource def = resolver.getResource("file:config/web3j.def"); 88 | File defFile = def.getFile(); 89 | Map map = new HashMap(); 90 | if (defFile.exists()) { 91 | log.info("defFile detect."); 92 | List lines = FileUtils.readLines(defFile, "utf8"); 93 | if (!CollectionUtils.isEmpty(lines)) { 94 | for (String line : lines) { 95 | line = line.replaceAll("\"", ""); 96 | String[] tokens = StringUtils.split(line, ","); 97 | if (tokens.length < 4) { 98 | continue; 99 | } 100 | Web3jTypeVO vo = new Web3jTypeVO(); 101 | vo.setSolidityType(tokens[0]).setSqlType(tokens[1]).setJavaType(tokens[2]).setTypeMethod(tokens[3]); 102 | map.put(tokens[0], vo); 103 | log.info("Find Web3j type definetion : {}", JacksonUtils.toJson(vo)); 104 | } 105 | } 106 | } 107 | return map; 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/ApplicationPropParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.io.File; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.stereotype.Component; 24 | 25 | import com.google.common.collect.Maps; 26 | import com.webank.webasemonkey.code.template.face.ConfigGenerateParas; 27 | import com.webank.webasemonkey.config.ButtonEnvironmentConfig; 28 | import com.webank.webasemonkey.config.ServerConfig; 29 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 30 | import com.webank.webasemonkey.config.ZookeeperConfig; 31 | import com.webank.webasemonkey.constants.ConfigConstants; 32 | import com.webank.webasemonkey.constants.ConfigFileConstants; 33 | import com.webank.webasemonkey.constants.PackageConstants; 34 | import com.webank.webasemonkey.constants.TemplateConstants; 35 | import com.webank.webasemonkey.enums.SubProjectEnum; 36 | import com.webank.webasemonkey.vo.ContractInfo; 37 | import com.webank.webasemonkey.vo.EventMetaInfo; 38 | import com.webank.webasemonkey.vo.MethodMetaInfo; 39 | 40 | /** 41 | * ApplicationPropParas to generate application.properties. 42 | * 43 | * @author maojiayu 44 | * @data Dec 28, 2018 2:47:01 PM 45 | * 46 | */ 47 | @Component 48 | public class ApplicationPropParas implements ConfigGenerateParas { 49 | 50 | @Autowired 51 | private SystemEnvironmentConfig systemEnvironmentConfig; 52 | @Autowired 53 | private ButtonEnvironmentConfig buttonEnvironmentConfig; 54 | @Autowired 55 | private ServerConfig serverConfig; 56 | @Autowired 57 | private ZookeeperConfig zookeeperConfig; 58 | 59 | @Override 60 | public Map getMap(ContractInfo info) { 61 | List eventList = info.getEventList(); 62 | List methodList = info.getMethodList(); 63 | Map map = Maps.newLinkedHashMap(); 64 | map.put("eventList", eventList); 65 | map.put("methodList", methodList); 66 | map.put("port", serverConfig.getPort()); 67 | map.put("nodeStr", systemEnvironmentConfig.getNodeStr()); 68 | map.put("groupId", systemEnvironmentConfig.getGroupId()); 69 | map.put("group", systemEnvironmentConfig.getGroup()); 70 | map.put("projectName", PackageConstants.PROJECT_PKG_NAME + "." + PackageConstants.SUB_PROJECT_PKG_CORE); 71 | map.put("multiLiving", systemEnvironmentConfig.getMultiLiving()); 72 | map.put("contractPackName", systemEnvironmentConfig.getContractPackName()); 73 | map.put("contractPath", ConfigConstants.CONTRACT_PATH); 74 | map.put("crawlBatchUnit", systemEnvironmentConfig.getCrawlBatchUnit()); 75 | map.put("frequency", systemEnvironmentConfig.getFrequency()); 76 | map.put("serverListStr", zookeeperConfig.getServerList()); 77 | map.put("nameSpace", zookeeperConfig.getNamespace()); 78 | map.put("startBlockHeight", systemEnvironmentConfig.getStartBlockHeight()); 79 | map.put("startDate", systemEnvironmentConfig.getStartDate()); 80 | map.put("encryptType", systemEnvironmentConfig.getEncryptType()); 81 | 82 | map.put("swagger", buttonEnvironmentConfig.getSwagger()); 83 | 84 | return map; 85 | } 86 | 87 | @Override 88 | public String getTemplatePath() { 89 | return TemplateConstants.APPLICATION_PROPERTIES_PATH; 90 | } 91 | 92 | @Override 93 | public String getGeneratedFilePath(ContractInfo info) { 94 | return SubProjectEnum.CORE.getPathName() + File.separator 95 | + ConfigFileConstants.GENERATED_APPLICATION_PROPERTIES_FILE_PATH; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/PropertiesUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import java.io.IOException; 19 | import java.util.Iterator; 20 | import java.util.Properties; 21 | 22 | import javax.annotation.PostConstruct; 23 | 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.stereotype.Component; 26 | 27 | import cn.hutool.core.io.resource.ClassPathResource; 28 | import cn.hutool.core.util.StrUtil; 29 | import lombok.extern.slf4j.Slf4j; 30 | 31 | /** 32 | * PropertiesUtils 33 | * 34 | * @Description: PropertiesUtils 35 | * @author maojiayu 36 | * @data Dec 28, 2018 4:10:49 PM 37 | * 38 | */ 39 | @Slf4j 40 | @Component 41 | public class PropertiesUtils { 42 | 43 | @Autowired 44 | private ClassPathResource autoResource; 45 | 46 | private static ClassPathResource resource; 47 | 48 | @PostConstruct 49 | private void init() { 50 | resource = this.autoResource; 51 | } 52 | 53 | /** 54 | * return the first mapping result of args. 55 | * 56 | * 57 | * @param property key 58 | * @return property value 59 | */ 60 | public static String getProperty(String...args) { 61 | Properties properties = new Properties(); 62 | try { 63 | properties.load(resource.getStream()); 64 | for (String key : args) { 65 | Iterator ite = properties.keySet().iterator(); 66 | while (ite.hasNext()) { 67 | String s = (String) ite.next(); 68 | if (key.equalsIgnoreCase(s)) { 69 | return properties.getProperty(s); 70 | } 71 | } 72 | } 73 | } catch (IOException e) { 74 | log.error("getProperty error {}", e.getMessage()); 75 | } 76 | return args[args.length - 1]; 77 | } 78 | 79 | /** 80 | * get the very specific value of settings. If the field is not set, then return the default value. 81 | * 82 | * @param contractName 83 | * @param eventName 84 | * @param feature 85 | * @param defaultValue 86 | * @return 87 | */ 88 | public static String getGlobalProperty(String type, String contractName, String eventName, String feature, 89 | String defaultValue) { 90 | String template = "{}.{}.{}"; 91 | String eventTemplate = "{}.{}.{}.{}"; 92 | String eventConfig = StrUtil.format(eventTemplate, type, contractName, eventName, feature); 93 | String contractConfig = StrUtil.format(template, type, contractName, feature); 94 | String defaultConfig = StrUtil.format(template, type, "default", feature); 95 | return PropertiesUtils.getProperty(eventConfig, contractConfig, defaultConfig, defaultValue); 96 | } 97 | 98 | /** 99 | * get the very specific value of settings without a default value. 100 | * 101 | * return the specific config value, eg. return event property prefer to contract value. 102 | * 103 | * @param contractName 104 | * @param eventName 105 | * @param feature 106 | * @return 107 | */ 108 | public static String getPropertyWithoutDefault(String type, String contractName, String eventName, String feature) { 109 | String contractTemplate = "{}.{}.{}"; 110 | String eventTemplate = "{}.{}.{}.{}"; 111 | String eventConfig = StrUtil.format(eventTemplate, type, contractName, eventName, feature); 112 | String contractConfig = StrUtil.format(contractTemplate, type, contractName, feature); 113 | return PropertiesUtils.getProperty(eventConfig, contractConfig); 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/main/resources/templates/crawler/crawler_method_impl_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.crawler.method; 5 | 6 | 7 | import java.math.BigInteger; 8 | import java.util.Date; 9 | import java.util.List; 10 | import java.util.Map; 11 | import org.apache.commons.collections4.CollectionUtils; 12 | import org.fisco.bcos.web3j.abi.FunctionReturnDecoder; 13 | import org.fisco.bcos.web3j.abi.datatypes.Type; 14 | import org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType; 15 | import org.fisco.bcos.web3j.protocol.core.methods.response.Transaction; 16 | import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt; 17 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 18 | import org.springframework.stereotype.Service; 19 | import ${group}.${projectName}.crawler.face.BcosMethodCrawlerInterface; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import com.webank.webasebee.common.constants.ContractConstants; 22 | import com.webank.webasebee.common.tools.AddressUtils; 23 | import com.webank.webasebee.common.tools.BigIntegerUtils; 24 | import com.webank.webasebee.common.tools.BoolUtils; 25 | import com.webank.webasebee.common.tools.BytesUtils; 26 | import com.webank.webasebee.common.tools.JacksonUtils; 27 | import com.webank.webasebee.common.tools.MethodUtils; 28 | import lombok.extern.slf4j.Slf4j; 29 | import ${group}.webasebee.common.bo.contract.ContractMapsInfo; 30 | import ${group}.webasebee.common.bo.data.MethodBO; 31 | import ${group}.${projectName}.generated.bo.method.${methodName}BO; 32 | 33 | @Slf4j 34 | @Service 35 | @ConditionalOnProperty(name = "monitor.${contractName}.${oriMethodName}MethodCrawlerService", havingValue = "on") 36 | public class ${methodName}MethodCrawlerImpl implements BcosMethodCrawlerInterface { 37 | 38 | @Autowired 39 | private ContractMapsInfo contractMapsInfo; 40 | 41 | @Override 42 | public MethodBO transactionHandler(Transaction transaction, TransactionReceipt receipt, BigInteger blockTimeStamp, 43 | Map.Entry entry, String methodName, Map txHashContractAddress) { 44 | log.debug("Begin process ${methodName} Transaction"); 45 | ${methodName}BO entity = new ${methodName}BO(); 46 | entity.setTxHash(transaction.getHash()); 47 | entity.setBlockHeight(transaction.getBlockNumber().longValue()); 48 | 49 | String input = transaction.getInput(); 50 | String mapKey = null; 51 | if(transaction.getTo() == null || transaction.getTo().equals(ContractConstants.EMPTY_ADDRESS)){ 52 | input = input.substring(2 + entry.getKey().length()); 53 | mapKey = entry.getValue() + "_" + entry.getValue(); 54 | entity.setContractAddress(txHashContractAddress.get(entity.getTxHash())); 55 | }else{ 56 | input = input.substring(10); 57 | mapKey = entry.getValue() + "_" + methodName; 58 | entity.setContractAddress(transaction.getTo()); 59 | } 60 | 61 | log.debug("input : {}", input); 62 | 63 | List list = contractMapsInfo.getMethodFiledsMap().get(methodName); 64 | if (!CollectionUtils.isEmpty(list)) { 65 | List params = FunctionReturnDecoder.decode(input, MethodUtils.getMethodTypeReferenceList(list)); 66 | 67 | <% 68 | for(var index = 0; index < list.~size; index++){ 69 | var javaCapName = list[index].javaCapName; 70 | var typeMethod = list[index].typeMethod; 71 | %> 72 | entity.set${javaCapName}(${typeMethod}(params.get(${index}).getValue())); 73 | <% } %> 74 | } 75 | 76 | List outputList = contractMapsInfo.getOutputMethodFiledsMap().get(mapKey); 77 | if (!CollectionUtils.isEmpty(outputList) && receipt.getStatus().equals("0x0")) { 78 | List outputParams = FunctionReturnDecoder.decode(receipt.getOutput(), 79 | MethodUtils.getMethodTypeReferenceList(outputList)); 80 | <% 81 | for(var index = 0; index < outputList.~size; index++){ 82 | var javaCapName = outputList[index].javaCapName; 83 | var typeMethod = outputList[index].typeMethod; 84 | %> 85 | entity.set${javaCapName}(${typeMethod}(outputParams.get(${index}).getValue())); 86 | <% } %> 87 | } 88 | 89 | 90 | 91 | entity.setBlockTimeStamp(new Date(blockTimeStamp.longValue())); 92 | entity.setIdentifier("${methodName}"); 93 | 94 | log.debug("end process ${methodName} Transaction"); 95 | return entity; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/service/ContractInfoService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.service; 17 | 18 | import java.io.IOException; 19 | import java.util.Arrays; 20 | import java.util.List; 21 | import java.util.Set; 22 | import java.util.stream.Collectors; 23 | 24 | import org.apache.commons.collections4.CollectionUtils; 25 | import org.apache.commons.lang3.StringUtils; 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.stereotype.Service; 28 | 29 | import com.google.common.collect.Lists; 30 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 31 | import com.webank.webasemonkey.constants.ConfigConstants; 32 | import com.webank.webasemonkey.parser.EventParser; 33 | import com.webank.webasemonkey.parser.MethodParser; 34 | import com.webank.webasemonkey.tools.ClazzScanUtils; 35 | import com.webank.webasemonkey.vo.ContractInfo; 36 | import com.webank.webasemonkey.vo.EventMetaInfo; 37 | import com.webank.webasemonkey.vo.MethodMetaInfo; 38 | 39 | /** 40 | * ContractInfoService 41 | * 42 | * @Description: ContractInfoService 43 | * @author maojiayu 44 | * @data Mar 27, 2019 12:07:48 PM 45 | * 46 | */ 47 | @Service 48 | public class ContractInfoService { 49 | /** @Fields monitorGeneratedConfig : monitor generated config params */ 50 | @Autowired 51 | private SystemEnvironmentConfig systemEnvironmentConfig; 52 | 53 | /** @Fields methodParser : parsing contract method and get method params */ 54 | @Autowired 55 | private MethodParser methodParser; 56 | 57 | /** @Fields eventParser : parsing contract event and get event params */ 58 | @Autowired 59 | private EventParser eventParser; 60 | 61 | /** 62 | * Scan all contracts file from contracts' package, get event meta info list and method meta info list. 63 | * 64 | * @return void 65 | * @throws ClassNotFoundException 66 | * @throws IOException 67 | */ 68 | public ContractInfo parseFromContract() throws ClassNotFoundException, IOException { 69 | Set> clazzSet = scanContract(); 70 | List eventMetaInfoList = Lists.newArrayList(); 71 | List methodMetaInfoList = Lists.newArrayList(); 72 | for (Class clazz : clazzSet) { 73 | // generate java code files for crawling event data from block chain network 74 | List el = eventParser.parseToInfoList(clazz); 75 | // generate java code files for crawling method data from blcok chain network 76 | List ml = methodParser.parseToInfoList(clazz); 77 | eventMetaInfoList.addAll(el); 78 | methodMetaInfoList.addAll(ml); 79 | } 80 | ContractInfo info = new ContractInfo().setEventList(eventMetaInfoList).setMethodList(methodMetaInfoList); 81 | return info; 82 | } 83 | 84 | private Set> scanContract() throws ClassNotFoundException, IOException { 85 | Set> clazzSet = ClazzScanUtils.scan(ConfigConstants.CONTRACT_PATH, 86 | systemEnvironmentConfig.getContractPackName()); 87 | if (CollectionUtils.isNotEmpty(clazzSet)) { 88 | return clazzSet; 89 | } 90 | clazzSet = ClazzScanUtils.scanJar(ConfigConstants.CONTRACT_PATH, 91 | systemEnvironmentConfig.getContractPackName()); 92 | if (clazzSet != null && StringUtils.isNotBlank(systemEnvironmentConfig.getContractName())) { 93 | return clazzSet.stream() 94 | .filter(clz -> 95 | Arrays.asList(StringUtils.split(systemEnvironmentConfig.getContractName(), ",")) 96 | .contains(clz.getSimpleName())) 97 | .collect(Collectors.toSet()); 98 | } 99 | return clazzSet; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/GrafanaGenerateService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template; 17 | 18 | import java.io.File; 19 | import java.io.IOException; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | import org.apache.commons.io.FileUtils; 24 | import org.beetl.core.GroupTemplate; 25 | import org.beetl.core.Template; 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.stereotype.Service; 28 | 29 | import com.google.common.collect.Lists; 30 | import com.webank.webasemonkey.code.service.ContractInfoService; 31 | import com.webank.webasemonkey.code.template.paras.GrafanaDashboardParas; 32 | import com.webank.webasemonkey.code.template.paras.GrafanaPanelTableParas; 33 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 34 | import com.webank.webasemonkey.vo.ContractInfo; 35 | import com.webank.webasemonkey.vo.ContractStructureMetaInfo; 36 | 37 | import lombok.extern.slf4j.Slf4j; 38 | 39 | /** 40 | * GrafanaGenerateService 41 | * 42 | * @Description: GrafanaGenerateService 43 | * @author maojiayu 44 | * @data Mar 27, 2019 5:25:27 PM 45 | * 46 | */ 47 | @Service 48 | @Slf4j 49 | public class GrafanaGenerateService { 50 | /** @Fields gt : group template */ 51 | @Autowired 52 | protected GroupTemplate gt; 53 | @Autowired 54 | protected SystemEnvironmentConfig systemEnvironmentConfig; 55 | @Autowired 56 | private ContractInfoService contractInfoService; 57 | @Autowired 58 | private GrafanaPanelTableParas panelPara; 59 | @Autowired 60 | private GrafanaDashboardParas grafanaDashboardParas; 61 | 62 | public void genereate() throws ClassNotFoundException, IOException { 63 | List panels = Lists.newArrayList(); 64 | ContractInfo info = contractInfoService.parseFromContract(); 65 | List tablesList = Lists.newArrayList(); 66 | tablesList.addAll(info.getEventList()); 67 | tablesList.addAll(info.getMethodList()); 68 | Template template = gt.getTemplate(panelPara.getTemplatePath()); 69 | int index = 1; 70 | for (ContractStructureMetaInfo e : tablesList) { 71 | Map map = panelPara.getMap(e); 72 | map.put("gridXPos", getX(index)); 73 | map.put("gridYPos", getY(index)); 74 | map.put("id", getId(index)); 75 | template.binding(map); 76 | String panelStr = template.render(); 77 | panels.add(panelStr); 78 | index++; 79 | } 80 | Map map = grafanaDashboardParas.getMap(panels); 81 | Template dashboardTemplate = gt.getTemplate(grafanaDashboardParas.getTemplatePath()); 82 | dashboardTemplate.binding(map); 83 | String dashboardStr = dashboardTemplate.render(); 84 | write(dashboardStr); 85 | 86 | } 87 | 88 | public void write(String dashboardStr) { 89 | try { 90 | log.info("Begin to create grafana file: {}", grafanaDashboardParas.getGeneratedFilePath()); 91 | FileUtils.write(new File(systemEnvironmentConfig.getBaseProjectPath() + File.separator 92 | + grafanaDashboardParas.getGeneratedFilePath()), dashboardStr, "utf-8", false); 93 | log.info("Write succeed: {}", grafanaDashboardParas.getGeneratedFilePath()); 94 | } catch (IOException e1) { 95 | log.error("Grafana Dashboard json file create. Error occourred: {}", e1.getMessage()); 96 | } 97 | } 98 | 99 | public int getX(int index) { 100 | /* 101 | * if (index % 2 == 0) { return 12; } else { return 0; } 102 | */ 103 | return 0; 104 | 105 | } 106 | 107 | public int getY(int index) { 108 | // index = (index + 1) / 2; 109 | // return 32 + index * 8; 110 | return 24 + index * 8; 111 | } 112 | 113 | public int getId(int index) { 114 | return index + 30; 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/main/resources/templates/crawler/crawler_event_impl_template.txt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2018 WeBank, Inc. All Rights Reserved. 3 | */ 4 | package ${group}.${projectName}.generated.crawler.event; 5 | 6 | import java.math.BigInteger; 7 | import java.sql.Date; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import org.apache.commons.lang3.StringUtils; 13 | import org.fisco.bcos.web3j.abi.EventValues; 14 | import org.fisco.bcos.web3j.crypto.Credentials; 15 | import org.fisco.bcos.web3j.protocol.Web3j; 16 | import org.fisco.bcos.web3j.protocol.core.methods.response.Log; 17 | import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt; 18 | import org.fisco.bcos.web3j.tx.gas.ContractGasProvider; 19 | import org.fisco.bcos.web3j.tx.txdecode.EventResultEntity; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 24 | import org.springframework.context.annotation.Bean; 25 | import org.springframework.stereotype.Service; 26 | import org.springframework.util.CollectionUtils; 27 | 28 | import ${contractPackName}.${contractName}; 29 | import ${contractPackName}.${contractName}.${eventName}EventResponse; 30 | import ${group}.webasebee.common.bo.data.EventBO; 31 | import ${group}.${projectName}.crawler.face.BcosEventCrawlerInterface; 32 | import ${group}.${projectName}.generated.bo.event.${class_name}; 33 | import com.webank.webasebee.common.constants.ContractConstants; 34 | import com.webank.webasebee.common.tools.AddressUtils; 35 | import com.webank.webasebee.common.tools.BigIntegerUtils; 36 | import com.webank.webasebee.common.tools.BytesUtils; 37 | import com.webank.webasebee.common.tools.JacksonUtils; 38 | import lombok.extern.slf4j.Slf4j; 39 | 40 | @Service 41 | @Slf4j 42 | @ConditionalOnProperty(name = "monitor.${contractName}.${eventName}CrawlerService", havingValue = "on") 43 | public class ${contractName}${eventName}CrawlerImpl implements BcosEventCrawlerInterface { 44 | @Autowired 45 | private Web3j web3j; 46 | @Autowired 47 | private Credentials credentials; 48 | @Autowired 49 | private ContractGasProvider contractGasProvider; 50 | @Autowired 51 | private ${contractName} contract; 52 | 53 | @Bean 54 | @ConditionalOnMissingBean 55 | public ${contractName} get${contractName}() { 56 | return ${contractName}.load(ContractConstants.EMPTY_ADDRESS, web3j, credentials, contractGasProvider); 57 | 58 | } 59 | 60 | @Override 61 | public List handleReceipt(TransactionReceipt receipt, BigInteger blockTimeStamp) { 62 | <% 63 | var upperEvent = strutil.toUpperCase(eventName); 64 | %> 65 | List list = new ArrayList<>(); 66 | try { 67 | Map>> map = 68 | contract.transactionDecoder.decodeEventReturnObject(receipt.getLogs()); 69 | map.forEach((k, v) -> { 70 | String eventName = StringUtils.substringBefore(k, "("); 71 | if (StringUtils.equalsAnyIgnoreCase(eventName, contract.${upperEvent}_EVENT.getName())) { 72 | List> events = v; 73 | for (List l : events) { 74 | ${class_name} ${lowContractName}${eventName} = new ${class_name}(); 75 | 76 | ${lowContractName}${eventName}.setIdentifier("${contractName}${eventName}"); 77 | ${lowContractName}${eventName}.setBlockHeight(receipt.getBlockNumber().longValue()); 78 | ${lowContractName}${eventName}.setEventContractAddress(receipt.getContractAddress()); 79 | ${lowContractName}${eventName}.setTxHash(receipt.getTransactionHash()); 80 | 81 | <% 82 | for(var i=0;i 88 | ${lowContractName}${eventName}.set${javaCapName}(${typeMethod}(l.get(${i}).getData())); 89 | <% } %> 90 | ${lowContractName}${eventName}.setBlockTimeStamp(new Date(blockTimeStamp.longValue())); 91 | log.debug("depot ${eventName}:{}", ${lowContractName}${eventName}.toString()); 92 | list.add(${lowContractName}${eventName}); 93 | } 94 | } 95 | }); 96 | } catch (Exception e) { 97 | log.warn("Event parse error: {}", e); 98 | } 99 | return list; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/SolJavaTypeMappingUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | /** 21 | * SolJavaTypeMappingUtils 22 | * 23 | * @Description: SolJavaTypeMappingUtils 24 | * @author maojiayu 25 | * @data Apr 27, 2020 3:31:08 PM 26 | * 27 | */ 28 | public class SolJavaTypeMappingUtils { 29 | 30 | public static String fromSolBasicTypeToJavaType(String solBasicType) { 31 | if (StringUtils.contains(solBasicType, "[") && StringUtils.contains(solBasicType, "]")) { 32 | return "String"; 33 | } 34 | switch (solBasicType) { 35 | case "address": 36 | case "string": 37 | case "bytes1": 38 | case "bytes2": 39 | case "bytes3": 40 | case "bytes4": 41 | case "bytes5": 42 | case "bytes6": 43 | case "bytes7": 44 | case "bytes8": 45 | case "bytes9": 46 | case "bytes10": 47 | case "bytes11": 48 | case "bytes12": 49 | case "bytes13": 50 | case "bytes14": 51 | case "bytes15": 52 | case "bytes16": 53 | case "bytes17": 54 | case "bytes18": 55 | case "bytes19": 56 | case "bytes20": 57 | case "bytes21": 58 | case "bytes22": 59 | case "bytes23": 60 | case "bytes24": 61 | case "bytes25": 62 | case "bytes26": 63 | case "bytes27": 64 | case "bytes28": 65 | case "bytes29": 66 | case "bytes30": 67 | case "bytes31": 68 | case "bytes32": 69 | case "bytes": 70 | return "String"; 71 | case "bool": 72 | return "boolean"; 73 | case "uint8": 74 | case "int8": 75 | case "uint16": 76 | case "int16": 77 | case "uint24": 78 | case "int24": 79 | case "uint32": 80 | case "int32": 81 | case "uint40": 82 | case "int40": 83 | case "uint48": 84 | case "int48": 85 | case "uint56": 86 | case "int56": 87 | case "uint64": 88 | case "int64": 89 | case "uint72": 90 | case "int72": 91 | case "uint80": 92 | case "int80": 93 | case "uint88": 94 | case "int88": 95 | case "uint96": 96 | case "int96": 97 | case "uint104": 98 | case "int104": 99 | case "uint112": 100 | case "int112": 101 | case "uint120": 102 | case "int120": 103 | case "uint128": 104 | case "int128": 105 | case "uint136": 106 | case "int136": 107 | case "uint144": 108 | case "int144": 109 | case "uint152": 110 | case "int152": 111 | case "uint160": 112 | case "int160": 113 | case "uint168": 114 | case "int168": 115 | case "uint176": 116 | case "int176": 117 | case "uint184": 118 | case "int184": 119 | case "uint192": 120 | case "int192": 121 | case "uint200": 122 | case "int200": 123 | case "uint208": 124 | case "int208": 125 | case "uint216": 126 | case "int216": 127 | case "uint224": 128 | case "int224": 129 | case "uint232": 130 | case "int232": 131 | case "uint240": 132 | case "int240": 133 | case "uint248": 134 | case "int248": 135 | case "uint256": 136 | case "int256": 137 | return "long"; 138 | default: 139 | return null; 140 | } 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /src/test/java/com/webank/webasemonkey/parser/MethodParserTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.parser; 17 | 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import org.fisco.bcos.web3j.abi.TypeReference; 22 | import org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType; 23 | import org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType.Type; 24 | import org.fisco.bcos.web3j.tx.txdecode.BaseException; 25 | import org.fisco.bcos.web3j.tx.txdecode.ContractAbiUtil; 26 | import org.fisco.bcos.web3j.tx.txdecode.DynamicArrayReference; 27 | import org.junit.jupiter.api.Assertions; 28 | import org.junit.jupiter.api.Test; 29 | import org.springframework.beans.factory.annotation.Autowired; 30 | 31 | import com.webank.webasemonkey.WebasemonkeyApplicationTests; 32 | import com.webank.webasemonkey.tools.JacksonUtils; 33 | import com.webank.webasemonkey.vo.FieldVO; 34 | import com.webank.webasemonkey.vo.MethodMetaInfo; 35 | 36 | 37 | /** 38 | * MethodParserTest 39 | * 40 | * @Description: MethodParserTest 41 | * @author maojiayu 42 | * @data Apr 17, 2020 3:27:00 PM 43 | * 44 | */ 45 | public class MethodParserTest extends WebasemonkeyApplicationTests { 46 | @Autowired 47 | private MethodParser methodParser; 48 | 49 | @SuppressWarnings("unchecked") 50 | @Test 51 | public void testGetField() throws BaseException, ClassNotFoundException { 52 | String methodMetaInfoStr = 53 | "{\"contractName\":\"AccessRestriction\",\"name\":\"revokeUser\",\"shardingNO\":1,\"list\":null}"; 54 | String inputsAddress = "[{\"name\":\"_user\",\"type\":\"address\",\"type0\":null,\"indexed\":false}]"; 55 | String fieldList = 56 | "[{\"sqlName\":\"_user_\",\"solidityName\":\"_user\",\"javaName\":\"_user\",\"sqlType\":\"varchar(255)\",\"solidityType\":\"Address\",\"javaType\":\"String\",\"entityType\":null,\"typeMethod\":\"AddressUtils.bigIntegerToString\",\"javaCapName\":\"_user\",\"length\":0}]"; 57 | MethodMetaInfo mmi = JacksonUtils.fromJson(methodMetaInfoStr, MethodMetaInfo.class); 58 | List nt = JacksonUtils.fromJson(inputsAddress, List.class, NamedType.class); 59 | List list = methodParser.getFieldList(mmi, nt); 60 | 61 | String methodMetaInfoStaticArrayStr = 62 | "{\"contractName\":\"RecordData\",\"name\":\"insertRecord\",\"shardingNO\":1,\"list\":null}"; 63 | String inputsStaticArray = "[{\"name\":\"record\",\"type\":\"bytes[]\",\"type0\":null,\"indexed\":false}]"; 64 | String fieldList2 = 65 | "[{\"sqlName\":\"_record_\",\"solidityName\":\"record\",\"javaName\":\"record\",\"sqlType\":\"varchar(10240)\",\"solidityType\":\"DynamicArray\",\"javaType\":\"String\",\"entityType\":null,\"typeMethod\":\"BytesUtils.dynamicBytesListObjectToString\",\"javaCapName\":\"Record\",\"length\":0}]"; 66 | MethodMetaInfo mmi2 = JacksonUtils.fromJson(methodMetaInfoStaticArrayStr, MethodMetaInfo.class); 67 | List nt2 = JacksonUtils.fromJson(inputsStaticArray, List.class, NamedType.class); 68 | List list2 = methodParser.getFieldList(mmi2, nt2); 69 | 70 | List> listOfTypeReference = ContractAbiUtil.paramFormat(nt); 71 | System.out.println(JacksonUtils.toJson(listOfTypeReference)); 72 | System.out.println(JacksonUtils.toJson(ContractAbiUtil.paramFormat(nt2))); 73 | 74 | for(NamedType n : nt2) { 75 | Type type = new Type(n.getType()); 76 | System.out.println(JacksonUtils.toJson(type)); 77 | TypeReference tr = DynamicArrayReference.create(type.getBaseName(), n.isIndexed()); 78 | System.out.println(tr.getClass().getSimpleName()); 79 | } 80 | } 81 | 82 | 83 | @SuppressWarnings("unchecked") 84 | public static void main(String[] args) { 85 | String s = "[{\"value\":\"aw==\",\"typeAsString\":\"bytes1\"},{\"value\":\"dg==\",\"typeAsString\":\"bytes1\"}]"; 86 | List> list = JacksonUtils.fromJson(s, List.class, Map.class); 87 | System.out.println(JacksonUtils.toJson(list.get(0).get("value"))); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/code/template/paras/DBEnvironmentParas.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.code.template.paras; 17 | 18 | import java.io.File; 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | import java.util.Map; 22 | import java.util.stream.Collectors; 23 | 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.stereotype.Component; 26 | 27 | import com.google.common.collect.Maps; 28 | import com.webank.webasemonkey.code.template.face.ConfigGenerateParas; 29 | import com.webank.webasemonkey.config.SystemEnvironmentConfig; 30 | import com.webank.webasemonkey.constants.ConfigFileConstants; 31 | import com.webank.webasemonkey.constants.ParserConstants; 32 | import com.webank.webasemonkey.constants.TemplateConstants; 33 | import com.webank.webasemonkey.enums.SubProjectEnum; 34 | import com.webank.webasemonkey.enums.SysTableEnum; 35 | import com.webank.webasemonkey.tools.PropertiesUtils; 36 | import com.webank.webasemonkey.tools.SqlNameUtils; 37 | import com.webank.webasemonkey.vo.ContractInfo; 38 | import com.webank.webasemonkey.vo.ContractStructureMetaInfo; 39 | import com.webank.webasemonkey.vo.EventMetaInfo; 40 | import com.webank.webasemonkey.vo.MethodMetaInfo; 41 | 42 | import lombok.extern.slf4j.Slf4j; 43 | 44 | /** 45 | * DBYmlParas to generate db.yml 46 | * 47 | * @author maojiayu 48 | * @data Dec 28, 2018 2:47:37 PM 49 | * 50 | */ 51 | @Component 52 | @Slf4j 53 | public class DBEnvironmentParas implements ConfigGenerateParas { 54 | 55 | @Autowired 56 | protected SystemEnvironmentConfig systemEnvironmentConfig; 57 | @Autowired 58 | private SqlNameUtils sqlNameUtils; 59 | 60 | @Override 61 | public Map getMap(ContractInfo contractsInfo) { 62 | Map map = Maps.newLinkedHashMap(); 63 | map.put("dbUrl", systemEnvironmentConfig.getDbUrl()); 64 | log.debug("dbUrl: {}", systemEnvironmentConfig.getDbUrl()); 65 | map.put("dbUser", systemEnvironmentConfig.getDbUser()); 66 | log.debug("dbUser: {}", systemEnvironmentConfig.getDbUser()); 67 | map.put("dbPassword", systemEnvironmentConfig.getDbPassword()); 68 | log.debug("dbPassword: {}", systemEnvironmentConfig.getDbPassword()); 69 | 70 | List eventList = contractsInfo.getEventList(); 71 | List methodList = contractsInfo.getMethodList(); 72 | List list = getContractStructureMetaInfoList( 73 | eventList.stream().map(e -> (ContractStructureMetaInfo) e).collect(Collectors.toList())); 74 | for (SysTableEnum e : SysTableEnum.values()) { 75 | int length = Integer.parseInt(PropertiesUtils.getGlobalProperty(ParserConstants.SYSTEM, "sys", e.getName(), 76 | ParserConstants.SHARDINGNO, "0")); 77 | if (length > 0) { 78 | ContractStructureMetaInfo c = 79 | new ContractStructureMetaInfo().setShardingNO(length).setName(e.getTableName()); 80 | list.add(c); 81 | } 82 | } 83 | list.addAll(getContractStructureMetaInfoList( 84 | methodList.stream().map(e -> (ContractStructureMetaInfo) e).collect(Collectors.toList()))); 85 | map.put("list", list); 86 | return map; 87 | } 88 | 89 | private List getContractStructureMetaInfoList( 90 | List ContractStructureMetaInfoList) { 91 | List list = new ArrayList<>(); 92 | for (ContractStructureMetaInfo info : ContractStructureMetaInfoList) { 93 | if (info.getShardingNO() > 1) { 94 | String tableName = sqlNameUtils.getSqlName(info.getContractName(), info.getName()); 95 | info.setName(tableName); 96 | list.add(info); 97 | } 98 | } 99 | return list; 100 | } 101 | 102 | @Override 103 | public String getTemplatePath() { 104 | return TemplateConstants.DB_PROPERTIES_PATH; 105 | } 106 | 107 | @Override 108 | public String getGeneratedFilePath(ContractInfo contractsInfo) { 109 | return SubProjectEnum.CORE.getPathName() + File.separator + ConfigFileConstants.GENERATED_DB_ENV_FILE_PATH; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/SolSqlTypeMappingUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | /** 21 | * SolSqlTypeMappingUtils 22 | * 23 | * @Description: SolSqlTypeMappingUtils 24 | * @author maojiayu 25 | * @data Apr 27, 2020 4:10:36 PM 26 | * 27 | */ 28 | public class SolSqlTypeMappingUtils { 29 | public static String fromSolBasicTypeToSqlType(String solBasicType) { 30 | if (StringUtils.contains(solBasicType, "[") && StringUtils.contains(solBasicType, "]")) { 31 | return "varchar(3072)"; 32 | } 33 | switch (solBasicType) { 34 | case "address": 35 | return "varchar(256)"; 36 | case "string": 37 | return "varchar(2048)"; 38 | case "bytes1": 39 | case "bytes2": 40 | case "bytes3": 41 | case "bytes4": 42 | case "bytes5": 43 | case "bytes6": 44 | case "bytes7": 45 | case "bytes8": 46 | return "varchar(64)"; 47 | case "bytes9": 48 | case "bytes10": 49 | case "bytes11": 50 | case "bytes12": 51 | case "bytes13": 52 | case "bytes14": 53 | case "bytes15": 54 | case "bytes16": 55 | return "varchar(128)"; 56 | case "bytes17": 57 | case "bytes18": 58 | case "bytes19": 59 | case "bytes20": 60 | case "bytes21": 61 | case "bytes22": 62 | case "bytes23": 63 | case "bytes24": 64 | return "varchar(192)"; 65 | case "bytes25": 66 | case "bytes26": 67 | case "bytes27": 68 | case "bytes28": 69 | case "bytes29": 70 | case "bytes30": 71 | case "bytes31": 72 | case "bytes32": 73 | return "varchar(256)"; 74 | case "bytes": 75 | return "varchar(1024)"; 76 | case "bool": 77 | return "boolean"; 78 | case "uint8": 79 | case "int8": 80 | return "tinyint"; 81 | case "uint16": 82 | case "int16": 83 | case "uint24": 84 | case "int24": 85 | return "smallint"; 86 | case "uint32": 87 | case "int32": 88 | case "uint40": 89 | case "int40": 90 | case "uint48": 91 | case "int48": 92 | case "uint56": 93 | case "int56": 94 | case "uint64": 95 | case "int64": 96 | return "int"; 97 | case "uint72": 98 | case "int72": 99 | case "uint80": 100 | case "int80": 101 | case "uint88": 102 | case "int88": 103 | case "uint96": 104 | case "int96": 105 | case "uint104": 106 | case "int104": 107 | case "uint112": 108 | case "int112": 109 | case "uint120": 110 | case "int120": 111 | case "uint128": 112 | case "int128": 113 | case "uint136": 114 | case "int136": 115 | case "uint144": 116 | case "int144": 117 | case "uint152": 118 | case "int152": 119 | case "uint160": 120 | case "int160": 121 | case "uint168": 122 | case "int168": 123 | case "uint176": 124 | case "int176": 125 | case "uint184": 126 | case "int184": 127 | case "uint192": 128 | case "int192": 129 | case "uint200": 130 | case "int200": 131 | case "uint208": 132 | case "int208": 133 | case "uint216": 134 | case "int216": 135 | case "uint224": 136 | case "int224": 137 | case "uint232": 138 | case "int232": 139 | case "uint240": 140 | case "int240": 141 | case "uint248": 142 | case "int248": 143 | case "uint256": 144 | case "int256": 145 | return "bigint"; 146 | default: 147 | return null; 148 | } 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/SolTypeMethodMappingUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import com.webank.webasemonkey.bo.JavaArrayTypeBO; 19 | import com.webank.webasemonkey.bo.JavaBasicTypeBO; 20 | 21 | /** 22 | * SolTypeMethod 23 | * 24 | * @Description: SolTypeMethod 25 | * @author maojiayu 26 | * @data Apr 27, 2020 4:32:04 PM 27 | * 28 | */ 29 | public class SolTypeMethodMappingUtils { 30 | 31 | public static String fromSolBasicTypeToTypeMethod(String solBasicType) { 32 | JavaBasicTypeBO bo = TypeReferenceUtils.convertType(solBasicType); 33 | if(bo.isArray()) { 34 | JavaArrayTypeBO a = (JavaArrayTypeBO) bo; 35 | int type = a.getType().getArgType(); 36 | if(type == 1) { 37 | return "BytesUtils.numericTypeListToString"; 38 | } 39 | else if (type == 2) { 40 | return "BytesUtils.bytesTypeListToString"; 41 | } 42 | else if (type == 4) { 43 | return "BoolUtils.toBoolean"; 44 | } else { 45 | return "BytesUtils.stringListToString"; 46 | } 47 | } 48 | 49 | switch (solBasicType) { 50 | case "address": 51 | case "string": 52 | return "String.valueOf"; 53 | case "bytes1": 54 | case "bytes2": 55 | case "bytes3": 56 | case "bytes4": 57 | case "bytes5": 58 | case "bytes6": 59 | case "bytes7": 60 | case "bytes8": 61 | case "bytes9": 62 | case "bytes10": 63 | case "bytes11": 64 | case "bytes12": 65 | case "bytes13": 66 | case "bytes14": 67 | case "bytes15": 68 | case "bytes16": 69 | case "bytes17": 70 | case "bytes18": 71 | case "bytes19": 72 | case "bytes20": 73 | case "bytes21": 74 | case "bytes22": 75 | case "bytes23": 76 | case "bytes24": 77 | case "bytes25": 78 | case "bytes26": 79 | case "bytes27": 80 | case "bytes28": 81 | case "bytes29": 82 | case "bytes30": 83 | case "bytes31": 84 | case "bytes32": 85 | case "bytes": 86 | return "BytesUtils.bytesArrayToString"; 87 | case "bool": 88 | return "BoolUtils.toBoolean"; 89 | case "uint8": 90 | case "int8": 91 | case "uint16": 92 | case "int16": 93 | case "uint24": 94 | case "int24": 95 | case "uint32": 96 | case "int32": 97 | case "uint40": 98 | case "int40": 99 | case "uint48": 100 | case "int48": 101 | case "uint56": 102 | case "int56": 103 | case "uint64": 104 | case "int64": 105 | case "uint72": 106 | case "int72": 107 | case "uint80": 108 | case "int80": 109 | case "uint88": 110 | case "int88": 111 | case "uint96": 112 | case "int96": 113 | case "uint104": 114 | case "int104": 115 | case "uint112": 116 | case "int112": 117 | case "uint120": 118 | case "int120": 119 | case "uint128": 120 | case "int128": 121 | case "uint136": 122 | case "int136": 123 | case "uint144": 124 | case "int144": 125 | case "uint152": 126 | case "int152": 127 | case "uint160": 128 | case "int160": 129 | case "uint168": 130 | case "int168": 131 | case "uint176": 132 | case "int176": 133 | case "uint184": 134 | case "int184": 135 | case "uint192": 136 | case "int192": 137 | case "uint200": 138 | case "int200": 139 | case "uint208": 140 | case "int208": 141 | case "uint216": 142 | case "int216": 143 | case "uint224": 144 | case "int224": 145 | case "uint232": 146 | case "int232": 147 | case "uint240": 148 | case "int240": 149 | case "uint248": 150 | case "int248": 151 | case "uint256": 152 | case "int256": 153 | return "BigIntegerUtils.toLong"; 154 | default: 155 | return null; 156 | } 157 | } 158 | 159 | } 160 | -------------------------------------------------------------------------------- /src/main/java/com/webank/webasemonkey/tools/JacksonUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.webank.webasemonkey.tools; 17 | 18 | import java.io.IOException; 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | import com.fasterxml.jackson.core.JsonParseException; 24 | import com.fasterxml.jackson.core.JsonProcessingException; 25 | import com.fasterxml.jackson.databind.DeserializationFeature; 26 | import com.fasterxml.jackson.databind.JavaType; 27 | import com.fasterxml.jackson.databind.JsonMappingException; 28 | import com.fasterxml.jackson.databind.ObjectMapper; 29 | import com.fasterxml.jackson.databind.SerializationFeature; 30 | 31 | import lombok.extern.slf4j.Slf4j; 32 | 33 | /** 34 | * a useful toolkit of json based on Jackson. 35 | * 36 | * @author maojiayu 37 | * @data Dec 28, 2018 3:50:47 PM 38 | * 39 | */ 40 | @Slf4j 41 | public class JacksonUtils { 42 | public static ObjectMapper objectMapper = new ObjectMapper(); 43 | 44 | static { 45 | objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); 46 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 47 | } 48 | 49 | public static T fromJson(String json, Class clazz) { 50 | try { 51 | return fromJsonWithException(json, clazz); 52 | } catch (Exception e) { 53 | log.error("json is: " + json, e); 54 | return null; 55 | } 56 | } 57 | 58 | @SuppressWarnings("rawtypes") 59 | public static T fromJson(String json, Class c, Class...t) { 60 | try { 61 | return fromJsonWithException(json, c, t); 62 | } catch (IOException e) { 63 | throw new JacksonException(e); 64 | } 65 | } 66 | 67 | public static T fromJson(String json, JavaType type) { 68 | try { 69 | return fromJsonWithException(json, type); 70 | } catch (IOException e) { 71 | throw new JacksonException(e); 72 | } 73 | } 74 | 75 | public static T fromJsonWithException(String json, Class clazz) 76 | throws JsonParseException, JsonMappingException, IOException { 77 | return objectMapper.readValue(json, clazz); 78 | } 79 | 80 | @SuppressWarnings("rawtypes") 81 | public static T fromJsonWithException(String json, Class c, Class...t) 82 | throws JsonParseException, JsonMappingException, IOException { 83 | JavaType javaType = objectMapper.getTypeFactory().constructParametricType(c, t); 84 | return objectMapper.readValue(json, javaType); 85 | } 86 | 87 | @SuppressWarnings("unchecked") 88 | public static T fromJsonWithException(String json, JavaType type) 89 | throws JsonParseException, JsonMappingException, IOException { 90 | T ret = (T) objectMapper.readValue(json, type); 91 | return ret; 92 | } 93 | 94 | public static List fromJsonList(String json, Class c) { 95 | try { 96 | return fromJsonListWithException(json, c); 97 | } catch (IOException e) { 98 | throw new JacksonException(e); 99 | } 100 | } 101 | 102 | @SuppressWarnings("unchecked") 103 | public static List fromJsonListWithException(String json, Class c) throws IOException { 104 | JavaType type = getCollectionType(ArrayList.class, c); 105 | return (List) objectMapper.readValue(json, type); 106 | } 107 | 108 | public static JavaType getCollectionType(Class collectionClass, Class...elementClasses) { 109 | return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); 110 | } 111 | 112 | public static String toJsonWithException(Object o) throws JsonProcessingException { 113 | return objectMapper.writeValueAsString(o); 114 | } 115 | 116 | public static String toJson(Object o) { 117 | try { 118 | return toJsonWithException(o); 119 | } catch (Exception e) { 120 | throw new JacksonException(e); 121 | } 122 | } 123 | 124 | public static Map convertValue(Object req, Class keyClazz, Class valueClazz) { 125 | Map ret = objectMapper.convertValue(req, 126 | objectMapper.getTypeFactory().constructMapType(Map.class, keyClazz, valueClazz)); 127 | return ret; 128 | } 129 | 130 | @SuppressWarnings("rawtypes") 131 | public static T convertMap(Map map, Class retClazz) { 132 | return objectMapper.convertValue(map, retClazz); 133 | } 134 | 135 | } 136 | --------------------------------------------------------------------------------