Compare commits
153 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e21fe56e9 | ||
|
|
f0ac042b3c | ||
|
|
13a4264488 | ||
|
|
7ffb65cc38 | ||
|
|
2464c360c0 | ||
|
|
1917ccf35c | ||
|
|
4f9c14da97 | ||
|
|
58e23c1b8c | ||
|
|
34156fc092 | ||
|
|
86fde56129 | ||
|
|
d997d3a7bb | ||
|
|
4e689855f4 | ||
|
|
eace16d7fd | ||
|
|
53b7f9b26f | ||
|
|
983c667725 | ||
|
|
96fd660d46 | ||
|
|
7d1770da8a | ||
|
|
b83f6867f3 | ||
|
|
fa7db42f50 | ||
|
|
80c07a5c5b | ||
|
|
59a4c85aeb | ||
|
|
7fc01d38e8 | ||
|
|
3010422f4e | ||
|
|
1f78bb1a98 | ||
|
|
1d7f32efe4 | ||
|
|
1e3d980437 | ||
|
|
8654b5eef8 | ||
|
|
5781ad1de0 | ||
|
|
e2a1fa15d3 | ||
|
|
50da7413d8 | ||
|
|
e45b945024 | ||
|
|
f3668a77ef | ||
|
|
2aee6cab20 | ||
|
|
6233a0b960 | ||
|
|
0ae469d8e2 | ||
|
|
bcad47ebd5 | ||
|
|
71d179b8c3 | ||
|
|
af35e5100e | ||
|
|
96c97c7dd2 | ||
|
|
d5a41fc8b6 | ||
|
|
c3c3afb3c3 | ||
|
|
4f32dcda16 | ||
|
|
67717e73ad | ||
|
|
1753734581 | ||
|
|
3e408e535c | ||
|
|
f154292cb5 | ||
|
|
ac261533c3 | ||
|
|
1982c984ee | ||
|
|
fb5f82c44f | ||
|
|
840cea06db | ||
|
|
3237e2488e | ||
|
|
98e1e40e18 | ||
|
|
def3fc7b2e | ||
|
|
6057b8f90a | ||
|
|
2847f761bb | ||
|
|
b0a2d2ad80 | ||
|
|
564b814f77 | ||
|
|
433585a231 | ||
|
|
6a4750e302 | ||
|
|
748a6f11c1 | ||
|
|
bdd9a82682 | ||
|
|
c551c157d1 | ||
|
|
7e777229ba | ||
|
|
612bfcba37 | ||
|
|
c44e00a100 | ||
|
|
7f46b3194a | ||
|
|
50b7999dee | ||
|
|
871f84b1ff | ||
|
|
4018a95af0 | ||
|
|
ffe3ff5830 | ||
|
|
9b9b0ab10e | ||
|
|
b19e5fe839 | ||
|
|
f6caf41960 | ||
|
|
67d57de166 | ||
|
|
4d79f17a3a | ||
|
|
245d6d6fc7 | ||
|
|
66bd1feadf | ||
|
|
c02e3549eb | ||
|
|
210b8dcf9d | ||
|
|
bbefa788fd | ||
|
|
2f6eb0908e | ||
|
|
51e4d44fc1 | ||
|
|
ed502daeb9 | ||
|
|
d7da527bd5 | ||
|
|
7c5146bc97 | ||
|
|
91cdf9990b | ||
|
|
7d03609462 | ||
|
|
7be9a06e76 | ||
|
|
3eaecacac0 | ||
|
|
9c5e23090c | ||
|
|
c7b05e530d | ||
|
|
889fa25e58 | ||
|
|
da6945224f | ||
|
|
026a727480 | ||
|
|
2404f547ab | ||
|
|
a9dd82dcf4 | ||
|
|
95443be313 | ||
|
|
5d1805b10e | ||
|
|
72eb175dc3 | ||
|
|
d5cbcaaa15 | ||
|
|
bfa722f6da | ||
|
|
6b9d35983a | ||
|
|
6cf50c7cc9 | ||
|
|
cf05851752 | ||
|
|
1cbbf17392 | ||
|
|
96afeb19b2 | ||
|
|
56da72b16e | ||
|
|
b781868876 | ||
|
|
6a05f4b497 | ||
|
|
b9fa617374 | ||
|
|
c59baee1f5 | ||
|
|
98531d6b2f | ||
|
|
8df5b45525 | ||
|
|
ebba966d52 | ||
|
|
169f35d41f | ||
|
|
8bec9e88a7 | ||
|
|
854aa0153f | ||
|
|
77eaaae6ce | ||
|
|
fd7badec0a | ||
|
|
a834b6ea21 | ||
|
|
31fad72172 | ||
|
|
ef14195e71 | ||
|
|
a2fab32356 | ||
|
|
2dd0ecfa49 | ||
|
|
849fe15728 | ||
|
|
76afab3c78 | ||
|
|
21250794f5 | ||
|
|
9f85ceb2ed | ||
|
|
b6ecdef0c5 | ||
|
|
4dfd528533 | ||
|
|
3131601477 | ||
|
|
5921cf5f0d | ||
|
|
788f7d5eb1 | ||
|
|
79cb79481e | ||
|
|
41f0061dca | ||
|
|
f981e4f886 | ||
|
|
ebf30e488a | ||
|
|
7ab889baac | ||
|
|
8d9d893839 | ||
|
|
205233f8c9 | ||
|
|
6ee9527ed9 | ||
|
|
ee56ffa346 | ||
|
|
3fcc478356 | ||
|
|
26e4cd2f4a | ||
|
|
509524144d | ||
|
|
676285e20b | ||
|
|
43edb0f814 | ||
|
|
1c684a4e32 | ||
|
|
156af0e2a4 | ||
|
|
28d9c465fd | ||
|
|
a285510656 | ||
|
|
d6042d142e | ||
|
|
be8d368e72 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,3 +10,4 @@
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
/target/
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
<h1>项目介绍</h1>
|
||||
<p>
|
||||
Redkale (中文名: 红菜苔,一种湖北特产蔬菜) 是基于Java 8全新的微服务框架, 包含HTTP、WebSocket、TCP/UDP、数据序列化、数据缓存、依赖注入等功能。 本框架致力于简化集中式和微服务架构的开发,在增强开发敏捷性的同时保持高性能。
|
||||
Redkale (中文名: 红菜苔,一种湖北特产蔬菜) 是基于Java 11全新的微服务框架, 包含HTTP、WebSocket、TCP/UDP、数据序列化、数据缓存、依赖注入等功能。 本框架致力于简化集中式和微服务架构的开发,在增强开发敏捷性的同时保持高性能。
|
||||
</p>
|
||||
<strong>RedKale 有如下主要特点:</strong>
|
||||
<ol>
|
||||
<li>大量使用Java 8新特性(接口默认值、Stream、Lambda、JDk8内置的ASM等)</li>
|
||||
<li>提供HTTP服务,同时内置JSON功能与限时缓存功能</li>
|
||||
<li>TCP层完全使用NIO.2,并统一TCP与UDP的接口换</li>
|
||||
<li>TCP层完全使用NIO,并统一TCP与UDP的接口换</li>
|
||||
<li>提供分布式与集中式部署的无缝切换</li>
|
||||
<li>提供类似JPA功能,包含数据缓存自动同步、分表分库与简洁的数据层操作接口</li>
|
||||
<li>可以动态修改已依赖注入的资源</li>
|
||||
|
||||
@@ -6,7 +6,7 @@ APP_HOME=`dirname "$0"`
|
||||
|
||||
if [ ! -f "$APP_HOME"/conf/application.xml ]; then
|
||||
APP_HOME="$APP_HOME"/..
|
||||
fi
|
||||
fi
|
||||
|
||||
lib='.'
|
||||
for jar in `ls $APP_HOME/lib/*.jar`
|
||||
|
||||
@@ -4,5 +4,5 @@ SET APP_HOME=%~dp0
|
||||
|
||||
IF NOT EXIST "%APP_HOME%\conf\application.xml" SET APP_HOME=%~dp0..
|
||||
|
||||
java -DAPP_HOME="%APP_HOME%" -classpath "%APP_HOME%"\lib\* org.redkale.boot.Application
|
||||
java -server -DAPP_HOME="%APP_HOME%" -classpath "%APP_HOME%"\lib\* org.redkale.boot.Application
|
||||
|
||||
|
||||
@@ -24,5 +24,5 @@ done
|
||||
export CLASSPATH=$CLASSPATH:$lib
|
||||
|
||||
echo "$APP_HOME"
|
||||
nohup java -DAPP_HOME="$APP_HOME" org.redkale.boot.Application > "$APP_HOME"/logs.out &
|
||||
nohup java -server -DAPP_HOME="$APP_HOME" org.redkale.boot.Application > "$APP_HOME"/logs.out &
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<application nodeid="10" port="2121">
|
||||
|
||||
<!-- 详细配置说明见: http://redkale.org/redkale.html#redkale_confxml -->
|
||||
|
||||
<application nodeid="10000" port="2020">
|
||||
|
||||
<resources>
|
||||
|
||||
</resources>
|
||||
|
||||
<server protocol="HTTP" port="6060">
|
||||
<server protocol="HTTP" port="5050">
|
||||
|
||||
<request>
|
||||
<remoteaddr value="request.headers.X-RemoteAddress"/>
|
||||
@@ -24,7 +22,7 @@
|
||||
|
||||
<filters autoload="true"/>
|
||||
|
||||
<rest path="/pipes" /> <!-- base指定的自定义HttpServlet子类必须标记@HttpUserType, 不设置base则视为没有当前用户信息设置 -->
|
||||
<rest path="/pipes" />
|
||||
|
||||
<servlets path="/pipes" autoload="true" />
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
|
||||
<persistence version="2.0">
|
||||
|
||||
<persistence-unit name="" transaction-type="RESOURCE_LOCAL">
|
||||
<shared-cache-mode>ALL</shared-cache-mode>
|
||||
@@ -25,7 +24,7 @@
|
||||
<shared-cache-mode>ALL</shared-cache-mode>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/center?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"/>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.password" value="1234"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
@@ -1 +1 @@
|
||||
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>jarĬ<EFBFBD>Ϸ<EFBFBD><EFBFBD>ڴ˴<EFBFBD>
|
||||
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>jarĬ<EFBFBD>Ϸ<EFBFBD><EFBFBD>ڴ˴<EFBFBD>
|
||||
37
my/pom.xml
37
my/pom.xml
@@ -4,16 +4,33 @@
|
||||
<groupId>org.redkale</groupId>
|
||||
<artifactId>redkale</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>RedkaleProject</name>
|
||||
<url>http://redkale.org</url>
|
||||
<description>redkale -- java framework</description>
|
||||
<version>1.6.2</version>
|
||||
<version>2.5.0</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.7.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache 2</name>
|
||||
<url>http://www.apache.org/licenses/</url>
|
||||
<url>https://www.apache.org/licenses/</url>
|
||||
<distribution>repo</distribution>
|
||||
<comments>Apache License</comments>
|
||||
</license>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<developers>
|
||||
@@ -21,13 +38,13 @@
|
||||
<id>Redkale</id>
|
||||
<name>redkale</name>
|
||||
<email>redkale@qq.com</email>
|
||||
<url>http://redkale.org</url>
|
||||
<url>https://redkale.org</url>
|
||||
<roles>
|
||||
<role>Project Manager</role>
|
||||
<role>Architect</role>
|
||||
</roles>
|
||||
<organization>redkale</organization>
|
||||
<organizationUrl>http://redkale.org</organizationUrl>
|
||||
<organizationUrl>https://redkale.org</organizationUrl>
|
||||
<properties>
|
||||
<dept>No</dept>
|
||||
</properties>
|
||||
@@ -35,12 +52,6 @@
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<name>Redkale</name>
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
@@ -51,11 +62,13 @@
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<scm>
|
||||
<url>https://github.com/redkale/redkale</url>
|
||||
<connection>scm:git:git@github.com/redkale/redkale.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:redkale/redkale.git</developerConnection>
|
||||
</scm>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -99,6 +112,7 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
@@ -111,6 +125,7 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>release</id>
|
||||
<id>release</id>
|
||||
<!--
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -92,6 +93,7 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
-->
|
||||
</profile>
|
||||
</profiles>
|
||||
</settings>
|
||||
138
pom.xml
Normal file
138
pom.xml
Normal file
@@ -0,0 +1,138 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.redkale</groupId>
|
||||
<artifactId>redkale</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>RedkaleProject</name>
|
||||
<url>https://redkale.org</url>
|
||||
<description>redkale -- java framework</description>
|
||||
<version>2.6.0</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
|
||||
<junit.version>5.7.0</junit.version>
|
||||
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
|
||||
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
|
||||
<maven-failsafe-plugin.version>3.0.0-M5</maven-failsafe-plugin.version>
|
||||
</properties>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache 2</name>
|
||||
<url>https://www.apache.org/licenses/</url>
|
||||
<distribution>repo</distribution>
|
||||
<comments>Apache License</comments>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<name>Central Repository</name>
|
||||
<url>https://repo.maven.apache.org/maven2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>sonatype-nexus-snapshots</id>
|
||||
<name>Sonatype Nexus Snapshots</name>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>Redkale</id>
|
||||
<name>redkale</name>
|
||||
<email>redkale@qq.com</email>
|
||||
<url>https://redkale.org</url>
|
||||
<roles>
|
||||
<role>Project Manager</role>
|
||||
<role>Architect</role>
|
||||
</roles>
|
||||
<organization>redkale</organization>
|
||||
<organizationUrl>https://redkale.org</organizationUrl>
|
||||
<properties>
|
||||
<dept>No</dept>
|
||||
</properties>
|
||||
<timezone>8</timezone>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<scm>
|
||||
<url>https://github.com/redkale/redkale</url>
|
||||
<connection>scm:git:git@github.com/redkale/redkale.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:redkale/redkale.git</developerConnection>
|
||||
</scm>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<compilerArgs>
|
||||
<arg>-parameters</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- 需要注释掉, 否则会生成native-image配置信息
|
||||
<plugin>
|
||||
<groupId>org.redkale.maven.plugins</groupId>
|
||||
<artifactId>redkale-maven-plugin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>redkale-compile</id>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<addMavenDescriptor>false</addMavenDescriptor>
|
||||
<manifest>
|
||||
<mainClass>org.redkale.boot.Application</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${maven-failsafe-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -33,6 +33,14 @@
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
【节点全局唯一】 @since 2.3.0
|
||||
全局Serivce执行的线程池, Application.workExecutor, 没配置该节点将自动创建一个。
|
||||
threads: 线程数,为0表示不启用workExecutor,只用IO线程。默认: CPU核数, 核数=1的情况下默认值为2
|
||||
hash: 是否使用ThreadHashExecutor作为线程池,默认值为:false
|
||||
-->
|
||||
<executor threads="4" hash="false"/>
|
||||
|
||||
<!--
|
||||
【节点全局唯一】
|
||||
transport节点只能有一个,用于配置所有Transport的池参数,没配置该节点将自动创建一个。
|
||||
@@ -71,6 +79,7 @@
|
||||
<!--
|
||||
MQ管理接口配置
|
||||
不同MQ节点所配置的MQ集群不能重复。
|
||||
MQ跟着协议走,所以mq的属性值需要被赋值在rest节点上, 由于SncpServlet是自动生成的,故SNCP协议下,mq属性值被赋值在service/services节点上
|
||||
name: 服务的名称,用于监控识别,多个mq节点时只能有一个name为空的节点,mq.name不能重复,命名规则: 字母、数字、下划线
|
||||
value: 实现类名,必须是org.redkale.mq.MessageAgent的子类
|
||||
MQ节点下的子节点配置没有固定格式, 根据MessageAgent实现方的定义来配置
|
||||
@@ -127,15 +136,12 @@
|
||||
如果name是mimetype.property.开头的值将会在进程启动时进行MimeType.add("yyyy", "YYYYYY")操作。
|
||||
load: 加载文件,多个用;隔开。
|
||||
默认置入的system.property.的有:
|
||||
System.setProperty("net.transport.poolmaxconns", "100");
|
||||
System.setProperty("net.transport.pinginterval", "30");
|
||||
System.setProperty("net.transport.checkinterval", "30");
|
||||
System.setProperty("convert.json.tiny", "true");
|
||||
System.setProperty("convert.bson.tiny", "true");
|
||||
System.setProperty("convert.json.pool.size", "128");
|
||||
System.setProperty("convert.bson.pool.size", "128");
|
||||
System.setProperty("convert.json.writer.buffer.defsize", "4096");
|
||||
System.setProperty("convert.bson.writer.buffer.defsize", "4096");
|
||||
System.setProperty("redkale.net.transport.poolmaxconns", "100");
|
||||
System.setProperty("redkale.net.transport.pinginterval", "30");
|
||||
System.setProperty("redkale.net.transport.checkinterval", "30");
|
||||
System.setProperty("redkale.convert.tiny", "true");
|
||||
System.setProperty("redkale.convert.pool.size", "128");
|
||||
System.setProperty("redkale.convert.writer.buffer.defsize", "4096");
|
||||
|
||||
<properties>节点下也可包含非<property>节点.
|
||||
非<property>其节点可以通过@Resource(name="properties.xxxxxx")进行注入, 被注解的字段类型只能是AnyValue、AnyValue[]
|
||||
@@ -158,30 +164,38 @@
|
||||
excludelibs: 排除lib.path与excludes中的正则表达式匹配的路径, 多个正则表达式用分号;隔开
|
||||
charset: 文本编码, 默认: UTF-8
|
||||
backlog: 默认10K
|
||||
threads: 线程数, 默认: CPU核数*2,最小8个
|
||||
threads【已废弃】: 线程数, 默认: CPU核数*2,最小8个【已废弃 @since 2.3.0】
|
||||
maxconns: 最大连接数, 小于1表示无限制, 默认: 0
|
||||
maxbody: request.body最大值, 默认: 64K
|
||||
bufferCapacity: ByteBuffer的初始化大小, TCP默认: 32K; (HTTP 2.0、WebSocket,必须要16k以上); UDP默认: 1350B
|
||||
bufferPoolSize: ByteBuffer池的大小,默认: 线程数*4
|
||||
responsePoolSize: Response池的大小,默认: 线程数*2
|
||||
responsePoolSize: Response池的大小,默认: 1024
|
||||
aliveTimeoutSeconds: KeepAlive读操作超时秒数, 默认30, 0表示永久不超时; -1表示禁止KeepAlive
|
||||
readTimeoutSeconds: 读操作超时秒数, 默认0, 表示永久不超时
|
||||
writeTimeoutSeconds: 写操作超时秒数, 默认0, 表示永久不超时
|
||||
netimpl: ProtocolServer的实现类。TCP情况下值可以是aio或nio,默认值为aio;UDP情况下值可以是bio,默认值为bio;
|
||||
interceptor: 启动/关闭NodeServer时被调用的拦截器实现类,必须是org.redkale.boot.NodeInterceptor的子类,默认为null
|
||||
-->
|
||||
<server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib="">
|
||||
|
||||
<!--
|
||||
【节点在<server>中唯一】
|
||||
value: 创建SSLContext的实现类, 可自定义,必须是org.redkale.net.SSLCreator的子类
|
||||
clientauth: true/false/want
|
||||
keystorepass: KEY密码
|
||||
keystorefile: KEY文件
|
||||
truststorepass: TRUST密码
|
||||
truststorefile: TRUST文件
|
||||
builder: 创建SSLContext的实现类, 可自定义,必须是org.redkale.net.SSLBuilder的子类
|
||||
sslProvider: java.security.Provider自定义的实现类,如第三方: org.conscrypt.OpenSSLProvider、org.bouncycastle.jce.provider.BouncyCastleProvider
|
||||
jsseProvider: java.security.Provider自定义的实现类,如第三方: org.conscrypt.JSSEProvider、 org.bouncycastle.jce.provider.BouncyCastleJsseProvider
|
||||
protocol: TLS版本,默认值: TLS
|
||||
protocols: 设置setEnabledProtocols, 多个用,隔开 如: TLSv1.2,TLSv1.3
|
||||
clientAuth: WANT/NEED/NONE, 默认值: NONE
|
||||
ciphers: 设置setEnabledCipherSuites, 多个用,隔开 如: TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256
|
||||
keystorePass: KEY密码
|
||||
keystoreFile: KEY文件 .jks
|
||||
keystoreType: KEY类型, 默认值为JKS
|
||||
keystoreAlgorithm: KEY文件的algorithm, 默认值为SunX509
|
||||
truststorePass: TRUST密码
|
||||
truststoreFile: TRUST文件
|
||||
truststoreType: TRUST类型, 默认值为JKS
|
||||
truststoreAlgorithm: TRUST文件的algorithm, 默认值为SunX509
|
||||
-->
|
||||
<ssl creator=""/>
|
||||
<ssl builder=""/>
|
||||
|
||||
<!--
|
||||
加载所有的Service服务;
|
||||
@@ -202,6 +216,7 @@
|
||||
<service value="com.xxx.XXX1Service"/>
|
||||
<!--
|
||||
name: 显式指定name,覆盖默认的空字符串值。 注意: name不能包含$符号。
|
||||
mq: 所属的MQ管理器,当 protocol == SNCP 时该值才有效, 存在该属性表示Service的SNCP协议采用消息总线代理模式
|
||||
groups: 显式指定groups,覆盖<services>节点的groups默认值。
|
||||
ignore: 是否禁用, 默认为false。
|
||||
-->
|
||||
@@ -289,7 +304,7 @@
|
||||
period>0表示定时获取时间; 设置1000表示每秒刷新Date时间
|
||||
-->
|
||||
<response>
|
||||
<contenttype plain="text/plain; charset=utf-8" json="application/json; charset=utf-8"/>
|
||||
<content-type plain="text/plain; charset=utf-8" json="application/json; charset=utf-8"/>
|
||||
<defcookie domain="" path=""/>
|
||||
<addheader name="Access-Control-Allow-Origin" value="request.headers.Origin" />
|
||||
<setheader name="Access-Control-Allow-Headers" value="request.headers.Access-Control-Request-Headers"/>
|
||||
@@ -301,8 +316,9 @@
|
||||
【节点在<server>中唯一】
|
||||
当Server为HTTP协议时,render才有效. 指定输出引擎的实现类
|
||||
value: 输出引擎的实现类, 必须是org.redkale.net.http.HttpRender的子类
|
||||
suffixs: 引擎文件名后缀,多个用;隔开,默认值为: .htel
|
||||
-->
|
||||
<render value="org.redkalex.htel.HttpTemplateRender"/>
|
||||
<render value="org.redkalex.htel.HttpTemplateRender" suffixs=".htel"/>
|
||||
<!--
|
||||
【节点在<server>中唯一】
|
||||
当Server为HTTP协议时,ResourceServlet才有效. 默认存在一个有默认属性的resource-servlet节点
|
||||
@@ -12,28 +12,18 @@
|
||||
是否开启缓存(标记为@Cacheable的Entity类),值目前只支持两种: ALL: 所有开启缓存。 NONE: 关闭所有缓存, 非NONE字样统一视为ALL
|
||||
-->
|
||||
<property name="javax.persistence.cachemode" value="ALL"/>
|
||||
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/dbuser?characterEncoding=utf8"/>
|
||||
<!--
|
||||
javax.persistence.jdbc.driver在JPA的值是JDBC驱动,Redkale有所不同,值应该是javax.sql.DataSource的子类。
|
||||
为了兼容用户习惯,Redkale内置常见JDBC驱动到javax.sql.DataSource的映射关系:
|
||||
org.mariadb.jdbc.Driver —————— org.mariadb.jdbc.MySQLDataSource
|
||||
org.postgresql.Driver —————— org.postgresql.ds.PGConnectionPoolDataSource
|
||||
com.mysql.jdbc.Driver —————— com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
|
||||
com.mysql.cj.jdbc.Driver —————— com.mysql.cj.jdbc.MysqlConnectionPoolDataSource
|
||||
oracle.jdbc.driver.OracleDriver —————— oracle.jdbc.pool.OracleConnectionPoolDataSource
|
||||
com.microsoft.sqlserver.jdbc.SQLServerDriver —————— com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource
|
||||
org.h2.Driver —————— org.h2.jdbcx.JdbcDataSource
|
||||
因此 com.mysql.jdbc.Driver 会被自动转换成 com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
|
||||
并且如果JDBC驱动是以上几个版本,javax.persistence.jdbc.driver属性都可以省略,Redkale会根据javax.persistence.jdbc.url的值来识别驱动
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="javax.persistence.jdbc.source" value="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"/>
|
||||
是否自动建表当表不存在的时候, 目前只支持mysql、postgres, 默认为false
|
||||
-->
|
||||
<property name="javax.persistence.table.autoddl" value="false"/>
|
||||
|
||||
<!-- 多个URL用;隔开,如分布式SearchSource需要配多个URL -->
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/dbuser?characterEncoding=utf8"/>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.password" value="123456"/>
|
||||
|
||||
<!-- 最大连接数,默认值:CPU数*16 -->
|
||||
<property name="javax.persistence.connections.limit" value="32"/>
|
||||
<!-- 最大连接数,默认值:CPU数 -->
|
||||
<property name="javax.persistence.connections.limit" value="12"/>
|
||||
|
||||
<!-- 包含的SQL模板,相当于反向LIKE,不同的JDBC驱动的SQL语句不一样,Redkale内置了MySQL的语句 -->
|
||||
<property name="javax.persistence.contain.sqltemplate" value="LOCATE(${keystr}, ${column}) > 0"/>
|
||||
@@ -41,7 +31,7 @@
|
||||
|
||||
<!-- 复制表结构的SQL模板,Redkale内置了MySQL的语句 -->
|
||||
<property name="javax.persistence.tablenotexist.sqlstates" value="42000;42S02"/>
|
||||
<property name="javax.persistence.tablecopy.sqltemplate" value="CREATE TABLE ${newtable} LIKE ${oldtable}"/>
|
||||
<property name="javax.persistence.tablecopy.sqltemplate" value="CREATE TABLE IF NOT EXISTS ${newtable} LIKE ${oldtable}"/>
|
||||
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
@@ -23,11 +23,17 @@ import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 值越大,优先级越高
|
||||
*
|
||||
*
|
||||
* @since Common Annotations 1.2
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Priority {
|
||||
|
||||
/**
|
||||
* 优先级值
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int value();
|
||||
}
|
||||
@@ -16,17 +16,68 @@ import java.lang.annotation.Target;
|
||||
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Resource {
|
||||
|
||||
/**
|
||||
* AuthenticationType
|
||||
*/
|
||||
@Deprecated
|
||||
public enum AuthenticationType {
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
CONTAINER,
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
APPLICATION
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源名称
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String name() default "";
|
||||
|
||||
/**
|
||||
* 依赖注入的类型
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
public Class<?> type() default Object.class;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return AuthenticationType
|
||||
*/
|
||||
@Deprecated
|
||||
public AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean shareable() default true;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@Deprecated
|
||||
public String description() default "";
|
||||
|
||||
/**
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@Deprecated
|
||||
public String mappedName() default "";
|
||||
|
||||
/**
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@Deprecated
|
||||
public String lookup() default "";
|
||||
}
|
||||
@@ -51,4 +51,12 @@ public @interface Cacheable {
|
||||
* @return int
|
||||
*/
|
||||
int interval() default 0;
|
||||
|
||||
/**
|
||||
* DataSource是否直接返回对象的真实引用, 而不是copy一份
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
boolean direct() default false;
|
||||
|
||||
}
|
||||
@@ -81,12 +81,19 @@ public @interface Column {
|
||||
boolean unique() default false;
|
||||
|
||||
/**
|
||||
* (Optional) Whether the database column is nullable.
|
||||
* (Optional) Whether the database column is required.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
boolean nullable() default true;
|
||||
|
||||
/**
|
||||
* for OpenAPI Specification 3
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
String example() default "";
|
||||
|
||||
/**
|
||||
* (Optional) Whether the column is included in SQL INSERT
|
||||
* statements generated by the persistence provider.
|
||||
@@ -109,11 +116,13 @@ public @interface Column {
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@Deprecated
|
||||
String table() default "";
|
||||
|
||||
/**
|
||||
* (Optional) The column length. (Applies only if a
|
||||
* string-valued column is used.)
|
||||
* if type==String and length == 65535 then sqltype is text
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@@ -15,9 +15,7 @@
|
||||
******************************************************************************/
|
||||
package javax.persistence;
|
||||
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@@ -27,6 +25,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
*
|
||||
* @since Java Persistence 1.0
|
||||
*/
|
||||
@Inherited
|
||||
@Documented
|
||||
@Target(TYPE)
|
||||
@Retention(RUNTIME)
|
||||
@@ -82,6 +82,11 @@ public @interface Table {
|
||||
*/
|
||||
Index[] indexes() default {};
|
||||
|
||||
/**
|
||||
* comment
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
String comment() default "";
|
||||
|
||||
}
|
||||
@@ -4,26 +4,29 @@
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
module org.redkale {
|
||||
*/
|
||||
module redkale {
|
||||
|
||||
requires java.base;
|
||||
requires java.logging;
|
||||
requires java.xml;
|
||||
requires java.logging;
|
||||
requires java.net.http;
|
||||
requires java.sql;
|
||||
|
||||
requires jdk.unsupported; //sun.misc.Unsafe
|
||||
|
||||
exports javax.annotation;
|
||||
exports javax.persistence;
|
||||
|
||||
exports org.redkale.asm;
|
||||
exports org.redkale.boot;
|
||||
exports org.redkale.boot.watch;
|
||||
exports org.redkale.cluster;
|
||||
exports org.redkale.convert;
|
||||
exports org.redkale.convert.bson;
|
||||
exports org.redkale.convert.ext;
|
||||
exports org.redkale.convert.json;
|
||||
exports org.redkale.mq;
|
||||
exports org.redkale.net;
|
||||
exports org.redkale.net.client;
|
||||
exports org.redkale.net.http;
|
||||
exports org.redkale.net.sncp;
|
||||
exports org.redkale.service;
|
||||
@@ -31,10 +34,11 @@ module org.redkale {
|
||||
exports org.redkale.util;
|
||||
exports org.redkale.watch;
|
||||
|
||||
uses org.redkale.cluster.ClusterAgent;
|
||||
uses org.redkale.mq.MessageAgent;
|
||||
uses org.redkale.source.CacheSource;
|
||||
uses org.redkale.source.SourceLoader;
|
||||
uses org.redkale.cluster.ClusterAgent;
|
||||
uses org.redkale.convert.ConvertProvider;
|
||||
uses org.redkale.source.CacheSourceProvider;
|
||||
uses org.redkale.source.DataSourceProvider;
|
||||
uses org.redkale.util.ResourceInjectLoader;
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -60,8 +60,8 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java annotation. The methods of this class must be
|
||||
* called in the following order: ( <code>visit</code> | <code>visitEnum</code> |
|
||||
* <code>visitAnnotation</code> | <code>visitArray</code> )* <code>visitEnd</code>.
|
||||
* called in the following order: ( <tt>visit</tt> | <tt>visitEnum</tt> |
|
||||
* <tt>visitAnnotation</tt> | <tt>visitArray</tt> )* <tt>visitEnd</tt>.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
* @author Eugene Kuleshov
|
||||
@@ -151,7 +151,7 @@ public abstract class AnnotationVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the nested annotation class.
|
||||
* @return a visitor to visit the actual nested annotation value, or
|
||||
* <code>null</code> if this visitor is not interested in visiting this
|
||||
* <tt>null</tt> if this visitor is not interested in visiting this
|
||||
* nested annotation. <i>The nested annotation value must be fully
|
||||
* visited before calling other methods on this annotation
|
||||
* visitor</i>.
|
||||
@@ -172,7 +172,7 @@ public abstract class AnnotationVisitor {
|
||||
* @param name
|
||||
* the value name.
|
||||
* @return a visitor to visit the actual array value elements, or
|
||||
* <code>null</code> if this visitor is not interested in visiting these
|
||||
* <tt>null</tt> if this visitor is not interested in visiting these
|
||||
* values. The 'name' parameters passed to the methods of this
|
||||
* visitor are ignored. <i>All the array values must be visited
|
||||
* before calling other methods on this annotation visitor</i>.
|
||||
@@ -77,7 +77,7 @@ final class AnnotationWriter extends AnnotationVisitor {
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* <code>true<code> if values are named, <code>false</code> otherwise. Annotation
|
||||
* <tt>true<tt> if values are named, <tt>false</tt> otherwise. Annotation
|
||||
* writers used for annotation default and annotation arrays use unnamed
|
||||
* values.
|
||||
*/
|
||||
@@ -122,13 +122,13 @@ final class AnnotationWriter extends AnnotationVisitor {
|
||||
* @param cw
|
||||
* the class writer to which this annotation must be added.
|
||||
* @param named
|
||||
* <code>true<code> if values are named, <code>false</code> otherwise.
|
||||
* <tt>true<tt> if values are named, <tt>false</tt> otherwise.
|
||||
* @param bv
|
||||
* where the annotation values must be stored.
|
||||
* @param parent
|
||||
* where the number of annotation values must be stored.
|
||||
* @param offset
|
||||
* where in <code>parent</code> the number of annotation values must
|
||||
* where in <tt>parent</tt> the number of annotation values must
|
||||
* be stored.
|
||||
*/
|
||||
AnnotationWriter(final ClassWriter cw, final boolean named,
|
||||
@@ -354,7 +354,7 @@ final class AnnotationWriter extends AnnotationVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <code>null</code> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param out
|
||||
* where the type reference and type path must be put.
|
||||
*/
|
||||
@@ -58,13 +58,15 @@
|
||||
*/
|
||||
package org.redkale.asm;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* A non standard class, field, method or code attribute.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
* @author Eugene Kuleshov
|
||||
*/
|
||||
class Attribute {
|
||||
public class Attribute {
|
||||
|
||||
/**
|
||||
* The type of this attribute.
|
||||
@@ -77,7 +79,7 @@ class Attribute {
|
||||
byte[] value;
|
||||
|
||||
/**
|
||||
* The next attribute in this attribute list. May be <code>null</code>.
|
||||
* The next attribute in this attribute list. May be <tt>null</tt>.
|
||||
*/
|
||||
Attribute next;
|
||||
|
||||
@@ -92,19 +94,19 @@ class Attribute {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if this type of attribute is unknown. The default
|
||||
* implementation of this method always returns <code>true</code>.
|
||||
* Returns <tt>true</tt> if this type of attribute is unknown. The default
|
||||
* implementation of this method always returns <tt>true</tt>.
|
||||
*
|
||||
* @return <code>true</code> if this type of attribute is unknown.
|
||||
* @return <tt>true</tt> if this type of attribute is unknown.
|
||||
*/
|
||||
public boolean isUnknown() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if this type of attribute is a code attribute.
|
||||
* Returns <tt>true</tt> if this type of attribute is a code attribute.
|
||||
*
|
||||
* @return <code>true</code> if this type of attribute is a code attribute.
|
||||
* @return <tt>true</tt> if this type of attribute is a code attribute.
|
||||
*/
|
||||
public boolean isCodeAttribute() {
|
||||
return false;
|
||||
@@ -113,7 +115,7 @@ class Attribute {
|
||||
/**
|
||||
* Returns the labels corresponding to this attribute.
|
||||
*
|
||||
* @return the labels corresponding to this attribute, or <code>null</code> if
|
||||
* @return the labels corresponding to this attribute, or <tt>null</tt> if
|
||||
* this attribute is not a code attribute that contains labels.
|
||||
*/
|
||||
protected Label[] getLabels() {
|
||||
@@ -123,7 +125,7 @@ class Attribute {
|
||||
/**
|
||||
* Reads a {@link #type type} attribute. This method must return a
|
||||
* <i>new</i> {@link Attribute} object, of type {@link #type type},
|
||||
* corresponding to the <code>len</code> bytes starting at the given offset, in
|
||||
* corresponding to the <tt>len</tt> bytes starting at the given offset, in
|
||||
* the given class reader.
|
||||
*
|
||||
* @param cr
|
||||
@@ -146,7 +148,7 @@ class Attribute {
|
||||
* containing the type and the length of the attribute, are not
|
||||
* taken into account here.
|
||||
* @param labels
|
||||
* the labels of the method's code, or <code>null</code> if the
|
||||
* the labels of the method's code, or <tt>null</tt> if the
|
||||
* attribute to be read is not a code attribute.
|
||||
* @return a <i>new</i> {@link Attribute} object corresponding to the given
|
||||
* bytes.
|
||||
@@ -169,11 +171,11 @@ class Attribute {
|
||||
* class the items that corresponds to this attribute.
|
||||
* @param code
|
||||
* the bytecode of the method corresponding to this code
|
||||
* attribute, or <code>null</code> if this attribute is not a code
|
||||
* attribute, or <tt>null</tt> if this attribute is not a code
|
||||
* attributes.
|
||||
* @param len
|
||||
* the length of the bytecode of the method corresponding to this
|
||||
* code attribute, or <code>null</code> if this attribute is not a
|
||||
* code attribute, or <tt>null</tt> if this attribute is not a
|
||||
* code attribute.
|
||||
* @param maxStack
|
||||
* the maximum stack size of the method corresponding to this
|
||||
@@ -216,11 +218,11 @@ class Attribute {
|
||||
* byte arrays, with the {@link #write write} method.
|
||||
* @param code
|
||||
* the bytecode of the method corresponding to these code
|
||||
* attributes, or <code>null</code> if these attributes are not code
|
||||
* attributes, or <tt>null</tt> if these attributes are not code
|
||||
* attributes.
|
||||
* @param len
|
||||
* the length of the bytecode of the method corresponding to
|
||||
* these code attributes, or <code>null</code> if these attributes
|
||||
* these code attributes, or <tt>null</tt> if these attributes
|
||||
* are not code attributes.
|
||||
* @param maxStack
|
||||
* the maximum stack size of the method corresponding to these
|
||||
@@ -254,11 +256,11 @@ class Attribute {
|
||||
* byte arrays, with the {@link #write write} method.
|
||||
* @param code
|
||||
* the bytecode of the method corresponding to these code
|
||||
* attributes, or <code>null</code> if these attributes are not code
|
||||
* attributes, or <tt>null</tt> if these attributes are not code
|
||||
* attributes.
|
||||
* @param len
|
||||
* the length of the bytecode of the method corresponding to
|
||||
* these code attributes, or <code>null</code> if these attributes
|
||||
* these code attributes, or <tt>null</tt> if these attributes
|
||||
* are not code attributes.
|
||||
* @param maxStack
|
||||
* the maximum stack size of the method corresponding to these
|
||||
@@ -281,4 +283,82 @@ class Attribute {
|
||||
attr = attr.next;
|
||||
}
|
||||
}
|
||||
|
||||
//The stuff below is temporary - once proper support for nestmate attribute has been added, it can be safely removed.
|
||||
//see also changes in ClassReader.accept.
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static class NestMembers extends Attribute {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public NestMembers() {
|
||||
super("NestMembers");
|
||||
}
|
||||
|
||||
byte[] bytes;
|
||||
String[] classes;
|
||||
|
||||
@Override
|
||||
protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
|
||||
int offset = off;
|
||||
NestMembers a = new NestMembers();
|
||||
int size = cr.readShort(off);
|
||||
a.classes = new String[size];
|
||||
off += 2;
|
||||
for (int i = 0; i < size ; i++) {
|
||||
a.classes[i] = cr.readClass(off, buf);
|
||||
off += 2;
|
||||
}
|
||||
a.bytes = Arrays.copyOfRange(cr.b, offset, offset + len);
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
|
||||
ByteVector v = new ByteVector(bytes.length);
|
||||
v.putShort(classes.length);
|
||||
for (String s : classes) {
|
||||
v.putShort(cw.newClass(s));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static class NestHost extends Attribute {
|
||||
|
||||
byte[] bytes;
|
||||
String clazz;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public NestHost() {
|
||||
super("NestHost");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
|
||||
int offset = off;
|
||||
NestHost a = new NestHost();
|
||||
a.clazz = cr.readClass(off, buf);
|
||||
a.bytes = Arrays.copyOfRange(cr.b, offset, offset + len);
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
|
||||
ByteVector v = new ByteVector(bytes.length);
|
||||
v.putShort(cw.newClass(clazz));
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
static final Attribute[] DEFAULT_ATTRIBUTE_PROTOS = new Attribute[] {
|
||||
new NestMembers(),
|
||||
new NestHost()
|
||||
};
|
||||
}
|
||||
@@ -332,7 +332,7 @@ public class ByteVector {
|
||||
* automatically enlarged if necessary.
|
||||
*
|
||||
* @param b
|
||||
* an array of bytes. May be <code>null</code> to put <code>len</code>
|
||||
* an array of bytes. May be <tt>null</tt> to put <tt>len</tt>
|
||||
* null bytes into this byte vector.
|
||||
* @param off
|
||||
* index of the fist byte of b that must be copied.
|
||||
@@ -185,9 +185,9 @@ public class ClassReader {
|
||||
public ClassReader(final byte[] b, final int off, final int len) {
|
||||
this.b = b;
|
||||
// checks the class version
|
||||
if (readShort(off + 6) > Opcodes.V10) {
|
||||
//throw new IllegalArgumentException();
|
||||
}
|
||||
//if (readShort(off + 6) > Opcodes.V11) {
|
||||
// throw new IllegalArgumentException();
|
||||
//}
|
||||
// parses the constant pool
|
||||
items = new int[readUnsignedShort(off + 8)];
|
||||
int n = items.length;
|
||||
@@ -205,6 +205,10 @@ public class ClassReader {
|
||||
case ClassWriter.FLOAT:
|
||||
case ClassWriter.NAME_TYPE:
|
||||
case ClassWriter.INDY:
|
||||
// @@@ ClassWriter.CONDY
|
||||
// Enables MethodHandles.lookup().defineClass to function correctly
|
||||
// when it reads the class name
|
||||
case 17:
|
||||
size = 5;
|
||||
break;
|
||||
case ClassWriter.LONG:
|
||||
@@ -267,7 +271,7 @@ public class ClassReader {
|
||||
* {@link Type#getInternalName() getInternalName}). For interfaces, the
|
||||
* super class is {@link Object}.
|
||||
*
|
||||
* @return the internal name of super class, or <code>null</code> for
|
||||
* @return the internal name of super class, or <tt>null</tt> for
|
||||
* {@link Object} class.
|
||||
*
|
||||
* @see ClassVisitor#visit(int, int, String, String, String, String[])
|
||||
@@ -281,7 +285,7 @@ public class ClassReader {
|
||||
* {@link Type#getInternalName() getInternalName}).
|
||||
*
|
||||
* @return the array of internal names for all implemented interfaces or
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*
|
||||
* @see ClassVisitor#visit(int, int, String, String, String, String[])
|
||||
*/
|
||||
@@ -526,7 +530,7 @@ public class ClassReader {
|
||||
* , {@link #SKIP_FRAMES}, {@link #SKIP_CODE}.
|
||||
*/
|
||||
public void accept(final ClassVisitor classVisitor, final int flags) {
|
||||
accept(classVisitor, new Attribute[0], flags);
|
||||
accept(classVisitor, Attribute.DEFAULT_ATTRIBUTE_PROTOS, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1932,7 +1936,7 @@ public class ClassReader {
|
||||
* @param v
|
||||
* start offset in {@link #b b} of the annotations to be read.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotations to be read are visible at
|
||||
* <tt>true</tt> if the annotations to be read are visible at
|
||||
* runtime.
|
||||
*/
|
||||
private void readParameterAnnotations(final MethodVisitor mv,
|
||||
@@ -2474,9 +2478,9 @@ public class ClassReader {
|
||||
* and the length of the attribute, are not taken into account
|
||||
* here.
|
||||
* @param labels
|
||||
* the labels of the method's code, or <code>null</code> if the
|
||||
* the labels of the method's code, or <tt>null</tt> if the
|
||||
* attribute to be read is not a code attribute.
|
||||
* @return the attribute that has been read, or <code>null</code> to skip this
|
||||
* @return the attribute that has been read, or <tt>null</tt> to skip this
|
||||
* attribute.
|
||||
*/
|
||||
private Attribute readAttribute(final Attribute[] attrs, final String type,
|
||||
@@ -60,11 +60,11 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java class. The methods of this class must be called in
|
||||
* the following order: <code>visit</code> [ <code>visitSource</code> ] [
|
||||
* <code>visitModule</code> ][ <code>visitOuterClass</code> ] ( <code>visitAnnotation</code> |
|
||||
* <code>visitTypeAnnotation</code> | <code>visitAttribute</code> )* (
|
||||
* <code>visitInnerClass</code> | <code>visitField</code> | <code>visitMethod</code> )*
|
||||
* <code>visitEnd</code>.
|
||||
* the following order: <tt>visit</tt> [ <tt>visitSource</tt> ] [
|
||||
* <tt>visitModule</tt> ][ <tt>visitOuterClass</tt> ] ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitTypeAnnotation</tt> | <tt>visitAttribute</tt> )* (
|
||||
* <tt>visitInnerClass</tt> | <tt>visitField</tt> | <tt>visitMethod</tt> )*
|
||||
* <tt>visitEnd</tt>.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
*/
|
||||
@@ -120,18 +120,18 @@ public abstract class ClassVisitor {
|
||||
* the internal name of the class (see
|
||||
* {@link Type#getInternalName() getInternalName}).
|
||||
* @param signature
|
||||
* the signature of this class. May be <code>null</code> if the class
|
||||
* the signature of this class. May be <tt>null</tt> if the class
|
||||
* is not a generic one, and does not extend or implement generic
|
||||
* classes or interfaces.
|
||||
* @param superName
|
||||
* the internal of name of the super class (see
|
||||
* {@link Type#getInternalName() getInternalName}). For
|
||||
* interfaces, the super class is {@link Object}. May be
|
||||
* <code>null</code>, but only for the {@link Object} class.
|
||||
* <tt>null</tt>, but only for the {@link Object} class.
|
||||
* @param interfaces
|
||||
* the internal names of the class's interfaces (see
|
||||
* {@link Type#getInternalName() getInternalName}). May be
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visit(int version, int access, String name, String signature,
|
||||
String superName, String[] interfaces) {
|
||||
@@ -145,11 +145,11 @@ public abstract class ClassVisitor {
|
||||
*
|
||||
* @param source
|
||||
* the name of the source file from which the class was compiled.
|
||||
* May be <code>null</code>.
|
||||
* May be <tt>null</tt>.
|
||||
* @param debug
|
||||
* additional debug information to compute the correspondance
|
||||
* between source and compiled elements of the class. May be
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visitSource(String source, String debug) {
|
||||
if (cv != null) {
|
||||
@@ -166,7 +166,7 @@ public abstract class ClassVisitor {
|
||||
* and {@code ACC_MANDATED}.
|
||||
* @param version
|
||||
* module version or null.
|
||||
* @return a visitor to visit the module values, or <code>null</code> if
|
||||
* @return a visitor to visit the module values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this module.
|
||||
*/
|
||||
public ModuleVisitor visitModule(String name, int access, String version) {
|
||||
@@ -187,11 +187,11 @@ public abstract class ClassVisitor {
|
||||
* internal name of the enclosing class of the class.
|
||||
* @param name
|
||||
* the name of the method that contains the class, or
|
||||
* <code>null</code> if the class is not enclosed in a method of its
|
||||
* <tt>null</tt> if the class is not enclosed in a method of its
|
||||
* enclosing class.
|
||||
* @param desc
|
||||
* the descriptor of the method that contains the class, or
|
||||
* <code>null</code> if the class is not enclosed in a method of its
|
||||
* <tt>null</tt> if the class is not enclosed in a method of its
|
||||
* enclosing class.
|
||||
*/
|
||||
public void visitOuterClass(String owner, String name, String desc) {
|
||||
@@ -206,8 +206,8 @@ public abstract class ClassVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||
@@ -231,12 +231,12 @@ public abstract class ClassVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <code>null</code> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||
@@ -269,10 +269,10 @@ public abstract class ClassVisitor {
|
||||
* @param outerName
|
||||
* the internal name of the class to which the inner class
|
||||
* belongs (see {@link Type#getInternalName() getInternalName}).
|
||||
* May be <code>null</code> for not member classes.
|
||||
* May be <tt>null</tt> for not member classes.
|
||||
* @param innerName
|
||||
* the (simple) name of the inner class inside its enclosing
|
||||
* class. May be <code>null</code> for anonymous inner classes.
|
||||
* class. May be <tt>null</tt> for anonymous inner classes.
|
||||
* @param access
|
||||
* the access flags of the inner class as originally declared in
|
||||
* the enclosing class.
|
||||
@@ -295,20 +295,20 @@ public abstract class ClassVisitor {
|
||||
* @param desc
|
||||
* the field's descriptor (see {@link Type Type}).
|
||||
* @param signature
|
||||
* the field's signature. May be <code>null</code> if the field's
|
||||
* the field's signature. May be <tt>null</tt> if the field's
|
||||
* type does not use generic types.
|
||||
* @param value
|
||||
* the field's initial value. This parameter, which may be
|
||||
* <code>null</code> if the field does not have an initial value,
|
||||
* <tt>null</tt> if the field does not have an initial value,
|
||||
* must be an {@link Integer}, a {@link Float}, a {@link Long}, a
|
||||
* {@link Double} or a {@link String} (for <code>int</code>,
|
||||
* <code>float</code>, <code>long</code> or <code>String</code> fields
|
||||
* {@link Double} or a {@link String} (for <tt>int</tt>,
|
||||
* <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
|
||||
* respectively). <i>This parameter is only used for static
|
||||
* fields</i>. Its value is ignored for non static fields, which
|
||||
* must be initialized through bytecode instructions in
|
||||
* constructors or methods.
|
||||
* @return a visitor to visit field annotations and attributes, or
|
||||
* <code>null</code> if this class visitor is not interested in visiting
|
||||
* <tt>null</tt> if this class visitor is not interested in visiting
|
||||
* these annotations and attributes.
|
||||
*/
|
||||
public FieldVisitor visitField(int access, String name, String desc,
|
||||
@@ -321,7 +321,7 @@ public abstract class ClassVisitor {
|
||||
|
||||
/**
|
||||
* Visits a method of the class. This method <i>must</i> return a new
|
||||
* {@link MethodVisitor} instance (or <code>null</code>) each time it is called,
|
||||
* {@link MethodVisitor} instance (or <tt>null</tt>) each time it is called,
|
||||
* i.e., it should not return a previously returned visitor.
|
||||
*
|
||||
* @param access
|
||||
@@ -333,14 +333,14 @@ public abstract class ClassVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor (see {@link Type Type}).
|
||||
* @param signature
|
||||
* the method's signature. May be <code>null</code> if the method
|
||||
* the method's signature. May be <tt>null</tt> if the method
|
||||
* parameters, return type and exceptions do not use generic
|
||||
* types.
|
||||
* @param exceptions
|
||||
* the internal names of the method's exception classes (see
|
||||
* {@link Type#getInternalName() getInternalName}). May be
|
||||
* <code>null</code>.
|
||||
* @return an object to visit the byte code of the method, or <code>null</code>
|
||||
* <tt>null</tt>.
|
||||
* @return an object to visit the byte code of the method, or <tt>null</tt>
|
||||
* if this class visitor is not interested in visiting the code of
|
||||
* this method.
|
||||
*/
|
||||
@@ -391,8 +391,8 @@ public class ClassWriter extends ClassVisitor {
|
||||
* A type table used to temporarily store internal names that will not
|
||||
* necessarily be stored in the constant pool. This type table is used by
|
||||
* the control flow and data flow analysis algorithm used to compute stack
|
||||
* map frames from scratch. This array associates to each index <code>i</code>
|
||||
* the Item whose index is <code>i</code>. All Item objects stored in this array
|
||||
* map frames from scratch. This array associates to each index <tt>i</tt>
|
||||
* the Item whose index is <tt>i</tt>. All Item objects stored in this array
|
||||
* are also stored in the {@link #items} hash table. These two arrays allow
|
||||
* to retrieve an Item from its index or, conversely, to get the index of an
|
||||
* Item from its value. Each Item stores an internal name in its
|
||||
@@ -556,7 +556,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
private int compute;
|
||||
|
||||
/**
|
||||
* <code>true</code> if some methods have wide forward jumps using ASM pseudo
|
||||
* <tt>true</tt> if some methods have wide forward jumps using ASM pseudo
|
||||
* instructions, which need to be expanded into sequences of standard
|
||||
* bytecode instructions. In this case the class is re-read and re-written
|
||||
* with a ClassReader -> ClassWriter chain to perform this transformation.
|
||||
@@ -1486,7 +1486,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor.
|
||||
* @param itf
|
||||
* <code>true</code> if <code>owner</code> is an interface.
|
||||
* <tt>true</tt> if <tt>owner</tt> is an interface.
|
||||
* @return a new or already existing method reference item.
|
||||
*/
|
||||
Item newMethodItem(final String owner, final String name,
|
||||
@@ -1515,7 +1515,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor.
|
||||
* @param itf
|
||||
* <code>true</code> if <code>owner</code> is an interface.
|
||||
* <tt>true</tt> if <tt>owner</tt> is an interface.
|
||||
* @return the index of a new or already existing method reference item.
|
||||
*/
|
||||
public int newMethod(final String owner, final String name,
|
||||
@@ -1778,7 +1778,7 @@ public class ClassWriter extends ClassVisitor {
|
||||
* @param key
|
||||
* a constant pool item.
|
||||
* @return the constant pool's hash table item which is equal to the given
|
||||
* item, or <code>null</code> if there is no such item.
|
||||
* item, or <tt>null</tt> if there is no such item.
|
||||
*/
|
||||
private Item get(final Item key) {
|
||||
Item i = items[key.hashCode % items.length];
|
||||
@@ -60,8 +60,8 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java field. The methods of this class must be called in
|
||||
* the following order: ( <code>visitAnnotation</code> |
|
||||
* <code>visitTypeAnnotation</code> | <code>visitAttribute</code> )* <code>visitEnd</code>.
|
||||
* the following order: ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitTypeAnnotation</tt> | <tt>visitAttribute</tt> )* <tt>visitEnd</tt>.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
*/
|
||||
@@ -111,8 +111,8 @@ public abstract class FieldVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||
@@ -132,12 +132,12 @@ public abstract class FieldVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <code>null</code> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||
@@ -100,28 +100,28 @@ final class FieldWriter extends FieldVisitor {
|
||||
private int value;
|
||||
|
||||
/**
|
||||
* The runtime visible annotations of this field. May be <code>null</code>.
|
||||
* The runtime visible annotations of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter anns;
|
||||
|
||||
/**
|
||||
* The runtime invisible annotations of this field. May be <code>null</code>.
|
||||
* The runtime invisible annotations of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ianns;
|
||||
|
||||
/**
|
||||
* The runtime visible type annotations of this field. May be <code>null</code>.
|
||||
* The runtime visible type annotations of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter tanns;
|
||||
|
||||
/**
|
||||
* The runtime invisible type annotations of this field. May be
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter itanns;
|
||||
|
||||
/**
|
||||
* The non standard attributes of this field. May be <code>null</code>.
|
||||
* The non standard attributes of this field. May be <tt>null</tt>.
|
||||
*/
|
||||
private Attribute attrs;
|
||||
|
||||
@@ -141,9 +141,9 @@ final class FieldWriter extends FieldVisitor {
|
||||
* @param desc
|
||||
* the field's descriptor (see {@link Type}).
|
||||
* @param signature
|
||||
* the field's signature. May be <code>null</code>.
|
||||
* the field's signature. May be <tt>null</tt>.
|
||||
* @param value
|
||||
* the field's constant value. May be <code>null</code>.
|
||||
* the field's constant value. May be <tt>null</tt>.
|
||||
*/
|
||||
FieldWriter(final ClassWriter cw, final int access, final String name,
|
||||
final String desc, final String signature, final Object value) {
|
||||
@@ -1403,7 +1403,7 @@ class Frame {
|
||||
|
||||
/**
|
||||
* Merges the input frame of the given basic block with the input and output
|
||||
* frames of this basic block. Returns <code>true</code> if the input frame of
|
||||
* frames of this basic block. Returns <tt>true</tt> if the input frame of
|
||||
* the given label has been changed by this operation.
|
||||
*
|
||||
* @param cw
|
||||
@@ -1413,7 +1413,7 @@ class Frame {
|
||||
* @param edge
|
||||
* the kind of the {@link Edge} between this label and 'label'.
|
||||
* See {@link Edge#info}.
|
||||
* @return <code>true</code> if the input frame of the given label has been
|
||||
* @return <tt>true</tt> if the input frame of the given label has been
|
||||
* changed by this operation.
|
||||
*/
|
||||
final boolean merge(final ClassWriter cw, final Frame frame, final int edge) {
|
||||
@@ -1511,7 +1511,7 @@ class Frame {
|
||||
|
||||
/**
|
||||
* Merges the type at the given index in the given type array with the given
|
||||
* type. Returns <code>true</code> if the type array has been modified by this
|
||||
* type. Returns <tt>true</tt> if the type array has been modified by this
|
||||
* operation.
|
||||
*
|
||||
* @param cw
|
||||
@@ -1522,7 +1522,7 @@ class Frame {
|
||||
* an array of types.
|
||||
* @param index
|
||||
* the index of the type that must be merged in 'types'.
|
||||
* @return <code>true</code> if the type array has been modified by this
|
||||
* @return <tt>true</tt> if the type array has been modified by this
|
||||
* operation.
|
||||
*/
|
||||
private static boolean merge(final ClassWriter cw, int t,
|
||||
@@ -99,7 +99,6 @@ public final class Handle {
|
||||
*/
|
||||
final boolean itf;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new field or method handle.
|
||||
*
|
||||
@@ -82,7 +82,7 @@ class Handler {
|
||||
|
||||
/**
|
||||
* Internal name of the type of exceptions handled by this handler, or
|
||||
* <code>null</code> to catch any exceptions.
|
||||
* <tt>null</tt> to catch any exceptions.
|
||||
*/
|
||||
String desc;
|
||||
|
||||
@@ -306,8 +306,8 @@ final class Item {
|
||||
* @param i
|
||||
* the item to be compared to this one. Both items must have the
|
||||
* same {@link #type}.
|
||||
* @return <code>true</code> if the given item if equal to this one,
|
||||
* <code>false</code> otherwise.
|
||||
* @return <tt>true</tt> if the given item if equal to this one,
|
||||
* <tt>false</tt> otherwise.
|
||||
*/
|
||||
boolean isEqualTo(final Item i) {
|
||||
switch (type) {
|
||||
@@ -325,8 +325,8 @@ public class Label {
|
||||
* the position of first byte of the bytecode instruction that
|
||||
* contains this label.
|
||||
* @param wideOffset
|
||||
* <code>true</code> if the reference must be stored in 4 bytes, or
|
||||
* <code>false</code> if it must be stored with 2 bytes.
|
||||
* <tt>true</tt> if the reference must be stored in 4 bytes, or
|
||||
* <tt>false</tt> if it must be stored with 2 bytes.
|
||||
* @throws IllegalArgumentException
|
||||
* if this label has not been created by the given code writer.
|
||||
*/
|
||||
@@ -389,7 +389,7 @@ public class Label {
|
||||
* the position of this label in the bytecode.
|
||||
* @param data
|
||||
* the bytecode of the method.
|
||||
* @return <code>true</code> if a blank that was left for this label was too
|
||||
* @return <tt>true</tt> if a blank that was left for this label was too
|
||||
* small to store the offset. In such a case the corresponding jump
|
||||
* instruction is replaced with a pseudo instruction (using unused
|
||||
* opcodes) using an unsigned two bytes offset. These pseudo
|
||||
@@ -5,7 +5,10 @@
|
||||
*/
|
||||
package org.redkale.asm;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import static org.redkale.asm.Opcodes.*;
|
||||
|
||||
/**
|
||||
* MethodVisitor 的调试类
|
||||
@@ -54,11 +57,20 @@ public class MethodDebugVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param visitor MethodVisitor
|
||||
*/
|
||||
public MethodDebugVisitor(MethodVisitor visitor) {
|
||||
//super(Opcodes.ASM5, visitor);
|
||||
this.visitor = visitor;
|
||||
}
|
||||
|
||||
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
|
||||
visitor.visitTryCatchBlock(start, end, handler, type);
|
||||
if (debug) System.out.println("mv.visitTryCatchBlock(label0, label1, label2, \"" + type + "\");");
|
||||
}
|
||||
|
||||
public AnnotationVisitor visitParameterAnnotation(int i, String string, boolean bln) {
|
||||
AnnotationVisitor av = visitor.visitParameterAnnotation(i, string, bln);
|
||||
if (debug) System.out.println("mv.visitParameterAnnotation(" + i + ", \"" + string + "\", " + bln + ");");
|
||||
@@ -189,4 +201,74 @@ public class MethodDebugVisitor {
|
||||
visitor.visitEnd();
|
||||
if (debug) System.out.println("mv.visitEnd();\r\n\r\n\r\n");
|
||||
}
|
||||
|
||||
public static void pushInt(MethodDebugVisitor mv, int num) {
|
||||
if (num < 6) {
|
||||
mv.visitInsn(ICONST_0 + num);
|
||||
} else if (num <= Byte.MAX_VALUE) {
|
||||
mv.visitIntInsn(BIPUSH, num);
|
||||
} else if (num <= Short.MAX_VALUE) {
|
||||
mv.visitIntInsn(SIPUSH, num);
|
||||
} else {
|
||||
mv.visitLdcInsn(num);
|
||||
}
|
||||
}
|
||||
|
||||
public static void pushInt(MethodVisitor mv, int num) {
|
||||
if (num < 6) {
|
||||
mv.visitInsn(ICONST_0 + num);
|
||||
} else if (num <= Byte.MAX_VALUE) {
|
||||
mv.visitIntInsn(BIPUSH, num);
|
||||
} else if (num <= Short.MAX_VALUE) {
|
||||
mv.visitIntInsn(SIPUSH, num);
|
||||
} else {
|
||||
mv.visitLdcInsn(num);
|
||||
}
|
||||
}
|
||||
|
||||
public static void visitAnnotation(final AnnotationVisitor av, final Annotation ann) {
|
||||
try {
|
||||
for (Method anm : ann.annotationType().getMethods()) {
|
||||
final String mname = anm.getName();
|
||||
if ("equals".equals(mname) || "hashCode".equals(mname) || "toString".equals(mname) || "annotationType".equals(mname)) continue;
|
||||
final Object r = anm.invoke(ann);
|
||||
if (r instanceof String[]) {
|
||||
AnnotationVisitor av1 = av.visitArray(mname);
|
||||
for (String item : (String[]) r) {
|
||||
av1.visit(null, item);
|
||||
}
|
||||
av1.visitEnd();
|
||||
} else if (r instanceof Class[]) {
|
||||
AnnotationVisitor av1 = av.visitArray(mname);
|
||||
for (Class item : (Class[]) r) {
|
||||
av1.visit(null, Type.getType(item));
|
||||
}
|
||||
av1.visitEnd();
|
||||
} else if (r instanceof Enum[]) {
|
||||
AnnotationVisitor av1 = av.visitArray(mname);
|
||||
for (Enum item : (Enum[]) r) {
|
||||
av1.visitEnum(null, Type.getDescriptor(item.getClass()), ((Enum) item).name());
|
||||
}
|
||||
av1.visitEnd();
|
||||
} else if (r instanceof Annotation[]) {
|
||||
AnnotationVisitor av1 = av.visitArray(mname);
|
||||
for (Annotation item : (Annotation[]) r) {
|
||||
visitAnnotation(av1.visitAnnotation(null, Type.getDescriptor(((Annotation) item).annotationType())), item);
|
||||
}
|
||||
av1.visitEnd();
|
||||
} else if (r instanceof Class) {
|
||||
av.visit(mname, Type.getType((Class) r));
|
||||
} else if (r instanceof Enum) {
|
||||
av.visitEnum(mname, Type.getDescriptor(r.getClass()), ((Enum) r).name());
|
||||
} else if (r instanceof Annotation) {
|
||||
visitAnnotation(av.visitAnnotation(null, Type.getDescriptor(((Annotation) r).annotationType())), (Annotation) r);
|
||||
} else {
|
||||
av.visit(mname, r);
|
||||
}
|
||||
}
|
||||
av.visitEnd();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,24 +60,24 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java method. The methods of this class must be called in
|
||||
* the following order: ( <code>visitParameter</code> )* [
|
||||
* <code>visitAnnotationDefault</code> ] ( <code>visitAnnotation</code> |
|
||||
* <code>visitParameterAnnotation</code> <code>visitTypeAnnotation</code> |
|
||||
* <code>visitAttribute</code> )* [ <code>visitCode</code> ( <code>visitFrame</code> |
|
||||
* <code>visit<i>X</i>Insn</code> | <code>visitLabel</code> |
|
||||
* <code>visitInsnAnnotation</code> | <code>visitTryCatchBlock</code> |
|
||||
* <code>visitTryCatchAnnotation</code> | <code>visitLocalVariable</code> |
|
||||
* <code>visitLocalVariableAnnotation</code> | <code>visitLineNumber</code> )*
|
||||
* <code>visitMaxs</code> ] <code>visitEnd</code>. In addition, the
|
||||
* <code>visit<i>X</i>Insn</code> and <code>visitLabel</code> methods must be called in
|
||||
* the following order: ( <tt>visitParameter</tt> )* [
|
||||
* <tt>visitAnnotationDefault</tt> ] ( <tt>visitAnnotation</tt> |
|
||||
* <tt>visitParameterAnnotation</tt> <tt>visitTypeAnnotation</tt> |
|
||||
* <tt>visitAttribute</tt> )* [ <tt>visitCode</tt> ( <tt>visitFrame</tt> |
|
||||
* <tt>visit<i>X</i>Insn</tt> | <tt>visitLabel</tt> |
|
||||
* <tt>visitInsnAnnotation</tt> | <tt>visitTryCatchBlock</tt> |
|
||||
* <tt>visitTryCatchAnnotation</tt> | <tt>visitLocalVariable</tt> |
|
||||
* <tt>visitLocalVariableAnnotation</tt> | <tt>visitLineNumber</tt> )*
|
||||
* <tt>visitMaxs</tt> ] <tt>visitEnd</tt>. In addition, the
|
||||
* <tt>visit<i>X</i>Insn</tt> and <tt>visitLabel</tt> methods must be called in
|
||||
* the sequential order of the bytecode instructions of the visited code,
|
||||
* <code>visitInsnAnnotation</code> must be called <i>after</i> the annotated
|
||||
* instruction, <code>visitTryCatchBlock</code> must be called <i>before</i> the
|
||||
* <tt>visitInsnAnnotation</tt> must be called <i>after</i> the annotated
|
||||
* instruction, <tt>visitTryCatchBlock</tt> must be called <i>before</i> the
|
||||
* labels passed as arguments have been visited,
|
||||
* <code>visitTryCatchBlockAnnotation</code> must be called <i>after</i> the
|
||||
* <tt>visitTryCatchBlockAnnotation</tt> must be called <i>after</i> the
|
||||
* corresponding try catch block has been visited, and the
|
||||
* <code>visitLocalVariable</code>, <code>visitLocalVariableAnnotation</code> and
|
||||
* <code>visitLineNumber</code> methods must be called <i>after</i> the labels
|
||||
* <tt>visitLocalVariable</tt>, <tt>visitLocalVariableAnnotation</tt> and
|
||||
* <tt>visitLineNumber</tt> methods must be called <i>after</i> the labels
|
||||
* passed as arguments have been visited.
|
||||
*
|
||||
* @author Eric Bruneton
|
||||
@@ -132,8 +132,8 @@ public abstract class MethodVisitor {
|
||||
* @param name
|
||||
* parameter name or null if none is provided.
|
||||
* @param access
|
||||
* the parameter's access flags, only <code>ACC_FINAL</code>,
|
||||
* <code>ACC_SYNTHETIC</code> or/and <code>ACC_MANDATED</code> are
|
||||
* the parameter's access flags, only <tt>ACC_FINAL</tt>,
|
||||
* <tt>ACC_SYNTHETIC</tt> or/and <tt>ACC_MANDATED</tt> are
|
||||
* allowed (see {@link Opcodes}).
|
||||
*/
|
||||
public void visitParameter(String name, int access) {
|
||||
@@ -146,7 +146,7 @@ public abstract class MethodVisitor {
|
||||
* Visits the default value of this annotation interface method.
|
||||
*
|
||||
* @return a visitor to the visit the actual default value of this
|
||||
* annotation interface method, or <code>null</code> if this visitor is
|
||||
* annotation interface method, or <tt>null</tt> if this visitor is
|
||||
* not interested in visiting this default value. The 'name'
|
||||
* parameters passed to the methods of this annotation visitor are
|
||||
* ignored. Moreover, exacly one visit method must be called on this
|
||||
@@ -165,8 +165,8 @@ public abstract class MethodVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||
@@ -193,12 +193,12 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <code>null</code> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||
@@ -217,8 +217,8 @@ public abstract class MethodVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitParameterAnnotation(int parameter,
|
||||
@@ -444,7 +444,6 @@ public abstract class MethodVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Visits a method instruction. A method instruction is an instruction that
|
||||
* invokes a method.
|
||||
@@ -569,7 +568,7 @@ public abstract class MethodVisitor {
|
||||
* the constant to be loaded on the stack. This parameter must be
|
||||
* a non null {@link Integer}, a {@link Float}, a {@link Long}, a
|
||||
* {@link Double}, a {@link String}, a {@link Type} of OBJECT or
|
||||
* ARRAY sort for <code>.class</code> constants, for classes whose
|
||||
* ARRAY sort for <tt>.class</tt> constants, for classes whose
|
||||
* version is 49.0, a {@link Type} of METHOD sort or a
|
||||
* {@link Handle} for MethodType and MethodHandle constants, for
|
||||
* classes whose version is 51.0.
|
||||
@@ -604,8 +603,8 @@ public abstract class MethodVisitor {
|
||||
* @param dflt
|
||||
* beginning of the default handler block.
|
||||
* @param labels
|
||||
* beginnings of the handler blocks. <code>labels[i]</code> is the
|
||||
* beginning of the handler block for the <code>min + i</code> key.
|
||||
* beginnings of the handler blocks. <tt>labels[i]</tt> is the
|
||||
* beginning of the handler block for the <tt>min + i</tt> key.
|
||||
*/
|
||||
public void visitTableSwitchInsn(int min, int max, Label dflt,
|
||||
Label... labels) {
|
||||
@@ -622,8 +621,8 @@ public abstract class MethodVisitor {
|
||||
* @param keys
|
||||
* the values of the keys.
|
||||
* @param labels
|
||||
* beginnings of the handler blocks. <code>labels[i]</code> is the
|
||||
* beginning of the handler block for the <code>keys[i]</code> key.
|
||||
* beginnings of the handler blocks. <tt>labels[i]</tt> is the
|
||||
* beginning of the handler block for the <tt>keys[i]</tt> key.
|
||||
*/
|
||||
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
|
||||
if (mv != null) {
|
||||
@@ -668,12 +667,12 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <code>null</code> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitInsnAnnotation(int typeRef,
|
||||
@@ -699,7 +698,7 @@ public abstract class MethodVisitor {
|
||||
* beginning of the exception handler's code.
|
||||
* @param type
|
||||
* internal name of the type of exceptions handled by the
|
||||
* handler, or <code>null</code> to catch any exceptions (for
|
||||
* handler, or <tt>null</tt> to catch any exceptions (for
|
||||
* "finally" blocks).
|
||||
* @throws IllegalArgumentException
|
||||
* if one of the labels has already been visited by this visitor
|
||||
@@ -725,12 +724,12 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <code>null</code> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitTryCatchAnnotation(int typeRef,
|
||||
@@ -750,7 +749,7 @@ public abstract class MethodVisitor {
|
||||
* the type descriptor of this local variable.
|
||||
* @param signature
|
||||
* the type signature of this local variable. May be
|
||||
* <code>null</code> if the local variable type does not use generic
|
||||
* <tt>null</tt> if the local variable type does not use generic
|
||||
* types.
|
||||
* @param start
|
||||
* the first instruction corresponding to the scope of this local
|
||||
@@ -782,7 +781,7 @@ public abstract class MethodVisitor {
|
||||
* @param typePath
|
||||
* the path to the annotated type argument, wildcard bound, array
|
||||
* element type, or static inner type within 'typeRef'. May be
|
||||
* <code>null</code> if the annotation targets 'typeRef' as a whole.
|
||||
* <tt>null</tt> if the annotation targets 'typeRef' as a whole.
|
||||
* @param start
|
||||
* the fist instructions corresponding to the continuous ranges
|
||||
* that make the scope of this local variable (inclusive).
|
||||
@@ -796,8 +795,8 @@ public abstract class MethodVisitor {
|
||||
* @param desc
|
||||
* the class descriptor of the annotation class.
|
||||
* @param visible
|
||||
* <code>true</code> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <code>null</code> if
|
||||
* <tt>true</tt> if the annotation is visible at runtime.
|
||||
* @return a visitor to visit the annotation values, or <tt>null</tt> if
|
||||
* this visitor is not interested in visiting this annotation.
|
||||
*/
|
||||
public AnnotationVisitor visitLocalVariableAnnotation(int typeRef,
|
||||
@@ -819,7 +818,7 @@ public abstract class MethodVisitor {
|
||||
* @param start
|
||||
* the first instruction corresponding to this line number.
|
||||
* @throws IllegalArgumentException
|
||||
* if <code>start</code> has not already been visited by this
|
||||
* if <tt>start</tt> has not already been visited by this
|
||||
* visitor (by the {@link #visitLabel visitLabel} method).
|
||||
*/
|
||||
public void visitLineNumber(int line, Label start) {
|
||||
@@ -218,41 +218,41 @@ class MethodWriter extends MethodVisitor {
|
||||
int[] exceptions;
|
||||
|
||||
/**
|
||||
* The annotation default attribute of this method. May be <code>null</code>.
|
||||
* The annotation default attribute of this method. May be <tt>null</tt>.
|
||||
*/
|
||||
private ByteVector annd;
|
||||
|
||||
/**
|
||||
* The runtime visible annotations of this method. May be <code>null</code>.
|
||||
* The runtime visible annotations of this method. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter anns;
|
||||
|
||||
/**
|
||||
* The runtime invisible annotations of this method. May be <code>null</code>.
|
||||
* The runtime invisible annotations of this method. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ianns;
|
||||
|
||||
/**
|
||||
* The runtime visible type annotations of this method. May be <code>null</code>
|
||||
* The runtime visible type annotations of this method. May be <tt>null</tt>
|
||||
* .
|
||||
*/
|
||||
private AnnotationWriter tanns;
|
||||
|
||||
/**
|
||||
* The runtime invisible type annotations of this method. May be
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter itanns;
|
||||
|
||||
/**
|
||||
* The runtime visible parameter annotations of this method. May be
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter[] panns;
|
||||
|
||||
/**
|
||||
* The runtime invisible parameter annotations of this method. May be
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter[] ipanns;
|
||||
|
||||
@@ -381,12 +381,12 @@ class MethodWriter extends MethodVisitor {
|
||||
private int lastCodeOffset;
|
||||
|
||||
/**
|
||||
* The runtime visible type annotations of the code. May be <code>null</code>.
|
||||
* The runtime visible type annotations of the code. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ctanns;
|
||||
|
||||
/**
|
||||
* The runtime invisible type annotations of the code. May be <code>null</code>.
|
||||
* The runtime invisible type annotations of the code. May be <tt>null</tt>.
|
||||
*/
|
||||
private AnnotationWriter ictanns;
|
||||
|
||||
@@ -446,7 +446,7 @@ class MethodWriter extends MethodVisitor {
|
||||
* is relative to the beginning of the current basic block, i.e., the true
|
||||
* stack size after the last visited instruction is equal to the
|
||||
* {@link Label#inputStackTop beginStackSize} of the current basic block
|
||||
* plus <code>stackSize</code>.
|
||||
* plus <tt>stackSize</tt>.
|
||||
*/
|
||||
private int stackSize;
|
||||
|
||||
@@ -455,7 +455,7 @@ class MethodWriter extends MethodVisitor {
|
||||
* This size is relative to the beginning of the current basic block, i.e.,
|
||||
* the true maximum stack size after the last visited instruction is equal
|
||||
* to the {@link Label#inputStackTop beginStackSize} of the current basic
|
||||
* block plus <code>stackSize</code>.
|
||||
* block plus <tt>stackSize</tt>.
|
||||
*/
|
||||
private int maxStackSize;
|
||||
|
||||
@@ -475,10 +475,10 @@ class MethodWriter extends MethodVisitor {
|
||||
* @param desc
|
||||
* the method's descriptor (see {@link Type}).
|
||||
* @param signature
|
||||
* the method's signature. May be <code>null</code>.
|
||||
* the method's signature. May be <tt>null</tt>.
|
||||
* @param exceptions
|
||||
* the internal names of the method's exceptions. May be
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
* @param compute
|
||||
* Indicates what must be automatically computed (see #compute).
|
||||
*/
|
||||
@@ -60,9 +60,9 @@ package org.redkale.asm;
|
||||
|
||||
/**
|
||||
* A visitor to visit a Java module. The methods of this class must be called in
|
||||
* the following order: <code>visitMainClass</code> | ( <code>visitPackage</code> |
|
||||
* <code>visitRequire</code> | <code>visitExport</code> | <code>visitOpen</code> |
|
||||
* <code>visitUse</code> | <code>visitProvide</code> )* <code>visitEnd</code>.
|
||||
* the following order: <tt>visitMainClass</tt> | ( <tt>visitPackage</tt> |
|
||||
* <tt>visitRequire</tt> | <tt>visitExport</tt> | <tt>visitOpen</tt> |
|
||||
* <tt>visitUse</tt> | <tt>visitProvide</tt> )* <tt>visitEnd</tt>.
|
||||
*
|
||||
* The methods {@link #visitRequire(String, int, String)}, {@link #visitExport(String, int, String...)},
|
||||
* {@link #visitOpen(String, int, String...)} and {@link #visitPackage(String)}
|
||||
@@ -157,7 +157,7 @@ public abstract class ModuleVisitor {
|
||||
* {@code ACC_MANDATED}.
|
||||
* @param modules the qualified names of the modules that can access to
|
||||
* the public classes of the exported package or
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visitExport(String packaze, int access, String... modules) {
|
||||
if (mv != null) {
|
||||
@@ -174,7 +174,7 @@ public abstract class ModuleVisitor {
|
||||
* {@code ACC_MANDATED}.
|
||||
* @param modules the qualified names of the modules that can use deep
|
||||
* reflection to the classes of the open package or
|
||||
* <code>null</code>.
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void visitOpen(String packaze, int access, String... modules) {
|
||||
if (mv != null) {
|
||||
@@ -73,19 +73,23 @@ package org.redkale.asm;
|
||||
public interface Opcodes {
|
||||
|
||||
// ASM API versions
|
||||
|
||||
int ASM4 = 4 << 16 | 0 << 8 | 0;
|
||||
int ASM5 = 5 << 16 | 0 << 8 | 0;
|
||||
int ASM6 = 6 << 16 | 0 << 8 | 0;
|
||||
int ASM4 = 4 << 16 | 0 << 8;
|
||||
int ASM5 = 5 << 16 | 0 << 8;
|
||||
int ASM6 = 6 << 16 | 0 << 8;
|
||||
|
||||
// versions
|
||||
|
||||
int V1_1 = 3 << 16 | 45;
|
||||
int V1_2 = 0 << 16 | 46;
|
||||
int V1_3 = 0 << 16 | 47;
|
||||
int V1_4 = 0 << 16 | 48;
|
||||
int V1_5 = 0 << 16 | 49;
|
||||
int V1_6 = 0 << 16 | 50;
|
||||
int V1_7 = 0 << 16 | 51;
|
||||
int V1_8 = 0 << 16 | 52;
|
||||
int V9 = 0 << 16 | 53;
|
||||
int V10 = 0 << 16 | 54;
|
||||
int V11 = 0 << 16 | 55;
|
||||
|
||||
// access flags
|
||||
|
||||
@@ -71,47 +71,47 @@ import java.lang.reflect.Method;
|
||||
public class Type {
|
||||
|
||||
/**
|
||||
* The sort of the <code>void</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>void</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int VOID = 0;
|
||||
|
||||
/**
|
||||
* The sort of the <code>boolean</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>boolean</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int BOOLEAN = 1;
|
||||
|
||||
/**
|
||||
* The sort of the <code>char</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>char</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int CHAR = 2;
|
||||
|
||||
/**
|
||||
* The sort of the <code>byte</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>byte</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int BYTE = 3;
|
||||
|
||||
/**
|
||||
* The sort of the <code>short</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>short</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int SHORT = 4;
|
||||
|
||||
/**
|
||||
* The sort of the <code>int</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>int</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int INT = 5;
|
||||
|
||||
/**
|
||||
* The sort of the <code>float</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>float</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int FLOAT = 6;
|
||||
|
||||
/**
|
||||
* The sort of the <code>long</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>long</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int LONG = 7;
|
||||
|
||||
/**
|
||||
* The sort of the <code>double</code> type. See {@link #getSort getSort}.
|
||||
* The sort of the <tt>double</tt> type. See {@link #getSort getSort}.
|
||||
*/
|
||||
public static final int DOUBLE = 8;
|
||||
|
||||
@@ -131,55 +131,55 @@ public class Type {
|
||||
public static final int METHOD = 11;
|
||||
|
||||
/**
|
||||
* The <code>void</code> type.
|
||||
* The <tt>void</tt> type.
|
||||
*/
|
||||
public static final Type VOID_TYPE = new Type(VOID, null, ('V' << 24)
|
||||
| (5 << 16) | (0 << 8) | 0, 1);
|
||||
|
||||
/**
|
||||
* The <code>boolean</code> type.
|
||||
* The <tt>boolean</tt> type.
|
||||
*/
|
||||
public static final Type BOOLEAN_TYPE = new Type(BOOLEAN, null, ('Z' << 24)
|
||||
| (0 << 16) | (5 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <code>char</code> type.
|
||||
* The <tt>char</tt> type.
|
||||
*/
|
||||
public static final Type CHAR_TYPE = new Type(CHAR, null, ('C' << 24)
|
||||
| (0 << 16) | (6 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <code>byte</code> type.
|
||||
* The <tt>byte</tt> type.
|
||||
*/
|
||||
public static final Type BYTE_TYPE = new Type(BYTE, null, ('B' << 24)
|
||||
| (0 << 16) | (5 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <code>short</code> type.
|
||||
* The <tt>short</tt> type.
|
||||
*/
|
||||
public static final Type SHORT_TYPE = new Type(SHORT, null, ('S' << 24)
|
||||
| (0 << 16) | (7 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <code>int</code> type.
|
||||
* The <tt>int</tt> type.
|
||||
*/
|
||||
public static final Type INT_TYPE = new Type(INT, null, ('I' << 24)
|
||||
| (0 << 16) | (0 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <code>float</code> type.
|
||||
* The <tt>float</tt> type.
|
||||
*/
|
||||
public static final Type FLOAT_TYPE = new Type(FLOAT, null, ('F' << 24)
|
||||
| (2 << 16) | (2 << 8) | 1, 1);
|
||||
|
||||
/**
|
||||
* The <code>long</code> type.
|
||||
* The <tt>long</tt> type.
|
||||
*/
|
||||
public static final Type LONG_TYPE = new Type(LONG, null, ('J' << 24)
|
||||
| (1 << 16) | (1 << 8) | 2, 1);
|
||||
|
||||
/**
|
||||
* The <code>double</code> type.
|
||||
* The <tt>double</tt> type.
|
||||
*/
|
||||
public static final Type DOUBLE_TYPE = new Type(DOUBLE, null, ('D' << 24)
|
||||
| (3 << 16) | (3 << 8) | 2, 1);
|
||||
@@ -439,8 +439,8 @@ public class Type {
|
||||
* @return the size of the arguments of the method (plus one for the
|
||||
* implicit this argument), argSize, and the size of its return
|
||||
* value, retSize, packed into a single int i =
|
||||
* <code>(argSize << 2) | retSize</code> (argSize is therefore equal to
|
||||
* <code>i >> 2</code>, and retSize to <code>i & 0x03</code>).
|
||||
* <tt>(argSize << 2) | retSize</tt> (argSize is therefore equal to
|
||||
* <tt>i >> 2</tt>, and retSize to <tt>i & 0x03</tt>).
|
||||
*/
|
||||
public static int getArgumentsAndReturnSizes(final String desc) {
|
||||
int n = 1;
|
||||
@@ -645,9 +645,9 @@ public class Type {
|
||||
* @return the size of the arguments (plus one for the implicit this
|
||||
* argument), argSize, and the size of the return value, retSize,
|
||||
* packed into a single
|
||||
* int i = <code>(argSize << 2) | retSize</code>
|
||||
* (argSize is therefore equal to <code>i >> 2</code>,
|
||||
* and retSize to <code>i & 0x03</code>).
|
||||
* int i = <tt>(argSize << 2) | retSize</tt>
|
||||
* (argSize is therefore equal to <tt>i >> 2</tt>,
|
||||
* and retSize to <tt>i & 0x03</tt>).
|
||||
*/
|
||||
public int getArgumentsAndReturnSizes() {
|
||||
return getArgumentsAndReturnSizes(getDescriptor());
|
||||
@@ -838,8 +838,8 @@ public class Type {
|
||||
* Returns the size of values of this type. This method must not be used for
|
||||
* method types.
|
||||
*
|
||||
* @return the size of values of this type, i.e., 2 for <code>long</code> and
|
||||
* <code>double</code>, 0 for <code>void</code> and 1 otherwise.
|
||||
* @return the size of values of this type, i.e., 2 for <tt>long</tt> and
|
||||
* <tt>double</tt>, 0 for <tt>void</tt> and 1 otherwise.
|
||||
*/
|
||||
public int getSize() {
|
||||
// the size is in byte 0 of 'off' for primitive types (buf == null)
|
||||
@@ -855,8 +855,8 @@ public class Type {
|
||||
* ISTORE, IALOAD, IASTORE, IADD, ISUB, IMUL, IDIV, IREM, INEG,
|
||||
* ISHL, ISHR, IUSHR, IAND, IOR, IXOR and IRETURN.
|
||||
* @return an opcode that is similar to the given opcode, but adapted to
|
||||
* this Java type. For example, if this type is <code>float</code> and
|
||||
* <code>opcode</code> is IRETURN, this method returns FRETURN.
|
||||
* this Java type. For example, if this type is <tt>float</tt> and
|
||||
* <tt>opcode</tt> is IRETURN, this method returns FRETURN.
|
||||
*/
|
||||
public int getOpcode(final int opcode) {
|
||||
if (opcode == Opcodes.IALOAD || opcode == Opcodes.IASTORE) {
|
||||
@@ -879,7 +879,7 @@ public class Type {
|
||||
*
|
||||
* @param o
|
||||
* the object to be compared to this type.
|
||||
* @return <code>true</code> if the given object is equal to this type.
|
||||
* @return <tt>true</tt> if the given object is equal to this type.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
@@ -161,7 +161,7 @@ public class TypePath {
|
||||
* @return the corresponding TypePath object, or null if the path is empty.
|
||||
*/
|
||||
public static TypePath fromString(final String typePath) {
|
||||
if (typePath == null || typePath.length() == 0) {
|
||||
if (typePath == null || typePath.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
int n = typePath.length();
|
||||
535
src/main/java/org/redkale/boot/ApiDocsService.java
Normal file
535
src/main/java/org/redkale/boot/ApiDocsService.java
Normal file
@@ -0,0 +1,535 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.boot;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.math.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.logging.*;
|
||||
import javax.persistence.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.mq.MessageMultiConsumer;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.service.RetResult;
|
||||
import org.redkale.source.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* API接口文档生成类,作用:生成Application实例中所有HttpServer的可用HttpServlet的API接口方法 <br>
|
||||
* 继承 HttpBaseServlet 是为了获取 HttpMapping 信息 <br>
|
||||
* https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public final class ApiDocsService {
|
||||
|
||||
private static final java.lang.reflect.Type TYPE_RETRESULT_OBJECT = new TypeToken<RetResult<Object>>() {
|
||||
}.getType();
|
||||
|
||||
private static final java.lang.reflect.Type TYPE_RETRESULT_STRING = new TypeToken<RetResult<String>>() {
|
||||
}.getType();
|
||||
|
||||
private static final java.lang.reflect.Type TYPE_RETRESULT_INTEGER = new TypeToken<RetResult<Integer>>() {
|
||||
}.getType();
|
||||
|
||||
private static final java.lang.reflect.Type TYPE_RETRESULT_LONG = new TypeToken<RetResult<Long>>() {
|
||||
}.getType();
|
||||
|
||||
private final Application app; //Application全局对象
|
||||
|
||||
public ApiDocsService(Application app) {
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
public void run(String[] args) throws Exception {
|
||||
//是否跳过RPC接口
|
||||
final boolean skipRPC = Arrays.toString(args).toLowerCase().contains("skip-rpc") && !Arrays.toString(args).toLowerCase().contains("skip-rpc=false");
|
||||
|
||||
List<Map> serverList = new ArrayList<>();
|
||||
Field __prefix = HttpServlet.class.getDeclaredField("_prefix");
|
||||
__prefix.setAccessible(true);
|
||||
Map<String, Map<String, Map<String, Object>>> typesMap = new LinkedHashMap<>();
|
||||
//https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md
|
||||
Map<String, Object> swaggerPathsMap = new LinkedHashMap<>();
|
||||
List<Map> swaggerServers = new ArrayList<>();
|
||||
List<Map> swaggerTags = new ArrayList<>();
|
||||
Map<String, Map<String, Object>> swaggerComponentsMap = new LinkedHashMap<>();
|
||||
for (NodeServer node : app.servers) {
|
||||
if (!(node instanceof NodeHttpServer)) continue;
|
||||
final Map<String, Object> map = new LinkedHashMap<>();
|
||||
serverList.add(map);
|
||||
HttpServer server = node.getServer();
|
||||
map.put("address", server.getSocketAddress());
|
||||
swaggerServers.add(Utility.ofMap("url", "http://localhost:" + server.getSocketAddress().getPort()));
|
||||
List<Map<String, Object>> servletsList = new ArrayList<>();
|
||||
map.put("servlets", servletsList);
|
||||
String plainContentType = server.getResponseConfig() == null ? "application/json" : server.getResponseConfig().plainContentType;
|
||||
if (plainContentType == null || plainContentType.isEmpty()) plainContentType = "application/json";
|
||||
if (plainContentType.indexOf(';') > 0) plainContentType = plainContentType.substring(0, plainContentType.indexOf(';'));
|
||||
|
||||
for (HttpServlet servlet : server.getPrepareServlet().getServlets()) {
|
||||
if (!(servlet instanceof HttpServlet)) continue;
|
||||
if (servlet instanceof WebSocketServlet) continue;
|
||||
if (servlet.getClass().getAnnotation(MessageMultiConsumer.class) != null) {
|
||||
node.logger.log(Level.INFO, servlet + " be skipped because has @MessageMultiConsumer");
|
||||
continue;
|
||||
}
|
||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||
if (ws == null) {
|
||||
node.logger.log(Level.WARNING, servlet + " not found @WebServlet");
|
||||
continue;
|
||||
}
|
||||
if (ws.name().isEmpty()) {
|
||||
node.logger.log(Level.INFO, servlet + " be skipped because @WebServlet.name is empty");
|
||||
continue;
|
||||
}
|
||||
final String tag = ws.name().isEmpty() ? servlet.getClass().getSimpleName().replace("Servlet", "").toLowerCase() : ws.name();
|
||||
final Map<String, Object> servletMap = new LinkedHashMap<>();
|
||||
String prefix = (String) __prefix.get(servlet);
|
||||
String[] urlregs = ws.value();
|
||||
if (prefix != null && !prefix.isEmpty()) {
|
||||
for (int i = 0; i < urlregs.length; i++) {
|
||||
urlregs[i] = prefix + urlregs[i];
|
||||
}
|
||||
}
|
||||
servletMap.put("urlregs", urlregs);
|
||||
servletMap.put("moduleid", ws.moduleid());
|
||||
servletMap.put("name", ws.name());
|
||||
servletMap.put("comment", ws.comment());
|
||||
|
||||
List<Map> mappingsList = new ArrayList<>();
|
||||
servletMap.put("mappings", mappingsList);
|
||||
final Class selfClz = servlet.getClass();
|
||||
Class clz = servlet.getClass();
|
||||
HashSet<String> actionUrls = new HashSet<>();
|
||||
do {
|
||||
if (Modifier.isAbstract(clz.getModifiers())) break;
|
||||
for (Method method : clz.getMethods()) {
|
||||
if (method.getParameterCount() != 2) continue;
|
||||
HttpMapping action = method.getAnnotation(HttpMapping.class);
|
||||
if (action == null) continue;
|
||||
if (!action.inherited() && selfClz != clz) continue; //忽略不被继承的方法
|
||||
if (actionUrls.contains(action.url())) continue;
|
||||
if (HttpScope.class.isAssignableFrom(action.result())) continue; //忽略模板引擎的方法
|
||||
if (action.rpconly() && skipRPC) continue; //不生成RPC接口
|
||||
|
||||
final List<Map<String, Object>> swaggerParamsList = new ArrayList<>();
|
||||
|
||||
final Map<String, Object> mappingMap = new LinkedHashMap<>();
|
||||
mappingMap.put("url", prefix + action.url());
|
||||
actionUrls.add(action.url());
|
||||
mappingMap.put("auth", action.auth());
|
||||
mappingMap.put("actionid", action.actionid());
|
||||
mappingMap.put("comment", action.comment());
|
||||
List<Map> paramsList = new ArrayList<>();
|
||||
mappingMap.put("params", paramsList);
|
||||
List<String> results = new ArrayList<>();
|
||||
Type resultType = action.result();
|
||||
if (!action.resultref().isEmpty()) {
|
||||
Field f = servlet.getClass().getDeclaredField(action.resultref());
|
||||
f.setAccessible(true);
|
||||
resultType = (Type) f.get(servlet);
|
||||
}
|
||||
// for (final Class rtype : action.results()) {
|
||||
// results.add(rtype.getName());
|
||||
// if (typesMap.containsKey(rtype.getName())) continue;
|
||||
// if (rtype.getName().startsWith("java.")) continue;
|
||||
// if (rtype.getName().startsWith("javax.")) continue;
|
||||
// final boolean filter = FilterBean.class.isAssignableFrom(rtype);
|
||||
// final Map<String, Map<String, Object>> typeMap = new LinkedHashMap<>();
|
||||
// Class loop = rtype;
|
||||
// do {
|
||||
// if (loop == null || loop.isInterface()) break;
|
||||
// for (Field field : loop.getDeclaredFields()) {
|
||||
// if (Modifier.isFinal(field.getModifiers())) continue;
|
||||
// if (Modifier.isStatic(field.getModifiers())) continue;
|
||||
//
|
||||
// Map<String, Object> fieldmap = new LinkedHashMap<>();
|
||||
// fieldmap.put("type", field.getType().isArray() ? (field.getType().getComponentType().getName() + "[]") : field.getGenericType().getTypeName());
|
||||
//
|
||||
// Comment comment = field.getAnnotation(Comment.class);
|
||||
// Column col = field.getAnnotation(Column.class);
|
||||
// FilterColumn fc = field.getAnnotation(FilterColumn.class);
|
||||
// if (comment != null) {
|
||||
// fieldmap.put("comment", comment.value());
|
||||
// } else if (col != null) {
|
||||
// fieldmap.put("comment", col.comment());
|
||||
// } else if (fc != null) {
|
||||
// fieldmap.put("comment", fc.comment());
|
||||
// }
|
||||
// fieldmap.put("primary", !filter && (field.getAnnotation(Id.class) != null));
|
||||
// fieldmap.put("updatable", (filter || col == null || col.updatable()));
|
||||
// if (servlet.getClass().getAnnotation(Rest.RestDyn.class) != null) {
|
||||
// if (field.getAnnotation(RestAddress.class) != null) continue;
|
||||
// }
|
||||
//
|
||||
// typeMap.put(field.getName(), fieldmap);
|
||||
// }
|
||||
// } while ((loop = loop.getSuperclass()) != Object.class);
|
||||
// typesMap.put(rtype.getName(), typeMap);
|
||||
// }
|
||||
mappingMap.put("results", results);
|
||||
boolean hasbodyparam = false;
|
||||
Map<String, Object> swaggerRequestBody = new LinkedHashMap<>();
|
||||
for (HttpParam param : method.getAnnotationsByType(HttpParam.class)) {
|
||||
final Map<String, Object> oldapisParamMap = new LinkedHashMap<>();
|
||||
final boolean isarray = param.type().isArray();
|
||||
final Class ptype = isarray ? param.type().getComponentType() : param.type();
|
||||
oldapisParamMap.put("name", param.name());
|
||||
oldapisParamMap.put("radix", param.radix());
|
||||
oldapisParamMap.put("type", ptype.getName() + (isarray ? "[]" : ""));
|
||||
oldapisParamMap.put("style", param.style());
|
||||
oldapisParamMap.put("comment", param.comment());
|
||||
oldapisParamMap.put("required", param.required());
|
||||
paramsList.add(oldapisParamMap);
|
||||
{
|
||||
final Map<String, Object> paramSchemaMap = new LinkedHashMap<>();
|
||||
Type paramGenericType = param.type();
|
||||
if (!param.typeref().isEmpty()) {
|
||||
Field f = servlet.getClass().getDeclaredField(param.typeref());
|
||||
f.setAccessible(true);
|
||||
paramGenericType = (Type) f.get(servlet);
|
||||
}
|
||||
simpleSchemaType(node.getLogger(), swaggerComponentsMap, param.type(), paramGenericType, paramSchemaMap, true);
|
||||
if (param.style() == HttpParam.HttpParameterStyle.BODY) {
|
||||
swaggerRequestBody.put("description", param.comment());
|
||||
swaggerRequestBody.put("content", Utility.ofMap(plainContentType, Utility.ofMap("schema", paramSchemaMap)));
|
||||
} else {
|
||||
final Map<String, Object> swaggerParamMap = new LinkedHashMap<>();
|
||||
swaggerParamMap.put("name", param.name());
|
||||
swaggerParamMap.put("in", param.style().name().toLowerCase());
|
||||
swaggerParamMap.put("description", param.comment());
|
||||
swaggerParamMap.put("required", param.required());
|
||||
if (param.deprecated()) {
|
||||
swaggerParamMap.put("deprecated", param.deprecated());
|
||||
}
|
||||
//https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterStyle
|
||||
swaggerParamMap.put("style", param.style() == HttpParam.HttpParameterStyle.HEADER || param.name().indexOf('#') == 0 ? "simple" : "form");
|
||||
swaggerParamMap.put("explode", true);
|
||||
swaggerParamMap.put("schema", paramSchemaMap);
|
||||
Object example = formatExample(param.example(), param.type(), paramGenericType);
|
||||
if (example != null) swaggerParamMap.put("example", example);
|
||||
if (!param.example().isEmpty()) {
|
||||
swaggerParamMap.put("example", param.example());
|
||||
}
|
||||
swaggerParamsList.add(swaggerParamMap);
|
||||
}
|
||||
}
|
||||
if (param.style() == HttpParam.HttpParameterStyle.BODY) hasbodyparam = true;
|
||||
if (ptype.isPrimitive() || ptype == String.class) continue;
|
||||
if (typesMap.containsKey(ptype.getName())) continue;
|
||||
if (ptype.getName().startsWith("java.")) continue;
|
||||
if (ptype.getName().startsWith("javax.")) continue;
|
||||
|
||||
final Map<String, Map<String, Object>> typeMap = new LinkedHashMap<>();
|
||||
Class loop = ptype;
|
||||
final boolean filter = FilterBean.class.isAssignableFrom(loop);
|
||||
do {
|
||||
if (loop == null || loop.isInterface()) break;
|
||||
for (Field field : loop.getDeclaredFields()) {
|
||||
if (Modifier.isFinal(field.getModifiers())) continue;
|
||||
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||
|
||||
Map<String, Object> fieldmap = new LinkedHashMap<>();
|
||||
fieldmap.put("type", field.getType().isArray() ? (field.getType().getComponentType().getName() + "[]") : field.getGenericType().getTypeName());
|
||||
|
||||
Column col = field.getAnnotation(Column.class);
|
||||
FilterColumn fc = field.getAnnotation(FilterColumn.class);
|
||||
Comment comment = field.getAnnotation(Comment.class);
|
||||
if (comment != null) {
|
||||
fieldmap.put("comment", comment.value());
|
||||
} else if (col != null) {
|
||||
fieldmap.put("comment", col.comment());
|
||||
} else if (fc != null) {
|
||||
fieldmap.put("comment", fc.comment());
|
||||
}
|
||||
fieldmap.put("primary", !filter && (field.getAnnotation(Id.class) != null));
|
||||
fieldmap.put("updatable", (filter || col == null || col.updatable()));
|
||||
|
||||
if (servlet.getClass().getAnnotation(Rest.RestDyn.class) != null) {
|
||||
if (field.getAnnotation(RestAddress.class) != null) continue;
|
||||
}
|
||||
|
||||
typeMap.put(field.getName(), fieldmap);
|
||||
}
|
||||
} while ((loop = loop.getSuperclass()) != Object.class);
|
||||
|
||||
typesMap.put(ptype.getName(), typeMap);
|
||||
}
|
||||
mappingMap.put("result", action.result().getSimpleName().replace("void", "Object"));
|
||||
mappingsList.add(mappingMap);
|
||||
|
||||
final Map<String, Object> swaggerOperatMap = new LinkedHashMap<>();
|
||||
swaggerOperatMap.put("tags", new String[]{tag});
|
||||
swaggerOperatMap.put("operationId", action.name());
|
||||
if (method.getAnnotation(Deprecated.class) != null) {
|
||||
swaggerOperatMap.put("deprecated", true);
|
||||
}
|
||||
Map<String, Object> respSchemaMap = new LinkedHashMap<>();
|
||||
simpleSchemaType(node.getLogger(), swaggerComponentsMap, action.result(), resultType, respSchemaMap, true);
|
||||
|
||||
Map<String, Object> respMap = new LinkedHashMap<>();
|
||||
respMap.put("schema", respSchemaMap);
|
||||
Object example = formatExample(action.example(), action.result(), resultType);
|
||||
if (example != null) swaggerOperatMap.put("example", example);
|
||||
if (!swaggerRequestBody.isEmpty()) swaggerOperatMap.put("requestBody", swaggerRequestBody);
|
||||
swaggerOperatMap.put("parameters", swaggerParamsList);
|
||||
String actiondesc = action.comment();
|
||||
if (action.rpconly()) actiondesc = "[Only for RPC API] " + actiondesc;
|
||||
swaggerOperatMap.put("responses", Utility.ofMap("200", Utility.ofMap("description", actiondesc, "content", Utility.ofMap("application/json", respMap))));
|
||||
|
||||
String m = action.methods() == null || action.methods().length == 0 ? null : action.methods()[0].toLowerCase();
|
||||
if (m == null) {
|
||||
m = hasbodyparam || TYPE_RETRESULT_STRING.equals(resultType) || TYPE_RETRESULT_INTEGER.equals(resultType)
|
||||
|| TYPE_RETRESULT_LONG.equals(resultType) || action.name().contains("create") || action.name().contains("insert")
|
||||
|| action.name().contains("update") || action.name().contains("delete") || action.name().contains("send") ? "post" : "get";
|
||||
}
|
||||
swaggerPathsMap.put(prefix + action.url(), Utility.ofMap("description", action.comment(), m, swaggerOperatMap));
|
||||
}
|
||||
} while ((clz = clz.getSuperclass()) != HttpServlet.class);
|
||||
mappingsList.sort((o1, o2) -> ((String) o1.get("url")).compareTo((String) o2.get("url")));
|
||||
servletsList.add(servletMap);
|
||||
if (!actionUrls.isEmpty()) swaggerTags.add(Utility.ofMap("name", tag, "description", ws.comment()));
|
||||
}
|
||||
servletsList.sort((o1, o2) -> {
|
||||
String[] urlregs1 = (String[]) o1.get("urlregs");
|
||||
String[] urlregs2 = (String[]) o2.get("urlregs");
|
||||
return urlregs1.length > 0 ? (urlregs2.length > 0 ? urlregs1[0].compareTo(urlregs2[0]) : 1) : -1;
|
||||
});
|
||||
}
|
||||
{ // https://github.com/OAI/OpenAPI-Specification
|
||||
Map<String, Object> swaggerResultMap = new LinkedHashMap<>();
|
||||
swaggerResultMap.put("openapi", "3.0.0");
|
||||
Map<String, Object> infomap = new LinkedHashMap<>();
|
||||
infomap.put("title", "Redkale generate apidoc");
|
||||
infomap.put("version", "1.0.0");
|
||||
swaggerResultMap.put("info", infomap);
|
||||
swaggerResultMap.put("servers", swaggerServers);
|
||||
swaggerResultMap.put("paths", swaggerPathsMap);
|
||||
swaggerResultMap.put("tags", swaggerTags);
|
||||
if (!swaggerComponentsMap.isEmpty()) swaggerResultMap.put("components", Utility.ofMap("schemas", swaggerComponentsMap));
|
||||
final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "openapi-doc.json"));
|
||||
out.write(JsonConvert.root().convertTo(swaggerResultMap).getBytes(StandardCharsets.UTF_8));
|
||||
out.close();
|
||||
}
|
||||
{
|
||||
Map<String, Object> oldapisResultMap = new LinkedHashMap<>();
|
||||
oldapisResultMap.put("servers", serverList);
|
||||
oldapisResultMap.put("types", typesMap);
|
||||
final String json = JsonConvert.root().convertTo(oldapisResultMap);
|
||||
final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "apidoc.json"));
|
||||
out.write(json.getBytes(StandardCharsets.UTF_8));
|
||||
out.close();
|
||||
File doctemplate = new File(app.getConfPath().toString(), "apidoc-template.html");
|
||||
InputStream in = null;
|
||||
if (doctemplate.isFile() && doctemplate.canRead()) {
|
||||
in = new FileInputStream(doctemplate);
|
||||
}
|
||||
if (in == null) in = ApiDocsService.class.getResourceAsStream("apidoc-template.html");
|
||||
String content = Utility.read(in).replace("'${content}'", json);
|
||||
in.close();
|
||||
FileOutputStream outhtml = new FileOutputStream(new File(app.getHome(), "apidoc.html"));
|
||||
outhtml.write(content.getBytes(StandardCharsets.UTF_8));
|
||||
outhtml.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void simpleSchemaType(Logger logger, Map<String, Map<String, Object>> componentsMap, Class type, Type genericType, Map<String, Object> schemaMap, boolean recursive) {
|
||||
if (type == int.class || type == Integer.class || type == AtomicInteger.class) {
|
||||
schemaMap.put("type", "integer");
|
||||
schemaMap.put("format", "int32");
|
||||
} else if (type == long.class || type == Long.class
|
||||
|| type == AtomicLong.class || type == LongAdder.class || type == BigInteger.class) {
|
||||
schemaMap.put("type", "integer");
|
||||
schemaMap.put("format", "int64");
|
||||
} else if (type == float.class || type == Float.class) {
|
||||
schemaMap.put("type", "number");
|
||||
schemaMap.put("format", "float");
|
||||
} else if (type == double.class || type == Double.class || type == BigDecimal.class) {
|
||||
schemaMap.put("type", "number");
|
||||
schemaMap.put("format", "double");
|
||||
} else if (type == boolean.class || type == Boolean.class || type == AtomicBoolean.class) {
|
||||
schemaMap.put("type", "boolean");
|
||||
} else if (type.isPrimitive() || Number.class.isAssignableFrom(type)) {
|
||||
schemaMap.put("type", "number");
|
||||
} else if (type == String.class || CharSequence.class.isAssignableFrom(type)) {
|
||||
schemaMap.put("type", "string");
|
||||
} else if (recursive && (type.isArray() || Collection.class.isAssignableFrom(type))) {
|
||||
schemaMap.put("type", "array");
|
||||
Map<String, Object> sbumap = new LinkedHashMap<>();
|
||||
if (type.isArray()) {
|
||||
simpleSchemaType(logger, componentsMap, type.getComponentType(), type.getComponentType(), sbumap, false);
|
||||
} else if (genericType instanceof ParameterizedType) {
|
||||
Type subpt = ((ParameterizedType) genericType).getActualTypeArguments()[0];
|
||||
if (subpt instanceof Class) {
|
||||
simpleSchemaType(logger, componentsMap, (Class) subpt, subpt, sbumap, false);
|
||||
} else if (subpt instanceof ParameterizedType && ((ParameterizedType) subpt).getOwnerType() instanceof Class) {
|
||||
simpleSchemaType(logger, componentsMap, (Class) ((ParameterizedType) subpt).getOwnerType(), subpt, sbumap, false);
|
||||
} else {
|
||||
sbumap.put("type", "object");
|
||||
}
|
||||
} else {
|
||||
sbumap.put("type", "object");
|
||||
}
|
||||
schemaMap.put("items", sbumap);
|
||||
} else if (!type.getName().startsWith("java.") && !type.getName().startsWith("javax.")) {
|
||||
String ct = simpleComponentType(logger, componentsMap, type, genericType);
|
||||
if (ct == null) {
|
||||
schemaMap.put("type", "object");
|
||||
} else {
|
||||
schemaMap.put("$ref", "#/components/schemas/" + ct);
|
||||
}
|
||||
} else {
|
||||
schemaMap.put("type", "object");
|
||||
}
|
||||
}
|
||||
|
||||
private static String simpleComponentType(Logger logger, Map<String, Map<String, Object>> componentsMap, Class type, Type genericType) {
|
||||
try {
|
||||
Encodeable encodeable = JsonFactory.root().loadEncoder(genericType);
|
||||
String ct = componentKey(logger, componentsMap, null, encodeable, true);
|
||||
if (ct == null || ct.length() == 0) return null;
|
||||
if (componentsMap.containsKey(ct)) return ct;
|
||||
Map<String, Object> cmap = new LinkedHashMap<>();
|
||||
componentsMap.put(ct, cmap); //必须在调用simpleSchemaType之前put,不然嵌套情况下死循环
|
||||
|
||||
cmap.put("type", "object");
|
||||
List<String> requireds = new ArrayList<>();
|
||||
Map<String, Object> properties = new LinkedHashMap<>();
|
||||
if (encodeable instanceof ObjectEncoder) {
|
||||
for (EnMember member : ((ObjectEncoder) encodeable).getMembers()) {
|
||||
Map<String, Object> schemaMap = new LinkedHashMap<>();
|
||||
simpleSchemaType(logger, componentsMap, TypeToken.typeToClassOrElse(member.getEncoder().getType(), Object.class), member.getEncoder().getType(), schemaMap, true);
|
||||
String desc = "";
|
||||
if (member.getField() != null) {
|
||||
Column col = member.getField().getAnnotation(Column.class);
|
||||
if (col == null) {
|
||||
FilterColumn fcol = member.getField().getAnnotation(FilterColumn.class);
|
||||
if (fcol != null) {
|
||||
desc = fcol.comment();
|
||||
if (fcol.required()) requireds.add(member.getAttribute().field());
|
||||
}
|
||||
} else {
|
||||
desc = col.comment();
|
||||
if (!col.nullable()) requireds.add(member.getAttribute().field());
|
||||
}
|
||||
if (desc.isEmpty() && member.getField().getAnnotation(Comment.class) != null) {
|
||||
desc = member.getField().getAnnotation(Comment.class).value();
|
||||
}
|
||||
} else if (member.getMethod() != null) {
|
||||
Column col = member.getMethod().getAnnotation(Column.class);
|
||||
if (col == null) {
|
||||
FilterColumn fcol = member.getMethod().getAnnotation(FilterColumn.class);
|
||||
if (fcol != null) {
|
||||
desc = fcol.comment();
|
||||
if (fcol.required()) requireds.add(member.getAttribute().field());
|
||||
}
|
||||
} else {
|
||||
desc = col.comment();
|
||||
if (!col.nullable()) requireds.add(member.getAttribute().field());
|
||||
}
|
||||
if (desc.isEmpty() && member.getMethod().getAnnotation(Comment.class) != null) {
|
||||
desc = member.getMethod().getAnnotation(Comment.class).value();
|
||||
}
|
||||
}
|
||||
if (!desc.isEmpty()) schemaMap.put("description", desc);
|
||||
properties.put(member.getAttribute().field(), schemaMap);
|
||||
}
|
||||
}
|
||||
if (!requireds.isEmpty()) cmap.put("required", requireds);
|
||||
cmap.put("properties", properties);
|
||||
return ct;
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, genericType + " generate component info error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String componentKey(Logger logger, Map<String, Map<String, Object>> componentsMap, EnMember field, Encodeable encodeable, boolean first) {
|
||||
if (encodeable instanceof ObjectEncoder) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(((ObjectEncoder) encodeable).getTypeClass().getSimpleName());
|
||||
for (EnMember member : ((ObjectEncoder) encodeable).getMembers()) {
|
||||
if (member.getEncoder() instanceof ArrayEncoder
|
||||
|| member.getEncoder() instanceof CollectionEncoder) {
|
||||
String subsb = componentKey(logger, componentsMap, member, member.getEncoder(), false);
|
||||
if (subsb == null) return null;
|
||||
AccessibleObject real = member.getField() == null ? member.getMethod() : member.getField();
|
||||
if (real == null) continue;
|
||||
Class cz = real instanceof Field ? ((Field) real).getType() : ((Method) real).getReturnType();
|
||||
Type ct = real instanceof Field ? ((Field) real).getGenericType() : ((Method) real).getGenericReturnType();
|
||||
if (cz == ct) continue;
|
||||
if (sb.length() > 0 && subsb.length() > 0) sb.append("_");
|
||||
sb.append(subsb);
|
||||
} else if (member.getEncoder() instanceof ObjectEncoder || member.getEncoder() instanceof SimpledCoder) {
|
||||
AccessibleObject real = member.getField() == null ? member.getMethod() : member.getField();
|
||||
if (real == null) continue;
|
||||
if (member.getEncoder() instanceof SimpledCoder) {
|
||||
simpleSchemaType(logger, componentsMap, ((SimpledCoder) member.getEncoder()).getType(), ((SimpledCoder) member.getEncoder()).getType(), new LinkedHashMap<>(), true);
|
||||
} else {
|
||||
simpleSchemaType(logger, componentsMap, ((ObjectEncoder) member.getEncoder()).getTypeClass(), ((ObjectEncoder) member.getEncoder()).getType(), new LinkedHashMap<>(), true);
|
||||
}
|
||||
Class cz = real instanceof Field ? ((Field) real).getType() : ((Method) real).getReturnType();
|
||||
Type ct = real instanceof Field ? ((Field) real).getGenericType() : ((Method) real).getGenericReturnType();
|
||||
if (cz == ct) continue;
|
||||
String subsb = componentKey(logger, componentsMap, member, member.getEncoder(), false);
|
||||
if (subsb == null) return null;
|
||||
if (sb.length() > 0 && subsb.length() > 0) sb.append("_");
|
||||
sb.append(subsb);
|
||||
} else if (member.getEncoder() instanceof MapEncoder) {
|
||||
continue;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
} else if (encodeable instanceof ArrayEncoder || encodeable instanceof CollectionEncoder) {
|
||||
final boolean array = (encodeable instanceof ArrayEncoder);
|
||||
Encodeable subEncodeable = array ? ((ArrayEncoder) encodeable).getComponentEncoder() : ((CollectionEncoder) encodeable).getComponentEncoder();
|
||||
if (subEncodeable instanceof SimpledCoder && field != null) return "";
|
||||
final String sb = componentKey(logger, componentsMap, null, subEncodeable, false);
|
||||
if (sb == null || sb.isEmpty()) return sb;
|
||||
if (field != null && field.getField() != null && field.getField().getDeclaringClass() == Sheet.class) {
|
||||
return sb;
|
||||
}
|
||||
return sb + (array ? "_Array" : "_Collection");
|
||||
} else if (encodeable instanceof SimpledCoder) {
|
||||
Class stype = ((SimpledCoder) encodeable).getType();
|
||||
if (stype.isPrimitive() || stype == Boolean.class || Number.class.isAssignableFrom(stype) || CharSequence.class.isAssignableFrom(stype)) {
|
||||
return stype.getSimpleName();
|
||||
}
|
||||
return "";
|
||||
} else if (encodeable instanceof MapEncoder) {
|
||||
return first ? null : "";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object formatExample(String example, Class type, Type genericType) {
|
||||
if (example == null || example.isEmpty()) return null;
|
||||
if (type == Flipper.class) {
|
||||
return new Flipper();
|
||||
} else if (TYPE_RETRESULT_OBJECT.equals(genericType)) {
|
||||
return RetResult.success();
|
||||
} else if (TYPE_RETRESULT_STRING.equals(genericType)) {
|
||||
return RetResult.success();
|
||||
} else if (TYPE_RETRESULT_INTEGER.equals(genericType)) {
|
||||
return RetResult.success(0);
|
||||
} else if (TYPE_RETRESULT_LONG.equals(genericType)) {
|
||||
return RetResult.success(0L);
|
||||
}
|
||||
return example;
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -35,6 +35,30 @@ public interface ApplicationListener {
|
||||
default void preStart(Application application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Application 在运行start后调用
|
||||
*
|
||||
* @param application Application
|
||||
*/
|
||||
default void postStart(Application application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Application 在运行Compile前调用
|
||||
*
|
||||
* @param application Application
|
||||
*/
|
||||
default void preCompile(Application application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Application 在运行Compile后调用
|
||||
*
|
||||
* @param application Application
|
||||
*/
|
||||
default void postCompile(Application application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Application 在运行shutdown前调用
|
||||
*
|
||||
@@ -9,6 +9,7 @@ import java.io.*;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.function.Predicate;
|
||||
@@ -63,11 +64,11 @@ public final class ClassFilter<T> {
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
public ClassFilter(ClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses) {
|
||||
public ClassFilter(RedkaleClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses) {
|
||||
this(classLoader, annotationClass, superClass, excludeSuperClasses, null);
|
||||
}
|
||||
|
||||
public ClassFilter(ClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses, AnyValue conf) {
|
||||
public ClassFilter(RedkaleClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses, AnyValue conf) {
|
||||
this.annotationClass = annotationClass;
|
||||
this.superClass = superClass;
|
||||
this.excludeSuperClasses = excludeSuperClasses;
|
||||
@@ -75,8 +76,8 @@ public final class ClassFilter<T> {
|
||||
this.classLoader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
|
||||
}
|
||||
|
||||
public static ClassFilter create(Class[] excludeSuperClasses, String includeregs, String excluderegs, Set<String> includeValues, Set<String> excludeValues) {
|
||||
ClassFilter filter = new ClassFilter(null, null, null, excludeSuperClasses);
|
||||
public static ClassFilter create(RedkaleClassLoader classLoader, Class[] excludeSuperClasses, String includeregs, String excluderegs, Set<String> includeValues, Set<String> excludeValues) {
|
||||
ClassFilter filter = new ClassFilter(classLoader, null, null, excludeSuperClasses);
|
||||
filter.setIncludePatterns(includeregs == null ? null : includeregs.split(";"));
|
||||
filter.setExcludePatterns(excluderegs == null ? null : excluderegs.split(";"));
|
||||
filter.setPrivilegeIncludes(includeValues);
|
||||
@@ -208,7 +209,7 @@ public final class ClassFilter<T> {
|
||||
} catch (Throwable cfe) {
|
||||
if (finest && !clazzname.startsWith("sun.") && !clazzname.startsWith("javax.")
|
||||
&& !clazzname.startsWith("com.sun.") && !clazzname.startsWith("jdk.") && !clazzname.startsWith("META-INF")
|
||||
&& !clazzname.startsWith("com.mysql.") && !clazzname.startsWith("com.microsoft.")
|
||||
&& !clazzname.startsWith("com.mysql.") && !clazzname.startsWith("com.microsoft.") && !clazzname.startsWith("freemarker.")
|
||||
&& !clazzname.startsWith("org.redkale") && (clazzname.contains("Service") || clazzname.contains("Servlet"))) {
|
||||
//&& (!(cfe instanceof NoClassDefFoundError) || (cfe instanceof UnsupportedClassVersionError) || ((NoClassDefFoundError) cfe).getMessage().startsWith("java.lang.NoClassDefFoundError: java"))) {
|
||||
logger.log(Level.FINEST, ClassFilter.class.getSimpleName() + " filter error for class: " + clazzname + (url == null ? "" : (" in " + url)), cfe);
|
||||
@@ -483,20 +484,20 @@ public final class ClassFilter<T> {
|
||||
* 加载当前线程的classpath扫描所有class进行过滤
|
||||
*
|
||||
* @param excludeFile 不需要扫描的文件夹, 可以为null
|
||||
* @param loader RedkaleClassloader, 不可为null
|
||||
* @param excludeRegs 包含此关键字的文件将被跳过, 可以为null
|
||||
* @param filters 过滤器
|
||||
*
|
||||
* @throws IOException 异常
|
||||
*/
|
||||
public static void load(final File excludeFile, final String[] excludeRegs, final ClassFilter... filters) throws IOException {
|
||||
RedkaleClassLoader loader = (RedkaleClassLoader) Thread.currentThread().getContextClassLoader();
|
||||
public static void load(final File excludeFile, RedkaleClassLoader loader, final String[] excludeRegs, final ClassFilter... filters) throws IOException {
|
||||
List<URL> urlfiles = new ArrayList<>(2);
|
||||
List<URL> urljares = new ArrayList<>(2);
|
||||
final URL exurl = excludeFile != null ? excludeFile.toURI().toURL() : null;
|
||||
final Pattern[] excludePatterns = toPattern(excludeRegs);
|
||||
for (URL url : loader.getAllURLs()) {
|
||||
if (exurl != null && exurl.sameFile(url)) continue;
|
||||
if (excludePatterns != null) {
|
||||
if (excludePatterns != null && url != RedkaleClassLoader.URL_NONE) {
|
||||
boolean skip = false;
|
||||
for (Pattern p : excludePatterns) {
|
||||
if (p.matcher(url.toString()).matches()) {
|
||||
@@ -519,7 +520,7 @@ public final class ClassFilter<T> {
|
||||
Set<String> classes = cache.get(url);
|
||||
if (classes == null) {
|
||||
classes = new LinkedHashSet<>();
|
||||
try (JarFile jar = new JarFile(URLDecoder.decode(url.getFile(), "UTF-8"))) {
|
||||
try (JarFile jar = new JarFile(URLDecoder.decode(url.getFile(), StandardCharsets.UTF_8))) {
|
||||
Enumeration<JarEntry> it = jar.entries();
|
||||
while (it.hasMoreElements()) {
|
||||
String entryname = it.nextElement().getName().replace('/', '.');
|
||||
@@ -527,6 +528,7 @@ public final class ClassFilter<T> {
|
||||
String classname = entryname.substring(0, entryname.length() - 6);
|
||||
if (classname.startsWith("javax.") || classname.startsWith("com.sun.")) continue;
|
||||
//常见的jar跳过
|
||||
if (classname.startsWith("com.redkaledyn.")) break; //redkale动态生成的类
|
||||
if (classname.startsWith("com.mysql.")) break;
|
||||
if (classname.startsWith("org.mariadb.")) break;
|
||||
if (classname.startsWith("oracle.jdbc.")) break;
|
||||
@@ -553,17 +555,27 @@ public final class ClassFilter<T> {
|
||||
Set<String> classes = cache.get(url);
|
||||
if (classes == null) {
|
||||
classes = new LinkedHashSet<>();
|
||||
files.clear();
|
||||
File root = new File(url.getFile());
|
||||
String rootpath = root.getPath();
|
||||
loadClassFiles(excludeFile, root, files);
|
||||
for (File f : files) {
|
||||
String classname = f.getPath().substring(rootpath.length() + 1, f.getPath().length() - 6).replace(File.separatorChar, '.');
|
||||
if (classname.startsWith("javax.") || classname.startsWith("com.sun.")) continue;
|
||||
classes.add(classname);
|
||||
if (debug) debugstr.append(classname).append("\r\n");
|
||||
for (final ClassFilter filter : filters) {
|
||||
if (filter != null) filter.filter(null, classname, url);
|
||||
final Set<String> cs = classes;
|
||||
if (url == RedkaleClassLoader.URL_NONE) loader.forEachCacheClass(v -> cs.add(v));
|
||||
if (cs.isEmpty()) {
|
||||
files.clear();
|
||||
File root = new File(url.getFile());
|
||||
String rootpath = root.getPath();
|
||||
loadClassFiles(excludeFile, root, files);
|
||||
for (File f : files) {
|
||||
String classname = f.getPath().substring(rootpath.length() + 1, f.getPath().length() - 6).replace(File.separatorChar, '.');
|
||||
if (classname.startsWith("javax.") || classname.startsWith("com.sun.")) continue;
|
||||
classes.add(classname);
|
||||
if (debug) debugstr.append(classname).append("\r\n");
|
||||
for (final ClassFilter filter : filters) {
|
||||
if (filter != null) filter.filter(null, classname, url);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (String classname : classes) {
|
||||
for (final ClassFilter filter : filters) {
|
||||
if (filter != null) filter.filter(null, classname, url);
|
||||
}
|
||||
}
|
||||
}
|
||||
cache.put(url, classes);
|
||||
@@ -5,6 +5,8 @@
|
||||
*/
|
||||
package org.redkale.boot;
|
||||
|
||||
import org.redkale.util.RedkaleClassLoader;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import static java.nio.file.StandardCopyOption.*;
|
||||
@@ -24,12 +26,15 @@ import java.util.regex.Pattern;
|
||||
* @author zhangjx
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class LogFileHandler extends Handler {
|
||||
public class LoggingFileHandler extends Handler {
|
||||
|
||||
//public static final String FORMATTER_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s%n%5$s%6$s%n";
|
||||
public static final String FORMATTER_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s\r\n%5$s%6$s\r\n";
|
||||
|
||||
/**
|
||||
* SNCP的日志输出Handler
|
||||
*/
|
||||
public static class SncpLogFileHandler extends LogFileHandler {
|
||||
public static class LoggingSncpFileHandler extends LoggingFileHandler {
|
||||
|
||||
@Override
|
||||
public String getPrefix() {
|
||||
@@ -39,12 +44,11 @@ public class LogFileHandler extends Handler {
|
||||
|
||||
/**
|
||||
* 默认的日志时间格式化类
|
||||
* 与SimpleFormatter的区别在于level不使用本地化
|
||||
*
|
||||
*/
|
||||
public static class LoggingFormater extends Formatter {
|
||||
|
||||
private static final String format = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s\r\n%5$s%6$s\r\n";
|
||||
|
||||
@Override
|
||||
public String format(LogRecord log) {
|
||||
String source;
|
||||
@@ -71,7 +75,7 @@ public class LogFileHandler extends Handler {
|
||||
pw.close();
|
||||
throwable = sw.toString();
|
||||
}
|
||||
return String.format(format,
|
||||
return String.format(FORMATTER_FORMAT,
|
||||
System.currentTimeMillis(),
|
||||
source,
|
||||
log.getLoggerName(),
|
||||
@@ -79,7 +83,23 @@ public class LogFileHandler extends Handler {
|
||||
message,
|
||||
throwable);
|
||||
}
|
||||
}
|
||||
|
||||
public static void initDebugLogConfig() {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
final PrintStream ps = new PrintStream(out);
|
||||
ps.println("handlers = java.util.logging.ConsoleHandler");
|
||||
ps.println(".level = FINEST");
|
||||
ps.println("jdk.level = INFO");
|
||||
ps.println("sun.level = INFO");
|
||||
ps.println("com.sun.level = INFO");
|
||||
ps.println("javax.level = INFO");
|
||||
ps.println("java.util.logging.ConsoleHandler.level = FINEST");
|
||||
ps.println("java.util.logging.ConsoleHandler.formatter = " + LoggingFileHandler.LoggingFormater.class.getName());
|
||||
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(out.toByteArray()));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
protected final LinkedBlockingQueue<LogRecord> logqueue = new LinkedBlockingQueue();
|
||||
@@ -114,7 +134,7 @@ public class LogFileHandler extends Handler {
|
||||
|
||||
private OutputStream logunusualstream;
|
||||
|
||||
public LogFileHandler() {
|
||||
public LoggingFileHandler() {
|
||||
updateTomorrow();
|
||||
configure();
|
||||
open();
|
||||
@@ -218,7 +238,7 @@ public class LogFileHandler extends Handler {
|
||||
|
||||
private void configure() {
|
||||
LogManager manager = LogManager.getLogManager();
|
||||
String cname = LogFileHandler.class.getName();
|
||||
String cname = LoggingFileHandler.class.getName();
|
||||
this.pattern = manager.getProperty(cname + ".pattern");
|
||||
if (this.pattern == null) {
|
||||
this.pattern = "logs-%m/" + getPrefix() + "log-%d.log";
|
||||
@@ -280,6 +300,7 @@ public class LogFileHandler extends Handler {
|
||||
try {
|
||||
if (filterstr != null) {
|
||||
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(filterstr);
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(clz, clz.getName());
|
||||
setFilter((Filter) clz.getDeclaredConstructor().newInstance());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -288,6 +309,7 @@ public class LogFileHandler extends Handler {
|
||||
try {
|
||||
if (formatterstr != null) {
|
||||
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(formatterstr);
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(clz, clz.getName());
|
||||
setFormatter((Formatter) clz.getDeclaredConstructor().newInstance());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -1,399 +1,446 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.boot;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.logging.Level;
|
||||
import javax.annotation.*;
|
||||
import static org.redkale.boot.Application.RESNAME_SNCP_ADDR;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.cluster.ClusterAgent;
|
||||
import org.redkale.mq.MessageAgent;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.net.sncp.Sncp;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||
import org.redkale.util.*;
|
||||
import org.redkale.watch.*;
|
||||
|
||||
/**
|
||||
* HTTP Server节点的配置Server
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@NodeProtocol("HTTP")
|
||||
public class NodeHttpServer extends NodeServer {
|
||||
|
||||
protected final boolean rest; //是否加载REST服务, 为true加载rest节点信息并将所有可REST化的Service生成RestServlet
|
||||
|
||||
protected final HttpServer httpServer;
|
||||
|
||||
public NodeHttpServer(Application application, AnyValue serconf) {
|
||||
super(application, createServer(application, serconf));
|
||||
this.httpServer = (HttpServer) server;
|
||||
this.rest = serconf == null ? false : serconf.getAnyValue("rest") != null;
|
||||
}
|
||||
|
||||
private static Server createServer(Application application, AnyValue serconf) {
|
||||
return new HttpServer(application.getStartTime(), application.getResourceFactory().createChild());
|
||||
}
|
||||
|
||||
public HttpServer getHttpServer() {
|
||||
return httpServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getSocketAddress() {
|
||||
return httpServer == null ? null : httpServer.getSocketAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ClassFilter<Service> createServiceClassFilter() {
|
||||
return createClassFilter(this.sncpGroup, null, Service.class, new Class[]{org.redkale.watch.WatchService.class}, Annotation.class, "services", "service");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ClassFilter<Filter> createFilterClassFilter() {
|
||||
return createClassFilter(null, null, HttpFilter.class, new Class[]{WatchFilter.class}, null, "filters", "filter");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ClassFilter<Servlet> createServletClassFilter() {
|
||||
return createClassFilter(null, WebServlet.class, HttpServlet.class, new Class[]{WatchServlet.class}, null, "servlets", "servlet");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ClassFilter createOtherClassFilter() {
|
||||
return createClassFilter(null, RestWebSocket.class, WebSocket.class, null, null, "rest", "websocket");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadService(ClassFilter<? extends Service> serviceFilter, ClassFilter otherFilter) throws Exception {
|
||||
super.loadService(serviceFilter, otherFilter);
|
||||
initWebSocketService();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadFilter(ClassFilter<? extends Filter> filterFilter, ClassFilter otherFilter) throws Exception {
|
||||
if (httpServer != null) loadHttpFilter(this.serverConf.getAnyValue("filters"), filterFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadServlet(ClassFilter<? extends Servlet> servletFilter, ClassFilter otherFilter) throws Exception {
|
||||
if (httpServer != null) loadHttpServlet(servletFilter, otherFilter);
|
||||
}
|
||||
|
||||
private void initWebSocketService() {
|
||||
final NodeServer self = this;
|
||||
final ResourceFactory regFactory = application.getResourceFactory();
|
||||
resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, Object attachment) -> { //主要用于单点的服务
|
||||
try {
|
||||
if (field.getAnnotation(Resource.class) == null) return;
|
||||
if (!(src instanceof WebSocketServlet)) return;
|
||||
ResourceFactory.ResourceLoader loader = null;
|
||||
ResourceFactory sncpResFactory = null;
|
||||
for (NodeServer ns : application.servers) {
|
||||
if (!ns.isSNCP()) continue;
|
||||
sncpResFactory = ns.resourceFactory;
|
||||
loader = sncpResFactory.findLoader(WebSocketNode.class, field);
|
||||
if (loader != null) break;
|
||||
}
|
||||
if (loader != null) loader.load(sncpResFactory, src, resourceName, field, attachment);
|
||||
synchronized (regFactory) {
|
||||
Service nodeService = (Service) rf.find(resourceName, WebSocketNode.class);
|
||||
if (sncpResFactory != null && resourceFactory.find(RESNAME_SNCP_ADDR, String.class) == null) {
|
||||
resourceFactory.register(RESNAME_SNCP_ADDR, InetSocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, InetSocketAddress.class));
|
||||
resourceFactory.register(RESNAME_SNCP_ADDR, SocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, SocketAddress.class));
|
||||
resourceFactory.register(RESNAME_SNCP_ADDR, String.class, sncpResFactory.find(RESNAME_SNCP_ADDR, String.class));
|
||||
}
|
||||
if (nodeService == null) {
|
||||
MessageAgent messageAgent = null;
|
||||
try {
|
||||
Field c = src.getClass().getDeclaredField("messageAgent");
|
||||
c.setAccessible(true);
|
||||
messageAgent = (MessageAgent) c.get(src);
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.WARNING, "WebSocketServlet getMessageAgent error", ex);
|
||||
}
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, messageAgent, application.getResourceFactory(), application.getSncpTransportFactory(), (InetSocketAddress) null, (Set<String>) null, (AnyValue) null);
|
||||
regFactory.register(resourceName, WebSocketNode.class, nodeService);
|
||||
}
|
||||
resourceFactory.inject(nodeService, self);
|
||||
field.set(src, nodeService);
|
||||
logger.fine("[" + Thread.currentThread().getName() + "] Load Service " + nodeService);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "WebSocketNode inject error", e);
|
||||
}
|
||||
}, WebSocketNode.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadHttpFilter(final AnyValue filtersConf, final ClassFilter<? extends Filter> classFilter) throws Exception {
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<FilterEntry<? extends Filter>> list = new ArrayList(classFilter.getFilterEntrys());
|
||||
for (FilterEntry<? extends Filter> en : list) {
|
||||
Class<HttpFilter> clazz = (Class<HttpFilter>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||
final HttpFilter filter = clazz.getDeclaredConstructor().newInstance();
|
||||
resourceFactory.inject(filter, this);
|
||||
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
||||
this.httpServer.addHttpFilter(filter, filterConf);
|
||||
if (sb != null) sb.append(localThreadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
||||
}
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadHttpServlet(final ClassFilter<? extends Servlet> servletFilter, ClassFilter<? extends WebSocket> webSocketFilter) throws Exception {
|
||||
final AnyValue servletsConf = this.serverConf.getAnyValue("servlets");
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
String prefix0 = servletsConf == null ? "" : servletsConf.getValue("path", "");
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||
final String prefix = prefix0;
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<FilterEntry<? extends Servlet>> list = new ArrayList(servletFilter.getFilterEntrys());
|
||||
list.sort((FilterEntry<? extends Servlet> o1, FilterEntry<? extends Servlet> o2) -> { //必须保证WebSocketServlet优先加载, 因为要确保其他的HttpServlet可以注入本地模式的WebSocketNode
|
||||
boolean ws1 = WebSocketServlet.class.isAssignableFrom(o1.getType());
|
||||
boolean ws2 = WebSocketServlet.class.isAssignableFrom(o2.getType());
|
||||
if (ws1 == ws2) {
|
||||
Priority p1 = o1.getType().getAnnotation(Priority.class);
|
||||
Priority p2 = o2.getType().getAnnotation(Priority.class);
|
||||
int v = (p2 == null ? 0 : p2.value()) - (p1 == null ? 0 : p1.value());
|
||||
return v == 0 ? o1.getType().getName().compareTo(o2.getType().getName()) : 0;
|
||||
}
|
||||
return ws1 ? -1 : 1;
|
||||
});
|
||||
final List<AbstractMap.SimpleEntry<String, String[]>> ss = sb == null ? null : new ArrayList<>();
|
||||
for (FilterEntry<? extends Servlet> en : list) {
|
||||
Class<HttpServlet> clazz = (Class<HttpServlet>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||
WebServlet ws = clazz.getAnnotation(WebServlet.class);
|
||||
if (ws == null) continue;
|
||||
if (ws.value().length == 0) {
|
||||
logger.log(Level.INFO, "not found @WebServlet.value in " + clazz.getName());
|
||||
continue;
|
||||
}
|
||||
final HttpServlet servlet = clazz.getDeclaredConstructor().newInstance();
|
||||
resourceFactory.inject(servlet, this);
|
||||
final String[] mappings = ws.value();
|
||||
String pref = ws.repair() ? prefix : "";
|
||||
DefaultAnyValue servletConf = (DefaultAnyValue) en.getProperty();
|
||||
this.httpServer.addHttpServlet(servlet, pref, servletConf, mappings);
|
||||
if (ss != null) {
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
mappings[i] = pref + mappings[i];
|
||||
}
|
||||
ss.add(new AbstractMap.SimpleEntry<>(clazz.getName(), mappings));
|
||||
}
|
||||
}
|
||||
int max = 0;
|
||||
if (ss != null && sb != null) {
|
||||
ss.sort((AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
if (as.getKey().length() > max) max = as.getKey().length();
|
||||
}
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
sb.append(localThreadName).append(" Load ").append(as.getKey());
|
||||
for (int i = 0; i < max - as.getKey().length(); i++) {
|
||||
sb.append(' ');
|
||||
}
|
||||
sb.append(" mapping to ").append(Arrays.toString(as.getValue())).append(LINE_SEPARATOR);
|
||||
}
|
||||
}
|
||||
if (rest && serverConf != null) {
|
||||
final List<Object> restedObjects = new ArrayList<>();
|
||||
for (AnyValue restConf : serverConf.getAnyValues("rest")) {
|
||||
loadRestServlet(webSocketFilter, restConf, restedObjects, sb);
|
||||
}
|
||||
}
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString().trim());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadRestServlet(final ClassFilter<? extends WebSocket> webSocketFilter, final AnyValue restConf, final List<Object> restedObjects, final StringBuilder sb) throws Exception {
|
||||
if (!rest) return;
|
||||
if (restConf == null) return; //不存在REST服务
|
||||
|
||||
final long starts = System.currentTimeMillis();
|
||||
String prefix0 = restConf.getValue("path", "");
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
final List<AbstractMap.SimpleEntry<String, String[]>> ss = sb == null ? null : new ArrayList<>();
|
||||
String mqname = restConf.getValue("mq");
|
||||
MessageAgent agent0 = null;
|
||||
if (mqname != null) {
|
||||
agent0 = application.getMessageAgent(mqname);
|
||||
if (agent0 == null) throw new RuntimeException("not found " + MessageAgent.class.getSimpleName() + " config for (name=" + mqname + ")");
|
||||
}
|
||||
final MessageAgent messageAgent = agent0;
|
||||
if (messageAgent != null) prefix0 = ""; //开启MQ时,prefix字段失效
|
||||
final String prefix = prefix0;
|
||||
final boolean autoload = restConf.getBoolValue("autoload", true);
|
||||
{ //加载RestService
|
||||
String userTypeStr = restConf.getValue("usertype");
|
||||
final Class userType = userTypeStr == null ? null : this.serverClassLoader.loadClass(userTypeStr);
|
||||
|
||||
final Class baseServletType = this.serverClassLoader.loadClass(restConf.getValue("base", HttpServlet.class.getName()));
|
||||
final Set<String> includeValues = new HashSet<>();
|
||||
final Set<String> excludeValues = new HashSet<>();
|
||||
for (AnyValue item : restConf.getAnyValues("service")) {
|
||||
if (item.getBoolValue("ignore", false)) {
|
||||
excludeValues.add(item.getValue("value", ""));
|
||||
} else {
|
||||
includeValues.add(item.getValue("value", ""));
|
||||
}
|
||||
}
|
||||
|
||||
final ClassFilter restFilter = ClassFilter.create(null, restConf.getValue("includes", ""), restConf.getValue("excludes", ""), includeValues, excludeValues);
|
||||
final boolean finest = logger.isLoggable(Level.FINEST);
|
||||
final CountDownLatch scdl = new CountDownLatch(super.interceptorServices.size());
|
||||
super.interceptorServices.stream().parallel().forEach((service) -> {
|
||||
try {
|
||||
final Class stype = Sncp.getServiceType(service);
|
||||
final String name = Sncp.getResourceName(service);
|
||||
RestService rs = (RestService) stype.getAnnotation(RestService.class);
|
||||
if (rs == null || rs.ignore()) return;
|
||||
|
||||
final String stypename = stype.getName();
|
||||
if (!autoload && !includeValues.contains(stypename)) return;
|
||||
if (!restFilter.accept(stypename)) return;
|
||||
synchronized (restedObjects) {
|
||||
if (restedObjects.contains(service)) {
|
||||
logger.log(Level.WARNING, stype.getName() + " repeat create rest servlet, so ignore");
|
||||
return;
|
||||
}
|
||||
restedObjects.add(service); //避免重复创建Rest对象
|
||||
}
|
||||
HttpServlet servlet = httpServer.addRestServlet(serverClassLoader, service, userType, baseServletType, prefix);
|
||||
if (servlet == null) return; //没有HttpMapping方法的HttpServlet调用Rest.createRestServlet就会返回null
|
||||
String prefix2 = prefix;
|
||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||
if (ws != null && !ws.repair()) prefix2 = "";
|
||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||
dynServletMap.put(service, servlet);
|
||||
if (messageAgent != null) messageAgent.putService(this, service, servlet);
|
||||
//if (finest) logger.finest(localThreadName + " Create RestServlet(resource.name='" + name + "') = " + servlet);
|
||||
if (ss != null) {
|
||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
mappings[i] = prefix2 + mappings[i];
|
||||
}
|
||||
synchronized (ss) {
|
||||
ss.add(new AbstractMap.SimpleEntry<>(servlet.getClass().getName() + "(rest.name='" + name + "')", mappings));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
scdl.countDown();
|
||||
}
|
||||
});
|
||||
scdl.await();
|
||||
}
|
||||
if (webSocketFilter != null) { //加载RestWebSocket
|
||||
final Set<String> includeValues = new HashSet<>();
|
||||
final Set<String> excludeValues = new HashSet<>();
|
||||
for (AnyValue item : restConf.getAnyValues("websocket")) {
|
||||
if (item.getBoolValue("ignore", false)) {
|
||||
excludeValues.add(item.getValue("value", ""));
|
||||
} else {
|
||||
includeValues.add(item.getValue("value", ""));
|
||||
}
|
||||
}
|
||||
|
||||
final ClassFilter restFilter = ClassFilter.create(null, restConf.getValue("includes", ""), restConf.getValue("excludes", ""), includeValues, excludeValues);
|
||||
final boolean finest = logger.isLoggable(Level.FINEST);
|
||||
|
||||
List<FilterEntry<? extends WebSocket>> list = new ArrayList(webSocketFilter.getFilterEntrys());
|
||||
for (FilterEntry<? extends WebSocket> en : list) {
|
||||
Class<WebSocket> clazz = (Class<WebSocket>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) {
|
||||
logger.log(Level.FINE, clazz.getName() + " cannot abstract on rest websocket, so ignore");
|
||||
continue;
|
||||
}
|
||||
if (Modifier.isFinal(clazz.getModifiers())) {
|
||||
logger.log(Level.FINE, clazz.getName() + " cannot final on rest websocket, so ignore");
|
||||
continue;
|
||||
}
|
||||
final Class<? extends WebSocket> stype = en.getType();
|
||||
RestWebSocket rs = stype.getAnnotation(RestWebSocket.class);
|
||||
if (rs == null || rs.ignore()) return;
|
||||
|
||||
final String stypename = stype.getName();
|
||||
if (!autoload && !includeValues.contains(stypename)) return;
|
||||
if (!restFilter.accept(stypename)) return;
|
||||
if (restedObjects.contains(stype)) {
|
||||
logger.log(Level.WARNING, stype.getName() + " repeat create rest websocket, so ignore");
|
||||
return;
|
||||
}
|
||||
restedObjects.add(stype); //避免重复创建Rest对象
|
||||
WebSocketServlet servlet = httpServer.addRestWebSocketServlet(serverClassLoader, stype, messageAgent, prefix, en.getProperty());
|
||||
if (servlet == null) return; //没有RestOnMessage方法的HttpServlet调用Rest.createRestWebSocketServlet就会返回null
|
||||
String prefix2 = prefix;
|
||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||
if (ws != null && !ws.repair()) prefix2 = "";
|
||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||
if (finest) logger.finest(localThreadName + " " + stype.getName() + " create a RestWebSocketServlet");
|
||||
if (ss != null) {
|
||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
mappings[i] = prefix2 + mappings[i];
|
||||
}
|
||||
ss.add(new AbstractMap.SimpleEntry<>(servlet.getClass().getName(), mappings));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (messageAgent != null) this.messageAgents.put(messageAgent.getName(), messageAgent);
|
||||
//输出信息
|
||||
if (ss != null && !ss.isEmpty() && sb != null) {
|
||||
ss.sort((AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));
|
||||
int max = 0;
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
if (as.getKey().length() > max) max = as.getKey().length();
|
||||
}
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
sb.append(localThreadName).append(" Load ").append(as.getKey());
|
||||
for (int i = 0; i < max - as.getKey().length(); i++) {
|
||||
sb.append(' ');
|
||||
}
|
||||
sb.append(" mapping to ").append(Arrays.toString(as.getValue())).append(LINE_SEPARATOR);
|
||||
}
|
||||
sb.append(localThreadName).append(" All HttpServlets load cost ").append(System.currentTimeMillis() - starts).append(" ms").append(LINE_SEPARATOR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override //loadServlet执行之后调用
|
||||
protected void postLoadServlets() {
|
||||
final ClusterAgent cluster = application.clusterAgent;
|
||||
if (cluster != null) {
|
||||
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
|
||||
String protocol = pros.value().toUpperCase();
|
||||
if (!cluster.containsProtocol(protocol)) return;
|
||||
if (!cluster.containsPort(server.getSocketAddress().getPort())) return;
|
||||
cluster.register(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterClusterDeregisterOnPreDestroyServices(ClusterAgent cluster, String protocol) {
|
||||
cluster.deregister(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.boot;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Stream;
|
||||
import javax.annotation.*;
|
||||
import static org.redkale.boot.Application.RESNAME_SNCP_ADDR;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.cluster.ClusterAgent;
|
||||
import org.redkale.mq.MessageAgent;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.net.sncp.Sncp;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||
import org.redkale.util.*;
|
||||
import org.redkale.watch.*;
|
||||
|
||||
/**
|
||||
* HTTP Server节点的配置Server
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@NodeProtocol("HTTP")
|
||||
public class NodeHttpServer extends NodeServer {
|
||||
|
||||
protected final boolean rest; //是否加载REST服务, 为true加载rest节点信息并将所有可REST化的Service生成RestServlet
|
||||
|
||||
protected final HttpServer httpServer;
|
||||
|
||||
public NodeHttpServer(Application application, AnyValue serconf) {
|
||||
super(application, createServer(application, serconf));
|
||||
this.httpServer = (HttpServer) server;
|
||||
this.rest = serconf == null ? false : serconf.getAnyValue("rest") != null;
|
||||
}
|
||||
|
||||
private static Server createServer(Application application, AnyValue serconf) {
|
||||
return new HttpServer(application, application.getStartTime(), application.getResourceFactory().createChild());
|
||||
}
|
||||
|
||||
public HttpServer getHttpServer() {
|
||||
return httpServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getSocketAddress() {
|
||||
return httpServer == null ? null : httpServer.getSocketAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ClassFilter<Service> createServiceClassFilter() {
|
||||
return createClassFilter(this.sncpGroup, null, Service.class, new Class[]{org.redkale.watch.WatchService.class}, Annotation.class, "services", "service");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ClassFilter<Filter> createFilterClassFilter() {
|
||||
return createClassFilter(null, null, HttpFilter.class, new Class[]{WatchFilter.class}, null, "filters", "filter");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ClassFilter<Servlet> createServletClassFilter() {
|
||||
return createClassFilter(null, WebServlet.class, HttpServlet.class, new Class[]{WatchServlet.class}, null, "servlets", "servlet");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ClassFilter createOtherClassFilter() {
|
||||
return createClassFilter(null, RestWebSocket.class, WebSocket.class, null, null, "rest", "websocket");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadService(ClassFilter<? extends Service> serviceFilter, ClassFilter otherFilter) throws Exception {
|
||||
super.loadService(serviceFilter, otherFilter);
|
||||
initWebSocketService();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadFilter(ClassFilter<? extends Filter> filterFilter, ClassFilter otherFilter) throws Exception {
|
||||
if (httpServer != null) loadHttpFilter(this.serverConf.getAnyValue("filters"), filterFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadServlet(ClassFilter<? extends Servlet> servletFilter, ClassFilter otherFilter) throws Exception {
|
||||
if (httpServer != null) loadHttpServlet(servletFilter, otherFilter);
|
||||
}
|
||||
|
||||
private void initWebSocketService() {
|
||||
final NodeServer self = this;
|
||||
final ResourceFactory regFactory = application.getResourceFactory();
|
||||
resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, Object attachment) -> { //主要用于单点的服务
|
||||
try {
|
||||
if (field.getAnnotation(Resource.class) == null) return;
|
||||
if (!(src instanceof WebSocketServlet)) return;
|
||||
ResourceFactory.ResourceLoader loader = null;
|
||||
ResourceFactory sncpResFactory = null;
|
||||
for (NodeServer ns : application.servers) {
|
||||
if (!ns.isSNCP()) continue;
|
||||
sncpResFactory = ns.resourceFactory;
|
||||
loader = sncpResFactory.findLoader(WebSocketNode.class, field);
|
||||
if (loader != null) break;
|
||||
}
|
||||
if (loader != null) loader.load(sncpResFactory, src, resourceName, field, attachment);
|
||||
synchronized (regFactory) {
|
||||
Service nodeService = (Service) rf.find(resourceName, WebSocketNode.class);
|
||||
if (sncpResFactory != null && resourceFactory.find(RESNAME_SNCP_ADDR, String.class) == null) {
|
||||
resourceFactory.register(RESNAME_SNCP_ADDR, InetSocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, InetSocketAddress.class));
|
||||
resourceFactory.register(RESNAME_SNCP_ADDR, SocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, SocketAddress.class));
|
||||
resourceFactory.register(RESNAME_SNCP_ADDR, String.class, sncpResFactory.find(RESNAME_SNCP_ADDR, String.class));
|
||||
}
|
||||
if (nodeService == null) {
|
||||
MessageAgent messageAgent = null;
|
||||
try {
|
||||
Field c = WebSocketServlet.class.getDeclaredField("messageAgent");
|
||||
c.setAccessible(true);
|
||||
messageAgent = (MessageAgent) c.get(src);
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.WARNING, "WebSocketServlet getMessageAgent error", ex);
|
||||
}
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, org.redkale.net.http.WebSocketNodeService.class, messageAgent, application.getResourceFactory(), application.getSncpTransportFactory(), (InetSocketAddress) null, (Set<String>) null, (AnyValue) null);
|
||||
regFactory.register(resourceName, WebSocketNode.class, nodeService);
|
||||
}
|
||||
resourceFactory.inject(nodeService, self);
|
||||
field.set(src, nodeService);
|
||||
logger.fine("[" + Thread.currentThread().getName() + "] Load Service " + nodeService);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "WebSocketNode inject error", e);
|
||||
}
|
||||
}, WebSocketNode.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadHttpFilter(final AnyValue filtersConf, final ClassFilter<? extends Filter> classFilter) throws Exception {
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<FilterEntry<? extends Filter>> list = new ArrayList(classFilter.getFilterEntrys());
|
||||
for (FilterEntry<? extends Filter> en : list) {
|
||||
Class<HttpFilter> clazz = (Class<HttpFilter>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName());
|
||||
final HttpFilter filter = clazz.getDeclaredConstructor().newInstance();
|
||||
resourceFactory.inject(filter, this);
|
||||
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
||||
this.httpServer.addHttpFilter(filter, filterConf);
|
||||
if (sb != null) sb.append(localThreadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
||||
}
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadHttpServlet(final ClassFilter<? extends Servlet> servletFilter, ClassFilter<? extends WebSocket> webSocketFilter) throws Exception {
|
||||
RedkaleClassLoader.putReflectionPublicClasses(HttpServlet.class.getName());
|
||||
RedkaleClassLoader.putReflectionPublicClasses(HttpPrepareServlet.class.getName());
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(HttpResourceServlet.class, HttpResourceServlet.class.getName());
|
||||
final AnyValue servletsConf = this.serverConf.getAnyValue("servlets");
|
||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||
String prefix0 = servletsConf == null ? "" : servletsConf.getValue("path", "");
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||
final String prefix = prefix0;
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
List<FilterEntry<? extends Servlet>> list = new ArrayList(servletFilter.getFilterEntrys());
|
||||
list.sort((FilterEntry<? extends Servlet> o1, FilterEntry<? extends Servlet> o2) -> { //必须保证WebSocketServlet优先加载, 因为要确保其他的HttpServlet可以注入本地模式的WebSocketNode
|
||||
boolean ws1 = WebSocketServlet.class.isAssignableFrom(o1.getType());
|
||||
boolean ws2 = WebSocketServlet.class.isAssignableFrom(o2.getType());
|
||||
if (ws1 == ws2) {
|
||||
Priority p1 = o1.getType().getAnnotation(Priority.class);
|
||||
Priority p2 = o2.getType().getAnnotation(Priority.class);
|
||||
int v = (p2 == null ? 0 : p2.value()) - (p1 == null ? 0 : p1.value());
|
||||
return v == 0 ? o1.getType().getName().compareTo(o2.getType().getName()) : 0;
|
||||
}
|
||||
return ws1 ? -1 : 1;
|
||||
});
|
||||
final long starts = System.currentTimeMillis();
|
||||
final List<AbstractMap.SimpleEntry<String, String[]>> ss = sb == null ? null : new ArrayList<>();
|
||||
for (FilterEntry<? extends Servlet> en : list) {
|
||||
Class<HttpServlet> clazz = (Class<HttpServlet>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||
if (clazz.getAnnotation(Rest.RestDyn.class) != null) continue; //动态生成的跳过
|
||||
WebServlet ws = clazz.getAnnotation(WebServlet.class);
|
||||
if (ws == null) continue;
|
||||
if (ws.value().length == 0) {
|
||||
logger.log(Level.INFO, "not found @WebServlet.value in " + clazz.getName());
|
||||
continue;
|
||||
}
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName());
|
||||
final HttpServlet servlet = clazz.getDeclaredConstructor().newInstance();
|
||||
resourceFactory.inject(servlet, this);
|
||||
final String[] mappings = ws.value();
|
||||
String pref = ws.repair() ? prefix : "";
|
||||
DefaultAnyValue servletConf = (DefaultAnyValue) en.getProperty();
|
||||
this.httpServer.addHttpServlet(servlet, pref, servletConf, mappings);
|
||||
if (ss != null) {
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
mappings[i] = pref + mappings[i];
|
||||
}
|
||||
ss.add(new AbstractMap.SimpleEntry<>("HttpServlet (type=" + clazz.getName() + ")", mappings));
|
||||
}
|
||||
}
|
||||
final List<AbstractMap.SimpleEntry<String, String[]>> rests = sb == null ? null : new ArrayList<>();
|
||||
final List<AbstractMap.SimpleEntry<String, String[]>> webss = sb == null ? null : new ArrayList<>();
|
||||
if (rest && serverConf != null) {
|
||||
final List<Object> restedObjects = new ArrayList<>();
|
||||
for (AnyValue restConf : serverConf.getAnyValues("rest")) {
|
||||
loadRestServlet(webSocketFilter, restConf, restedObjects, sb, rests, webss);
|
||||
}
|
||||
}
|
||||
int max = 0;
|
||||
if (ss != null && sb != null) {
|
||||
int maxTypeLength = 0;
|
||||
int maxNameLength = 0;
|
||||
if (rests != null) {
|
||||
for (AbstractMap.SimpleEntry<String, String[]> en : rests) {
|
||||
int pos = en.getKey().indexOf('#');
|
||||
if (pos > maxTypeLength) maxTypeLength = pos;
|
||||
int len = en.getKey().length() - pos - 1;
|
||||
if (len > maxNameLength) maxNameLength = len;
|
||||
}
|
||||
}
|
||||
if (webss != null) {
|
||||
for (AbstractMap.SimpleEntry<String, String[]> en : webss) {
|
||||
int pos = en.getKey().indexOf('#');
|
||||
if (pos > maxTypeLength) maxTypeLength = pos;
|
||||
int len = en.getKey().length() - pos - 1;
|
||||
if (len > maxNameLength) maxNameLength = len;
|
||||
}
|
||||
}
|
||||
if (rests != null) {
|
||||
for (AbstractMap.SimpleEntry<String, String[]> en : rests) {
|
||||
StringBuilder sub = new StringBuilder();
|
||||
int pos = en.getKey().indexOf('#');
|
||||
sub.append("RestDynServlet (type=").append(en.getKey().substring(0, pos));
|
||||
for (int i = 0; i < maxTypeLength - pos; i++) {
|
||||
sub.append(' ');
|
||||
}
|
||||
sub.append(", name='").append(en.getKey().substring(pos + 1));
|
||||
for (int i = 0; i < maxNameLength - pos; i++) {
|
||||
sub.append(' ');
|
||||
}
|
||||
sub.append("')");
|
||||
ss.add(new AbstractMap.SimpleEntry<>(sub.toString(), en.getValue()));
|
||||
}
|
||||
}
|
||||
if (webss != null) {
|
||||
for (AbstractMap.SimpleEntry<String, String[]> en : webss) {
|
||||
StringBuilder sub = new StringBuilder();
|
||||
int pos = en.getKey().indexOf('#');
|
||||
sub.append("RestWebSocket (type=").append(en.getKey().substring(0, pos));
|
||||
for (int i = 0; i < maxTypeLength - pos; i++) {
|
||||
sub.append(' ');
|
||||
}
|
||||
sub.append(", name='").append(en.getKey().substring(pos + 1));
|
||||
for (int i = 0; i < maxNameLength - pos; i++) {
|
||||
sub.append(' ');
|
||||
}
|
||||
sub.append("')");
|
||||
ss.add(new AbstractMap.SimpleEntry<>(sub.toString(), en.getValue()));
|
||||
}
|
||||
}
|
||||
ss.sort((AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
if (as.getKey().length() > max) max = as.getKey().length();
|
||||
}
|
||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||
sb.append(localThreadName).append("Load ").append(as.getKey());
|
||||
for (int i = 0; i < max - as.getKey().length(); i++) {
|
||||
sb.append(' ');
|
||||
}
|
||||
sb.append(" mapping to ").append(Arrays.toString(as.getValue())).append(LINE_SEPARATOR);
|
||||
}
|
||||
sb.append(localThreadName).append("All HttpServlets load cost ").append(System.currentTimeMillis() - starts).append(" ms").append(LINE_SEPARATOR);
|
||||
}
|
||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString().trim());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void loadRestServlet(final ClassFilter<? extends WebSocket> webSocketFilter, final AnyValue restConf, final List<Object> restedObjects, final StringBuilder sb,
|
||||
final List<AbstractMap.SimpleEntry<String, String[]>> rests, final List<AbstractMap.SimpleEntry<String, String[]>> webss) throws Exception {
|
||||
if (!rest) return;
|
||||
if (restConf == null) return; //不存在REST服务
|
||||
|
||||
String prefix0 = restConf.getValue("path", "");
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||
|
||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
String mqname = restConf.getValue("mq");
|
||||
MessageAgent agent0 = null;
|
||||
if (mqname != null) {
|
||||
agent0 = application.getMessageAgent(mqname);
|
||||
if (agent0 == null) throw new RuntimeException("not found " + MessageAgent.class.getSimpleName() + " config for (name=" + mqname + ")");
|
||||
}
|
||||
final MessageAgent messageAgent = agent0;
|
||||
if (messageAgent != null) prefix0 = ""; //开启MQ时,prefix字段失效
|
||||
final String prefix = prefix0;
|
||||
final boolean autoload = restConf.getBoolValue("autoload", true);
|
||||
{ //加载RestService
|
||||
String userTypeStr = restConf.getValue("usertype");
|
||||
final Class userType = userTypeStr == null ? null : this.serverClassLoader.loadClass(userTypeStr);
|
||||
|
||||
final Class baseServletType = this.serverClassLoader.loadClass(restConf.getValue("base", HttpServlet.class.getName()));
|
||||
final Set<String> includeValues = new HashSet<>();
|
||||
final Set<String> excludeValues = new HashSet<>();
|
||||
for (AnyValue item : restConf.getAnyValues("service")) {
|
||||
if (item.getBoolValue("ignore", false)) {
|
||||
excludeValues.add(item.getValue("value", ""));
|
||||
} else {
|
||||
includeValues.add(item.getValue("value", ""));
|
||||
}
|
||||
}
|
||||
|
||||
final ClassFilter restFilter = ClassFilter.create(serverClassLoader, null, application.isCompileMode() ? "" : restConf.getValue("includes", ""), application.isCompileMode() ? "" : restConf.getValue("excludes", ""), includeValues, excludeValues);
|
||||
final CountDownLatch scdl = new CountDownLatch(super.interceptorServices.size());
|
||||
Stream<Service> stream = super.interceptorServices.stream();
|
||||
if (!application.isCompileMode()) stream = stream.parallel(); //不能并行,否则在maven plugin运行环境下ClassLoader不对
|
||||
stream.forEach((service) -> {
|
||||
try {
|
||||
final Class stype = Sncp.getServiceType(service);
|
||||
final String name = Sncp.getResourceName(service);
|
||||
RestService rs = (RestService) stype.getAnnotation(RestService.class);
|
||||
if (rs == null || rs.ignore()) return;
|
||||
|
||||
final String stypename = stype.getName();
|
||||
if (!autoload && !includeValues.contains(stypename)) return;
|
||||
if (!restFilter.accept(stypename)) return;
|
||||
synchronized (restedObjects) {
|
||||
if (restedObjects.contains(service)) {
|
||||
logger.log(Level.WARNING, stype.getName() + " repeat create rest servlet, so ignore");
|
||||
return;
|
||||
}
|
||||
restedObjects.add(service); //避免重复创建Rest对象
|
||||
}
|
||||
HttpServlet servlet = httpServer.addRestServlet(serverClassLoader, service, userType, baseServletType, prefix);
|
||||
if (servlet == null) return; //没有HttpMapping方法的HttpServlet调用Rest.createRestServlet就会返回null
|
||||
String prefix2 = prefix;
|
||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||
if (ws != null && !ws.repair()) prefix2 = "";
|
||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||
dynServletMap.put(service, servlet);
|
||||
if (messageAgent != null) messageAgent.putService(this, service, servlet);
|
||||
//if (finest) logger.finest(localThreadName + " Create RestServlet(resource.name='" + name + "') = " + servlet);
|
||||
if (rests != null) {
|
||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
mappings[i] = prefix2 + mappings[i];
|
||||
}
|
||||
synchronized (rests) {
|
||||
rests.add(new AbstractMap.SimpleEntry<>(Sncp.getServiceType(service).getName() + "#" + name, mappings));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
scdl.countDown();
|
||||
}
|
||||
});
|
||||
scdl.await();
|
||||
}
|
||||
if (webSocketFilter != null) { //加载RestWebSocket
|
||||
final Set<String> includeValues = new HashSet<>();
|
||||
final Set<String> excludeValues = new HashSet<>();
|
||||
for (AnyValue item : restConf.getAnyValues("websocket")) {
|
||||
if (item.getBoolValue("ignore", false)) {
|
||||
excludeValues.add(item.getValue("value", ""));
|
||||
} else {
|
||||
includeValues.add(item.getValue("value", ""));
|
||||
}
|
||||
}
|
||||
final ClassFilter restFilter = ClassFilter.create(serverClassLoader, null, application.isCompileMode() ? "" : restConf.getValue("includes", ""), application.isCompileMode() ? "" : restConf.getValue("excludes", ""), includeValues, excludeValues);
|
||||
final boolean finest = logger.isLoggable(Level.FINEST);
|
||||
|
||||
List<FilterEntry<? extends WebSocket>> list = new ArrayList(webSocketFilter.getFilterEntrys());
|
||||
for (FilterEntry<? extends WebSocket> en : list) {
|
||||
Class<WebSocket> clazz = (Class<WebSocket>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) {
|
||||
logger.log(Level.FINE, clazz.getName() + " cannot abstract on rest websocket, so ignore");
|
||||
continue;
|
||||
}
|
||||
if (Modifier.isFinal(clazz.getModifiers())) {
|
||||
logger.log(Level.FINE, clazz.getName() + " cannot final on rest websocket, so ignore");
|
||||
continue;
|
||||
}
|
||||
final Class<? extends WebSocket> stype = en.getType();
|
||||
if (stype.getAnnotation(Rest.RestDyn.class) != null) continue;
|
||||
RestWebSocket rs = stype.getAnnotation(RestWebSocket.class);
|
||||
if (rs == null || rs.ignore()) continue;
|
||||
|
||||
final String stypename = stype.getName();
|
||||
if (!autoload && !includeValues.contains(stypename)) continue;
|
||||
if (!restFilter.accept(stypename)) continue;
|
||||
if (restedObjects.contains(stype)) {
|
||||
logger.log(Level.WARNING, stype.getName() + " repeat create rest websocket, so ignore");
|
||||
continue;
|
||||
}
|
||||
restedObjects.add(stype); //避免重复创建Rest对象
|
||||
WebSocketServlet servlet = httpServer.addRestWebSocketServlet(serverClassLoader, stype, messageAgent, prefix, en.getProperty());
|
||||
if (servlet == null) continue; //没有RestOnMessage方法的HttpServlet调用Rest.createRestWebSocketServlet就会返回null
|
||||
String prefix2 = prefix;
|
||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||
if (ws != null && !ws.repair()) prefix2 = "";
|
||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||
if (finest) logger.finest(localThreadName + " " + stype.getName() + " create a RestWebSocketServlet");
|
||||
if (webss != null) {
|
||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||
for (int i = 0; i < mappings.length; i++) {
|
||||
mappings[i] = prefix2 + mappings[i];
|
||||
}
|
||||
synchronized (webss) {
|
||||
webss.add(new AbstractMap.SimpleEntry<>(stype.getName() + "#" + rs.name(), mappings));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (messageAgent != null) this.messageAgents.put(messageAgent.getName(), messageAgent);
|
||||
}
|
||||
|
||||
@Override //loadServlet执行之后调用
|
||||
protected void postLoadServlets() {
|
||||
final ClusterAgent cluster = application.clusterAgent;
|
||||
if (!application.isCompileMode() && cluster != null) {
|
||||
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
|
||||
String protocol = pros.value().toUpperCase();
|
||||
if (!cluster.containsProtocol(protocol)) return;
|
||||
if (!cluster.containsPort(server.getSocketAddress().getPort())) return;
|
||||
cluster.register(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterClusterDeregisterOnPreDestroyServices(ClusterAgent cluster, String protocol) {
|
||||
cluster.deregister(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -33,8 +33,8 @@ public class NodeSncpServer extends NodeServer {
|
||||
private NodeSncpServer(Application application, AnyValue serconf) {
|
||||
super(application, createServer(application, serconf));
|
||||
this.sncpServer = (SncpServer) this.server;
|
||||
this.consumer = sncpServer == null || application.singletonrun ? null : (agent, x) -> {//singleton模式下不生成SncpServlet
|
||||
if (x.getClass().getAnnotation(Local.class) != null) return;
|
||||
this.consumer = sncpServer == null || application.isSingletonMode() ? null : (agent, x) -> {//singleton模式下不生成SncpServlet
|
||||
if (x.getClass().getAnnotation(Local.class) != null) return; //本地模式的Service不生成SncpServlet
|
||||
SncpDynServlet servlet = sncpServer.addSncpServlet(x);
|
||||
dynServletMap.put(x, servlet);
|
||||
if (agent != null) agent.putService(this, x, servlet);
|
||||
@@ -46,7 +46,7 @@ public class NodeSncpServer extends NodeServer {
|
||||
}
|
||||
|
||||
private static Server createServer(Application application, AnyValue serconf) {
|
||||
return new SncpServer(application.getStartTime(), application.getResourceFactory().createChild());
|
||||
return new SncpServer(application, application.getStartTime(), serconf, application.getResourceFactory().createChild());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,6 +95,7 @@ public class NodeSncpServer extends NodeServer {
|
||||
for (FilterEntry<? extends Filter> en : list) {
|
||||
Class<SncpFilter> clazz = (Class<SncpFilter>) en.getType();
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName());
|
||||
final SncpFilter filter = clazz.getDeclaredConstructor().newInstance();
|
||||
resourceFactory.inject(filter, this);
|
||||
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
||||
@@ -106,6 +107,8 @@ public class NodeSncpServer extends NodeServer {
|
||||
|
||||
@Override
|
||||
protected void loadServlet(ClassFilter<? extends Servlet> servletFilter, ClassFilter otherFilter) throws Exception {
|
||||
RedkaleClassLoader.putReflectionPublicClasses(SncpServlet.class.getName());
|
||||
RedkaleClassLoader.putReflectionPublicClasses(SncpDynServlet.class.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
86
src/main/java/org/redkale/boot/PrepareCompiler.java
Normal file
86
src/main/java/org/redkale/boot/PrepareCompiler.java
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.boot;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import javax.persistence.Entity;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.convert.Decodeable;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.source.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 执行一次Application.run提前获取所有动态类
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public class PrepareCompiler {
|
||||
|
||||
// public static void main(String[] args) throws Exception {
|
||||
// new PrepareCompiler().run();
|
||||
// }
|
||||
public Application run() throws Exception {
|
||||
final Application application = new Application(false, true, Application.loadAppConfig());
|
||||
application.init();
|
||||
for (ApplicationListener listener : application.listeners) {
|
||||
listener.preStart(application);
|
||||
}
|
||||
for (ApplicationListener listener : application.listeners) {
|
||||
listener.preCompile(application);
|
||||
}
|
||||
application.start();
|
||||
final boolean hasSncp = application.getNodeServers().stream().filter(v -> v instanceof NodeSncpServer).findFirst().isPresent();
|
||||
final String[] exlibs = (application.excludelibs != null ? (application.excludelibs + ";") : "").split(";");
|
||||
|
||||
final ClassFilter<?> entityFilter = new ClassFilter(application.getClassLoader(), Entity.class, Object.class, (Class[]) null);
|
||||
final ClassFilter<?> beanFilter = new ClassFilter(application.getClassLoader(), Bean.class, Object.class, (Class[]) null);
|
||||
final ClassFilter<?> filterFilter = new ClassFilter(application.getClassLoader(), null, FilterBean.class, (Class[]) null);
|
||||
|
||||
ClassFilter.Loader.load(application.getHome(), application.getClassLoader(), exlibs, entityFilter, beanFilter, filterFilter);
|
||||
|
||||
for (FilterEntry en : entityFilter.getFilterEntrys()) {
|
||||
Class clz = en.getType();
|
||||
if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers())) continue;
|
||||
try {
|
||||
application.dataSources.forEach(source -> source.compile(clz));
|
||||
JsonFactory.root().loadEncoder(clz);
|
||||
if (hasSncp) BsonFactory.root().loadEncoder(clz);
|
||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||
if (hasSncp) BsonFactory.root().loadDecoder(clz);
|
||||
decoder.convertFrom(new JsonReader("{}"));
|
||||
} catch (Exception e) { //JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||
}
|
||||
}
|
||||
for (FilterEntry en : beanFilter.getFilterEntrys()) {
|
||||
Class clz = en.getType();
|
||||
if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers())) continue;
|
||||
try {
|
||||
JsonFactory.root().loadEncoder(clz);
|
||||
if (hasSncp) BsonFactory.root().loadEncoder(clz);
|
||||
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||
if (hasSncp) BsonFactory.root().loadDecoder(clz);
|
||||
decoder.convertFrom(new JsonReader("{}"));
|
||||
} catch (Exception e) { //JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||
}
|
||||
}
|
||||
for (FilterEntry en : filterFilter.getFilterEntrys()) {
|
||||
Class clz = en.getType();
|
||||
if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers())) continue;
|
||||
try {
|
||||
FilterNodeBean.load(clz);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
for (ApplicationListener listener : application.listeners) {
|
||||
listener.postCompile(application);
|
||||
}
|
||||
application.shutdown();
|
||||
return application;
|
||||
}
|
||||
}
|
||||
@@ -55,8 +55,8 @@
|
||||
} else {
|
||||
var w = param.required ? "font-weight:bold;" : "";
|
||||
var c = ' style="' + w + '"';
|
||||
if (param.src == "HEADER") c = ' style="color:red;' + w + '"';
|
||||
if (param.src == "COOKIE") c = ' style="color:blue;' + w + '"';
|
||||
if (param.style == "HEADER") c = ' style="color:red;' + w + '"';
|
||||
if (param.style == "COOKIE") c = ' style="color:blue;' + w + '"';
|
||||
paramshtml.push('<tr><td ' + c + '> ' + param.name + ' </td><td> ' + t + '</td><td> ' + param.comment + '</td></tr>');
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,15 @@ import org.redkale.watch.WatchService;
|
||||
*/
|
||||
public abstract class AbstractWatchService extends AbstractService implements WatchService {
|
||||
|
||||
/**
|
||||
* 缺少参数
|
||||
*/
|
||||
@Comment("缺少参数")
|
||||
public static final int RET_WATCH_PARAMS_ILLEGAL = 1600_0001;
|
||||
|
||||
/**
|
||||
* 执行异常
|
||||
*/
|
||||
@Comment("执行异常")
|
||||
public static final int RET_WATCH_RUN_EXCEPTION = 1600_0002;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
final Server server = node.getServer();
|
||||
InetSocketAddress newAddr = new InetSocketAddress(newhost == null || newhost.isEmpty() ? server.getSocketAddress().getHostString() : newhost, newport);
|
||||
try {
|
||||
server.changeAddress(newAddr);
|
||||
server.changeAddress(application, newAddr);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new RetResult(RET_SERVER_CHANGEPORT_ERROR, "changeaddress error");
|
||||
@@ -72,7 +72,7 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
private Map<String, Object> formatToMap(NodeServer node) {
|
||||
Server server = node.getServer();
|
||||
Map<String, Object> rs = new LinkedHashMap<>();
|
||||
String protocol = server.getProtocol();
|
||||
String protocol = server.getNetprotocol();
|
||||
if (node instanceof NodeSncpServer) {
|
||||
protocol += "/SNCP";
|
||||
} else if (node instanceof NodeWatchServer) {
|
||||
@@ -86,7 +86,6 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
rs.put("name", server.getName());
|
||||
rs.put("protocol", protocol);
|
||||
rs.put("address", server.getSocketAddress());
|
||||
rs.put("threads", server.getThreads());
|
||||
rs.put("backlog", server.getBacklog());
|
||||
rs.put("bufferCapacity", server.getBufferCapacity());
|
||||
rs.put("bufferPoolSize", server.getBufferPoolSize());
|
||||
@@ -81,7 +81,7 @@ public class TransportWatchService extends AbstractWatchService {
|
||||
break;
|
||||
}
|
||||
}
|
||||
application.restoreConfig();
|
||||
//application.restoreConfig();
|
||||
}
|
||||
return RetResult.success();
|
||||
}
|
||||
@@ -113,7 +113,7 @@ public class TransportWatchService extends AbstractWatchService {
|
||||
break;
|
||||
}
|
||||
}
|
||||
application.restoreConfig();
|
||||
//application.restoreConfig();
|
||||
}
|
||||
return RetResult.success();
|
||||
}
|
||||
327
src/main/java/org/redkale/cluster/CacheClusterAgent.java
Normal file
327
src/main/java/org/redkale/cluster/CacheClusterAgent.java
Normal file
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.cluster;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.Level;
|
||||
import javax.annotation.Resource;
|
||||
import org.redkale.boot.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.source.CacheSource;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 使用CacheSource实现的第三方服务发现管理接口cluster
|
||||
*
|
||||
*
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class CacheClusterAgent extends ClusterAgent implements Resourcable {
|
||||
|
||||
@Resource(name = "$")
|
||||
private CacheSource source;
|
||||
|
||||
private String sourceName;
|
||||
|
||||
protected int ttls = 10; //定时检查的秒数
|
||||
|
||||
protected ScheduledThreadPoolExecutor scheduler;
|
||||
|
||||
//可能被HttpMessageClient用到的服务 key: servicename
|
||||
protected final ConcurrentHashMap<String, Collection<InetSocketAddress>> httpAddressMap = new ConcurrentHashMap<>();
|
||||
|
||||
//可能被mqtp用到的服务 key: servicename
|
||||
protected final ConcurrentHashMap<String, Collection<InetSocketAddress>> mqtpAddressMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void init(AnyValue config) {
|
||||
super.init(config);
|
||||
this.sourceName = getSourceName();
|
||||
|
||||
AnyValue[] properties = config.getAnyValues("property");
|
||||
for (AnyValue property : properties) {
|
||||
if ("ttls".equalsIgnoreCase(property.getValue("name"))) {
|
||||
this.ttls = Integer.parseInt(property.getValue("value", "").trim());
|
||||
if (this.ttls < 5) this.ttls = 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy(AnyValue config) {
|
||||
if (scheduler != null) scheduler.shutdownNow();
|
||||
}
|
||||
|
||||
public String getSourceName() {
|
||||
AnyValue[] properties = config.getAnyValues("property");
|
||||
for (AnyValue property : properties) {
|
||||
if ("source".equalsIgnoreCase(property.getValue("name"))
|
||||
&& property.getValue("value") != null) {
|
||||
this.sourceName = property.getValue("value");
|
||||
return this.sourceName;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resourceName() {
|
||||
return sourceName;
|
||||
}
|
||||
|
||||
@Override //ServiceLoader时判断配置是否符合当前实现类
|
||||
public boolean acceptsConf(AnyValue config) {
|
||||
if (config == null) return false;
|
||||
AnyValue[] properties = config.getAnyValues("property");
|
||||
if (properties == null || properties.length == 0) return false;
|
||||
for (AnyValue property : properties) {
|
||||
if ("source".equalsIgnoreCase(property.getValue("name"))
|
||||
&& property.getValue("value") != null) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
if (this.scheduler == null) {
|
||||
this.scheduler = new ScheduledThreadPoolExecutor(4, (Runnable r) -> {
|
||||
final Thread t = new Thread(r, "Redkale-" + CacheClusterAgent.class.getSimpleName() + "-Task-Thread");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
|
||||
this.scheduler.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
checkApplicationHealth();
|
||||
checkHttpAddressHealth();
|
||||
loadMqtpAddressHealth();
|
||||
localEntrys.values().stream().filter(e -> !e.canceled).forEach(entry -> {
|
||||
checkLocalHealth(entry);
|
||||
});
|
||||
remoteEntrys.values().stream().filter(entry -> "SNCP".equalsIgnoreCase(entry.protocol)).forEach(entry -> {
|
||||
updateSncpTransport(entry);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "scheduleAtFixedRate check error", e instanceof CompletionException ? ((CompletionException) e).getCause() : e);
|
||||
}
|
||||
}, Math.max(2000, ttls * 1000), Math.max(2000, ttls * 1000), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
protected void loadMqtpAddressHealth() {
|
||||
List<String> keys = source.queryKeysStartsWith("cluster.mqtp:");
|
||||
keys.forEach(servicename -> {
|
||||
try {
|
||||
this.mqtpAddressMap.put(servicename, queryAddress(servicename).get(3, TimeUnit.SECONDS));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "loadMqtpAddressHealth check " + servicename + " error", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void checkHttpAddressHealth() {
|
||||
try {
|
||||
this.httpAddressMap.keySet().stream().forEach(servicename -> {
|
||||
try {
|
||||
this.httpAddressMap.put(servicename, queryAddress(servicename).get(3, TimeUnit.SECONDS));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "checkHttpAddressHealth check " + servicename + " error", e);
|
||||
}
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.SEVERE, "checkHttpAddressHealth check error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void checkLocalHealth(final ClusterEntry entry) {
|
||||
AddressEntry newaddr = new AddressEntry();
|
||||
newaddr.addr = entry.address;
|
||||
newaddr.nodeid = this.nodeid;
|
||||
newaddr.time = System.currentTimeMillis();
|
||||
source.hset(entry.checkname, entry.checkid, AddressEntry.class, newaddr);
|
||||
}
|
||||
|
||||
@Override //获取MQTP的HTTP远程服务的可用ip列表, key = servicename的后半段
|
||||
public CompletableFuture<Map<String, Collection<InetSocketAddress>>> queryMqtpAddress(String protocol, String module, String resname) {
|
||||
final Map<String, Collection<InetSocketAddress>> rsmap = new ConcurrentHashMap<>();
|
||||
final String servicenamprefix = generateHttpServiceName(protocol, module, null) + ":";
|
||||
mqtpAddressMap.keySet().stream().filter(k -> k.startsWith(servicenamprefix))
|
||||
.forEach(sn -> rsmap.put(sn.substring(servicenamprefix.length()), mqtpAddressMap.get(sn)));
|
||||
return CompletableFuture.completedFuture(rsmap);
|
||||
}
|
||||
|
||||
@Override //获取HTTP远程服务的可用ip列表
|
||||
public CompletableFuture<Collection<InetSocketAddress>> queryHttpAddress(String protocol, String module, String resname) {
|
||||
final String servicename = generateHttpServiceName(protocol, module, resname);
|
||||
Collection<InetSocketAddress> rs = httpAddressMap.get(servicename);
|
||||
if (rs != null) return CompletableFuture.completedFuture(rs);
|
||||
return queryAddress(servicename).thenApply(t -> {
|
||||
httpAddressMap.put(servicename, t);
|
||||
return t;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Collection<InetSocketAddress>> queryAddress(final ClusterEntry entry) {
|
||||
return queryAddress(entry.servicename);
|
||||
}
|
||||
|
||||
private CompletableFuture<Collection<InetSocketAddress>> queryAddress(final String servicename) {
|
||||
final CompletableFuture<Map<String, AddressEntry>> future = source.hmapAsync(servicename, AddressEntry.class, 0, 10000);
|
||||
return future.thenApply(map -> {
|
||||
final Set<InetSocketAddress> set = new HashSet<>();
|
||||
map.forEach((n, v) -> {
|
||||
if (v != null && (System.currentTimeMillis() - v.time) / 1000 < ttls) set.add(v.addr);
|
||||
});
|
||||
return set;
|
||||
});
|
||||
}
|
||||
|
||||
protected boolean isApplicationHealth() {
|
||||
String servicename = generateApplicationServiceName();
|
||||
String serviceid = generateApplicationServiceId();
|
||||
AddressEntry entry = (AddressEntry) source.hget(servicename, serviceid, AddressEntry.class);
|
||||
return entry != null && (System.currentTimeMillis() - entry.time) / 1000 < ttls;
|
||||
}
|
||||
|
||||
protected void checkApplicationHealth() {
|
||||
String checkname = generateApplicationServiceName();
|
||||
String checkid = generateApplicationCheckId();
|
||||
AddressEntry entry = new AddressEntry();
|
||||
entry.addr = this.appAddress;
|
||||
entry.nodeid = this.nodeid;
|
||||
entry.time = System.currentTimeMillis();
|
||||
source.hset(checkname, checkid, AddressEntry.class, entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(Application application) {
|
||||
if (isApplicationHealth()) throw new RuntimeException("application.nodeid=" + nodeid + " exists in cluster");
|
||||
deregister(application);
|
||||
|
||||
String serviceid = generateApplicationServiceId();
|
||||
String servicename = generateApplicationServiceName();
|
||||
AddressEntry entry = new AddressEntry();
|
||||
entry.addr = this.appAddress;
|
||||
entry.nodeid = this.nodeid;
|
||||
entry.time = System.currentTimeMillis();
|
||||
source.hset(servicename, serviceid, AddressEntry.class, entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregister(Application application) {
|
||||
String servicename = generateApplicationServiceName();
|
||||
source.remove(servicename);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ClusterEntry register(NodeServer ns, String protocol, Service service) {
|
||||
deregister(ns, protocol, service, false);
|
||||
//
|
||||
ClusterEntry clusterEntry = new ClusterEntry(ns, protocol, service);
|
||||
AddressEntry entry = new AddressEntry();
|
||||
entry.addr = clusterEntry.address;
|
||||
entry.nodeid = this.nodeid;
|
||||
entry.time = System.currentTimeMillis();
|
||||
source.hset(clusterEntry.servicename, clusterEntry.serviceid, AddressEntry.class, entry);
|
||||
return clusterEntry;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deregister(NodeServer ns, String protocol, Service service) {
|
||||
deregister(ns, protocol, service, true);
|
||||
}
|
||||
|
||||
protected void deregister(NodeServer ns, String protocol, Service service, boolean realcanceled) {
|
||||
String servicename = generateServiceName(ns, protocol, service);
|
||||
String serviceid = generateServiceId(ns, protocol, service);
|
||||
ClusterEntry currEntry = null;
|
||||
for (final ClusterEntry entry : localEntrys.values()) {
|
||||
if (entry.servicename.equals(servicename) && entry.serviceid.equals(serviceid)) {
|
||||
currEntry = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currEntry == null) {
|
||||
for (final ClusterEntry entry : remoteEntrys.values()) {
|
||||
if (entry.servicename.equals(servicename) && entry.serviceid.equals(serviceid)) {
|
||||
currEntry = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
source.hremove(servicename, serviceid);
|
||||
if (realcanceled && currEntry != null) currEntry.canceled = true;
|
||||
if (!"mqtp".equals(protocol) && currEntry != null && currEntry.submqtp) {
|
||||
deregister(ns, "mqtp", service, realcanceled);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateApplicationServiceName() {
|
||||
return "cluster." + super.generateApplicationServiceName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateServiceName(NodeServer ns, String protocol, Service service) {
|
||||
return "cluster." + super.generateServiceName(ns, protocol, service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateHttpServiceName(String protocol, String module, String resname) {
|
||||
return "cluster." + super.generateHttpServiceName(protocol, module, resname);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateApplicationCheckName() {
|
||||
return generateApplicationServiceName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateApplicationCheckId() {
|
||||
return generateApplicationServiceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateCheckName(NodeServer ns, String protocol, Service service) {
|
||||
return generateServiceName(ns, protocol, service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateCheckId(NodeServer ns, String protocol, Service service) {
|
||||
return generateServiceId(ns, protocol, service);
|
||||
}
|
||||
|
||||
public static class AddressEntry {
|
||||
|
||||
public InetSocketAddress addr;
|
||||
|
||||
public int nodeid;
|
||||
|
||||
public long time;
|
||||
|
||||
public AddressEntry() {
|
||||
}
|
||||
|
||||
public AddressEntry refresh() {
|
||||
this.time = System.currentTimeMillis();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -85,7 +85,7 @@ public abstract class ClusterAgent {
|
||||
}
|
||||
|
||||
//ServiceLoader时判断配置是否符合当前实现类
|
||||
public abstract boolean match(AnyValue config);
|
||||
public abstract boolean acceptsConf(AnyValue config);
|
||||
|
||||
public boolean containsProtocol(String protocol) {
|
||||
if (protocol == null || protocol.isEmpty()) return false;
|
||||
@@ -107,15 +107,14 @@ public abstract class ClusterAgent {
|
||||
//注册本地模式
|
||||
for (Service service : localServices) {
|
||||
if (!canRegister(protocol, service)) continue;
|
||||
register(ns, protocol, service);
|
||||
ClusterEntry htentry = new ClusterEntry(ns, protocol, service);
|
||||
ClusterEntry htentry = register(ns, protocol, service);
|
||||
localEntrys.put(htentry.serviceid, htentry);
|
||||
if (protocol.toLowerCase().startsWith("http")) {
|
||||
MessageMultiConsumer mmc = service.getClass().getAnnotation(MessageMultiConsumer.class);
|
||||
if (mmc != null) {
|
||||
register(ns, "mqtp", service);
|
||||
ClusterEntry mqentry = new ClusterEntry(ns, "mqtp", service);
|
||||
ClusterEntry mqentry = register(ns, "mqtp", service);
|
||||
localEntrys.put(mqentry.serviceid, mqentry);
|
||||
htentry.submqtp = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,6 +140,8 @@ public abstract class ClusterAgent {
|
||||
|
||||
protected boolean canRegister(String protocol, Service service) {
|
||||
if ("SNCP".equalsIgnoreCase(protocol) && service.getClass().getAnnotation(Local.class) != null) return false;
|
||||
AutoLoad al = service.getClass().getAnnotation(AutoLoad.class);
|
||||
if (al != null && !al.value() && service.getClass().getAnnotation(Local.class) != null) return false;
|
||||
if (service instanceof WebSocketNode) {
|
||||
if (((WebSocketNode) service).getLocalWebSocketEngine() == null) return false;
|
||||
}
|
||||
@@ -176,7 +177,7 @@ public abstract class ClusterAgent {
|
||||
protected abstract CompletableFuture<Collection<InetSocketAddress>> queryAddress(ClusterEntry entry);
|
||||
|
||||
//注册服务
|
||||
protected abstract void register(NodeServer ns, String protocol, Service service);
|
||||
protected abstract ClusterEntry register(NodeServer ns, String protocol, Service service);
|
||||
|
||||
//注销服务
|
||||
protected abstract void deregister(NodeServer ns, String protocol, Service service);
|
||||
@@ -311,7 +312,9 @@ public abstract class ClusterAgent {
|
||||
|
||||
public InetSocketAddress address;
|
||||
|
||||
public ScheduledFuture checkScheduledFuture;
|
||||
public boolean canceled;
|
||||
|
||||
public boolean submqtp;
|
||||
|
||||
public ClusterEntry(NodeServer ns, String protocol, Service service) {
|
||||
this.serviceid = generateServiceId(ns, protocol, service);
|
||||
@@ -319,10 +322,21 @@ public abstract class ClusterAgent {
|
||||
this.checkid = generateCheckId(ns, protocol, service);
|
||||
this.checkname = generateCheckName(ns, protocol, service);
|
||||
this.protocol = protocol;
|
||||
this.address = ns.getSocketAddress();
|
||||
InetSocketAddress addr = ns.getSocketAddress();
|
||||
String host = addr.getHostString();
|
||||
if ("0.0.0.0".equals(host)) {
|
||||
host = appAddress.getHostString();
|
||||
addr = new InetSocketAddress(host, addr.getPort());
|
||||
}
|
||||
this.address = addr;
|
||||
this.serviceref = new WeakReference(service);
|
||||
Server server = ns.getServer();
|
||||
this.netprotocol = server instanceof SncpServer ? ((SncpServer) server).getNetprotocol() : Transport.DEFAULT_PROTOCOL;
|
||||
this.netprotocol = server instanceof SncpServer ? ((SncpServer) server).getNetprotocol() : Transport.DEFAULT_NETPROTOCOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/main/java/org/redkale/cluster/ClusterAgentProvider.java
Normal file
25
src/main/java/org/redkale/cluster/ClusterAgentProvider.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.cluster;
|
||||
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
/**
|
||||
* 自定义的ClusterAgent加载器
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public interface ClusterAgentProvider {
|
||||
|
||||
public boolean acceptsConf(AnyValue config);
|
||||
|
||||
public Class<? extends ClusterAgent> agentClass();
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import static org.redkale.convert.Reader.ValueType.MAP;
|
||||
/**
|
||||
* 对不明类型的对象进行反序列化。 <br>
|
||||
* <b>注意: 目前只支持文本格式</b> <br>
|
||||
* <p>
|
||||
*
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
@@ -31,12 +31,17 @@ public class AnyDecoder implements Decodeable<Reader, Object> {
|
||||
|
||||
private static final Creator<HashMap> mapCreator = Creator.create(HashMap.class);
|
||||
|
||||
protected final Decodeable<Reader, String> stringDecoder;
|
||||
final Decodeable<Reader, String> stringDecoder;
|
||||
|
||||
protected final CollectionDecoder collectionDecoder;
|
||||
final CollectionDecoder collectionDecoder;
|
||||
|
||||
protected final MapDecoder mapDecoder;
|
||||
final MapDecoder mapDecoder;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param factory ConvertFactory
|
||||
*/
|
||||
public AnyDecoder(final ConvertFactory factory) {
|
||||
this.stringDecoder = factory.loadDecoder(String.class);
|
||||
this.collectionDecoder = new CollectionDecoder(factory, collectionObjectType, Object.class, collectionCreator, this);
|
||||
49
src/main/java/org/redkale/convert/AnyEncoder.java
Normal file
49
src/main/java/org/redkale/convert/AnyEncoder.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* 对不明类型的对象进行序列化; BSON序列化时将对象的类名写入Writer,JSON则不写入。
|
||||
*
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <T> 序列化的泛型类型
|
||||
*/
|
||||
public final class AnyEncoder<T> implements Encodeable<Writer, T> {
|
||||
|
||||
final ConvertFactory factory;
|
||||
|
||||
AnyEncoder(ConvertFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void convertTo(final Writer out, final T value) {
|
||||
if (value == null) {
|
||||
out.writeClassName(null);
|
||||
out.writeNull();
|
||||
} else {
|
||||
Class clazz = value.getClass();
|
||||
if (clazz == Object.class) {
|
||||
out.writeObjectB(value);
|
||||
out.writeObjectE(value);
|
||||
return;
|
||||
}
|
||||
if (out.needWriteClassName()) out.writeClassName(factory.getEntityAlias(clazz));
|
||||
factory.loadEncoder(clazz).convertTo(out, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
}
|
||||
40
src/main/java/org/redkale/convert/AnyValueDecoder.java
Normal file
40
src/main/java/org/redkale/convert/AnyValueDecoder.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
/**
|
||||
* AnyValue的Decoder实现
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类型
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public class AnyValueDecoder<R extends Reader> implements Decodeable<R, AnyValue> {
|
||||
|
||||
protected final ConvertFactory factory;
|
||||
|
||||
public AnyValueDecoder(final ConvertFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnyValue convertFrom(R in) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return AnyValue.class;
|
||||
}
|
||||
|
||||
}
|
||||
33
src/main/java/org/redkale/convert/AnyValueEncoder.java
Normal file
33
src/main/java/org/redkale/convert/AnyValueEncoder.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
/**
|
||||
* AnyValue的Encoder实现
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <W> Writer输出的子类
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public class AnyValueEncoder<W extends Writer> implements Encodeable<W, AnyValue> {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, AnyValue value) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return AnyValue.class;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -119,6 +119,7 @@ public class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
|
||||
}
|
||||
|
||||
protected T readMemberValue(Reader in, DeMember member, Decodeable<Reader, T> decoder, boolean first) {
|
||||
if (in == null) return null;
|
||||
return decoder.convertFrom(in);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
|
||||
protected final Encodeable<Writer, Object> componentEncoder;
|
||||
|
||||
protected final boolean subtypefinal;
|
||||
|
||||
protected volatile boolean inited = false;
|
||||
|
||||
protected final Object lock = new Object();
|
||||
@@ -47,6 +49,7 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
factory.register(type, this);
|
||||
this.componentEncoder = factory.loadEncoder(this.componentType);
|
||||
this.anyEncoder = factory.getAnyEncoder();
|
||||
this.subtypefinal = (this.componentType instanceof Class) && Modifier.isFinal(((Class) this.componentType).getModifiers());
|
||||
} finally {
|
||||
inited = true;
|
||||
synchronized (lock) {
|
||||
@@ -65,8 +68,9 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
out.writeNull();
|
||||
return;
|
||||
}
|
||||
if (value.length == 0) {
|
||||
out.writeArrayB(0, componentEncoder, value);
|
||||
int iMax = value.length - 1;
|
||||
if (iMax == -1) {
|
||||
out.writeArrayB(0, this, componentEncoder, value);
|
||||
out.writeArrayE();
|
||||
return;
|
||||
}
|
||||
@@ -81,19 +85,30 @@ public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (out.writeArrayB(value.length, componentEncoder, value) < 0) {
|
||||
final Type comp = this.componentType;
|
||||
boolean first = true;
|
||||
for (Object v : value) {
|
||||
if (!first) out.writeArrayMark();
|
||||
writeMemberValue(out, member, ((v != null && (v.getClass() == comp || out.specify() == comp)) ? componentEncoder : anyEncoder), v, first);
|
||||
if (first) first = false;
|
||||
Encodeable<Writer, Object> itemEncoder = this.componentEncoder;
|
||||
if (subtypefinal) {
|
||||
if (out.writeArrayB(value.length, this, itemEncoder, value) < 0) {
|
||||
for (int i = 0;; i++) {
|
||||
writeMemberValue(out, member, itemEncoder, value[i], i);
|
||||
if (i == iMax) break;
|
||||
out.writeArrayMark();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (out.writeArrayB(value.length, this, itemEncoder, value) < 0) {
|
||||
final Type comp = this.componentType;
|
||||
for (int i = 0;; i++) {
|
||||
Object v = value[i];
|
||||
writeMemberValue(out, member, ((v != null && (v.getClass() == comp || out.specify() == comp)) ? itemEncoder : anyEncoder), v, i);
|
||||
if (i == iMax) break;
|
||||
out.writeArrayMark();
|
||||
}
|
||||
}
|
||||
}
|
||||
out.writeArrayE();
|
||||
}
|
||||
|
||||
protected void writeMemberValue(Writer out, EnMember member, Encodeable<Writer, Object> encoder, Object value, boolean first) {
|
||||
protected void writeMemberValue(Writer out, EnMember member, Encodeable<Writer, Object> encoder, Object value, int index) {
|
||||
encoder.convertTo(out, value);
|
||||
}
|
||||
|
||||
@@ -32,5 +32,4 @@ public abstract class BinaryConvert<R extends Reader, W extends Writer> extends
|
||||
|
||||
public abstract byte[] convertTo(final Type type, final Object value);
|
||||
|
||||
public abstract byte[] convertMapTo(final Object... values);
|
||||
}
|
||||
@@ -129,6 +129,7 @@ public class CollectionDecoder<T> implements Decodeable<Reader, Collection<T>> {
|
||||
}
|
||||
|
||||
protected T readMemberValue(Reader in, DeMember member, Decodeable<Reader, T> decoder, boolean first) {
|
||||
if (in == null) return null;
|
||||
return decoder.convertFrom(in);
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ public class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> {
|
||||
return;
|
||||
}
|
||||
if (value.isEmpty()) {
|
||||
out.writeArrayB(0, componentEncoder, value);
|
||||
out.writeArrayB(0, this, componentEncoder, value);
|
||||
out.writeArrayE();
|
||||
return;
|
||||
}
|
||||
@@ -76,18 +76,18 @@ public class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (out.writeArrayB(value.size(), componentEncoder, value) < 0) {
|
||||
if (out.writeArrayB(value.size(), this, componentEncoder, value) < 0) {
|
||||
boolean first = true;
|
||||
for (Object v : value) {
|
||||
if (!first) out.writeArrayMark();
|
||||
writeValue(out, member, v);
|
||||
writeMemberValue(out, member, v, first);
|
||||
if (first) first = false;
|
||||
}
|
||||
}
|
||||
out.writeArrayE();
|
||||
}
|
||||
|
||||
protected void writeValue(Writer out, EnMember member, Object value) {
|
||||
protected void writeMemberValue(Writer out, EnMember member, Object value, boolean first) {
|
||||
componentEncoder.convertTo(out, value);
|
||||
}
|
||||
|
||||
@@ -96,6 +96,11 @@ public class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + "{componentType:" + this.type + ", encoder:" + this.componentEncoder + "}";
|
||||
}
|
||||
|
||||
public Encodeable<Writer, Object> getComponentEncoder() {
|
||||
return componentEncoder;
|
||||
}
|
||||
@@ -8,7 +8,7 @@ package org.redkale.convert;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.*;
|
||||
import org.redkale.util.Attribute;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 序列化/反序列化操作类
|
||||
@@ -50,18 +50,31 @@ public abstract class Convert<R extends Reader, W extends Writer> {
|
||||
|
||||
public abstract <T> T convertFrom(final Type type, final byte[] bytes);
|
||||
|
||||
//@since 2.2.0
|
||||
public abstract <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int length);
|
||||
|
||||
public abstract <T> T convertFrom(final Type type, final ByteBuffer... buffers);
|
||||
|
||||
public abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers);
|
||||
|
||||
public abstract void convertTo(final W writer, final Object value);
|
||||
|
||||
public abstract void convertTo(final W writer, final Type type, final Object value);
|
||||
|
||||
public abstract byte[] convertToBytes(final Object value);
|
||||
|
||||
public abstract byte[] convertToBytes(final Type type, final Object value);
|
||||
|
||||
public abstract void convertToBytes(final Object value, final ConvertBytesHandler handler);
|
||||
|
||||
public abstract void convertToBytes(final Type type, final Object value, final ConvertBytesHandler handler);
|
||||
|
||||
public abstract void convertToBytes(final ByteArray array, final Object value);
|
||||
|
||||
public abstract void convertToBytes(final ByteArray array, final Type type, final Object value);
|
||||
|
||||
public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value);
|
||||
|
||||
public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value);
|
||||
|
||||
public abstract ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values);
|
||||
|
||||
}
|
||||
23
src/main/java/org/redkale/convert/ConvertBytesHandler.java
Normal file
23
src/main/java/org/redkale/convert/ConvertBytesHandler.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
*
|
||||
* convertToBytes系列的方法的回调
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public interface ConvertBytesHandler {
|
||||
|
||||
<A> void completed(byte[] bs, int offset, int length, Consumer<A> callback, A attachment);
|
||||
}
|
||||
@@ -16,7 +16,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.*;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.ext.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -32,11 +34,15 @@ import org.redkale.util.*;
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
private static final AtomicBoolean loaderInited = new AtomicBoolean();
|
||||
|
||||
private static Convert defProtobufConvert;
|
||||
|
||||
private final ConvertFactory parent;
|
||||
|
||||
protected Convert<R, W> convert;
|
||||
|
||||
protected boolean tiny;
|
||||
protected boolean tiny; //String类型值为"",Boolean类型值为false时是否需要输出, 默认为true
|
||||
|
||||
private final Encodeable<W, ?> anyEncoder = new AnyEncoder(this);
|
||||
|
||||
@@ -65,6 +71,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
this.parent = parent;
|
||||
if (parent == null) {
|
||||
//---------------------------------------------------------
|
||||
|
||||
this.register(boolean.class, BoolSimpledCoder.instance);
|
||||
this.register(Boolean.class, BoolSimpledCoder.instance);
|
||||
|
||||
@@ -95,17 +102,23 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
||||
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
|
||||
this.register(java.util.Date.class, DateSimpledCoder.instance);
|
||||
this.register(java.time.Instant.class, InstantSimpledCoder.instance);
|
||||
this.register(java.time.LocalDate.class, LocalDateSimpledCoder.instance);
|
||||
this.register(java.time.LocalTime.class, LocalTimeSimpledCoder.instance);
|
||||
this.register(java.time.LocalDateTime.class, LocalDateTimeSimpledCoder.instance);
|
||||
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
|
||||
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
|
||||
this.register(AtomicLong.class, AtomicLongSimpledCoder.instance);
|
||||
this.register(BigInteger.class, BigIntegerSimpledCoder.instance);
|
||||
this.register(BigDecimal.class, BigDecimalSimpledCoder.instance);
|
||||
this.register(InetAddress.class, InetAddressSimpledCoder.instance);
|
||||
this.register(LongAdder.class, LongAdderSimpledCoder.instance);
|
||||
this.register(DLong.class, DLongSimpledCoder.instance);
|
||||
this.register(Class.class, TypeSimpledCoder.instance);
|
||||
this.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressSimpledCoder.instance);
|
||||
this.register(Pattern.class, PatternSimpledCoder.instance);
|
||||
this.register(File.class, FileSimpledCoder.instance);
|
||||
this.register(Throwable.class, ThrowableSimpledCoder.instance);
|
||||
this.register(CompletionHandler.class, CompletionHandlerSimpledCoder.instance);
|
||||
this.register(URL.class, URLSimpledCoder.instance);
|
||||
this.register(URI.class, URISimpledCoder.instance);
|
||||
@@ -134,48 +147,51 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
});
|
||||
try {
|
||||
Class sqldateClass = Class.forName("java.sql.Date");
|
||||
this.register(sqldateClass, new SimpledCoder<R, W, java.sql.Date>() {
|
||||
Class sqldateClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date");
|
||||
Invoker<Object, Object> sqldateInvoker = Invoker.create(sqldateClass, "valueOf", String.class);
|
||||
this.register(sqldateClass, new SimpledCoder<R, W, Object>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Date value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Date convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Date.valueOf(t);
|
||||
}
|
||||
|
||||
});
|
||||
Class sqltimeClass = Class.forName("java.sql.Time");
|
||||
this.register(sqltimeClass, new SimpledCoder<R, W, java.sql.Time>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Time value) {
|
||||
public void convertTo(W out, Object value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Time convertFrom(R in) {
|
||||
public Object convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Time.valueOf(t);
|
||||
return t == null ? null : sqldateInvoker.invoke(null, t);
|
||||
}
|
||||
|
||||
});
|
||||
Class timestampClass = Class.forName("java.sql.Timestamp");
|
||||
this.register(timestampClass, new SimpledCoder<R, W, java.sql.Timestamp>() {
|
||||
Class sqltimeClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Time");
|
||||
Invoker<Object, Object> sqltimeInvoker = Invoker.create(sqltimeClass, "valueOf", String.class);
|
||||
this.register(sqltimeClass, new SimpledCoder<R, W, Object>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Timestamp value) {
|
||||
public void convertTo(W out, Object value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Timestamp convertFrom(R in) {
|
||||
public Object convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Timestamp.valueOf(t);
|
||||
return t == null ? null : sqltimeInvoker.invoke(null, t);
|
||||
}
|
||||
|
||||
});
|
||||
Class timestampClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Timestamp");
|
||||
Invoker<Object, Object> timestampInvoker = Invoker.create(timestampClass, "valueOf", String.class);
|
||||
this.register(timestampClass, new SimpledCoder<R, W, Object>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, Object value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : timestampInvoker.invoke(null, t);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -188,6 +204,32 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
public static Convert findConvert(ConvertType type) {
|
||||
if (type == null) return null;
|
||||
if (type == ConvertType.JSON) return JsonConvert.root();
|
||||
if (type == ConvertType.BSON) return BsonConvert.root();
|
||||
if (loaderInited.get()) {
|
||||
if (type == ConvertType.PROTOBUF) return defProtobufConvert;
|
||||
}
|
||||
synchronized (loaderInited) {
|
||||
if (!loaderInited.get()) {
|
||||
Iterator<ConvertProvider> it = ServiceLoader.load(ConvertProvider.class).iterator();
|
||||
RedkaleClassLoader.putServiceLoader(ConvertProvider.class);
|
||||
while (it.hasNext()) {
|
||||
ConvertProvider cl = it.next();
|
||||
RedkaleClassLoader.putReflectionPublicConstructors(cl.getClass(), cl.getClass().getName());
|
||||
if (cl.type() == ConvertType.PROTOBUF) defProtobufConvert = cl.convert();
|
||||
}
|
||||
loaderInited.set(true);
|
||||
}
|
||||
}
|
||||
return type == ConvertType.PROTOBUF ? defProtobufConvert : null;
|
||||
}
|
||||
|
||||
protected static boolean getSystemPropertyBoolean(String key, String parentkey, boolean defvalue) {
|
||||
return Boolean.parseBoolean(System.getProperty(key, System.getProperty(parentkey, String.valueOf(defvalue))));
|
||||
}
|
||||
|
||||
public abstract ConvertType getConvertType();
|
||||
|
||||
public abstract boolean isReversible(); //是否可逆的
|
||||
@@ -202,11 +244,33 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
return new EnumSimpledCoder(enumClass);
|
||||
}
|
||||
|
||||
protected Type formatObjectType(Type type) {
|
||||
if (type instanceof Class) {
|
||||
Class<?> clazz = (Class) type;
|
||||
ConvertImpl ci = clazz.getAnnotation(ConvertImpl.class);
|
||||
if (ci != null) {
|
||||
if (!Modifier.isAbstract(clazz.getModifiers()) && !Modifier.isInterface(clazz.getModifiers())) {
|
||||
throw new ConvertException("@" + ConvertImpl.class.getSimpleName() + " must at interface or abstract class, but " + clazz + " not");
|
||||
}
|
||||
Class impl = ci.value();
|
||||
if (Modifier.isAbstract(impl.getModifiers()) || Modifier.isInterface(impl.getModifiers())) {
|
||||
throw new ConvertException("@" + ConvertImpl.class.getSimpleName() + " at class " + impl + " cannot be interface or abstract class");
|
||||
}
|
||||
return impl;
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
protected <E> Encodeable<W, E> createDyncEncoder(Type type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected ObjectDecoder createObjectDecoder(Type type) {
|
||||
return new ObjectDecoder(type);
|
||||
}
|
||||
|
||||
protected ObjectEncoder createObjectEncoder(Type type) {
|
||||
protected <E> ObjectEncoder<W, E> createObjectEncoder(Type type) {
|
||||
return new ObjectEncoder(type);
|
||||
}
|
||||
|
||||
@@ -282,10 +346,14 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
final Method method = (Method) element;
|
||||
fieldName = readGetSetFieldName(method);
|
||||
if (fieldName != null) {
|
||||
try {
|
||||
ccs = method.getDeclaringClass().getDeclaredField(fieldName).getAnnotationsByType(ConvertColumn.class);
|
||||
} catch (Exception e) { //说明没有该字段,忽略异常
|
||||
}
|
||||
Class mclz = method.getDeclaringClass();
|
||||
do {
|
||||
try {
|
||||
ccs = mclz.getDeclaredField(fieldName).getAnnotationsByType(ConvertColumn.class);
|
||||
break;
|
||||
} catch (Exception e) { //说明没有该字段,忽略异常
|
||||
}
|
||||
} while (mclz != Object.class && (mclz = mclz.getSuperclass()) != Object.class);
|
||||
}
|
||||
}
|
||||
if (onlyColumns != null && fieldName == null) {
|
||||
@@ -320,10 +388,20 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
return null;
|
||||
}
|
||||
|
||||
static Field readGetSetField(Method method) {
|
||||
String name = readGetSetFieldName(method);
|
||||
if (name == null) return null;
|
||||
try {
|
||||
return method.getDeclaringClass().getDeclaredField(name);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static String readGetSetFieldName(Method method) {
|
||||
if (method == null) return null;
|
||||
String fname = method.getName();
|
||||
if (!fname.startsWith("is") && !fname.startsWith("get") && !fname.startsWith("set")) return fname;
|
||||
if (!fname.startsWith("is") && !fname.startsWith("get") && !fname.startsWith("set")) return fname; //record类会直接用field名作为method名
|
||||
fname = fname.substring(fname.startsWith("is") ? 2 : 3);
|
||||
if (fname.length() > 1 && !(fname.charAt(1) >= 'A' && fname.charAt(1) <= 'Z')) {
|
||||
fname = Character.toLowerCase(fname.charAt(0)) + fname.substring(1);
|
||||
@@ -687,12 +765,15 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
simpleCoder = (Decodeable) method.invoke(null, this);
|
||||
RedkaleClassLoader.putReflectionDeclaredMethods(clazz.getName());
|
||||
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
if (simpleCoder == null) {
|
||||
od = createObjectDecoder(type);
|
||||
Type impl = formatObjectType(type);
|
||||
od = createObjectDecoder(impl);
|
||||
decoder = od;
|
||||
} else {
|
||||
decoder = simpleCoder;
|
||||
@@ -771,13 +852,19 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
simpleCoder = (Encodeable) method.invoke(null, this);
|
||||
RedkaleClassLoader.putReflectionDeclaredMethods(clazz.getName());
|
||||
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
if (simpleCoder == null) {
|
||||
oe = createObjectEncoder(type);
|
||||
encoder = oe;
|
||||
Type impl = formatObjectType(type);
|
||||
encoder = createDyncEncoder(impl);
|
||||
if (encoder == null) {
|
||||
oe = createObjectEncoder(impl);
|
||||
encoder = oe;
|
||||
}
|
||||
} else {
|
||||
encoder = simpleCoder;
|
||||
}
|
||||
53
src/main/java/org/redkale/convert/ConvertImpl.java
Normal file
53
src/main/java/org/redkale/convert/ConvertImpl.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 用于序列化时接口或抽象类的默认实现类, 被标记的类必须是接口或抽象类 <br>
|
||||
* 使用场景: <br>
|
||||
*
|
||||
* <blockquote><pre>
|
||||
* @ConvertImpl(OneImpl.class)
|
||||
* public interface OneEntity {
|
||||
* public String getName();
|
||||
* }
|
||||
*
|
||||
*
|
||||
* public class OneImpl implements OneEntity {
|
||||
* private String name;
|
||||
* public String getName(){return name;}
|
||||
* public void setName(String name){this.name=name;}
|
||||
* }
|
||||
*
|
||||
*
|
||||
* String json = "{'name':'hello'}";
|
||||
* OneEntity one = JsonConvert.root.convertFrom(OneEntity.class, json);
|
||||
* //one instanceof OneImpl
|
||||
*
|
||||
* </pre></blockquote>
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.5.0
|
||||
*/
|
||||
@Inherited
|
||||
@Documented
|
||||
@Target({TYPE})
|
||||
@Retention(RUNTIME)
|
||||
public @interface ConvertImpl {
|
||||
|
||||
/**
|
||||
* 默认的实现类
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
Class value();
|
||||
}
|
||||
@@ -3,18 +3,21 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.net;
|
||||
package org.redkale.convert;
|
||||
|
||||
/**
|
||||
* Convert的扩展实现类加载器
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public enum SSLClientAuth {
|
||||
NONE,
|
||||
NEED,
|
||||
WANT,
|
||||
CLIENT;
|
||||
public interface ConvertProvider {
|
||||
|
||||
public ConvertType type();
|
||||
|
||||
public Convert convert();
|
||||
}
|
||||
27
src/main/java/org/redkale/convert/ConvertSmallString.java
Normal file
27
src/main/java/org/redkale/convert/ConvertSmallString.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 序列化时标记String字段的值是否为无转义字符且长度不超过255的字符串
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.3.0
|
||||
*
|
||||
*/
|
||||
@Target({METHOD, FIELD})
|
||||
@Retention(RUNTIME)
|
||||
public @interface ConvertSmallString {
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user